stdin/stdout stream based hatch WIP collab/berick/hatch-stdio
authorBill Erickson <berickxx@gmail.com>
Mon, 7 Nov 2016 23:13:15 +0000 (18:13 -0500)
committerBill Erickson <berickxx@gmail.com>
Mon, 7 Nov 2016 23:13:15 +0000 (18:13 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
README
run.sh
src/org/evergreen_ils/hatch/MessageIO.java

diff --git a/README b/README
index 733d9f1..72a3f08 100644 (file)
--- a/README
+++ b/README
@@ -8,8 +8,8 @@ Hatch - Java Print / Storage / Etc Service
 % ln -s jdk1.8.0_25 jdk1.8
 
 % mkdir lib
-Download javax.json-api jar file from 
-http://search.maven.org/remotecontent?filepath=javax/json/javax.json-api/1.0/javax.json-api-1.0.jar
+Download org.json jar from and place into lib (modify run.sh CP)
+https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22
 
 # compile
 % ./run.sh
diff --git a/run.sh b/run.sh
index 1433313..3400107 100755 (executable)
--- a/run.sh
+++ b/run.sh
@@ -1,5 +1,5 @@
 JAVA_HOME=jdk1.8
-CP=lib:lib/javax.json-api-1.0.jar
+CP=lib:lib/json-20160810.jar
 
 # compile
 $JAVA_HOME/bin/javac -cp $CP -d lib src/org/evergreen_ils/hatch/*.java
index e040b27..45739e3 100644 (file)
@@ -3,13 +3,14 @@ package org.evergreen_ils.hatch;
 import java.util.logging.*;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.nio.ByteBuffer;
-import javax.json.*;
 import java.io.IOException;
+import org.json.*;
+
 
 public class MessageIO {
 
-    private LinkedBlockingQueue<JsonValue> inQueue;
-    private LinkedBlockingQueue<JsonValue> outQueue;
+    private LinkedBlockingQueue<JSONObject> inQueue;
+    private LinkedBlockingQueue<JSONObject> outQueue;
     //private static Logger logger = Logger.getLogger("org.evergreen_ils.hatch");
     private static Logger logger = Hatch.getLogger();
 
@@ -17,8 +18,8 @@ public class MessageIO {
     private MessageWriter writer;
 
     public MessageIO() {
-        inQueue = new LinkedBlockingQueue<JsonValue>();
-        outQueue = new LinkedBlockingQueue<JsonValue>();
+        inQueue = new LinkedBlockingQueue<JSONObject>();
+        outQueue = new LinkedBlockingQueue<JSONObject>();
         reader = new MessageReader();
         writer = new MessageWriter();
     }
@@ -37,8 +38,8 @@ public class MessageIO {
             return 
                   (bytes[3] << 24) & 0xff000000 
                 | (bytes[2] << 16) & 0x00ff0000
-                | (bytes[1] << 8)  & 0x0000ff00 
-                | (bytes[0] << 0)  & 0x000000ff;
+                | (bytes[1] <<  8) & 0x0000ff00 
+                | (bytes[0] <<  0) & 0x000000ff;
         }
 
         private String readOneMessage() throws EndOfStreamException, IOException {
@@ -76,23 +77,34 @@ public class MessageIO {
 
             while (true) {
 
+                String message = "";
+                JSONObject jsonMsg = null;
+
                 try {
                     
-                    String message = readOneMessage();
+                    message = readOneMessage();
+                    jsonMsg = new JSONObject(message);
 
                 } catch (EndOfStreamException eose) {
 
-                    logger.warning("STDIN closed.  MessageReader thread exiting");
-                    return;
+                    logger.warning("STDIN closed... exiting");
+                    System.exit(1);
 
                 } catch (IOException ioe) {
-                    logger.warning(ioe);
+                    logger.warning(ioe.toString());
+
+                } catch (JSONException je) {
+
+                    logger.warning("Error parsing JSON message on STDIN " +
+                        je.toString() + " : " + message);
+
+                    continue;
                 }
 
-                // TODO: convert to JSON
-                // TODO: push onto inQueue
+                inQueue.offer(jsonMsg);
+
+                logger.info("inQueue contains " + inQueue.size() + " messages");
             }
-    
         }
     }
 
@@ -107,9 +119,7 @@ public class MessageIO {
             return bytes;
         }
 
-
         public void writeOneMessage(String message) throws IOException {
-
             System.out.write(intToBytes(message.length()));
             System.out.write(message.getBytes("UTF-8"));
             System.out.flush();
@@ -117,7 +127,25 @@ public class MessageIO {
 
         public void run() {
 
+            while (true) {
+                logger.info("MessageWriter waiting for outQueue message");
+
+                try {
+
+                    // take() blocks the thread until a message is available
+                    JSONObject jsonMsg = outQueue.take();
+
+                    writeOneMessage(jsonMsg.toString());
+
+                } catch (InterruptedException e) {
+                    // interrupted, go back and listen
+                    continue;
+                } catch (IOException ioe) {
+                    logger.warning(
+                        "Error writing message to STDOUT: " + ioe.toString());
+                }
+           }
         }
     }
-    
 }
+