package rakka;

import com.sun.jdi.Bootstrap;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.VirtualMachineManager;
import com.sun.jdi.connect.Connector;
import com.sun.jdi.connect.IllegalConnectorArgumentsException;
import com.sun.jdi.connect.LaunchingConnector;
import com.sun.jdi.connect.VMStartException;
import com.sun.jdi.event.Event;
import com.sun.jdi.event.EventQueue;
import com.sun.jdi.event.EventSet;
import com.sun.jdi.event.VMDisconnectEvent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;

/**
 * Created by lidavidm on 2/10/16.
 */
public class RakkaMain {
    public static void main(String[] args) {
        // For reference: http://stackoverflow.com/questions/21654255
        VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
        LaunchingConnector connector = vmm.defaultConnector();
        Map<String, Connector.Argument> arguments = connector.defaultArguments();
        arguments.get("main").setValue("reki.RekiShim");
        arguments.get("options").setValue("-cp /home/lidavidm/Code/cornell/cs6360/server_prototype/out/production/server_prototype");
        try {
            VirtualMachine vm = connector.launch(arguments);
            vm.resume();

//            EventQueue events = vm.eventQueue();
//            EVENT_LOOP: while (true) {
//                EventSet e = events.remove();
//                for (Event event : e) {
//                    System.out.println(event);
//                    if (event instanceof VMDisconnectEvent) {
//                        break EVENT_LOOP;
//                    }
//                }
//            }
            BufferedReader reader = new BufferedReader(new InputStreamReader(vm.process().getInputStream()));
            reader.lines().forEach(System.out::println);

            BufferedReader stderr = new BufferedReader(new InputStreamReader(vm.process().getErrorStream()));
            stderr.lines().forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalConnectorArgumentsException e) {
            e.printStackTrace();
        } catch (VMStartException e) {
            e.printStackTrace();
        }
    }
}

@@ -43,44 +43,8 @@
 ap;%0A
-import com.sun.jdi.ThreadReference;%0A
 impo
@@ -477,84 +477,9 @@
 .io.
-BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader
+*
 ;%0Aim
@@ -567,16 +567,674 @@
 aMain %7B%0A
+    static class StreamHandlerThread extends Thread %7B%0A        private final BufferedReader reader;%0A        private final PrintStream writer;%0A%0A        public StreamHandlerThread(InputStream is, OutputStream os) %7B%0A            reader = new BufferedReader(new InputStreamReader(is));%0A            writer = new PrintStream(os);%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            try %7B%0A                String line = %22%22;%0A                while ((line = reader.readLine()) != null) %7B%0A                    writer.println(line);%0A                %7D%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A    %7D%0A%0A
     publ
@@ -1848,18 +1848,188 @@
 ume();%0A%0A
-//
+            new StreamHandlerThread(vm.process().getInputStream(), System.out).run();%0A            new StreamHandlerThread(vm.process().getErrorStream(), System.err).run();%0A
         
@@ -2065,26 +2065,25 @@
 entQueue();%0A
-//
+%0A
             
@@ -2093,16 +2093,28 @@
 NT_LOOP:
+%0A           
  while (
@@ -2117,26 +2117,24 @@
 le (true) %7B%0A
-//
             
@@ -2167,18 +2167,16 @@
 move();%0A
-//
         
@@ -2211,59 +2211,8 @@
 ) %7B%0A
-//                    System.out.println(event);%0A//
     
@@ -2269,18 +2269,16 @@
 vent) %7B%0A
-//
         
@@ -2311,18 +2311,16 @@
 T_LOOP;%0A
-//
         
@@ -2337,30 +2337,9 @@
   %7D%0A
-//                %7D%0A//
+%0A
     
@@ -2342,34 +2342,32 @@
             
-%7D%0A
         
     Buffered
@@ -2358,332 +2358,140 @@
     
-    BufferedReader reader = new BufferedReader(new InputStreamReader(vm.process().getInputStream()));%0A            reader.lines().forEach(System.out::println);%0A%0A            BufferedReader stderr = new BufferedReader(new InputStreamReader(vm.process().getErrorStream()));%0A            stderr.lines().forEach(System.out::println
+System.out.println(event);%0A                %7D%0A            %7D%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace(
 );%0A 

Move child VM IO processing to threads