From f93aa9bf64a75b43bca82d0957d14b675921c5c7 Mon Sep 17 00:00:00 2001 From: Johan Vos Date: Sat, 21 Sep 2019 16:33:28 +0200 Subject: [PATCH] more parts of CQC --- .../com/gluonhq/strange/cqc/CQCSession.java | 42 +++++++++++++++---- .../com/gluonhq/strange/cqc/Protocol.java | 4 ++ .../gluonhq/strange/cqc/ResponseMessage.java | 14 +++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java diff --git a/src/main/java/com/gluonhq/strange/cqc/CQCSession.java b/src/main/java/com/gluonhq/strange/cqc/CQCSession.java index 09d64e3..c7c3181 100644 --- a/src/main/java/com/gluonhq/strange/cqc/CQCSession.java +++ b/src/main/java/com/gluonhq/strange/cqc/CQCSession.java @@ -2,6 +2,7 @@ import java.io.DataOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; @@ -9,6 +10,7 @@ public class CQCSession { private Socket socket; private OutputStream os; + private InputStream is; private short appId; public CQCSession() { @@ -24,13 +26,34 @@ public void connect(String host, int port) throws IOException { } public void sendHello() throws IOException { - sendCqcHeadercd op - (Protocol.CQC_TP_HELLO, 0); + sendCqcHeader(Protocol.CQC_TP_HELLO, 0); } public void createQubit() throws IOException { sendSimpleCommand(Protocol.CQC_CMD_NEW, (short)0); } + private void sendComman() { + + } + public void createEPR(String name) throws IOException { + sendCommand(Protocol.CQC_CMD_EPR,(short)0,true,false, true, 12); + // sendSimpleCommand(Protocol.CQC_CMD_EPR, (short)0); + } + + public ResponseMessage readMessage() throws IOException { + if (is == null) { + is = socket.getInputStream(); + } + byte[] msg = new byte[256]; + int len = is.read(msg); + if (len < 8) { + throw new IOException ("Can't read message if it has less than 8 bytes (got "+len+")"); + } + ResponseMessage answer = new ResponseMessage(msg, len); + return answer; + } + static short appIdCounter = 0; private static synchronized short getNextAppId() { @@ -49,15 +72,20 @@ private void sendCqcHeader(byte type, int len) throws IOException { dos.writeShort(appId); dos.writeInt(len); dos.flush(); -// byte[] data = new byte[8]; -// data[0] = Protocol.VERSION; -// data[1] = type; -// data[2] = (byte)(appId & 0xff); -// data[3] = (byte)((appId >> 8) & 0xff); - } + private void sendCommand(byte command, short qubit_id, boolean notify, boolean action, boolean block, int length) throws IOException { sendCqcHeader(Protocol.CQC_TP_COMMAND, length); + if (command == Protocol.CQC_CMD_EPR) { + DataOutputStream dos = new DataOutputStream(os); + dos.writeShort(0); // qubit_id + dos.writeByte(command); + dos.writeByte(0); +dos.writeShort(1); +dos.writeShort(2); +dos.writeInt(4); +dos.flush(); + } } diff --git a/src/main/java/com/gluonhq/strange/cqc/Protocol.java b/src/main/java/com/gluonhq/strange/cqc/Protocol.java index ff34ec4..d29b3d7 100644 --- a/src/main/java/com/gluonhq/strange/cqc/Protocol.java +++ b/src/main/java/com/gluonhq/strange/cqc/Protocol.java @@ -6,6 +6,10 @@ public class Protocol { public static final byte CQC_TP_HELLO = 0x0; public static final byte CQC_TP_COMMAND = 0x1; + public static final byte CQC_TP_EPR_OK = 0x6; public static final byte CQC_CMD_NEW = 0x1; + public static final byte CQC_CMD_EPR = 0x7; + public static final byte CQC_CMD_EPR_RECV = 0x8; + } diff --git a/src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java b/src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java new file mode 100644 index 0000000..d037f11 --- /dev/null +++ b/src/main/java/com/gluonhq/strange/cqc/ResponseMessage.java @@ -0,0 +1,14 @@ +package com.gluonhq.strange.cqc; + +public class ResponseMessage { + + final byte type; + + public ResponseMessage(byte[] msg, int length) { + type = msg[1]; + } + + public byte getType() { + return type; + } +}