int msgLength = bytesToInt(lenBytes);
if (msgLength == 0) {
- throw new IOException("Inbound message is 0 bytes. Interrupted?");
+ throw new IOException(
+ "Inbound message is 0 bytes. I/O interrupted?");
}
- logger.info("MessageReader read message length: " + msgLength);
-
byte[] msgBytes = new byte[msgLength];
bytesRead = System.in.read(msgBytes);
String message = new String(msgBytes, "UTF-8");
- logger.info("MessageReader read message " + message);
+ logger.info("MessageReader read: " + message);
return message;
}
inQueue.offer(jsonMsg);
- logger.info("inQueue contains " + inQueue.size() + " messages");
+ logger.finest("inQueue contains " + inQueue.size() + " messages");
}
}
}
* Encodes and writes one message to STDOUT.
*/
public void writeOneMessage(String message) throws IOException {
+ logger.finest("MessageWriter sending: " + message);
System.out.write(intToBytes(message.length()));
System.out.write(message.getBytes("UTF-8"));
System.out.flush();
public void run() {
while (true) {
- logger.info("MessageWriter waiting for outQueue message");
-
try {
// take() blocks the thread until a message is available
import org.json.*;
import java.util.logging.*;
+/**
+ * Dispatches requests received via MessageIO, sends responses back
+ * via MessageIO.
+ */
public class RequestHandler extends Thread {
private MessageIO io;
static final Logger logger = Hatch.getLogger();
io = new MessageIO();
}
+ /**
+ * Unpack a JSON request and send it to the necessary Hatch handler.
+ */
void dispatchRequest(JSONObject request) throws JSONException {
long msgid = request.getLong("msgid");
boolean showDialog = request.optBoolean("showDialog");
logger.info("Received message action: " + action);
+
+ JSONObject response = new JSONObject();
+
+ switch (action) {
+
+ case "printers":
+ response.put("printers",
+ new PrintManager().getPrintersAsMaps());
+ break;
+
+ case "print":
+ String content = request.getString("content");
+ String contentType = request.getString("contentType");
+
+
+ default:
+ response.put("error", "Unknown action: " + action);
+ }
+
+ io.sendMessage(response);
}
--- /dev/null
+package org.evergreen_ils.hatch;
+
+import java.util.logging.Logger;
+import org.json.*;
+
+public class TestHatch {
+ static MessageIO io;
+ static final Logger logger = Hatch.getLogger();
+
+ public static void rest() {
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {}
+ }
+
+ public static void doSends() {
+ int msgId = 1;
+
+ JSONObject obj = new JSONObject();
+ obj.put("msgid", msgId++);
+ obj.put("action", "printers");
+ io.sendMessage(obj);
+
+ rest();
+
+ obj = new JSONObject();
+ obj.put("msgid", msgId++);
+ obj.put("action", "print");
+ obj.put("contentType", "text/plain");
+ obj.put("content", "Hello, World!");
+ io.sendMessage(obj);
+
+ rest();
+ }
+
+ /**
+ * Log all received message as a JSON string
+ */
+ public static void doReceive() {
+ while (true) {
+ JSONObject resp = io.recvMessage();
+ logger.info("TestJSON:doReceive(): " + resp.toString());
+ }
+ }
+
+ public static void main (String[] args) {
+ io = new MessageIO();
+ io.listen();
+
+ if (args.length > 0 && args[0].equals("receive")) {
+ doReceive();
+ } else {
+ doSends();
+ }
+ }
+}
+