Methods Summary |
---|
public void | addPeer(Peer p)Add a new Peer. In addition to the default Peer behavior, also create
a MsgPoller for the new Peer, to listen for incoming messages.
super.addPeer(p);
try {
MsgPoller poller = new MsgPoller(p);
p.getIdentity().setProperty(POLLER, poller);
poller.start();
}
catch (Exception e) {
System.out.println("Failed to attach poller to " +
p.getIdentity().getName() + " " +
p.getIdentity().getId());
e.printStackTrace();
}
|
public boolean | connect(java.util.Properties connProps)Connect to a peer given the connection properties. The SocketPeer looks
for a "host" and "port" property in the property list, and attempts a
Socket connection to the remote agent.
boolean success = false;
String host = (String)connProps.get(HOST_PROP);
Integer port = (Integer)connProps.get(PORT_PROP);
if (host != null && port != null) {
try {
Socket peerConn = new Socket(host, port.intValue());
SocketPeer peer = newPeer(peerConn);
addPeer(peer);
success = true;
}
catch (Exception e) {
System.out.println("Error connecting to peer " + host + ":" + port);
}
}
return success;
|
public boolean | connect(java.util.Properties connProps, Identity peerID)Connect to a peer given the connection properties and identity of the
peer. Subclasses can choose to implement this using both arguments, or
ignoring one in favor of the other (connection properties vs. properties
pulled from the remote peer identifier).
// If necessary, copy the critical properties from the identity of the
// remote peer to the connection properties.
if (peerID.getName() != null && connProps.getProperty(HOST_PROP) == null) {
connProps.put(HOST_PROP, peerID.getName());
}
if (peerID.getId() > 0 && connProps.getProperty(PORT_PROP) == null) {
connProps.put(PORT_PROP, new Integer(peerID.getId()));
}
return connect(connProps);
|
public void | handle(Message m)Handle incoming message.
System.out.println(getIdentity().getName() + "(" + getIdentity().getId() +
") received message " + m);
|
protected static jdc.patterns.peer.SocketPeer | newPeer(java.net.Socket s)Initialize a new SocketPeer with the expected properties.
SocketPeer peer = null;
try {
// Make an identity for the new peer, using the remote port as the ID
Identity i = new Identity(s.getPort());
// Use the remote host name as the peer's name
i.setName(s.getInetAddress().getHostName());
// Get the in and out streams from the socket
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
// Store the socket and the streams in the identity's property list
i.setProperty(SOCKET_PROP, s);
i.setProperty(PEER_IN, in);
i.setProperty(PEER_OUT, out);
// Make the new peer and set its identity
peer = new SocketPeer();
peer.setIdentity(i);
}
catch (Exception e) {
System.out.println("Failed to initialize peer:");
e.printStackTrace();
}
return peer;
|
public void | run()When run within a thread, just listen to server socket for remote peer
connect attempts, and make new peers out of them.
while (true) {
try {
Socket s = mSocket.accept();
addPeer(newPeer(s));
}
catch (Exception e) {
System.out.println("Error accepting socket connection.");
e.printStackTrace();
}
}
|
public boolean | send(Message msg, Identity peerID)Send the message to the identified peer.
boolean success = false;
try {
OutputStream out = (OutputStream)peerID.getProperty(PEER_OUT);
ObjectOutputStream oout = new ObjectOutputStream(out);
oout.writeObject(msg);
success = true;
}
catch (Exception e) {
System.out.println("Failed to send message to: " + peerID.getName() +
", " + peerID.getId());
e.printStackTrace();
}
return success;
|