Skip to content

Commit

Permalink
Use multicast DNS to locate peers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaimov committed Nov 29, 2009
1 parent 1e1f71b commit cb20a44
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 36 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added lib/log4j-1.2.15.jar
Binary file not shown.
15 changes: 7 additions & 8 deletions src/engine/LocalEngine.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package engine;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.nio.channels.SocketChannel;

import net.Protocol;
import net.Protocol.OfferHelpResponse;
import net.Protocol.ReceivedAgent;
import world.Agent;
import world.Cell;
import world.LocalCell;
Expand Down Expand Up @@ -119,8 +118,8 @@ private void handleMessages(){
switch(messageType){
case Protocol.SENDAGENT:
ReceivedAgent newAgent = Protocol.sendAgent(in);
this.placeAgent(newAgent.x, newAgent.y, newAgent.agent);
newAgent.agent.end();
this.placeAgent(newAgent.getX(), newAgent.getY(), newAgent.getAgent());
newAgent.getAgent().end();
break;
case Protocol.ENDTURN:
int turn = Protocol.endTurn(in);
Expand Down Expand Up @@ -174,8 +173,8 @@ public static void main(String[] args) {
RemoteEngine server = new RemoteEngine(socket);
Protocol.offerHelpReq(server.out);
OfferHelpResponse r = Protocol.offerHelpResp(server.in);
engine = new LocalEngine(r.tlx, r.tly, r.width, r.height, r.globalWidth,
r.globalHeight);
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);
Expand Down
2 changes: 2 additions & 0 deletions src/engine/RemoteEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

import net.Protocol;

import world.Agent;
import world.Cell;
import world.RemoteCell;
Expand Down
31 changes: 31 additions & 0 deletions src/net/Peer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net;

public class Peer {
private final String name;
private final String address;
private final int port;

public Peer(final String name, final String address, final int port) {
this.name = name;
this.address = address;
this.port = port;
}

public Peer(final String name) {
this.name = name;
this.address = "";
this.port = -1;
}

public String getName() {
return name;
}

public String getAddress() {
return address;
}

public int getPort() {
return port;
}
}
37 changes: 37 additions & 0 deletions src/net/PeerFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net;

import javax.swing.event.EventListenerList;

public abstract class PeerFinder {

protected EventListenerList listeners = new EventListenerList();

public void addPeerListener(PeerListener p) {
listeners.add(PeerListener.class, p);
}

public void removePeerListener(PeerListener p) {
listeners.remove(PeerListener.class, p);
}

public void firePeerFound(Peer p) {
for (PeerListener l : listeners.getListeners(PeerListener.class)) {
l.foundPeer(p);
}
}

public void firePeerLost(Peer p) {
for (PeerListener l : listeners.getListeners(PeerListener.class)) {
l.lostPeer(p);
}
}

public abstract void startSearching();

public abstract void stopSearching();

public abstract void register();

public abstract void unregister();

}
10 changes: 10 additions & 0 deletions src/net/PeerListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net;

import java.util.EventListener;

public interface PeerListener extends EventListener {

public void foundPeer(Peer p);

public void lostPeer(Peer p);
}
117 changes: 89 additions & 28 deletions src/engine/Protocol.java → src/net/Protocol.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package engine;
package net;

import java.io.DataInputStream;
import java.io.IOException;
Expand All @@ -10,27 +10,88 @@

import world.Agent;

class OfferHelpResponse {
int tlx;
int tly;
int width;
int height;
int globalWidth;
int globalHeight;
}

class ReceivedAgent {
int x;
int y;
Agent agent;

@Override
public String toString() {
return x + ", " + y + agent.toString();
public class Protocol {

public static class OfferHelpResponse {
private int tlx;
private int tly;
private int width;
private int height;
private int globalWidth;
private int globalHeight;
public void setTly(int tly) {
this.tly = tly;
}
public int getTly() {
return tly;
}
public void setTlx(int tlx) {
this.tlx = tlx;
}
public int getTlx() {
return tlx;
}
public void setGlobalHeight(int globalHeight) {
this.globalHeight = globalHeight;
}
public int getGlobalHeight() {
return globalHeight;
}
public void setGlobalWidth(int globalWidth) {
this.globalWidth = globalWidth;
}
public int getGlobalWidth() {
return globalWidth;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height;
}
}
}

public class Protocol {
public static class ReceivedAgent {
private int x;
private int y;
private Agent agent;

@Override
public String toString() {
return getX() + ", " + getY() + getAgent().toString();
}

public void setX(int x) {
this.x = x;
}

public int getX() {
return x;
}

public void setY(int y) {
this.y = y;
}

public int getY() {
return y;
}

public void setAgent(Agent agent) {
this.agent = agent;
}

public Agent getAgent() {
return agent;
}
}

public static final byte OFFERHELP = 0x1;
public static final byte SENDAGENT = 0x2;
Expand Down Expand Up @@ -69,12 +130,12 @@ public static OfferHelpResponse offerHelpResp(ObjectInputStream in) {
try {
// TODO verify message type
in.read();
r.tlx = in.readInt();
r.tly = in.readInt();
r.width = in.readInt();
r.height = in.readInt();
r.globalWidth = in.readInt();
r.globalHeight = in.readInt();
r.setTlx(in.readInt());
r.setTly(in.readInt());
r.setWidth(in.readInt());
r.setHeight(in.readInt());
r.setGlobalWidth(in.readInt());
r.setGlobalHeight(in.readInt());
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -104,9 +165,9 @@ public static ReceivedAgent sendAgent(ObjectInputStream in) {

try {
result = new ReceivedAgent();
result.x = in.readInt();
result.y = in.readInt();
result.agent = (Agent) in.readObject();
result.setX(in.readInt());
result.setY(in.readInt());
result.setAgent((Agent) in.readObject());

} catch (IOException e) {
// TODO Auto-generated catch block
Expand Down
114 changes: 114 additions & 0 deletions src/net/ZeroconfPeerFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package net;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

import com.apple.dnssd.BrowseListener;
import com.apple.dnssd.DNSSD;
import com.apple.dnssd.DNSSDException;
import com.apple.dnssd.DNSSDRegistration;
import com.apple.dnssd.DNSSDService;
import com.apple.dnssd.RegisterListener;
import com.apple.dnssd.ResolveListener;
import com.apple.dnssd.TXTRecord;

public class ZeroconfPeerFinder extends PeerFinder implements RegisterListener,
BrowseListener, ResolveListener {

private final Logger log = Logger.getLogger(this.getClass());

protected DNSSDRegistration localRegistration = null;
protected DNSSDService browser = null;

public static final String SERVICE_TYPE = "_cabs._tcp";

@Override
public void register() {
try {
localRegistration = DNSSD.register(null, SERVICE_TYPE, 1234, this);
} catch (DNSSDException e) {
e.printStackTrace();
}
}

@Override
public void startSearching() {
if (browser == null) {
try {
browser = DNSSD.browse(SERVICE_TYPE, this);
} catch (DNSSDException e) {
log.error("Unable to start searching for peers", e);
}
} else {
log
.info("Got request to start searching for peers, but was already searching.");
}
}

@Override
public void stopSearching() {
if (browser != null) {
browser.stop();
browser = null;
} else {
log
.info("Got request to stop searching for peers, but wasn't searching.");
}
}

@Override
public void unregister() {
if (localRegistration != null) {
localRegistration.stop();
localRegistration = null;
} else {
log
.info("Got request to unregister, but wasn't registered to begin with.");
}
}

@Override
public void serviceRegistered(DNSSDRegistration registration, int flags,
java.lang.String serviceName, java.lang.String regType,
java.lang.String domain) {
log.debug("Registered service: " + serviceName + " " + domain);
}

@Override
public void operationFailed(DNSSDService arg0, int arg1) {
log.error(MessageFormat.format("mDNS Error: {0}; Error code: {1}", arg0
.toString(), arg1));
}

@Override
public void serviceFound(DNSSDService browser, int flags, int ifIndex,
java.lang.String serviceName, java.lang.String regType,
java.lang.String domain) {

log.debug(MessageFormat.format("Found a service: {0}; {1}; {2}",
serviceName, regType, domain));
try {
DNSSD.resolve(flags, ifIndex, serviceName, regType, domain, this);
} catch (DNSSDException e) {
log.error("Error resolving found service", e);
}
}

@Override
public void serviceLost(DNSSDService browser, int flags, int ifIndex,
java.lang.String serviceName, java.lang.String regType,
java.lang.String domain) {
log.debug(MessageFormat.format("Lost a service: {0}", serviceName));
firePeerLost(new Peer(serviceName));
}

@Override
public void serviceResolved(DNSSDService resolver, int flags, int ifIndex,
java.lang.String fullName, java.lang.String hostName, int port,
TXTRecord txtRecord) {
log.debug(MessageFormat.format("Resolved a service: {0}; {1}; {2}",
fullName, hostName, port));
firePeerFound(new Peer(fullName, hostName, port));
}
}

0 comments on commit cb20a44

Please sign in to comment.