1 package org.xvsm.lookup.gnutella;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.util.Hashtable;
6
7 import org.jdom.Element;
8 import org.xvsm.core.AtomicEntry;
9 import org.xvsm.core.Capi;
10 import org.xvsm.core.ContainerRef;
11 import org.xvsm.interfaces.ICapi;
12 import org.xvsm.interfaces.lookup.ILookup;
13 import org.xvsm.internal.exceptions.XCoreException;
14 import org.xvsm.lookup.LookupManager;
15 import org.xvsm.lookup.exceptions.ContainerAlreadyPublishedException;
16 import org.xvsm.lookup.exceptions.ContainerNotPublishedException;
17 import org.xvsm.lookup.exceptions.LookupException;
18 import org.xvsm.lookup.gnutella.exceptions.NotFoundException;
19 import org.xvsm.selectors.KeySelector;
20
21 /***
22 * Is the implementation of the lookup interface using the gnutella api
23 * provided in this package
24 *
25 * @author Hannu-Daniel Goiss
26 */
27 /***
28 * @author hannu-daniel.goiss
29 *
30 */
31 public class GnutellaLookup implements ILookup {
32 private ICapi capi;
33 private GnutellaPeer gnutella;
34
35 /***
36 * Constructor using environment variable
37 *
38 * @param siteURI
39 * @param table
40 * @throws LookupException
41 */
42 public GnutellaLookup(URI siteURI, Hashtable<String, String> table)
43 throws LookupException {
44
45 String remotesite = table
46 .get("org.xvsm.lookup.gnutella.GnutellaLookup.RemoteURI");
47 String port = table.get("org.xvsm.lookup.gnutella.GnutellaLookup.Port");
48
49 this.connectToGnutella(remotesite, Integer.parseInt(port));
50 }
51
52 /***
53 * Constructor using XML-file
54 *
55 * @param siteURI
56 * @param element
57 * @throws LookupException
58 */
59 public GnutellaLookup(URI siteURI, Element element) throws LookupException {
60 String remotesite = element.getChild("RemoteURI").getTextTrim();
61 String port = element.getChild("Port").getTextTrim();
62
63 this.connectToGnutella(remotesite, Integer.parseInt(port));
64 }
65
66 /***
67 * This function connect to the Gnutella network. A Gnutella Server
68 * org.xvsm.lookup.gnutella.GnutellaServer has to be started at the
69 * ip-address and port first.
70 *
71 * @param remoteUri
72 * @param port
73 * @throws LookupException
74 */
75 public void connectToGnutella(String remoteUri, int port)
76 throws LookupException {
77 try {
78 capi = new Capi();
79 } catch (XCoreException xce) {
80 throw new LookupException(xce);
81 }
82
83 try {
84 gnutella = new GnutellaPeer(port, remoteUri.substring(0, remoteUri
85 .indexOf(":")), Integer.parseInt(remoteUri
86 .substring(remoteUri.indexOf(":") + 1)));
87 } catch (IOException ioe) {
88 throw new LookupException(ioe);
89 } catch (NumberFormatException e) {
90 throw new LookupException(e);
91 } catch (ClassNotFoundException e) {
92 throw new LookupException(e);
93 }
94 }
95
96
97
98
99 public ContainerRef[] lookup(String name)
100 throws ContainerNotPublishedException, LookupException {
101
102 Object object;
103 try {
104 object = gnutella.search(name);
105 } catch (IOException ioe) {
106 throw new LookupException(ioe);
107 } catch (ClassNotFoundException e) {
108 throw new LookupException(e);
109 } catch (NotFoundException e) {
110 throw new ContainerNotPublishedException(e);
111 }
112 ContainerRef[] containerArray = new ContainerRef[1];
113
114 containerArray[0] = new ContainerRef((String) object);
115
116 return containerArray;
117 }
118
119
120
121
122 public ContainerRef[] lookup(Hashtable list)
123 throws ContainerNotPublishedException, LookupException {
124 return null;
125 }
126
127
128
129
130 public ContainerRef[] lookup(String name, Hashtable list)
131 throws ContainerNotPublishedException, LookupException {
132 return null;
133 }
134
135
136
137
138 public void publish(ContainerRef cref)
139 throws ContainerAlreadyPublishedException, LookupException {
140 String containerName = null;
141
142 try {
143 containerName = this.getContainerName(cref);
144 } catch (XCoreException xce) {
145 throw new LookupException(xce);
146 }
147
148 gnutella.publish(containerName, cref.toString());
149 }
150
151
152
153
154 public void publish(String pCref)
155 throws ContainerAlreadyPublishedException, LookupException {
156 }
157
158
159
160
161 public ContainerRef[] publishedContainers() throws LookupException {
162 throw new LookupException("not provided by Gnutella");
163 }
164
165
166
167
168 public void unpublish(ContainerRef cref)
169 throws ContainerNotPublishedException, LookupException {
170 String containerName = null;
171
172 try {
173 containerName = this.getContainerName(cref);
174 } catch (XCoreException xce) {
175 throw new LookupException(xce);
176 }
177
178 gnutella.unpublish(containerName);
179 }
180
181
182
183
184 public void updateDescription(ContainerRef cref)
185 throws ContainerNotPublishedException, LookupException {
186 }
187
188 /***
189 * @param cref
190 * @return
191 * @throws XCoreException
192 */
193 private String getContainerName(ContainerRef cref) throws XCoreException {
194 ContainerRef metaCref = LookupManager.lookupMetaContainer(cref);
195 String containerName = (String) ((AtomicEntry) capi.read(metaCref, 0,
196 null, new KeySelector<String>("system", "name"))[0]).getValue();
197
198 return containerName;
199 }
200 }