Skip to content

Commit

Permalink
Add first part of CQC protol
Browse files Browse the repository at this point in the history
  • Loading branch information
johanvos committed Nov 5, 2019
1 parent 383a2b7 commit 3589b2e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/gluonhq/strange/cqc/CQCSession.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/gluonhq/strange/cqc/Protocol.java
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 3589b2e

Please sign in to comment.