diff src/main/java/christie/daemon/IncomingTcpConnection.java @ 12:b49a926cbdd9

add RemotePutTest and that is working
author Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp>
date Tue, 09 Jan 2018 17:37:43 +0900
parents
children bcd4f2c19185
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/christie/daemon/IncomingTcpConnection.java	Tue Jan 09 17:37:43 2018 +0900
@@ -0,0 +1,79 @@
+package christie.daemon;
+
+
+import christie.codegear.CodeGearManager;
+import christie.codegear.Command;
+import christie.codegear.CommandType;
+import christie.datagear.DataGear;
+import christie.datagear.RemoteDataGearManager;
+
+import org.msgpack.MessagePack;
+import org.msgpack.unpacker.Unpacker;
+
+import java.io.IOException;
+import java.nio.channels.ClosedChannelException;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class IncomingTcpConnection extends Thread {
+
+    ConcurrentHashMap<String, CodeGearManager> cgms;
+    Connection connection;
+    private static final MessagePack packer = new MessagePack();
+
+    public IncomingTcpConnection(Connection connection, CodeGearManager cgm) {
+        this.cgms = cgm.getCgms();
+        this.connection = connection;
+    }
+
+    public void run() {
+        Unpacker unpacker = null;
+        try {
+            unpacker = packer.createUnpacker(connection.socket.getInputStream());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        if (unpacker == null) {
+            return;
+        }
+        while (true) {
+            try {
+                Command cmd = null;
+                DataGear dg = null;
+                RemoteMessage msg = unpacker.read(RemoteMessage.class);
+                CommandType type = CommandType.getCommandTypeFromId(msg.type);
+                int dataSize = unpacker.readInt();
+                byte[] data = new byte[dataSize];
+                switch (type) {
+                    case PUT:
+                        connection.socket.getInputStream().read(data);
+
+                        cgms.get(msg.cgmName).getDGM().put(msg.key, data);
+
+                        break;
+                        /*
+                    case PEEK:
+                    case TAKE:
+                        cmd = new Command(null, dg, null, null, null);
+
+                        break;
+                    case REPLY:
+                        connection.socket.getInputStream().read(data);
+                        dg = new DataGear(data, data.getClass());
+
+                        Command rCmd = new Command(null, dg, null, null, null);
+                        //cmd.cg.idg.reply(cmd.dg, rCmd);
+
+                        break;*/
+                    default:
+                        break;
+                }
+            } catch (ClosedChannelException e) {
+                return;
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+
+}