1 package org.xvsm.remote.freepastry;
2
3 import java.net.InetAddress;
4 import java.net.InetSocketAddress;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.net.UnknownHostException;
8 import java.util.Hashtable;
9
10 import org.xvsm.internal.exceptions.FatalException;
11 import org.xvsm.internal.exceptions.XCoreRemoteException;
12 import org.xvsm.internal.tasks.Task;
13 import org.xvsm.remote.interfaces.IMarshaller;
14 import org.xvsm.remote.interfaces.ITransportSender;
15
16 import rice.pastry.NodeHandle;
17 import rice.pastry.socket.SocketPastryNodeFactory;
18
19 /***
20 * implements the ITransportSender interface and provides the functionality to
21 * send requests through the FreePastry network
22 *
23 * @author Hannu-Daniel Goiss
24 */
25 public class FreePastryTransportSender implements ITransportSender<byte[]> {
26 private Hashtable<String, String> env;
27 private FreePastryConnection freepastryConnection;
28 private FreePastryApplication application;
29 /***
30 * The URI where the listener should listen.
31 */
32 private URI uri;
33
34 /***
35 * The Marshaller that should be used.
36 */
37 private IMarshaller<byte[]> marshaller;
38
39 private void send(URI uri, Task t) {
40 String remoteUri = uri.toString();
41
42 remoteUri = remoteUri.substring(remoteUri.indexOf("://") + 3);
43
44 InetSocketAddress bootaddress = null;
45
46 try {
47 if (remoteUri.startsWith("localhost") == false
48 && remoteUri.contains(":") == true)
49 bootaddress = new InetSocketAddress(remoteUri.substring(0,
50 remoteUri.indexOf(":")), Integer.parseInt(remoteUri
51 .substring(remoteUri.indexOf(":") + 1)));
52 else
53 bootaddress = new InetSocketAddress(InetAddress.getLocalHost()
54 .getHostAddress(), Integer.parseInt(remoteUri
55 .substring(remoteUri.indexOf(":") + 1)));
56 } catch (UnknownHostException uhe) {
57 throw new FatalException(uhe);
58 }
59
60
61
62
63
64
65
66 NodeHandle nodeHandle = ((SocketPastryNodeFactory) freepastryConnection
67 .getPastryNodeFactory()).getNodeHandle(bootaddress);
68
69 byte[] bytes = this.marshaller.marshall(t);
70
71
72
73 application.send(nodeHandle, bytes);
74
75 }
76
77 public void sendRequest(URI uri, Task t) throws XCoreRemoteException {
78 this.send(uri, t);
79 }
80
81 public void sendResponse(URI uri, Task t) throws XCoreRemoteException {
82 this.send(uri, t);
83 }
84
85 public void setEnvironmentVariable(Hashtable env) {
86 this.env = env;
87
88 env.put("Port", uri.toString().substring(
89 uri.toString().lastIndexOf(":") + 1));
90
91 freepastryConnection = FreePastryConnection.getInstance(env);
92
93 application = FreePastryApplication.getInstance(freepastryConnection
94 .getPastryNode(), marshaller);
95 }
96
97 public void setMarshaller(IMarshaller<byte[]> m) {
98 this.marshaller = m;
99 }
100
101 public void setURI(URI uri) {
102 try {
103 uri = new URI(uri.getScheme() + "://" + uri.getHost() + ":"
104 + uri.getPort());
105 } catch (URISyntaxException urise) {
106 urise.printStackTrace();
107 }
108
109 this.uri = uri;
110 }
111 }