Skip to content

Commit

Permalink
Misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaimov committed Nov 29, 2009
1 parent cb20a44 commit 4f6c7e7
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 202 deletions.
26 changes: 15 additions & 11 deletions src/engine/Engine.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package engine;

import world.Cell;

public abstract class Engine {

int tlx;
int tly;
int width;
int height;


protected int tlx;
protected int tly;
protected int width;
protected int height;
protected int turn;

public abstract Cell findCell(int x, int y);

public Engine(){}

public Engine() {
}

public Engine(int tlx, int tly, int width, int height) {
this.tlx = tlx;
this.tly = tly;
this.width = width;
this.height = height;
}

public Boolean hasCell(int x, int y) {
return x >= tlx && y >= tly && x < tlx + width && y < tly + height;
}

public void setCoordinates(int tlx, int tly, int width, int height){
public void setCoordinates(int tlx, int tly, int width, int height) {
this.tlx = tlx;
this.tly = tly;
this.width = width;
this.height = height;
}

}
107 changes: 56 additions & 51 deletions src/engine/LocalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import world.impl.Rabbit;

public class LocalEngine extends Engine {

LocalCell[][] cells;
ArrayList<RemoteEngine> peerList;
int globalWidth;
int globalHeight;

public LocalEngine(int tlx, int tly, int width, int height, int globalWidth, int globalHeight) {

public LocalEngine(int tlx, int tly, int width, int height,
int globalWidth, int globalHeight) {
super(tlx, tly, width, height);
this.globalWidth = globalWidth;
this.globalHeight = globalHeight;
Expand All @@ -33,30 +34,31 @@ public LocalEngine(int tlx, int tly, int width, int height, int globalWidth, int
}
}
}

public void go(int turn) {
for (LocalCell[] cell : cells) {
for (LocalCell element : cell) {
element.go(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 @@ -72,31 +74,31 @@ public Cell findCell(int x, int y) {
x = (x % globalWidth) + globalWidth;
}
if (hasCell(x, y)) {
return getCell(x ,y);
return getCell(x, y);
} else {
return findRemoteCell(x, y);
}
}

public LocalCell getCell(int x, int 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);
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++){
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
LocalCell cell = cells[i][j];
if (cell.getAgents().size() > 0) {
System.out.print("* ");
Expand All @@ -107,107 +109,110 @@ public void print() {
System.out.println();
}
}

private void handleMessages(){
for(int i=0; i< peerList.size(); i++){
private void handleMessages() {
for (int i = 0; i < peerList.size(); i++) {
int messageType = 0;
try{
ObjectInputStream in = (ObjectInputStream)peerList.get(i).in;
while(messageType != -1){
try {
ObjectInputStream in = peerList.get(i).in;
while (messageType != -1) {
messageType = in.read();
switch(messageType){
switch (messageType) {
case Protocol.SENDAGENT:
ReceivedAgent newAgent = Protocol.sendAgent(in);
this.placeAgent(newAgent.getX(), newAgent.getY(), newAgent.getAgent());
this.placeAgent(newAgent.getX(), newAgent.getY(),
newAgent.getAgent());
newAgent.getAgent().end();
break;
case Protocol.ENDTURN:
int turn = Protocol.endTurn(in);
messageType = -1;
messageType = -1;
}
}
}catch(Exception e){
} catch (Exception e) {
e.printStackTrace();
}
}
}

private void sendCells(RemoteEngine remote){
//TODO: Send agents along with cells.
private void sendCells(RemoteEngine remote) {
// TODO: Send agents along with cells.
int rWidth = this.width / 2;
int rHeight = this.height;
int rTlx = this.width - rWidth;
int rTly = 0;
Protocol.offerHelpResp(remote.out, rTlx, rTly, rWidth, rHeight, globalWidth, globalHeight);
for(int i= rTlx; i < rWidth; i++){
for(int j = rTly; j < rHeight; j++){
Protocol.offerHelpResp(remote.out, rTlx, rTly, rWidth, rHeight,
globalWidth, globalHeight);
for (int i = rTlx; i < rWidth; i++) {
for (int j = rTly; j < rHeight; j++) {
LocalCell cell = getCell(i, j);
for(Agent a : cell.getAgents()){
for (Agent a : cell.getAgents()) {
Protocol.sendAgent(remote.out, cell.getX(), cell.getY(), a);
}
}
}
remote.setCoordinates(rTlx,rTly,rWidth,rHeight);
remote.setCoordinates(rTlx, rTly, rWidth, rHeight);
this.peerList.add(remote);
//TODO: Actually change the size of the data structure that
//holds the cells.
// TODO: Actually change the size of the data structure that
// holds the cells.
this.width = this.width - rWidth;

}

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.
// Use multicast instead.
InetAddress other = InetAddress.getByName(args[0]);
Socket socket = new Socket(other, port);
RemoteEngine server = new RemoteEngine(socket);
Protocol.offerHelpReq(server.out);
OfferHelpResponse r = Protocol.offerHelpResp(server.in);
engine = new LocalEngine(r.getTlx(), r.getTly(), r.getWidth(), r.getHeight(), r.getGlobalWidth(),
r.getGlobalHeight());
engine = new LocalEngine(r.getTlx(), r.getTly(), r.getWidth(),
r.getHeight(), r.getGlobalWidth(), r.getGlobalHeight());
server.setEngine(engine);
engine.peerList.add(server);
server.setCoordinates(0, 0, 5, 10);
//TODO: Get agents from server.
// TODO: Get agents from server.
}

// 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();
RemoteEngine client = new RemoteEngine(clientSocket, engine);
//This is to read the offerHelpReq message. This
//should be in a method.
if(client.in.read() != Protocol.OFFERHELP){
// This is to read the offerHelpReq message. This
// should be in a method.
if (client.in.read() != Protocol.OFFERHELP) {
throw new Exception("Expected offer help request.");
}
// TODO: Use a smart algorithm to figure out what
// coordinates to assign the other node.
engine.sendCells(client);

// We probably need some kind of ACK here.

engine.placeAgents(5);

}
engine.print();
for (int i = 0; i < 20; i++) {
Thread.sleep(1000);
System.out.println("Starting turn " + i);
engine.go(i);
for(int j=0;j<engine.peerList.size();j++){
for (int j = 0; j < engine.peerList.size(); j++) {
Protocol.endTurn(engine.peerList.get(j).out, i);
}
engine.handleMessages();
Expand Down
26 changes: 11 additions & 15 deletions src/engine/RemoteEngine.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package engine;

import java.io.IOException;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import net.Protocol;

import world.Agent;
import world.Cell;
import world.RemoteCell;

public class RemoteEngine extends Engine {

Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
LocalEngine localEngine;

public RemoteEngine(Socket socket){
public RemoteEngine(Socket socket) {
this.socket = socket;
try{
try {
this.out = new ObjectOutputStream(socket.getOutputStream());
this.in = new ObjectInputStream(socket.getInputStream());
}catch(Exception e){
} catch (Exception e) {
e.printStackTrace();
}
}
Expand All @@ -34,18 +30,18 @@ public RemoteEngine(Socket socket, LocalEngine localEngine) {
this(socket);
this.localEngine = localEngine;
}

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

@Override
public Cell findCell(int x, int y) {
// TODO: Send a 'findCell' request to this remote machine using
// 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 4f6c7e7

Please sign in to comment.