1 package org.xvsm.remote.freepastry;
2
3 import org.xvsm.internal.exceptions.XCoreException;
4 import org.xvsm.internal.tasks.Task;
5 import org.xvsm.remote.TransportHandler;
6 import org.xvsm.remote.interfaces.IMarshaller;
7
8 import rice.p2p.commonapi.Application;
9 import rice.p2p.commonapi.Endpoint;
10 import rice.p2p.commonapi.Id;
11 import rice.p2p.commonapi.Message;
12 import rice.p2p.commonapi.Node;
13 import rice.p2p.commonapi.NodeHandle;
14 import rice.p2p.commonapi.RouteMessage;
15
16 /***
17 * Implementation fo the Application interface of FreePastry. It is needed to
18 * send and receive requests from the FreePastry network
19 *
20 * @author Hannu-Daniel Goiss
21 */
22 public class FreePastryApplication implements Application {
23 protected Endpoint endpoint;
24 private static FreePastryApplication application = null;
25 private IMarshaller<byte[]> marshaller;
26
27 public synchronized static FreePastryApplication getInstance(Node node,
28 IMarshaller<byte[]> marshaller) {
29 if (application == null) {
30 application = new FreePastryApplication(node, marshaller);
31 }
32 return application;
33 }
34
35 public synchronized static FreePastryApplication getInstance(Node node) {
36 if (application == null) {
37 application = new FreePastryApplication(node, null);
38 }
39 return application;
40 }
41
42 private FreePastryApplication(Node node, IMarshaller<byte[]> marshaller) {
43 this.endpoint = node.buildEndpoint(this, "myinstance");
44
45
46 this.endpoint.register();
47 this.marshaller = marshaller;
48
49 try {
50 FreePastryConnection.getInstance().setEndpoint(this.endpoint);
51 } catch (XCoreException xce) {
52 xce.printStackTrace();
53 }
54 }
55
56 public void send(NodeHandle nodeHandle, byte data[]) {
57 Message msg = new FreePastryMessage(endpoint.getId(), nodeHandle
58 .getId(), data);
59 endpoint.route(null, msg, nodeHandle);
60 }
61
62 public void deliver(Id arg0, Message arg1) {
63 FreePastryMessage msg = (FreePastryMessage) arg1;
64
65 Task task = marshaller.unmarshall(msg.getData());
66
67 TransportHandler.getInstance().processTask(task);
68
69 }
70
71 public boolean forward(RouteMessage arg0) {
72 return true;
73 }
74
75 /***
76 * Called when you hear about a new neighbor. When can I use this method?
77 */
78 public void update(NodeHandle arg0, boolean arg1) {
79 FreePastryConfiguration fpc = FreePastryConfiguration.getInstance();
80 if (arg1 == true)
81 fpc.increaseNeighbours(arg0);
82 else
83 fpc.decreaseNeighbours(arg0);
84 }
85
86 }