Working and stable version of Java
authoradash <adash>
Sat, 7 Mar 2009 03:49:52 +0000 (03:49 +0000)
committeradash <adash>
Sat, 7 Mar 2009 03:49:52 +0000 (03:49 +0000)
Robust/src/Benchmarks/Distributed/RainForest/java/Player.java
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestClient.java
Robust/src/Benchmarks/Distributed/RainForest/java/RainForestServerExample.java

index 66704b143a0e5eaa7d6c6b57bf5589985e70cbed..1931b38c5a4cbfbd4b1e70c915b63a1be717b461 100644 (file)
@@ -19,7 +19,6 @@ public class Player {
   private int state;
   private int goalx, goaly;
   private int rows, cols;
-  private Random rand;
   private char action;
 
   public Player(int type, int x, int y) {
@@ -50,11 +49,10 @@ public class Player {
     this.state = 0; //0 => INIT state
     this.goalx = 0;
     this.goaly = 0;
-    rand = new Random(30); //seed to generate random numbers
     this.action = 'M'; //initalized to moving
   }
 
-  public void reset(GameMap[][] land, int row, int col, int bounds) {
+  public void reset(GameMap[][] land, int row, int col, int bounds, Random rand) {
     //Teleport to new location
     if(type == 1) { //PLANTER
       x = (rand.nextInt(Math.abs(row - 2) + 1)) + 1;
@@ -118,9 +116,7 @@ public class Player {
     this.y = y;
   }
 
-  //public void setNewPosition(int x, int y, int row, int col, int bounds) {
   public void setNewPosition(int row, int col, int bounds) {
-    //setPosition(x, y);
     setBoundary(bounds, row, col);
     goalx = -1;
     goaly = -1;
@@ -148,14 +144,13 @@ public class Player {
   /** Randomly finds a goal in a given boundary for player
    ** @return 0 on success and -1 when you cannot find any new goal
    **/
-  public int findGoal(GameMap[][] land) {
+  public int findGoal(GameMap[][] land, Random rand) {
     /* Try setting the goal for try count times
      * if not possible, then select a completely new goal
      */
     int trycount = (highx - lowx) + (highy - lowy);
     int i;
 
-    Random rand = new Random(0);
     for (i = 0; i < trycount; i++) {
       int row = (rand.nextInt(Math.abs(highx - lowx)) + 1) + lowx;
       int col = (rand.nextInt(Math.abs(highy - lowy)) + 1) + lowy;
index 2d76e454ad3176bca077eabf393e08731ab5f0ed..888e150f58aaa739f9f658ee2723f4af49b609e1 100644 (file)
@@ -1,5 +1,5 @@
-#define ROW                 200  /* columns in the map */
-#define COLUMN              200  /* rows of in the map */
+#define ROW                 100  /* columns in the map */
+#define COLUMN              100  /* 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 */
@@ -27,7 +27,7 @@ public class RainForestClient {
 
     Random rand = new Random(seed);
     RainForestClient rfc = new RainForestClient();
-    Socket sock = new Socket("dw-8.eecs.uci.edu",9002);
+    Socket sock = new Socket("dc-1.calit2.uci.edu",9002);
     SocketInputStream si=new SocketInputStream(sock);
 
     /* Read player type from Server */
@@ -44,7 +44,7 @@ public class RainForestClient {
 
     // Barriers for syncronization
     Barrier barr;
-    barr =  new Barrier("128.195.175.79");
+    barr =  new Barrier("128.195.136.162");
 
     //Generate a random x and y coordinate to start with
     int maxValue = ROW - 1;
@@ -55,9 +55,9 @@ public class RainForestClient {
     Player gamer = new Player(person, row, col, ROW, COLUMN, BLOCK);
 
     //
-    //Debug
+    // Debug
+    // System.println("Person= "+person+" LocX= "+row+" LocY= "+col); 
     //
-    /* System.println("Person= "+person+" LocX= "+row+" LocY= "+col); */
 
     GameMap[][] land = new GameMap[ROW][COLUMN];
     for(int i = 0; i<ROW; i++) {
@@ -67,75 +67,108 @@ public class RainForestClient {
     }
     byte buf[] = new byte[9];
     for(int i = 0; i<ROUNDS; i++) {
-      // 
-      // Send the continue to read
-      //
-      sock.write(rfc.fillBytes("U",i));
+      String retval;
+      do {
+        retval = null;
+        // 
+        // Send the continue to read
+        //
+        sock.write(rfc.fillBytes("U",i));
 
-      //
-      //Read information of land object from Server
-      //
-      String rstr;
-      while(true) {
-        rstr = rfc.readFromSock(si, 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;
-        } else {
-          rfc.extractCoordinates(buf, land);
+        //
+        //Read information of land object from Server
+        //
+        String rstr;
+        while(true) {
+          rstr = rfc.readFromSock(si, 9);
+          buf = rstr.getBytes();
+          str1 = rstr.subString(0, 1);
+
+          /* terminate if opcode sent is "O" */
+          if(str1.equalsIgnoreCase("O")) {
+            break;
+          } else {
+            rfc.extractCoordinates(buf, land);
+          }
         }
-      }
 
-      //Debug 
-      //rfc.printLand(land, ROW, COLUMN);
+        //Debug 
+        //rfc.printLand(land, ROW, COLUMN);
 
-      //
-      //Do rounds 
-      //do one move per round and write to server
-      //
-      rfc.doOneMove(land, gamer, sock);
+        //
+        //Do rounds 
+        //do one move per round and write to server
+        //
+        rfc.doOneMove(land, gamer, sock, rand);
 
-      //Receive ACK from server and do player updates
-      rstr = rfc.readFromSock(si, 1);
-      str1 = rstr.subString(0, 1);
+        //Receive ACK from server and do player updates
+        rstr = rfc.readFromSock(si, 1);
+        retval = 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
-        //i--;          
-        gamer.setNewPosition(ROW, COLUMN, BLOCK);
-        gamer.setState(INIT);
-      }
+        //
+        //Update player position and player boundaries if success i.e. cut/plant tree
+        //Continue to the next iteration
+        //
+        if(retval.equalsIgnoreCase("S") && ((gamer.action == 'C') || gamer.action == 'P')) {
+          // 
+          // Debug
+          // if(gamer.action == 'C')
+          //   System.println("Cutting");
+          // if(gamer.action == 'P')
+          //   System.println("Planting");
+          //
+
+          gamer.setNewPosition(ROW, COLUMN, BLOCK);
+          gamer.setState(INIT);
+          break;
+        }
+
+        //
+        // If success and player moving ... continue till goal reached
+        //
+        if(retval.equalsIgnoreCase("S") && gamer.action == 'M') {
+          //System.println("Moving");
+          break;
+        }
+
+        //
+        // If server returns failure then retry 
+        // move with a new goal
+        //
+        if(retval.equalsIgnoreCase("F")) {
+          // 
+          // Debug
+          // System.println("Failure....Retry\n");
+          //
+          gamer.setNewPosition(ROW, COLUMN, BLOCK);
+          gamer.setState(INIT);
+        }
+
+      } while(retval.equalsIgnoreCase("F"));
 
       //
       //Synchronize threads
       //
       Barrier.enterBarrier(barr);
     }
-
     //
     //Special character "T" to terminate computation
     //
     sock.write(rfc.fillBytes("T", 0));
     sock.close();
+
   }
 
+  /**
+   ** Create the land Map at client's end
+   ** @param b bytes read from client
+   ** @param land The map to be filled
+   **/
   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]);
+      System.println("Error: Trying to access elements out of bounds in the array");
     }
 
     if(b[0] == (byte) 'T') {
@@ -176,7 +209,7 @@ public class RainForestClient {
    ** @param gamer The player making the move
    ** @return 0 is success , -1 otherwise
    **/
-  public int doOneMove(GameMap[][] land, Player gamer, Socket sock) {
+  public int doOneMove(GameMap[][] land, Player gamer, Socket sock, Random rand) {
     // 1. Get start(x, y) position of the player
     int currx = gamer.getX();
     int curry = gamer.getY();
@@ -194,8 +227,8 @@ public class RainForestClient {
 
       //gamer.debugPlayer(); 
 
-      if (gamer.findGoal(land) < 0) {
-        gamer.reset(land, ROW, COLUMN, BLOCK);
+      if (gamer.findGoal(land, rand) < 0) {
+        gamer.reset(land, ROW, COLUMN, BLOCK, rand);
         gamer.action = 'M';
         sock.write(fillBytes(SHIFT, 0, 0));
         return 0;
@@ -222,7 +255,7 @@ public class RainForestClient {
         // System.println("Path from ("+currx+","+curry+") to ("+gamer.getGoalX()+","+gamer.getGoalY()+") is null"); 
         //
 
-        gamer.reset(land, ROW, COLUMN, BLOCK);
+        gamer.reset(land, ROW, COLUMN, BLOCK, rand);
         gamer.setState(INIT);
         gamer.action = 'M';
         sock.write(fillBytes(SHIFT, 0, 0));
index 094852d7d1030fdaa553982d1df231bba862c49d..df1a5400e2a4e6172da8831e8f85e2b8939d201a 100644 (file)
@@ -1,5 +1,5 @@
-#define ROW                 200  /* columns in the map */
-#define COLUMN              200  /* rows of in the map */
+#define ROW                 100  /* columns in the map */
+#define COLUMN              100  /* rows of in the map */
 
 public class RainForestServerExample {
   private int numThreads;
@@ -69,8 +69,9 @@ public class RainForestServerExample {
       rft[i].join();
     }
 
+    ss.close();
     System.printString("Finished\n");
-    //System.exit(0);
+    System.exit(0);
   }
 
   /**