1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
import java.net.Socket;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
class EntropybrokerConnector
{
String host;
int port = 55225;
final int maxBytes = 9999 / 8;
Socket socket;
InputStreamReader inputStream;
OutputStreamWriter outputStream;
EntropybrokerConnector(String host)
{
this.host = host;
}
EntropybrokerConnector(String host, int port)
{
this.host = host;
this.port = port;
}
void writeData(OutputStreamWriter outputStream, String what) throws Exception
{
int whatLength = what.length();
System.out.println("what: " + what);
for(int loop=0; loop<whatLength; loop++)
outputStream.write(what.charAt(loop));
outputStream.flush();
}
void writeData(OutputStreamWriter outputStream, byte [] what) throws Exception
{
int whatLength = what.length;
for(int loop=0; loop<whatLength; loop++)
outputStream.write(what[loop]);
outputStream.flush();
}
byte [] readData(InputStreamReader inputStream, int nBytes) throws Exception
{
byte [] output = new byte[nBytes];
for(int loop=0; loop<nBytes; loop++)
{
int curByte = inputStream.read();
if (curByte == -1)
throw new Exception("short read after " + loop + " of " + nBytes + " bytes");
output[loop] = (byte)curByte;
}
System.out.println("read: " + new String(output));
return output;
}
void sendClientType(OutputStreamWriter outputStream) throws Exception
{
String clientType = "EntropybrokerConnector (Java) v0.1";
String loginMessage = String.format("0006%04d", clientType.length()) + clientType;
writeData(outputStream, loginMessage);
}
private void connect() throws Exception
{
socket = new Socket(host, port);
inputStream = new InputStreamReader (socket.getInputStream());
outputStream = new OutputStreamWriter(socket.getOutputStream());
// tell what kind of client this is
sendClientType(outputStream);
}
byte [] getData(int bytes) throws Exception
{
if (bytes > maxBytes)
throw new Exception("EntropybrokerConnector::getData, maximum " + maxBytes + " supported");
if (socket == null || !socket.isConnected())
{
if (socket != null)
socket.close();
connect();
}
// request data
String requestData = String.format("0001%04d", bytes * 8);
writeData(outputStream, requestData);
// get request reply
byte [] reply = readData(inputStream, 8);
String replyString = new String(reply);
int nBits = Integer.valueOf(replyString.substring(4));
int nBytes = (nBits + 7) / 8;
if (replyString.substring(0, 3).equals("900") )
{
throw new Exception("Server is not refusing to send data");
}
return readData(inputStream, nBytes);
}
void putData(byte [] bytes) throws Exception
{
int whatLength = bytes.length;
if (socket == null || !socket.isConnected())
{
if (socket != null)
socket.close();
connect();
}
if (whatLength > maxBytes)
{
whatLength = maxBytes;
throw new Exception("EntropybrokerConnector::putData, maximum " + maxBytes + " supported");
}
// send send-request
String requestData = String.format("0002%04d", whatLength * 8);
writeData(outputStream, requestData);
// get send-request reply
byte [] reply = readData(inputStream, 8);
String replyString = new String(reply);
int nBits = Integer.valueOf(replyString.substring(4));
int nBytes = (nBits + 7) / 8;
if (replyString.substring(0, 4).equals("0001"))
{
writeData(outputStream, bytes);
}
else if (replyString.substring(0, 4).equals("9001"))
{
throw new Exception("Error code from server: all pools full, sleep " + replyString.substring(4) + " seconds");
}
}
}
|