diff src/org/dancres/blitz/remote/nio/Txer.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/dancres/blitz/remote/nio/Txer.java	Sat Mar 21 11:00:06 2009 +0000
@@ -0,0 +1,54 @@
+package org.dancres.blitz.remote.nio;
+
+import EDU.oswego.cs.dl.util.concurrent.QueuedExecutor;
+
+import java.io.DataOutputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * Client-side message transmitter.  Accepts a unique id for the message
+ * and the message itself.  This message is then queued and sent asynchronously.
+ */
+class Txer {
+    private QueuedExecutor _sender;
+    private DataOutputStream _socketTx;
+
+    Txer(OutputStream anOutgoing) {
+        _socketTx = new DataOutputStream(anOutgoing);
+        _sender = new QueuedExecutor();
+        _sender.setThreadFactory(new DaemonThreadFactory());
+    }
+
+    void send(int anId, byte[] anOp) throws InterruptedException {
+        _sender.execute(new SendTask(anId, anOp));
+    }
+
+    void halt() {
+        _sender.shutdownNow();
+    }
+
+    class SendTask implements Runnable {
+        private int _id;
+        private byte[] _op;
+
+        SendTask(int anId, byte[] anOp) {
+            _id = anId;
+            _op = anOp;
+        }
+
+        public void run() {
+            try {
+                // System.err.println("Sending: " + _id + ", " + _op.length);
+                _socketTx.writeInt(_id);
+                _socketTx.writeInt(_op.length);
+                _socketTx.write(_op);
+                _socketTx.flush();
+                // System.err.println("Done: " + _id);
+            } catch (IOException anIOE) {
+                System.err.println("Failed to send request:" + _id);
+                anIOE.printStackTrace(System.err);
+            }
+        }
+    }
+}