Raul: Thanks. I went to see the Ruby project,
but could only see snippets of code. I'll have
to get an svn client running, I guess.
I have added a directory, 'tools', at the same level
that 'client' and 'server' are on, under 'ATS'.
I have placed 'test1.java' there and this, too:
Code:
/*
* toyTws.java
*/
import java.io.*;
import java.net.*;
class Server extends Thread {
String inputFile = null;
String port = null;
public void run() {
ServerSocket ssock = null; // the server socket
Socket sock = null; // the client socket
RandomAccessFile raf = null; // the message input file
DataInputStream dis = null; // read stream from the client
DataOutputStream dos = null; // write stream to the client
long length = 0; // length of the input file
long read = 0; // no. of input file bytes read
int b; // an input byte
try {
// open the input file:
raf = new RandomAccessFile(inputFile, "r");
length = raf.length();
read = 0;
// offer a connection:
ssock = new ServerSocket(Integer.parseInt(port));
//
// wait for a connection, then
// read the client's version string
// send the server's version string to the client
// send the server's notion of the current time
// read the client's id string
// loop:
// if necessary, rewind the message file
// read a byte from the message file
// send a byte to the client
System.out.printf("Server: listening\n");
sock = ssock.accept();
dos = new DataOutputStream(sock.getOutputStream());
dis = new DataInputStream(sock.getInputStream());
System.out.printf("Server: client connected\n");
// get client version:
System.out.printf("Client version is ");
while (true) {
b = dis.readByte();
if (b == 0)
break;
else
System.out.printf("%c", b);
}
System.out.printf("\n");
// send server version:
dos.writeBytes("99");
dos.writeByte('\0');
// send server time:
dos.writeBytes("09:09:09");
dos.writeByte('\0');
// get client id
System.out.printf("Client id is ");
while (true) {
b = dis.readByte();
if (b == 0)
break;
else
System.out.printf("%c", b);
}
System.out.printf("\n");
// until an exception occurs,
// read from the input file, write to the client
while (true) {
if (read < length) {
b = raf.readByte();
read++;
dos.writeByte((byte)b);
/*
Thread.sleep(1); // approximately 1000 cps
*/
}
else {
raf.seek(0);
read = 0;
}
}
}
catch (Exception e0) {
System.out.printf("Server.run: caught %s\n", e0);
try {
ssock.close();
}
catch (Exception e1) {
System.out.printf("Server.run caught %s\n", e1);
}
try {
dis.close();
}
catch (Exception e2) {
System.out.printf("Server.run caught %s\n", e2);
}
try {
raf.close();
}
catch (Exception e3) {
System.out.printf("Server.run caught %s\n", e3);
}
try {
dos.close();
}
catch (Exception e4) {
System.out.printf("Server.run caught %s\n", e4);
}
try {
sock.close();
}
catch (Exception e5) {
System.out.printf("Server.run caught %s\n", e5);
}
System.exit(1);
}
}
public void fileName(String s) {
inputFile = s;
}
public void port(String s) {
port = s;
}
}
public class toyTws {
// usage: java toyTws [recordedMessagesFile [serverPort]]
public static void main(String args[]) {
String inputFile = null;
String port = null;
if (args.length == 0) {
inputFile = "msgs.junk";
port = "6500";
}
else if (args.length == 1) {
inputFile = args[0];
}
else if (args.length == 2) {
inputFile = args[0];
port = args[1];
}
else {
System.out.printf("usage: toyTws [file [port]]\n");
System.exit(1);
}
Server server = new Server();
server.fileName(inputFile); // server file to read msgs from
server.port(port); // server port no. to listen on
server.start();
}
}
toyTws is a server. Once a client connects to it,
it continually reads bytes from a file and sends
them to the client. It is assumed that the file is a
file containing a sequence of tws-to-api messages.
The client I have used it with is
TestJavaClient/Main, the original. To compile:
javac toyTws.java and to run:
java toyTws
I have modified what was placed in
ATS/client/TestJavaClient/recorder/com/ib/client/EReader.java
That is how the recorded tws->api messages file
can be obtained. I'll put it up later, maybe tomorrow night,
because it needs to provide an indication that it
is done writing the file, and I need to test it a little
more.