From 3589b2e560b3a4a32dd539c27a361e02a3902d88 Mon Sep 17 00:00:00 2001 From: Johan Vos Date: Fri, 20 Sep 2019 20:36:31 +0200 Subject: [PATCH] Add first part of CQC protol --- .../com/gluonhq/strange/cqc/CQCSession.java | 69 +++++++++++++++++++ .../com/gluonhq/strange/cqc/Protocol.java | 11 +++ 2 files changed, 80 insertions(+) create mode 100644 src/main/java/com/gluonhq/strange/cqc/CQCSession.java create mode 100644 src/main/java/com/gluonhq/strange/cqc/Protocol.java diff --git a/src/main/java/com/gluonhq/strange/cqc/CQCSession.java b/src/main/java/com/gluonhq/strange/cqc/CQCSession.java new file mode 100644 index 0000000..09d64e3 --- /dev/null +++ b/src/main/java/com/gluonhq/strange/cqc/CQCSession.java @@ -0,0 +1,69 @@ +package com.gluonhq.strange.cqc; + +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.Socket; + +public class CQCSession { + + private Socket socket; + private OutputStream os; + private short appId; + + public CQCSession() { + appId = getNextAppId(); + } + + public void connect(String host, int port) throws IOException { + System.err.println("Connecting to "+host+":"+port); + Socket socket = new Socket(host, port); + System.err.println("socket created: "+socket); + this.socket = socket; + this.os = socket.getOutputStream(); + } + + public void sendHello() throws IOException { + sendCqcHeadercd op - (Protocol.CQC_TP_HELLO, 0); + } + + public void createQubit() throws IOException { + sendSimpleCommand(Protocol.CQC_CMD_NEW, (short)0); + } + + static short appIdCounter = 0; + + private static synchronized short getNextAppId() { + appIdCounter++; + return appIdCounter; + } + /* + * len = exclusive header. + */ + private void sendCqcHeader(byte type, int len) throws IOException { + int totalLength = len + 8; + + DataOutputStream dos = new DataOutputStream(os); + dos.writeByte(Protocol.VERSION); + dos.writeByte(type); + 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); + + } + + private void sendSimpleCommand(byte command, short qid) throws IOException { + sendCommand(command, qid, false, false, false,0); + System.err.println("Sending simple command "+command); + } + +} diff --git a/src/main/java/com/gluonhq/strange/cqc/Protocol.java b/src/main/java/com/gluonhq/strange/cqc/Protocol.java new file mode 100644 index 0000000..ff34ec4 --- /dev/null +++ b/src/main/java/com/gluonhq/strange/cqc/Protocol.java @@ -0,0 +1,11 @@ +package com.gluonhq.strange.cqc; + +public class Protocol { + + public static final byte VERSION = 0x2; + + public static final byte CQC_TP_HELLO = 0x0; + public static final byte CQC_TP_COMMAND = 0x1; + + public static final byte CQC_CMD_NEW = 0x1; +}