package edu.berkeley.fleet.slipway;
+import edu.berkeley.fleet.api.*;
import java.io.*;
import java.net.*;
import java.util.*;
+import java.util.concurrent.*;
-public class Client {
+public class Client extends FleetProcess {
- public static void main(String[] args) throws Exception {
- Socket s = new Socket(InetAddress.getByName("bee441.cs.berkeley.edu"), 3133);
+ private Socket s;
+ private BlockingQueue<Long> queue = new LinkedBlockingQueue<Long>();
+
+ public void invokeInstruction(Instruction i) {
+ throw new RuntimeException("not implemented");
+ }
+
+ public long readWord() {
+ if (isTerminated())
+ throw new RuntimeException("this fleet has been terminated");
+ try {
+ return queue.take();
+ } catch (InterruptedException e) { throw new RuntimeException(e); }
+ }
+
+ protected void _terminate() {
+ try {
+ s.close();
+ } catch (Exception e) { e.printStackTrace(); }
+ }
+
+ public Client(byte[] program) throws IOException { this("superbowl.bit", program); }
+ public Client(String bitfile, byte[] program) throws IOException {
+ s = new Socket(InetAddress.getByName("bee441.cs.berkeley.edu"), 3133);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
- String bitfile = args.length==0 ? "main.bit" : args[0];
pw.print(Server.pass_string+" "+bitfile+"\n");
pw.flush();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buf = new byte[1024];
- while(true) {
- int numread = System.in.read(buf, 0, buf.length);
- if (numread==-1) break;
- baos.write(buf, 0, numread);
- }
- byte[] program = baos.toByteArray();
int numinstrs = (program.length / 6);
os.write((numinstrs >> (5*8)) & 0xff);
os.write((numinstrs >> (4*8)) & 0xff);
os.write((numinstrs >> (2*8)) & 0xff);
os.write((numinstrs >> (1*8)) & 0xff);
os.write((numinstrs >> (0*8)) & 0xff);
-
os.write(program);
os.flush();
- InputStream is = s.getInputStream();
- System.err.println("program uploaded...");
- while(true) {
- long result = 0;
- int val = 0;
- for(int i=0; i<6; i++) {
- val = is.read();
- if (val==-1) break;
- result |= ((long)val) << (i * 8);
+ final InputStream is = s.getInputStream();
+ new Thread() {
+ public void run() {
+ try {
+ while(true) {
+ long result = 0;
+ int val = 0;
+ for(int i=0; i<6; i++) {
+ val = is.read();
+ if (val==-1) break;
+ result |= ((long)val) << (i * 8);
+ }
+ if (val==-1) break;
+ queue.put(result);
+ }
+ } catch (SocketException e) {
+ } catch (Exception e) { throw new RuntimeException(e);
+ } finally { terminate(); }
}
- if (val==-1) break;
+ }.start();
+ }
+
+ public static void main(String[] args) throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] buf = new byte[1024];
+ while(true) {
+ int numread = System.in.read(buf, 0, buf.length);
+ if (numread==-1) break;
+ baos.write(buf, 0, numread);
+ }
+ Client client = new Client(args.length==0 ? "main.bit" : args[0], baos.toByteArray());
+ while(true) {
+ long result = client.readWord();
System.err.print(result);
System.err.print(" 0x");
System.err.print(Long.toString(result, 16));