package communication;
import BasicDataType.MapUpdate;
import lejos.nxt.Button;
import java.util.Random;
public class CommunicationTest implements Runnable, CommunicationListener {
Communicator com;
public void run(){
int id = (byte)Node.localNode().getIdentifier();
com = Communicator.createStandardCommunicator();
//I'm Master
Random rand = new Random(4123432);
int randnum;
int n=1000;
int n2= 127;
if(com.isMaster()){
System.out.println("Master");
com.addListener(this);
try{
Thread.sleep(1000);
}catch(Exception e){
System.out.println("Thread sleep exception");
}
//System.out.println("Trying to send");
for (int i=0; i<127;i++){
byte x = (byte)((3 * id + i) % 20);
byte y = (byte)((125 * id + i) %20);
MapUpdate mapup = new MapUpdate((byte)(id+1), x, y);
com.sendMapUpdate(mapup);
try{
Thread.sleep(1000);
}catch(Exception e){
System.out.println("Thread sleep exception");
}
}
try{
Thread.sleep((int)rand.nextInt(n+1));
}catch(Exception e){
System.out.println("Thread sleep exception");
}
Button.waitForPress();
com.removeListener(this);
}
else{
System.out.println("Slave");
com.addListener(this);
for (int i=0; i<127;i++){
byte x = (byte)((3 * id + i) % 20);
byte y = (byte)((125 * id + i) % 20);
if(id==3){
MapUpdate mapup = new MapUpdate((byte)-1, x, y);
}
MapUpdate mapup = new MapUpdate((byte)(id+1), x, y);
com.sendMapUpdate(mapup);
try{
Thread.sleep(1000);
}catch(Exception e){
System.out.println("Thread sleep exception");
}
}
Button.waitForPress();
com.removeListener(this);
}
//Coord Expvalue=1 Free X=1, Y=1
}
public void signal(){
}
public void receiveMapUpdate(MapUpdate update)
{
System.out.println("Expvalue: "+update.getExpValue()+"/n") ;
System.out.println("X value: "+update.getCoordX()+"/n") ;
System.out.println("Y value: "+update.getCoordY()+"/n") ;
}
/* public void sendMapUpdate(MapUpdate update){
com.sendMapUpdate(update);
}*/
}