changes and some bug fixes
authoradash <adash>
Sat, 7 Mar 2009 01:16:42 +0000 (01:16 +0000)
committeradash <adash>
Sat, 7 Mar 2009 01:16:42 +0000 (01:16 +0000)
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerExample.java
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerThread.java
Robust/src/Benchmarks/Distributed/RainForest/java/makefile

index fb219864aebdf3216efd09f692bc38ea10772012..c35038f146bf11daa1b0d33bf9a83ce79b3f72c4 100644 (file)
@@ -1,5 +1,5 @@
-#define ROW                 100  /* columns in the map */
-#define COLUMN              100  /* rows of in the map */
+#define ROW                 200  /* columns in the map */
+#define COLUMN              200  /* rows of in the map */
 #define ROUNDS              200   /* Number of moves by each player */
 #define PLAYERS             20   /* Number of Players when num Players != num of client machines */
 #define RATI0               0.5  /* Number of lumberjacks to number of planters */
@@ -31,11 +31,8 @@ public class RainForestClient {
 
     /* Read player type from Server */
     byte b[] = new byte[1]; //read planter or lumber jack
-    int numbytes;
-    while((numbytes = sock.read(b)) < 1) {
-      ;
-    }
-    String str = (new String(b)).subString(0, numbytes);
+    String str1 = rfc.readFromSock(sock, b, 1);
+    String str = str1.subString(0, 1);
     int person;
     if(str.equalsIgnoreCase("L")) {
       person = LUMBERJACK;
@@ -44,6 +41,10 @@ public class RainForestClient {
       person = PLANTER;
     }
 
+    // Barriers for syncronization
+    Barrier barr;
+    barr =  new Barrier("128.195.175.79");
+
     //Generate a random x and y coordinate to start with
     int maxValue = ROW - 1;
     int minValue = 1;
@@ -64,9 +65,6 @@ public class RainForestClient {
       }
     }
     byte buf[] = new byte[9];
-    String temp;
-    Barrier barr;
-    barr =  new Barrier("128.195.175.79");
     for(int i = 0; i<ROUNDS; i++) {
       // 
       // Send the continue to read
@@ -76,12 +74,19 @@ public class RainForestClient {
       //
       //Read information of land object from Server
       //
+      String rstr;
       while(true) {
-        numbytes = sock.read(buf);
-        temp = (new String(buf)).subString(0, numbytes);
-        if(temp.startsWith("sentLand"))
+        rstr = rfc.readFromSock(sock, buf, 9);
+        buf = rstr.getBytes();
+        str1 = rstr.subString(0, 1);
+
+        /* terminate if opcode sent is "O" */
+        if(str1.equalsIgnoreCase("O")) {
+          //System.println("Receive termination char O");
           break;
-        rfc.extractCoordinates(buf, land);
+        } else {
+          rfc.extractCoordinates(buf, land);
+        }
       }
 
       //Debug 
@@ -94,21 +99,27 @@ public class RainForestClient {
       rfc.doOneMove(land, gamer, sock);
 
       //Receive ACK from server and do player updates
-      while((numbytes = sock.read(b)) < 1) {
-        ;
-      }
-      if((b[0] == (byte)'S') && ((gamer.action == 'C') || gamer.action == 'P')) {
-        //Update position and boundaries
+      rstr = rfc.readFromSock(sock, b, 1);
+      str1 = rstr.subString(0, 1);
+
+      //
+      //Update player position and player boundaries if success
+      //
+      if(str1.equalsIgnoreCase("S") && ((gamer.action == 'C') || gamer.action == 'P')) {
         gamer.setNewPosition(ROW, COLUMN, BLOCK);
         gamer.setState(INIT);
       }
       
+      if(str1.equalsIgnoreCase("F")) {
       //Retry if failure
-      if(b[0] == (byte)'F'){
-        i--;          
+        //i--;          
+        gamer.setNewPosition(ROW, COLUMN, BLOCK);
+        gamer.setState(INIT);
       }
 
+      //
       //Synchronize threads
+      //
       Barrier.enterBarrier(barr);
     }
 
@@ -122,6 +133,9 @@ public class RainForestClient {
   public void extractCoordinates(byte[] b, GameMap[][] land) {
     int posX = getX(b); 
     int posY = getY(b);
+    if(posX >= ROW && posY >= COLUMN) {
+      System.println("b[0] = "+(char)b[0]+" b[1]= "+(char)b[1]+" b[2]= "+(char)b[2]+" b[3]= "+(char)b[3]+" b[4]= "+(char)b[4]+" b[5]= "+(char)b[5]+" b[6]= "+(char)b[6]+" b[7]= "+(char)b[7]+ " b[8]= " +(char)b[8]);
+    }
 
     if(b[0] == (byte) 'T') {
       land[posX][posY].putTree(new TreeType());
@@ -168,8 +182,8 @@ public class RainForestClient {
 
     //
     //Debug
+    //printLand(land, ROW, COLUMN); 
     //
-    /* printLand(land, ROW, COLUMN); */
 
     // 2. Get type of player (lumberjack or planter)
     int type = gamer.kind();
@@ -203,9 +217,9 @@ public class RainForestClient {
       /* Reset state if there in no path from start to goal */
       if(newpath == null) {
         //
-        //Debug
+        // Debug
+        // System.println("Path from ("+currx+","+curry+") to ("+gamer.getGoalX()+","+gamer.getGoalY()+") is null"); 
         //
-        /* System.println("Path from ("+currx+","+curry+") to ("+gamer.getGoalX()+","+gamer.getGoalY()+") is null"); */
 
         gamer.reset(land, ROW, COLUMN, BLOCK);
         gamer.setState(INIT);
@@ -347,4 +361,15 @@ 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;
+  }
 }
index 6dde612d6d988421b1d2b2d1264679100c0cae75..094852d7d1030fdaa553982d1df231bba862c49d 100644 (file)
@@ -1,5 +1,5 @@
-#define ROW                 100  /* columns in the map */
-#define COLUMN              100  /* rows of in the map */
+#define ROW                 200  /* columns in the map */
+#define COLUMN              200  /* rows of in the map */
 
 public class RainForestServerExample {
   private int numThreads;
@@ -70,6 +70,7 @@ public class RainForestServerExample {
     }
 
     System.printString("Finished\n");
+    //System.exit(0);
   }
 
   /**
index ebd370a9810ffc14bec0d1b391e36f19617380e0..a6096609788813a59e0f8f1c79d910a69567d438 100644 (file)
@@ -31,62 +31,51 @@ public class RainForestServerThread extends Thread {
 
     //
     //Debugging
+    //printLand(land, rows, cols); 
     //
-    /* printLand(land, rows, cols); */
 
     byte[] buf = new byte[5];    //1 byte to decide if terminate or continue + 4 bytes for getting the round index
-    int numbytes;
     byte[] buffer = new byte[9]; //1 byte presence of tree/rocks + 8 bytes for their x and y coordinates
 
     while(true) {
-
       /* Check for termination character */
-      while((numbytes = sock.read(buf)) < 5) {
-        //System.println("Looping in 2");
-        ;
-      }
-
-      String str1 = (new String(buf)).subString(0, 1);
+      String readStr = readFromSock(sock, buf, 5); 
+      String str1 = readStr.subString(0, 1);
 
       /* terminate if opcode sent is "t" */
       if(str1.equalsIgnoreCase("T")) {
         break;
       } else if(str1.equalsIgnoreCase("U")) {
+        buf = readStr.getBytes();
         int roundIndex = getX(buf); //Get the index from client
-        System.println("round id = " + roundIndex);
+
+        /* Update age of all trees in a Map */
         if((roundIndex % AGEUPDATETHRESHOLD) == 0 && (id == 0)) { 
-          /* Update age of all trees in a Map */
           updateAge(land, MAXAGE, rows, cols);
         }
 
-        /* Data representing presence/absence of trees */
+        /* Send data representing presence/absence of trees */
         for(int i=0 ; i<rows; i++) {
           for(int j=0; j<cols; j++) {
             sock.write(fillBytes(land, i, j, buffer));
           }
         }
 
-        StringBuffer header = new StringBuffer("sentLand");
-        String temp_str = new String(header);
-        sock.write(temp_str.getBytes());
-        header = null;
-        temp_str = null;
+        /* Send special character "O" to end transfer of land coordinates */ 
+        sock.write(fillBytes(0, 0, buffer));
 
         /* Read client's move */
-        while((numbytes = sock.read(buffer)) < 9) {
-          ;
-        }
-        str1 = (new String(buffer)).subString(0, 1);
+        readStr = readFromSock(sock, buffer, 9);
+        str1 = readStr.subString(0, 1);
+        buffer = readStr.getBytes();
 
         /* Take actions such as cutting or planting or moving */
         if(str1.equalsIgnoreCase("C")) {
           int val;
           if((val = doTreeCutting(land, buffer)) == 0) {
-            System.println("Cutting\n");
-            b[0] = (byte)'S';
+            b[0] = (byte)'S'; //success in move
           } else {
-            System.println("Retrying to cut\n");
-            b[0] = (byte)'F';
+            b[0] = (byte)'F';//failure in move
           }
           sock.write(b);
         }
@@ -94,17 +83,14 @@ public class RainForestServerThread extends Thread {
         if(str1.equalsIgnoreCase("P")) {
           int val;
           if((val = doTreePlanting(land, buffer)) == 0) {
-            System.println("Planting\n");
             b[0] = (byte)'S';
           } else {
-            System.println("Retrying to plant\n");
             b[0] = (byte)'F';
           }
           sock.write(b);
         }
 
         if(str1.equalsIgnoreCase("M")) {
-          System.println("Moving to goal\n");
           b[0] = (byte)'S';
           sock.write(b);
         }
@@ -140,6 +126,20 @@ public class RainForestServerThread extends Thread {
     return b;
   }
 
+  byte[] fillBytes(int x, int y, byte[] b) {
+    if(x == 0 && y == 0) 
+      b[0] = (byte)'O'; //land object coordinates transfer to client over
+    for(int i = 1; i<5; i++) {
+      int offset = (3-(i-1)) * 8;
+      b[i] = (byte) ((x >> offset) & 0xFF);
+    }
+    for(int i = 5; i<9; i++) {
+      int offset = (3-(i-5)) * 8;
+      b[i] = (byte) ((y >> offset) & 0xFF);
+    }
+    return b;
+  }
+
   /**
    ** Only for Debugging 
    **/
@@ -233,4 +233,18 @@ public class RainForestServerThread extends Thread {
     //System.println("Tree count=  "+countTrees); 
     //
   }
+
+  /**
+   ** 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;
+  }
 }
index 129108606a2ed9a69796676097e7613449678172..cbe1ecafd2e7ca8e9ed18d85fb812d8a71b64e5a 100644 (file)
@@ -8,28 +8,27 @@ SRC=tmp${MAINCLASS1}.java \
        Player.java \
        BarrierNonDSM.java
 
-SRC1 =tmp${MAINCLASS2}.java \
-         GameMap.java \
-         TreeType.java \
-         RockType.java \
-         Goal.java \
-         Path.java \
-         Node.java \
-         Player.java \
-         AStarPathFinder.java \
-         BarrierNonDSM.java
+SRC1=tmp${MAINCLASS2}.java \
+       GameMap.java \
+       TreeType.java \
+       RockType.java \
+       Goal.java \
+       Path.java \
+       Node.java \
+       Player.java \
+       AStarPathFinder.java \
+       BarrierNonDSM.java
 
-         
 FLAGS1= -thread -nooptimize -debug -mainclass ${MAINCLASS1}
 FLAGS2= -thread -nooptimize -debug -mainclass ${MAINCLASS2}
 
 default:
-       cpp RainForestServerExample.java > tmp1RainForestServerExample.java
-       cpp RainForestServerThread.java > tmp1RainForestServerThread.java
-       cpp RainForestClient.java > tmp1RainForestClient.java
-       ./extractLines
-       ../../../../buildscript ${FLAGS1} -o Server ${SRC}
-       ../../../../buildscript ${FLAGS2} -o Client ${SRC1}
+         cpp RainForestServerExample.java > tmp1RainForestServerExample.java
+         cpp RainForestServerThread.java > tmp1RainForestServerThread.java
+         cpp RainForestClient.java > tmp1RainForestClient.java
+         ./extractLines
+         ../../../../buildscript ${FLAGS1} -o Server ${SRC}
+         ../../../../buildscript ${FLAGS2} -o Client ${SRC1}
 
 clean:
        rm -rf tmpbuilddirectory