-#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 */
/* 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;
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;
}
}
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
//
//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
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);
}
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());
//
//Debug
+ //printLand(land, ROW, COLUMN);
//
- /* printLand(land, ROW, COLUMN); */
// 2. Get type of player (lumberjack or planter)
int type = gamer.kind();
/* 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);
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;
+ }
}
-#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;
}
System.printString("Finished\n");
+ //System.exit(0);
}
/**
//
//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);
}
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);
}
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
**/
//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;
+ }
}
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