Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/nchaimov/cabs
Browse files Browse the repository at this point in the history
Conflicts:
	MessageReader.java
	src/engine/LocalEngine.java
	src/engine/MessageReader.java
	src/engine/RemoteEngine.java
	src/net/Message.java
	src/net/MessageReader.java
	src/world/Agent.java
	src/world/LocalCell.java
  • Loading branch information
Tom Stellard committed Dec 6, 2009
2 parents 8c0bfbc + 19be8c1 commit 1138a0c
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 186 deletions.
7 changes: 7 additions & 0 deletions log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootCategory = debug, stdout
log4j.rootLogger=debug, stdout
88 changes: 41 additions & 47 deletions src/engine/LocalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Stack;
import java.nio.channels.SocketChannel;
import net.Message;
import net.Message.OfferHelpResponse;
import net.Message.ReceivedAgent;
Expand All @@ -26,29 +25,28 @@
import world.impl.Rabbit;

public class LocalEngine extends Engine {

LocalCell[][] cells;
ArrayList<RemoteEngine> peerList;
int globalWidth;
int globalHeight;
int turn = 0;
public int turn = 0;
boolean rollback = false;
HashMap<Integer, ArrayList<byte[]>> states;
PriorityQueue<Message> recvdMessages;
public PriorityQueue<Message> recvdMessages;
LinkedList<Message> processedMessages;
PriorityQueue<Message> sentMessages;

CellGrid gui;

Random random = new Random();

public LocalEngine(int tlx, int tly, int width, int height,
int globalWidth, int globalHeight) {
super(tlx, tly, width, height);
this.states = new HashMap<Integer, ArrayList<byte[]>>();
this.recvdMessages = new PriorityQueue<Message>(8,
Message.sendTurnComparator);
this.sentMessages = new PriorityQueue<Message>(8,
Message.reverseSendTurnComparator);
this.recvdMessages = new PriorityQueue<Message>(8, Message.sendTurnComparator);
this.sentMessages = new PriorityQueue<Message>(8, Message.reverseSendTurnComparator);
this.processedMessages = new LinkedList<Message>();
this.globalWidth = globalWidth;
this.globalHeight = globalHeight;
Expand Down Expand Up @@ -76,7 +74,6 @@ private void saveState() {

private void rollback(int turn) {
System.err.println("Rolling back from turn " + this.turn + " to turn " + turn);
// TODO: Send off anti-message queue.
rollback = true;
ArrayList<byte[]> state = states.get(turn);
for (byte[] b : state) {
Expand All @@ -92,6 +89,7 @@ private void rollback(int turn) {
* "Rolling back cell ({0}, {1}); {2} agents.", x, y, count));
*/
LocalCell cell = getCell(x, y);

cell.agents.clear();
while (count-- != 0) {
cell.add(Agent.read(dis));
Expand All @@ -100,44 +98,42 @@ private void rollback(int turn) {
e.printStackTrace();
}
}

// Put rolled-back events back onto the incoming queue
for(Message m : processedMessages) {
if(m.sendTurn >= turn) {
for (Message m : processedMessages) {
if (m.sendTurn >= turn) {
recvdMessages.offer(m);
}
}

// Send antimessages
while(!this.sentMessages.isEmpty() && sentMessages.peek().sendTurn >= turn) {
while (!this.sentMessages.isEmpty() && sentMessages.peek().sendTurn >= turn) {
Message msg = sentMessages.poll();
msg.sendMessage( peerList.get(msg.id).out );
msg.sendMessage(peerList.get(msg.id).out);
}

this.turn = turn;
}

public void go() {

while (turn < 50) {
// TODO: Remove this only for testing
/*
if (turn == 10) {
rollback(3);
}
*/
* if (turn == 10) { rollback(3); }
*/

if (!rollback) {
turn++;
saveState();
}

try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Starting turn " + turn);
for (LocalCell[] cell : cells) {
for (LocalCell element : cell) {
Expand All @@ -159,23 +155,22 @@ public void go() {
System.out.println("Ending turn " + turn);
}
}

public void moveAgent(Agent agent, LocalCell oldCell, int x, int y) {
Cell newCell = findCell(oldCell.getX() + x, oldCell.getY() + y);
newCell.add(agent);
oldCell.remove(agent);
}

private Cell findRemoteCell(int x, int y) {
for (int i = 0; i < peerList.size(); i++) {
if (peerList.get(i).hasCell(x, y)) {
if (peerList.get(i).hasCell(x, y))
return peerList.get(i).findCell(x, y);
}
}
System.err.println("Didn't find remote cell: " + x + ", " + y);
return null;
}

@Override
public Cell findCell(int x, int y) {
if (y >= globalHeight) {
Expand All @@ -190,29 +185,28 @@ public Cell findCell(int x, int y) {
if (x < 0) {
x = (x % globalWidth) + globalWidth;
}
if (hasCell(x, y)) {
if (hasCell(x, y))
return getCell(x, y);
} else {
else
return findRemoteCell(x, y);
}
}

public LocalCell getCell(int x, int y) {
return cells[y - tly][x - tlx];
}

public void placeAgent(int x, int y, Agent agent) {
LocalCell cell = getCell(x, y);
cell.add(agent);
}

public void placeAgents(int agents) {
for (int i = 0; i < agents; i++) {
LocalCell cell = getCell(0, i);
cell.add(new Rabbit());
}
}

public void print() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Expand All @@ -230,6 +224,7 @@ public void print() {
}

private void handleMessages() {

try {
// It is OK to check if recvdMessages is empty without
// synchronizing,
Expand All @@ -250,7 +245,7 @@ private void handleMessages() {
case Message.SENDAGENT:

ReceivedAgent newAgent = message.recvAgent();
this.placeAgent(newAgent.getX(), newAgent.getY(), newAgent.getAgent());
this.placeAgent(newAgent.x, newAgent.y, newAgent.agent);
this.processedMessages.add(message);
break;
case Message.ENDTURN:
Expand All @@ -268,9 +263,10 @@ private void sendCells(RemoteEngine remote) {
int rHeight = this.height;
int rTlx = this.width - rWidth;
int rTly = 0;

this.width = this.width - rWidth;
Message.sendOfferHelpResp(remote.out, rTlx, rTly, rWidth, rHeight,
globalWidth, globalHeight, tlx, tly, width, height);
Message.sendOfferHelpResp(remote.out, rTlx, rTly, rWidth, rHeight, globalWidth,
globalHeight, tlx, tly, width, height);
for (int i = rTlx; i < rWidth; i++) {
for (int j = rTly; j < rHeight; j++) {
LocalCell cell = getCell(i, j);
Expand All @@ -288,31 +284,31 @@ private void sendCells(RemoteEngine remote) {
gui = new CellGrid(height, width, tlx, tly);

}

public void storeAntimessage(Message message) {
message.sign = false;
synchronized(sentMessages) {
synchronized (sentMessages) {
sentMessages.offer(message);
}

}

public static void main(String[] args) {

int globalWidth = 10;
int globalHeight = 10;
int port = 1234;
LocalEngine engine = null;
boolean isClient = false;
try {

// Client case
if (args.length == 1) {
isClient = true;
// Use multicast instead.
InetAddress other = InetAddress.getByName(args[0]);
Socket socket = new Socket(other, port);
//TODO Remove magic number.
// TODO Remove magic number.
RemoteEngine server = new RemoteEngine(socket, 0);
Message.sendOfferHelpReq(server.out);
OfferHelpResponse r = Message.recvOfferHelpResp(server.in);
Expand All @@ -329,17 +325,15 @@ public static void main(String[] args) {
// Server case
else {
// TODO: Don't hard code everything.
engine = new LocalEngine(0, 0, globalWidth, globalHeight,
globalWidth, globalHeight);
engine = new LocalEngine(0, 0, globalWidth, globalHeight, globalWidth, globalHeight);
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
//TODO Remove magic number.
RemoteEngine client = new RemoteEngine(clientSocket, engine, 0);
// This is to read the offerHelpReq message. This
// should be in a method.
if (client.in.read() != Message.OFFERHELP) {
if (client.in.read() != Message.OFFERHELP)
throw new Exception("Expected offer help request.");
}
client.listen();
// TODO: Use a smart algorithm to figure out what
// coordinates to assign the other node.
Expand Down
19 changes: 10 additions & 9 deletions src/engine/RemoteEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,42 @@
import java.io.DataOutputStream;
import java.io.DataInputStream;
import net.Message;
import net.MessageReader;
import world.Agent;
import world.Cell;
import world.RemoteCell;

public class RemoteEngine extends Engine {

Socket socket;
InputStream in;
OutputStream out;
LocalEngine localEngine;
MessageReader reader;
Thread readerThread;
int id;
public RemoteEngine(Socket socket, int id){

public RemoteEngine(Socket socket, int id) {
this.socket = socket;
this.id = id;
try{
try {
this.out = socket.getOutputStream();
this.in = socket.getInputStream();
}catch(Exception e){
e.printStackTrace();
}
}

public RemoteEngine(Socket socket, LocalEngine localEngine, int id) {
this(socket, id);
this.localEngine = localEngine;
}

public void setEngine(LocalEngine engine) {
this.localEngine = engine;
}
public void listen(){

public void listen() {
reader = new MessageReader(localEngine, in);
readerThread = new Thread(reader);
readerThread.start();
Expand All @@ -52,7 +53,7 @@ public Cell findCell(int x, int y) {
// the message protocol.
return new RemoteCell(x, y, this);
}

public void sendAgent(RemoteCell newCell, Agent agent) {
// TODO: Send a 'sendAgent' request to the remote machine using
// the message protocol.
Expand Down
Loading

0 comments on commit 1138a0c

Please sign in to comment.