|
| 1 | +package com.gluonhq.strange.cqc; |
| 2 | + |
| 3 | +import java.io.DataOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.net.Socket; |
| 7 | + |
| 8 | +public class CQCSession { |
| 9 | + |
| 10 | + private Socket socket; |
| 11 | + private OutputStream os; |
| 12 | + private short appId; |
| 13 | + |
| 14 | + public CQCSession() { |
| 15 | + appId = getNextAppId(); |
| 16 | + } |
| 17 | + |
| 18 | + public void connect(String host, int port) throws IOException { |
| 19 | + System.err.println("Connecting to "+host+":"+port); |
| 20 | + Socket socket = new Socket(host, port); |
| 21 | + System.err.println("socket created: "+socket); |
| 22 | + this.socket = socket; |
| 23 | + this.os = socket.getOutputStream(); |
| 24 | + } |
| 25 | + |
| 26 | + public void sendHello() throws IOException { |
| 27 | + sendCqcHeadercd op - (Protocol.CQC_TP_HELLO, 0); |
| 28 | + } |
| 29 | + |
| 30 | + public void createQubit() throws IOException { |
| 31 | + sendSimpleCommand(Protocol.CQC_CMD_NEW, (short)0); |
| 32 | + } |
| 33 | + |
| 34 | + static short appIdCounter = 0; |
| 35 | + |
| 36 | + private static synchronized short getNextAppId() { |
| 37 | + appIdCounter++; |
| 38 | + return appIdCounter; |
| 39 | + } |
| 40 | + /* |
| 41 | + * len = exclusive header. |
| 42 | + */ |
| 43 | + private void sendCqcHeader(byte type, int len) throws IOException { |
| 44 | + int totalLength = len + 8; |
| 45 | + |
| 46 | + DataOutputStream dos = new DataOutputStream(os); |
| 47 | + dos.writeByte(Protocol.VERSION); |
| 48 | + dos.writeByte(type); |
| 49 | + dos.writeShort(appId); |
| 50 | + dos.writeInt(len); |
| 51 | + dos.flush(); |
| 52 | +// byte[] data = new byte[8]; |
| 53 | +// data[0] = Protocol.VERSION; |
| 54 | +// data[1] = type; |
| 55 | +// data[2] = (byte)(appId & 0xff); |
| 56 | +// data[3] = (byte)((appId >> 8) & 0xff); |
| 57 | + |
| 58 | + } |
| 59 | + private void sendCommand(byte command, short qubit_id, boolean notify, boolean action, boolean block, int length) throws IOException { |
| 60 | + sendCqcHeader(Protocol.CQC_TP_COMMAND, length); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + private void sendSimpleCommand(byte command, short qid) throws IOException { |
| 65 | + sendCommand(command, qid, false, false, false,0); |
| 66 | + System.err.println("Sending simple command "+command); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments