/*
 * The MIT License
 *
 * Copyright 2017 josue_000.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package br.pucminas.distributedsystems.roucairolcarvalho;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Josue
 */
public class PrintServer implements Runnable {

    private final Integer portNumber;
    private long lastTimestamp;

    public PrintServer(int portNumber) {
        this.portNumber = portNumber;
    }
    
    @Override
    public void run() {
        try {
            ServerSocket socket = new ServerSocket(portNumber);
            
            // Server loop
            while (true) {

                // Request Handle
                Socket connectionSocket = socket.accept();
                ObjectInputStream in = new ObjectInputStream(
                        connectionSocket.getInputStream());

                Object clientSentence = in.readObject();
                
                System.out.printf("PRINT SERVER: receive a job\n");
                
                lastTimestamp = new Date().getTime();
                
                for (int i = 0; i < 10; i++) {
                    System.out.println(lastTimestamp++);
                    Thread.sleep(500);
                }
                
                System.out.printf("PRINT SERVER: finish job\n");
            }
            
        } catch (IOException | ClassNotFoundException | InterruptedException e) {
            System.err.println("PRINT SERVER:" + e);
        }
        System.out.println("PRINT SERVER: running on port " + portNumber);
    }
    
}

@@ -2121,135 +2121,101 @@
     
-System.out.printf(%22PRINT SERVER: receive a job%5Cn%22);%0A                %0A                lastTimestamp = new Date().getTime(
+lastTimestamp = new Date().getTime();%0A                dispatch(lastTimestamp
 );%0A    
+  
+      %7D%0A
     
@@ -2235,142 +2235,293 @@
     
-        for (int i = 0; i %3C 10; i++) %7B%0A                    System.out.println(lastTimestamp++
+%7D catch (IOException %7C ClassNotFoundException e) %7B%0A            System.err.println(%22PRINT SERVER:%22 + e);%0A        %7D%0A        System.out.println(%22PRINT SERVER: running on port %22 + portNumber
 );%0A    
+%7D%0A
     
+%0A
     
- 
+private void dispatch(final long timestamp) %7B%0A
        
+ new 
 Thread
-.sleep(500);
+(new Runnable() %7B
 %0A   
@@ -2521,37 +2521,41 @@
 ) %7B%0A            
-    %7D
+@Override
 %0A               
@@ -2543,36 +2543,51 @@
 ide%0A            
-    
+public void run() %7B
 %0A               
@@ -2624,14 +2624,17 @@
 ER: 
-finish
+receive a
  job
@@ -2655,79 +2655,230 @@
     
-%7D%0A
     
-        %0A        %7D catch (IOException %7C ClassNotFoundException %7C 
+long value = timestamp;%0A                for (int i = 0; i %3C 10; i++) %7B%0A                    System.out.println(value++);%0A                    try %7B%0A                        Thread.sleep(500);%0A                    %7D catch (
 Inte
@@ -2903,32 +2903,44 @@
 ) %7B%0A            
+        
+    
 System.err.print
@@ -2948,32 +2948,42 @@
 n(%22PRINT SERVER:
+ dispatch 
 %22 + e);%0A        
@@ -2974,34 +2974,72 @@
 %22 + e);%0A        
-%7D%0A
+            %7D%0A                %7D%0A        
         System.o
@@ -3038,34 +3038,33 @@
 System.out.print
-ln
+f
 (%22PRINT SERVER: 
@@ -3067,50 +3067,62 @@
 ER: 
-running on port %22 + portNumber);%0A    %7D
+finish job%5Cn%22);%0A            %7D%0A        %7D).start();
 %0A    
+%7D
 %0A%7D%0A

need to fix