diff src/main/java/christie/codegear/Command.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 3ea61d0bfc34
children bcd4f2c19185
line wrap: on
line diff
--- a/src/main/java/christie/codegear/Command.java	Sat Dec 30 20:11:36 2017 +0900
+++ b/src/main/java/christie/codegear/Command.java	Tue Jan 09 17:37:43 2018 +0900
@@ -1,20 +1,74 @@
 package christie.codegear;
 
+import christie.daemon.RemoteMessage;
 import christie.datagear.DataGear;
+import org.msgpack.MessagePack;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
 
 public class Command {
-    public CodeGear cs;
+    public CodeGear cg = null;
     public DataGear dg;
-    public String dest;
+    public String cgmName = "first";
+    public String dsmName = "local";
     public String key;
     public CommandType type;
+    private static final MessagePack packer = new MessagePack();
 
-    public Command(CodeGear cs, DataGear dg, String dest, String key, CommandType type){
-        this.cs = cs;
+    //for put
+    public Command(DataGear dg, String cgmName, String dsmName, String key, CommandType type){
         this.dg = dg;
-        this.dest = dest;
+        this.cgmName = cgmName;
+        this.dsmName = dsmName;
+        this.key = key;
+        this.type = type;
+    }
+
+    //for take
+    public Command(CodeGear cg, DataGear dg, String cgmName, String dsmName, String key, CommandType type){
+        this.cg = cg;
+        this.dg = dg;
+        this.cgmName = cgmName;
+        this.dsmName = dsmName;
         this.key = key;
         this.type = type;
     }
 
+    public ByteBuffer convert() {
+        ByteBuffer buf = null;
+
+        try {
+            byte[] command = null;
+            byte[] data = null;
+            byte[] dataSize = null;
+
+            switch (type) {
+                case PUT:
+                case REPLY:
+
+                    RemoteMessage cm = new RemoteMessage();
+
+                    data = dg.getMessagePack();
+
+                    command = packer.write(cm);
+                    dataSize = packer.write(data.length);
+                    buf = ByteBuffer.allocate(command.length+dataSize.length+data.length);
+                    buf.put(command);
+                    buf.put(dataSize);
+                    buf.put(data);
+                    break;
+                default:
+                    command = packer.write(new RemoteMessage());
+                    buf = ByteBuffer.allocate(command.length);
+                    buf.put(command);
+                    break;
+            }
+
+            buf.flip();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return buf;
+    }
 }