package com.sister.kelompok5;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.imageio.ImageIO;

public class RMIClient {

	private Thread sendMessage(String worker, int index) {
        try {			
            /*
             * set to RMI server address and port
             * looking for service named "Echo", call remote method
             */
            Registry registry = LocateRegistry.getRegistry(worker, 5000);					
            MessageInterface message = (MessageInterface) registry.lookup("ConvertColor");
            
            // Full file path
            String imagePath = "";
            File path = new File(imagePath);
            
            // File list
            String[] files = path.list();
            
            // Number of files
            int numOfFiles = files.length;
            
            // Initialize byte array
            byte[] images;
            
            for (int i = index; i < numOfFiles; i+=4) {
            	// Image file will send to server
            	String fileIndex = files[i];
            	File sendFile = new File(imagePath + "/" + fileIndex);
            	
            	// Using regex to remove file extensions
            	String fileName = fileIndex.replaceFirst("[.][^.]+$", "");
            	
            	// Get file extesions
            	String fileExtension = fileIndex.substring(fileIndex.lastIndexOf('.')+1);
            	
            	BufferedImage img = ImageIO.read(sendFile);
            	
            	ByteArrayOutputStream baos = new ByteArrayOutputStream();
            	ImageIO.write(img, fileExtension, baos);
            	images = new byte[baos.size()];
            	images = baos.toByteArray();
            	
            	int fileNumber = i + 1;
            	
            	// Call method to start converting color
            	byte[] imageOut = message.Convert(images, fileNumber, fileName, fileExtension);
            	
            	// Printing message reply as log
            	System.out.println("Ukuran byte array: " + images.length);
    			System.out.println("Ukuran file: " + sendFile.length());
    			System.out.println(fileNumber + ". " + fileName + " berhasil dikirim\n");
    			
    			// Construct image like before
    			BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageOut));
    			String pathNewFile = "";
    				
    			File destinationPath = new File(pathNewFile + fileNumber + "_" + fileName + "_grayscale." + fileExtension);
    				
    			ImageIO.write(image, fileExtension, destinationPath);
            }
            
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
		return null;        
    }
    
    public static void main(String[] args) {
        RMIClient main = new RMIClient();
        
        String[] worker = {"127.0.0.1", "127.0.0.1"};
        
        for (int i = 0; i < 4; i++) {
        	Thread work = main.sendMessage(worker[i%2], i + 1);
        	work.start();
        }
    }
    
}

@@ -269,86 +269,244 @@
 O;%0A%0A
-public class RMIClient %7B%0A%0A%09private Thread sendMessage(String worker, int index
+class ThreadWorker extends Thread %7B%0A%09private String worker;%0A%09private int index;%0A%0A%09ThreadWorker(String server, int idx) %7B%0A%09%09this.worker = server;%0A%09%09this.index = idx;%0A%09%09%0A%09%09System.out.println(%22Sending to %22 + worker);%0A%09%7D%0A%09%0A%09public void run(
 ) %7B%0A
@@ -1251,16 +1251,20 @@
  = index
+ - 1
 ; i %3C nu
@@ -1276,17 +1276,17 @@
 les; i+=
-4
+2
 ) %7B%0A    
@@ -2971,42 +2971,51 @@
    %7D
-%0A%09%09return null;        %0A    %7D%0A    
+      %0A    %7D%0A%0A%7D%0A%0Apublic class RMIClient %7B%0A%09
 %0A   
@@ -3064,16 +3064,18 @@
         
+//
 RMIClien
@@ -3204,9 +3204,9 @@
 i %3C 
-4
+2
 ; i+
@@ -3229,32 +3229,36 @@
 read
- work = main.sendMessage
+Worker t1 = new ThreadWorker
 (wor
@@ -3288,18 +3288,14 @@
    %09
-work.start
+t1.run
 ();%0A

solve thread didn't work