add first draft of high-level client
[jFCPlib.git] / src / net / pterodactylus / fcp / highlevel / HighLevelClient.java
1 /*
2  * fcplib - HighLevelClient.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.fcp.highlevel;
21
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25
26 import net.pterodactylus.fcp.AllData;
27 import net.pterodactylus.fcp.ClientHello;
28 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
29 import net.pterodactylus.fcp.ConfigData;
30 import net.pterodactylus.fcp.DataFound;
31 import net.pterodactylus.fcp.EndListPeerNotes;
32 import net.pterodactylus.fcp.EndListPeers;
33 import net.pterodactylus.fcp.EndListPersistentRequests;
34 import net.pterodactylus.fcp.FCPPluginReply;
35 import net.pterodactylus.fcp.FcpConnection;
36 import net.pterodactylus.fcp.FcpListener;
37 import net.pterodactylus.fcp.FcpMessage;
38 import net.pterodactylus.fcp.FinishedCompression;
39 import net.pterodactylus.fcp.GetFailed;
40 import net.pterodactylus.fcp.IdentifierCollision;
41 import net.pterodactylus.fcp.NodeData;
42 import net.pterodactylus.fcp.NodeHello;
43 import net.pterodactylus.fcp.Peer;
44 import net.pterodactylus.fcp.PeerNote;
45 import net.pterodactylus.fcp.PeerRemoved;
46 import net.pterodactylus.fcp.PersistentGet;
47 import net.pterodactylus.fcp.PersistentPut;
48 import net.pterodactylus.fcp.PersistentPutDir;
49 import net.pterodactylus.fcp.PersistentRequestModified;
50 import net.pterodactylus.fcp.PersistentRequestRemoved;
51 import net.pterodactylus.fcp.PluginInfo;
52 import net.pterodactylus.fcp.ProtocolError;
53 import net.pterodactylus.fcp.PutFailed;
54 import net.pterodactylus.fcp.PutFetchable;
55 import net.pterodactylus.fcp.PutSuccessful;
56 import net.pterodactylus.fcp.SSKKeypair;
57 import net.pterodactylus.fcp.SimpleProgress;
58 import net.pterodactylus.fcp.StartedCompression;
59 import net.pterodactylus.fcp.SubscribedUSKUpdate;
60 import net.pterodactylus.fcp.TestDDAComplete;
61 import net.pterodactylus.fcp.TestDDAReply;
62 import net.pterodactylus.fcp.URIGenerated;
63 import net.pterodactylus.fcp.UnknownNodeIdentifier;
64 import net.pterodactylus.fcp.UnknownPeerNoteType;
65
66 /**
67  * A high-level client that allows simple yet full-featured access to a Freenet
68  * node.
69  * 
70  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
71  * @version $Id$
72  */
73 public class HighLevelClient {
74
75         /** Object for internal synchronization. */
76         private final Object syncObject = new Object();
77
78         /** The name of the client. */
79         private final String clientName;
80
81         /** The address of the node. */
82         private InetAddress address;
83
84         /** The port number of the node. */
85         private int port;
86
87         /** The FCP connection to the node. */
88         private FcpConnection fcpConnection;
89
90         /** The listener for the connection. */
91         private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
92
93         /** The callback for {@link #connect()}. */
94         private HighLevelCallback<ConnectResult> connectCallback;
95
96         /**
97          * Creates a new high-level client that connects to a node on
98          * <code>localhost</code>.
99          * 
100          * @param clientName
101          *            The name of the client
102          * @throws UnknownHostException
103          *             if the hostname of the node can not be resolved.
104          */
105         public HighLevelClient(String clientName) throws UnknownHostException {
106                 this(clientName, "localhost");
107         }
108
109         /**
110          * Creates a new high-level client that connects to a node on the given
111          * host.
112          * 
113          * @param clientName
114          *            The name of the client
115          * @param host
116          *            The hostname of the node
117          * @throws UnknownHostException
118          *             if the hostname of the node can not be resolved.
119          */
120         public HighLevelClient(String clientName, String host) throws UnknownHostException {
121                 this(clientName, host, FcpConnection.DEFAULT_PORT);
122         }
123
124         /**
125          * Creates a new high-level client that connects to a node on the given
126          * host.
127          * 
128          * @param clientName
129          *            The name of the client
130          * @param host
131          *            The hostname of the node
132          * @param port
133          *            The port number of the node
134          * @throws UnknownHostException
135          *             if the hostname of the node can not be resolved.
136          */
137         public HighLevelClient(String clientName, String host, int port) throws UnknownHostException {
138                 this(clientName, InetAddress.getByName(host), port);
139         }
140
141         /**
142          * Creates a new high-level client that connects to a node at the given
143          * address.
144          * 
145          * @param clientName
146          *            The name of the client
147          * @param address
148          *            The address of the node
149          * @param port
150          *            The port number of the node
151          */
152         public HighLevelClient(String clientName, InetAddress address, int port) {
153                 this.clientName = clientName;
154                 this.address = address;
155                 this.port = port;
156         }
157
158         //
159         // ACCESSORS
160         //
161
162         //
163         // ACTIONS
164         //
165
166         /**
167          * Connects the client.
168          * 
169          * @return A callback with a connection result
170          * @throws IOException
171          *             if an I/O error occurs communicating with the node
172          */
173         public HighLevelCallback<ConnectResult> connect() throws IOException {
174                 fcpConnection = new FcpConnection(address, port);
175                 fcpConnection.addFcpListener(highLevelClientFcpListener);
176                 ClientHello clientHello = new ClientHello(clientName);
177                 connectCallback = new HighLevelCallback<ConnectResult>();
178                 fcpConnection.sendMessage(clientHello);
179                 return connectCallback;
180         }
181
182         /**
183          * Disconnects the client from the node.
184          */
185         public void disconnect() {
186         }
187
188         /**
189          * FCP listener for {@link HighLevelClient}.
190          * 
191          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
192          * @version $Id$
193          */
194         private class HighLevelClientFcpListener implements FcpListener {
195
196                 /**
197                  * Creates a new FCP listener for {@link HighLevelClient}.
198                  */
199                 HighLevelClientFcpListener() {
200                 }
201
202                 /**
203                  * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection)
204                  */
205                 public void connectionClosed(FcpConnection fcpConnection) {
206                 }
207
208                 /**
209                  * @see net.pterodactylus.fcp.FcpListener#receivedAllData(net.pterodactylus.fcp.FcpConnection,
210                  *      net.pterodactylus.fcp.AllData)
211                  */
212                 public void receivedAllData(FcpConnection fcpConnection, AllData allData) {
213                 }
214
215                 /**
216                  * @see net.pterodactylus.fcp.FcpListener#receivedCloseConnectionDuplicateClientName(net.pterodactylus.fcp.FcpConnection,
217                  *      net.pterodactylus.fcp.CloseConnectionDuplicateClientName)
218                  */
219                 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
220                 }
221
222                 /**
223                  * @see net.pterodactylus.fcp.FcpListener#receivedConfigData(net.pterodactylus.fcp.FcpConnection,
224                  *      net.pterodactylus.fcp.ConfigData)
225                  */
226                 public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
227                 }
228
229                 /**
230                  * @see net.pterodactylus.fcp.FcpListener#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
231                  *      net.pterodactylus.fcp.DataFound)
232                  */
233                 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
234                 }
235
236                 /**
237                  * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeerNotes(net.pterodactylus.fcp.FcpConnection,
238                  *      net.pterodactylus.fcp.EndListPeerNotes)
239                  */
240                 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
241                 }
242
243                 /**
244                  * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeers(net.pterodactylus.fcp.FcpConnection,
245                  *      net.pterodactylus.fcp.EndListPeers)
246                  */
247                 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
248                 }
249
250                 /**
251                  * @see net.pterodactylus.fcp.FcpListener#receivedEndListPersistentRequests(net.pterodactylus.fcp.FcpConnection,
252                  *      net.pterodactylus.fcp.EndListPersistentRequests)
253                  */
254                 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
255                 }
256
257                 /**
258                  * @see net.pterodactylus.fcp.FcpListener#receivedFCPPluginReply(net.pterodactylus.fcp.FcpConnection,
259                  *      net.pterodactylus.fcp.FCPPluginReply)
260                  */
261                 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
262                 }
263
264                 /**
265                  * @see net.pterodactylus.fcp.FcpListener#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
266                  *      net.pterodactylus.fcp.GetFailed)
267                  */
268                 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
269                 }
270
271                 /**
272                  * @see net.pterodactylus.fcp.FcpListener#receivedIdentifierCollision(net.pterodactylus.fcp.FcpConnection,
273                  *      net.pterodactylus.fcp.IdentifierCollision)
274                  */
275                 public void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
276                 }
277
278                 /**
279                  * @see net.pterodactylus.fcp.FcpListener#receivedMessage(net.pterodactylus.fcp.FcpConnection,
280                  *      net.pterodactylus.fcp.FcpMessage)
281                  */
282                 public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
283                 }
284
285                 /**
286                  * @see net.pterodactylus.fcp.FcpListener#receivedNodeData(net.pterodactylus.fcp.FcpConnection,
287                  *      net.pterodactylus.fcp.NodeData)
288                  */
289                 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
290                 }
291
292                 /**
293                  * @see net.pterodactylus.fcp.FcpListener#receivedNodeHello(net.pterodactylus.fcp.FcpConnection,
294                  *      net.pterodactylus.fcp.NodeHello)
295                  */
296                 @SuppressWarnings("synthetic-access")
297                 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
298                         if (fcpConnection != HighLevelClient.this.fcpConnection) {
299                                 return;
300                         }
301                         ConnectResult connectResult = new ConnectResult();
302
303                         synchronized (syncObject) {
304                                 connectCallback.setResult(connectResult);
305                         }
306                 }
307
308                 /**
309                  * @see net.pterodactylus.fcp.FcpListener#receivedPeer(net.pterodactylus.fcp.FcpConnection,
310                  *      net.pterodactylus.fcp.Peer)
311                  */
312                 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
313                 }
314
315                 /**
316                  * @see net.pterodactylus.fcp.FcpListener#receivedPeerNote(net.pterodactylus.fcp.FcpConnection,
317                  *      net.pterodactylus.fcp.PeerNote)
318                  */
319                 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
320                 }
321
322                 /**
323                  * @see net.pterodactylus.fcp.FcpListener#receivedPeerRemoved(net.pterodactylus.fcp.FcpConnection,
324                  *      net.pterodactylus.fcp.PeerRemoved)
325                  */
326                 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
327                 }
328
329                 /**
330                  * @see net.pterodactylus.fcp.FcpListener#receivedPersistentGet(net.pterodactylus.fcp.FcpConnection,
331                  *      net.pterodactylus.fcp.PersistentGet)
332                  */
333                 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
334                 }
335
336                 /**
337                  * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
338                  *      net.pterodactylus.fcp.PersistentPut)
339                  */
340                 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
341                 }
342
343                 /**
344                  * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPutDir(net.pterodactylus.fcp.FcpConnection,
345                  *      net.pterodactylus.fcp.PersistentPutDir)
346                  */
347                 public void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
348                 }
349
350                 /**
351                  * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestModified(net.pterodactylus.fcp.FcpConnection,
352                  *      net.pterodactylus.fcp.PersistentRequestModified)
353                  */
354                 public void receivedPersistentRequestModified(FcpConnection fcpConnection, PersistentRequestModified persistentRequestModified) {
355                 }
356
357                 /**
358                  * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestRemoved(net.pterodactylus.fcp.FcpConnection,
359                  *      net.pterodactylus.fcp.PersistentRequestRemoved)
360                  */
361                 public void receivedPersistentRequestRemoved(FcpConnection fcpConnection, PersistentRequestRemoved persistentRequestRemoved) {
362                 }
363
364                 /**
365                  * @see net.pterodactylus.fcp.FcpListener#receivedPluginInfo(net.pterodactylus.fcp.FcpConnection,
366                  *      net.pterodactylus.fcp.PluginInfo)
367                  */
368                 public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
369                 }
370
371                 /**
372                  * @see net.pterodactylus.fcp.FcpListener#receivedProtocolError(net.pterodactylus.fcp.FcpConnection,
373                  *      net.pterodactylus.fcp.ProtocolError)
374                  */
375                 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
376                 }
377
378                 /**
379                  * @see net.pterodactylus.fcp.FcpListener#receivedPutFailed(net.pterodactylus.fcp.FcpConnection,
380                  *      net.pterodactylus.fcp.PutFailed)
381                  */
382                 public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
383                 }
384
385                 /**
386                  * @see net.pterodactylus.fcp.FcpListener#receivedPutFetchable(net.pterodactylus.fcp.FcpConnection,
387                  *      net.pterodactylus.fcp.PutFetchable)
388                  */
389                 public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
390                 }
391
392                 /**
393                  * @see net.pterodactylus.fcp.FcpListener#receivedPutSuccessful(net.pterodactylus.fcp.FcpConnection,
394                  *      net.pterodactylus.fcp.PutSuccessful)
395                  */
396                 public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
397                 }
398
399                 /**
400                  * @see net.pterodactylus.fcp.FcpListener#receivedSSKKeypair(net.pterodactylus.fcp.FcpConnection,
401                  *      net.pterodactylus.fcp.SSKKeypair)
402                  */
403                 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
404                 }
405
406                 /**
407                  * @see net.pterodactylus.fcp.FcpListener#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
408                  *      net.pterodactylus.fcp.SimpleProgress)
409                  */
410                 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
411                 }
412
413                 /**
414                  * @see net.pterodactylus.fcp.FcpListener#receivedStartedCompression(net.pterodactylus.fcp.FcpConnection,
415                  *      net.pterodactylus.fcp.StartedCompression)
416                  */
417                 public void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
418                 }
419
420                 /**
421                  * @see net.pterodactylus.fcp.FcpListener#receivedSubscribedUSKUpdate(net.pterodactylus.fcp.FcpConnection,
422                  *      net.pterodactylus.fcp.SubscribedUSKUpdate)
423                  */
424                 public void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
425                 }
426
427                 /**
428                  * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAComplete(net.pterodactylus.fcp.FcpConnection,
429                  *      net.pterodactylus.fcp.TestDDAComplete)
430                  */
431                 public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
432                 }
433
434                 /**
435                  * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAReply(net.pterodactylus.fcp.FcpConnection,
436                  *      net.pterodactylus.fcp.TestDDAReply)
437                  */
438                 public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
439                 }
440
441                 /**
442                  * @see net.pterodactylus.fcp.FcpListener#receivedURIGenerated(net.pterodactylus.fcp.FcpConnection,
443                  *      net.pterodactylus.fcp.URIGenerated)
444                  */
445                 public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
446                 }
447
448                 /**
449                  * @see net.pterodactylus.fcp.FcpListener#receivedUnknownNodeIdentifier(net.pterodactylus.fcp.FcpConnection,
450                  *      net.pterodactylus.fcp.UnknownNodeIdentifier)
451                  */
452                 public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
453                 }
454
455                 /**
456                  * @see net.pterodactylus.fcp.FcpListener#receivedUnknownPeerNoteType(net.pterodactylus.fcp.FcpConnection,
457                  *      net.pterodactylus.fcp.UnknownPeerNoteType)
458                  */
459                 public void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
460                 }
461
462                 /**
463                  * @see net.pterodactylus.fcp.FcpListener#receviedFinishedCompression(net.pterodactylus.fcp.FcpConnection,
464                  *      net.pterodactylus.fcp.FinishedCompression)
465                  */
466                 public void receviedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {
467                 }
468
469         }
470
471 }