Skip to content

Commit

Permalink
more parts of CQC
Browse files Browse the repository at this point in the history
  • Loading branch information
johanvos committed Nov 5, 2019
1 parent 3589b2e commit f93aa9b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/main/java/com/gluonhq/strange/cqc/CQCSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class CQCSession {

private Socket socket;
private OutputStream os;
private InputStream is;
private short appId;

public CQCSession() {
Expand All @@ -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() {
Expand All @@ -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();
}

}

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

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

0 comments on commit f93aa9b

Please sign in to comment.