anges
authorbdemsky <bdemsky>
Sat, 7 Mar 2009 01:30:00 +0000 (01:30 +0000)
committerbdemsky <bdemsky>
Sat, 7 Mar 2009 01:30:00 +0000 (01:30 +0000)
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerThread.java

index c35038f146bf11daa1b0d33bf9a83ce79b3f72c4..2d76e454ad3176bca077eabf393e08731ab5f0ed 100644 (file)
@@ -28,10 +28,11 @@ public class RainForestClient {
     Random rand = new Random(seed);
     RainForestClient rfc = new RainForestClient();
     Socket sock = new Socket("dw-8.eecs.uci.edu",9002);
+    SocketInputStream si=new SocketInputStream(sock);
 
     /* Read player type from Server */
     byte b[] = new byte[1]; //read planter or lumber jack
-    String str1 = rfc.readFromSock(sock, b, 1);
+    String str1 = rfc.readFromSock(si, 1);
     String str = str1.subString(0, 1);
     int person;
     if(str.equalsIgnoreCase("L")) {
@@ -76,7 +77,7 @@ public class RainForestClient {
       //
       String rstr;
       while(true) {
-        rstr = rfc.readFromSock(sock, buf, 9);
+        rstr = rfc.readFromSock(si, 9);
         buf = rstr.getBytes();
         str1 = rstr.subString(0, 1);
 
@@ -99,7 +100,7 @@ public class RainForestClient {
       rfc.doOneMove(land, gamer, sock);
 
       //Receive ACK from server and do player updates
-      rstr = rfc.readFromSock(sock, b, 1);
+      rstr = rfc.readFromSock(si, 1);
       str1 = rstr.subString(0, 1);
 
       //
@@ -362,14 +363,14 @@ public class RainForestClient {
     return b;
   }
 
-  String readFromSock(Socket sock, byte[] buf, int maxBytes) {
-    int numbytes = 0;
-    String rstr = new String("");
-    while(numbytes < maxBytes) {
-      int nread = sock.read(buf);
-      numbytes += nread;
-      rstr = rstr.concat((new String(buf)).subString(0,nread));
-    }
-    return rstr;
+
+  /**
+   ** Repeated read until you get all bytes
+   **/
+  String readFromSock(SocketInputStream si, int maxBytes) {
+    byte []b=new byte[maxBytes];
+    if (si.readAll(b)<0)
+        System.out.println("Error\n");
+    return new String(b);
   }
 }
index a6096609788813a59e0f8f1c79d910a69567d438..e7dd238085dab35c695fa9a95b0ac025c6864888 100644 (file)
@@ -8,6 +8,7 @@
 public class RainForestServerThread extends Thread {
   GameMap[][] land;
   Socket sock;
+  SocketInputStream si;
   int rows;
   int cols;
   int id;
@@ -18,6 +19,7 @@ public class RainForestServerThread extends Thread {
     this.rows = rows;
     this.cols = cols;
     this.id = id;
+    this.si=new SocketInputStream(sock);
   }
 
   public void run() {
@@ -39,7 +41,7 @@ public class RainForestServerThread extends Thread {
 
     while(true) {
       /* Check for termination character */
-      String readStr = readFromSock(sock, buf, 5); 
+      String readStr = readFromSock(5); 
       String str1 = readStr.subString(0, 1);
 
       /* terminate if opcode sent is "t" */
@@ -65,7 +67,7 @@ public class RainForestServerThread extends Thread {
         sock.write(fillBytes(0, 0, buffer));
 
         /* Read client's move */
-        readStr = readFromSock(sock, buffer, 9);
+        readStr = readFromSock(9);
         str1 = readStr.subString(0, 1);
         buffer = readStr.getBytes();
 
@@ -234,17 +236,16 @@ public class RainForestServerThread extends Thread {
     //
   }
 
+
+
+
   /**
    ** Repeated read until you get all bytes
    **/
-  String readFromSock(Socket sock, byte[] buf, int maxBytes) {
-    int numbytes = 0;
-    String rstr = new String("");
-    while(numbytes < maxBytes) {
-      int nread = sock.read(buf);
-      numbytes += nread;
-      rstr = rstr.concat((new String(buf)).subString(0,nread));
-    }
-    return rstr;
+  String readFromSock(int maxBytes) {
+    byte []b=new byte[maxBytes];
+    if (si.readAll(b)<0)
+       System.out.println("Error\n");
+    return new String(b);
   }
 }