2 * fcplib - HighLevelClient.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.fcp.highlevel;
22 import java.io.BufferedReader;
24 import java.io.FileReader;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Map.Entry;
36 import java.util.logging.Level;
37 import java.util.logging.Logger;
39 import net.pterodactylus.fcp.AddPeer;
40 import net.pterodactylus.fcp.AllData;
41 import net.pterodactylus.fcp.ClientGet;
42 import net.pterodactylus.fcp.ClientHello;
43 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
44 import net.pterodactylus.fcp.ConfigData;
45 import net.pterodactylus.fcp.DataFound;
46 import net.pterodactylus.fcp.EndListPeerNotes;
47 import net.pterodactylus.fcp.EndListPeers;
48 import net.pterodactylus.fcp.EndListPersistentRequests;
49 import net.pterodactylus.fcp.FCPPluginReply;
50 import net.pterodactylus.fcp.FcpConnection;
51 import net.pterodactylus.fcp.FcpListener;
52 import net.pterodactylus.fcp.FcpMessage;
53 import net.pterodactylus.fcp.FcpUtils;
54 import net.pterodactylus.fcp.FinishedCompression;
55 import net.pterodactylus.fcp.GenerateSSK;
56 import net.pterodactylus.fcp.GetFailed;
57 import net.pterodactylus.fcp.IdentifierCollision;
58 import net.pterodactylus.fcp.ListPeers;
59 import net.pterodactylus.fcp.ListPersistentRequests;
60 import net.pterodactylus.fcp.NodeData;
61 import net.pterodactylus.fcp.NodeHello;
62 import net.pterodactylus.fcp.NodeRef;
63 import net.pterodactylus.fcp.Peer;
64 import net.pterodactylus.fcp.PeerNote;
65 import net.pterodactylus.fcp.PeerRemoved;
66 import net.pterodactylus.fcp.PersistentGet;
67 import net.pterodactylus.fcp.PersistentPut;
68 import net.pterodactylus.fcp.PersistentPutDir;
69 import net.pterodactylus.fcp.PersistentRequestModified;
70 import net.pterodactylus.fcp.PersistentRequestRemoved;
71 import net.pterodactylus.fcp.PluginInfo;
72 import net.pterodactylus.fcp.ProtocolError;
73 import net.pterodactylus.fcp.PutFailed;
74 import net.pterodactylus.fcp.PutFetchable;
75 import net.pterodactylus.fcp.PutSuccessful;
76 import net.pterodactylus.fcp.ReturnType;
77 import net.pterodactylus.fcp.SSKKeypair;
78 import net.pterodactylus.fcp.SimpleProgress;
79 import net.pterodactylus.fcp.StartedCompression;
80 import net.pterodactylus.fcp.SubscribedUSKUpdate;
81 import net.pterodactylus.fcp.TestDDAComplete;
82 import net.pterodactylus.fcp.TestDDAReply;
83 import net.pterodactylus.fcp.TestDDARequest;
84 import net.pterodactylus.fcp.TestDDAResponse;
85 import net.pterodactylus.fcp.URIGenerated;
86 import net.pterodactylus.fcp.UnknownNodeIdentifier;
87 import net.pterodactylus.fcp.UnknownPeerNoteType;
88 import net.pterodactylus.fcp.WatchGlobal;
91 * A high-level client that allows simple yet full-featured access to a Freenet
94 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
96 public class HighLevelClient {
99 private static final Logger logger = Logger.getLogger(HighLevelClient.class.getName());
101 /** Object for internal synchronization. */
102 private final Object syncObject = new Object();
104 /** The name of the client. */
105 private final String clientName;
107 /** The FCP connection to the node. */
108 private FcpConnection fcpConnection = null;
110 /** Listeners for high-level client events. */
111 private List<HighLevelClientListener> highLevelClientListeners = Collections.synchronizedList(new ArrayList<HighLevelClientListener>());
113 /** The listener for the connection. */
114 private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
116 /** The listeners for progress events. */
117 private List<HighLevelProgressListener> highLevelProgressListeners = Collections.synchronizedList(new ArrayList<HighLevelProgressListener>());
119 /** The callback for {@link #connect(String)}. */
120 private HighLevelCallback<ConnectResult> connectCallback;
122 /** Mapping from request identifiers to callbacks. */
123 private Map<String, HighLevelCallback<KeyGenerationResult>> keyGenerationCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<KeyGenerationResult>>());
125 /** Mapping from request identifier to peer list callbacks. */
126 private Map<String, HighLevelCallback<PeerListResult>> peerListCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerListResult>>());
128 /** Mapping from request identifier to peer callbacks. */
129 private Map<String, HighLevelCallback<PeerResult>> peerCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerResult>>());
131 /** Mapping from directories to DDA callbacks. */
132 private Map<String, HighLevelCallback<DirectDiskAccessResult>> directDiskAccessCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<DirectDiskAccessResult>>());
134 /** Mapping from request identifiers to download callbacks. */
135 private Map<String, HighLevelProgressCallback<DownloadResult>> downloadCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelProgressCallback<DownloadResult>>());
137 /** The callback for {@link #getRequests()}. */
138 private HighLevelCallback<RequestListResult> requestListCallback;
141 * Creates a new high-level client that connects to a node on
142 * <code>localhost</code>.
145 * The name of the client
147 public HighLevelClient(String clientName) {
148 this.clientName = clientName;
156 * Adds the given high-level client listener to list of listeners.
158 * @param highLevelClientListener
159 * The listener to add
161 public void addHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
162 highLevelClientListeners.add(highLevelClientListener);
166 * Removes the given high-level client listener from the list of listeners.
168 * @param highLevelClientListener
169 * The listener to remove
171 public void removeHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
172 highLevelClientListeners.remove(highLevelClientListener);
176 * Notifies all listeners that a client has connected.
178 private void fireClientConnected() {
179 for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
180 highLevelClientListener.clientConnected(this);
185 * Notifies all listeners that a client has disconnected.
188 * The exception that caused the disconnect, or <code>null</code>
189 * if there was no exception
191 private void fireClientDisconnected(Throwable throwable) {
192 for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
193 highLevelClientListener.clientDisconnected(this, throwable);
198 * Adds a high-level progress listener.
200 * @param highLevelProgressListener
201 * The high-level progress listener to add
203 public void addHighLevelProgressListener(HighLevelProgressListener highLevelProgressListener) {
204 highLevelProgressListeners.add(highLevelProgressListener);
208 * Removes a high-level progress listener.
210 * @param highLevelProgressListener
211 * The high-level progress listener to remove
213 public void removeHighLevelProgressListener(HighLevelProgressListener highLevelProgressListener) {
214 highLevelProgressListeners.remove(highLevelProgressListener);
218 * Notifies all listeners that the request with the given identifier made
222 * The identifier of the request
223 * @param highLevelProgress
224 * The progress of the request
226 private void fireProgressReceived(String identifier, HighLevelProgress highLevelProgress) {
227 for (HighLevelProgressListener highLevelProgressListener: highLevelProgressListeners) {
228 highLevelProgressListener.progressReceived(this, identifier, highLevelProgress);
237 * Returns the FCP connection that backs this high-level client. This method
238 * should be used with care as fiddling around with the FCP connection can
239 * easily break the high-level client if you don’t know what you’re doing!
241 * @return The FCP connection of this client
243 public FcpConnection getFcpConnection() {
244 return fcpConnection;
248 * Returns whether the node is connected.
250 * @return <code>true</code> if the node is currently connected,
251 * <code>false</code> otherwise
253 public boolean isConnected() {
254 return fcpConnection != null;
262 * Connects the client.
265 * The hostname of the node
266 * @return A callback with a connection result
267 * @throws UnknownHostException
268 * if the hostname can not be resolved
269 * @throws IOException
270 * if an I/O error occurs communicating with the node
272 public HighLevelCallback<ConnectResult> connect(String hostname) throws UnknownHostException, IOException {
273 return connect(hostname, 9481);
277 * Connects the client.
280 * The hostname of the node
282 * The port number of the node
283 * @return A callback with a connection result
284 * @throws UnknownHostException
285 * if the hostname can not be resolved
286 * @throws IOException
287 * if an I/O error occurs communicating with the node
289 public HighLevelCallback<ConnectResult> connect(String hostname, int port) throws UnknownHostException, IOException {
290 return connect(InetAddress.getByName(hostname), port);
294 * Connects the client.
297 * The address of the node
299 * The port number of the node
300 * @return A callback with a connection result
301 * @throws IOException
302 * if an I/O error occurs communicating with the node
304 public HighLevelCallback<ConnectResult> connect(InetAddress address, int port) throws IOException {
306 synchronized (this) {
307 fcpConnection = new FcpConnection(address, port);
309 fcpConnection.addFcpListener(highLevelClientFcpListener);
310 fcpConnection.connect();
311 ClientHello clientHello = new ClientHello(clientName);
312 connectCallback = new HighLevelCallback<ConnectResult>(new ConnectResult());
313 fcpConnection.sendMessage(clientHello);
314 return connectCallback;
315 } catch (IOException ioe1) {
316 fcpConnection = null;
322 * Disconnects the client from the node.
324 public void disconnect() {
329 * Generates a new SSK keypair.
331 * @return A callback with the keypair
332 * @throws IOException
333 * if an I/O error occurs communicating with the node
334 * @throws HighLevelException
335 * if the client is not connected
337 public HighLevelCallback<KeyGenerationResult> generateKey() throws IOException, HighLevelException {
339 String identifier = generateIdentifier("generateSSK");
340 GenerateSSK generateSSK = new GenerateSSK(identifier);
341 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>(new KeyGenerationResult(identifier));
342 keyGenerationCallbacks.put(identifier, keyGenerationCallback);
343 fcpConnection.sendMessage(generateSSK);
344 return keyGenerationCallback;
348 * Sets whether to watch the global queue.
351 * <code>true</code> to watch the global queue in addition to
352 * the client-local queue, <code>false</code> to only watch the
354 * @throws IOException
355 * if an I/O error occurs communicating with the node
356 * @throws HighLevelException
357 * if the client is not connected
359 public void setWatchGlobal(boolean enabled) throws IOException, HighLevelException {
361 WatchGlobal watchGlobal = new WatchGlobal(enabled);
362 fcpConnection.sendMessage(watchGlobal);
366 * Gets a list of all peers from the node.
368 * @return A callback with the peer list
369 * @throws IOException
370 * if an I/O error occurs with the node
371 * @throws HighLevelException
372 * if the client is not connected
374 public HighLevelCallback<PeerListResult> getPeers() throws IOException, HighLevelException {
376 String identifier = generateIdentifier("listPeers");
377 ListPeers listPeers = new ListPeers(identifier, true, true);
378 HighLevelCallback<PeerListResult> peerListCallback = new HighLevelCallback<PeerListResult>(new PeerListResult(identifier));
379 peerListCallbacks.put(identifier, peerListCallback);
380 fcpConnection.sendMessage(listPeers);
381 return peerListCallback;
385 * Adds the peer whose noderef is stored in the given file.
388 * The name of the file the peer’s noderef is stored in
389 * @return A peer callback
390 * @throws IOException
391 * if an I/O error occurs communicating with the node
392 * @throws HighLevelException
393 * if the client is not connected
395 public HighLevelCallback<PeerResult> addPeer(String nodeRefFile) throws IOException, HighLevelException {
397 String identifier = generateIdentifier("addPeer");
398 AddPeer addPeer = new AddPeer(nodeRefFile);
399 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
400 peerCallbacks.put(identifier, peerCallback);
401 fcpConnection.sendMessage(addPeer);
406 * Adds the peer whose noderef is stored in the given file.
409 * The URL where the peer’s noderef is stored
410 * @return A peer callback
411 * @throws IOException
412 * if an I/O error occurs communicating with the node
413 * @throws HighLevelException
414 * if the client is not connected
416 public HighLevelCallback<PeerResult> addPeer(URL nodeRefURL) throws IOException, HighLevelException {
418 String identifier = generateIdentifier("addPeer");
419 AddPeer addPeer = new AddPeer(nodeRefURL);
420 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
421 peerCallbacks.put(identifier, peerCallback);
422 fcpConnection.sendMessage(addPeer);
427 * Adds the peer whose noderef is stored in the given file.
431 * @return A peer callback
432 * @throws IOException
433 * if an I/O error occurs communicating with the node
434 * @throws HighLevelException
435 * if the client is not connected
437 public HighLevelCallback<PeerResult> addPeer(NodeRef nodeRef) throws IOException, HighLevelException {
439 String identifier = generateIdentifier("addPeer");
440 AddPeer addPeer = new AddPeer(nodeRef);
441 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
442 peerCallbacks.put(identifier, peerCallback);
443 fcpConnection.sendMessage(addPeer);
448 * Checks whether direct disk access for the given directory is possible.
449 * You have to perform this check before you can upload or download anything
450 * from or the disk directly!
453 * The directory to check
455 * Whether you want to read the given directory
457 * Whether you want to write to the given directory
458 * @return A direct disk access callback
459 * @throws IOException
460 * if an I/O error occurs communicating with the node
461 * @throws HighLevelException
462 * if the client is not connected
464 public HighLevelCallback<DirectDiskAccessResult> checkDirectDiskAccess(String directory, boolean wantRead, boolean wantWrite) throws IOException, HighLevelException {
466 TestDDARequest testDDARequest = new TestDDARequest(directory, wantRead, wantWrite);
467 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = new HighLevelCallback<DirectDiskAccessResult>(new DirectDiskAccessResult(directory));
468 directDiskAccessCallbacks.put(directory, directDiskAccessCallback);
469 fcpConnection.sendMessage(testDDARequest);
470 return directDiskAccessCallback;
474 * Starts a download. Files can either be download to disk or streamed from
475 * the node. When downloading to disk you have to perform a direct disk
476 * access check for the directory you want to put the downloaded file in!
478 * @see #checkDirectDiskAccess(String, boolean, boolean)
482 * The filename to save the data to, or <code>null</code> to
483 * retrieve the data as InputStream from the
484 * {@link DownloadResult}
486 * Whether to put the download on the global queue
487 * @return A download result
488 * @throws IOException
489 * if an I/O error occurs communicating with the node
490 * @throws HighLevelException
491 * if the client is not connected
493 public HighLevelProgressCallback<DownloadResult> download(String uri, String filename, boolean global) throws IOException, HighLevelException {
495 String identifier = generateIdentifier("download");
496 ClientGet clientGet = new ClientGet(uri, identifier, (filename == null) ? ReturnType.direct : ReturnType.disk);
497 clientGet.setGlobal(global);
498 HighLevelProgressCallback<DownloadResult> downloadCallback = new HighLevelProgressCallback<DownloadResult>(new DownloadResult(identifier));
499 downloadCallbacks.put(identifier, downloadCallback);
500 fcpConnection.sendMessage(clientGet);
501 return downloadCallback;
505 * Requests a list of all running requests from the node.
507 * @return The request list result
508 * @throws IOException
509 * if an I/O errors communicating with the node
510 * @throws HighLevelException
511 * if the client is not connected
513 public HighLevelCallback<RequestListResult> getRequests() throws IOException, HighLevelException {
515 String identifier = generateIdentifier("list-persistent-requests");
516 ListPersistentRequests listPersistentRequests = new ListPersistentRequests();
517 synchronized (syncObject) {
518 if (requestListCallback != null) {
519 logger.log(Level.SEVERE, "getRequests() called with request still running!");
521 requestListCallback = new HighLevelCallback<RequestListResult>(new RequestListResult(identifier));
523 fcpConnection.sendMessage(listPersistentRequests);
524 return requestListCallback;
532 * Checks whether the client is already connected and throws an exception if
535 * @throws NotConnectedException
536 * if the client is not connected
538 private void checkConnection() throws NotConnectedException {
539 synchronized (this) {
540 if (fcpConnection == null) {
541 throw new NotConnectedException("client is not connected");
547 * Generates an identifier for the given function.
550 * The name of the function
551 * @return An identifier
553 private String generateIdentifier(String function) {
554 return "jFCPlib-" + function + "-" + System.currentTimeMillis();
558 * Disconnects the client from the node, handing the given Throwable to
559 * {@link #fireClientDisconnected(Throwable)}.
562 * The exception that caused the disconnect, or <code>null</code>
563 * if there was no exception
565 private void disconnect(Throwable throwable) {
566 if (fcpConnection != null) {
567 fcpConnection.close();
569 fcpConnection = null;
573 * FCP listener for {@link HighLevelClient}.
575 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
577 private class HighLevelClientFcpListener implements FcpListener {
579 /** Mapping from directory to written file (for cleanup). */
580 private final Map<DirectDiskAccessResult, String> writtenFiles = new HashMap<DirectDiskAccessResult, String>();
583 * Creates a new FCP listener for {@link HighLevelClient}.
585 HighLevelClientFcpListener() {
594 * Searches all callback collections for a callback with the given
595 * identifier and cancels it.
598 * The identifier to search for, or <code>null</code> to
599 * cancel all pending requests
601 @SuppressWarnings("synthetic-access")
602 private void cancelIdentifier(String identifier) {
603 synchronized (syncObject) {
604 if (connectCallback != null) {
605 connectCallback.getIntermediaryResult().setFailed(true);
606 connectCallback.setDone();
607 connectCallback = null;
609 if (requestListCallback != null) {
610 requestListCallback.getIntermediaryResult().setFailed(true);
611 requestListCallback.setDone();
612 requestListCallback = null;
615 if (identifier == null) {
616 /* key generation callbacks */
617 for (Entry<String, HighLevelCallback<KeyGenerationResult>> keyGenerationEntry: keyGenerationCallbacks.entrySet()) {
618 keyGenerationEntry.getValue().getIntermediaryResult().setFailed(true);
619 keyGenerationEntry.getValue().setDone();
621 keyGenerationCallbacks.clear();
622 /* peer list callbacks. */
623 for (Entry<String, HighLevelCallback<PeerListResult>> peerListEntry: peerListCallbacks.entrySet()) {
624 peerListEntry.getValue().getIntermediaryResult().setFailed(true);
625 peerListEntry.getValue().setDone();
627 peerListCallbacks.clear();
628 /* peer callbacks. */
629 for (Entry<String, HighLevelCallback<PeerResult>> peerEntry: peerCallbacks.entrySet()) {
630 peerEntry.getValue().getIntermediaryResult().setFailed(true);
631 peerEntry.getValue().setDone();
633 peerCallbacks.clear();
634 /* direct disk access callbacks. */
635 for (Entry<String, HighLevelCallback<DirectDiskAccessResult>> directDiskAccessEntry: directDiskAccessCallbacks.entrySet()) {
636 directDiskAccessEntry.getValue().getIntermediaryResult().setFailed(true);
637 directDiskAccessEntry.getValue().setDone();
639 directDiskAccessCallbacks.clear();
640 /* download callbacks. */
641 for (Entry<String, HighLevelProgressCallback<DownloadResult>> downloadEntry: downloadCallbacks.entrySet()) {
642 downloadEntry.getValue().getIntermediaryResult().setFailed(true);
643 downloadEntry.getValue().setDone();
645 downloadCallbacks.clear();
647 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(identifier);
648 if (keyGenerationCallback != null) {
649 keyGenerationCallback.getIntermediaryResult().setFailed(true);
650 keyGenerationCallback.setDone();
653 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
654 if (peerListCallback != null) {
655 peerListCallback.getIntermediaryResult().setFailed(true);
656 peerListCallback.setDone();
659 HighLevelCallback<PeerResult> peerCallback = peerCallbacks.remove(identifier);
660 if (peerCallback != null) {
661 peerCallback.getIntermediaryResult().setFailed(true);
662 peerCallback.setDone();
665 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = directDiskAccessCallbacks.remove(identifier);
666 if (directDiskAccessCallback != null) {
667 directDiskAccessCallback.getIntermediaryResult().setFailed(true);
668 directDiskAccessCallback.setDone();
671 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
672 if (downloadCallback != null) {
673 downloadCallback.getIntermediaryResult().setFailed(true);
674 downloadCallback.setDone();
681 * Reads the given file and returns the first line of the file.
683 * @param readFilename
684 * The name of the file to read
685 * @return The content of the file
687 private String readContent(String readFilename) {
688 FileReader fileReader = null;
689 BufferedReader bufferedFileReader = null;
691 fileReader = new FileReader(readFilename);
692 bufferedFileReader = new BufferedReader(fileReader);
693 String content = bufferedFileReader.readLine();
695 } catch (IOException ioe1) {
698 FcpUtils.close(bufferedFileReader);
699 FcpUtils.close(fileReader);
705 * Writes the given content to the given file.
707 * @param directDiskAccessResult
709 * @param writeFilename
710 * The name of the file to write to
711 * @param writeContent
712 * The content to write to the file
714 private void writeContent(DirectDiskAccessResult directDiskAccessResult, String writeFilename, String writeContent) {
715 if ((writeFilename == null) || (writeContent == null)) {
718 writtenFiles.put(directDiskAccessResult, writeFilename);
719 FileWriter fileWriter = null;
721 fileWriter = new FileWriter(writeFilename);
722 fileWriter.write(writeContent);
723 } catch (IOException ioe1) {
726 FcpUtils.close(fileWriter);
731 * Cleans up any files that written for the given result.
733 * @param directDiskAccessResult
734 * The direct disk access result
736 @SuppressWarnings("synthetic-access")
737 private void cleanFiles(DirectDiskAccessResult directDiskAccessResult) {
738 String writeFilename = writtenFiles.remove(directDiskAccessResult);
739 if (writeFilename != null) {
740 if (!new File(writeFilename).delete()) {
741 logger.warning("could not delete " + writeFilename);
747 // INTERFACE FcpListener
751 * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection,
754 @SuppressWarnings("synthetic-access")
755 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
756 if (fcpConnection != HighLevelClient.this.fcpConnection) {
759 cancelIdentifier(null);
760 disconnect(throwable);
761 fireClientDisconnected(throwable);
765 * @see net.pterodactylus.fcp.FcpListener#receivedAllData(net.pterodactylus.fcp.FcpConnection,
766 * net.pterodactylus.fcp.AllData)
768 public void receivedAllData(FcpConnection fcpConnection, AllData allData) {
773 * @see net.pterodactylus.fcp.FcpListener#receivedCloseConnectionDuplicateClientName(net.pterodactylus.fcp.FcpConnection,
774 * net.pterodactylus.fcp.CloseConnectionDuplicateClientName)
776 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
781 * @see net.pterodactylus.fcp.FcpListener#receivedConfigData(net.pterodactylus.fcp.FcpConnection,
782 * net.pterodactylus.fcp.ConfigData)
784 public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
789 * @see net.pterodactylus.fcp.FcpListener#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
790 * net.pterodactylus.fcp.DataFound)
792 @SuppressWarnings("synthetic-access")
793 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
794 if (fcpConnection != HighLevelClient.this.fcpConnection) {
797 String identifier = dataFound.getIdentifier();
798 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
799 if (downloadCallback != null) {
800 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
801 downloadResult.setFinished(true);
802 downloadResult.setFailed(false);
803 downloadCallback.progressUpdated();
804 downloadCallback.setDone();
806 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, true);
807 fireProgressReceived(identifier, highLevelProgress);
811 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeerNotes(net.pterodactylus.fcp.FcpConnection,
812 * net.pterodactylus.fcp.EndListPeerNotes)
814 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
819 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeers(net.pterodactylus.fcp.FcpConnection,
820 * net.pterodactylus.fcp.EndListPeers)
822 @SuppressWarnings("synthetic-access")
823 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
824 if (fcpConnection != HighLevelClient.this.fcpConnection) {
827 String identifier = endListPeers.getIdentifier();
828 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
829 if (peerListCallback == null) {
832 peerListCallback.setDone();
836 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPersistentRequests(net.pterodactylus.fcp.FcpConnection,
837 * net.pterodactylus.fcp.EndListPersistentRequests)
839 @SuppressWarnings("synthetic-access")
840 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
841 if (fcpConnection != HighLevelClient.this.fcpConnection) {
844 synchronized (syncObject) {
845 if (HighLevelClient.this.requestListCallback == null) {
846 logger.log(Level.WARNING, "got EndListPersistentRequests without running request!");
849 requestListCallback.setDone();
850 requestListCallback = null;
855 * @see net.pterodactylus.fcp.FcpListener#receivedFCPPluginReply(net.pterodactylus.fcp.FcpConnection,
856 * net.pterodactylus.fcp.FCPPluginReply)
858 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
863 * @see net.pterodactylus.fcp.FcpListener#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
864 * net.pterodactylus.fcp.GetFailed)
866 @SuppressWarnings("synthetic-access")
867 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
868 if (fcpConnection != HighLevelClient.this.fcpConnection) {
871 String identifier = getFailed.getIdentifier();
872 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
873 if (downloadCallback != null) {
874 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
875 downloadResult.setFailed(true);
876 downloadResult.setFinished(true);
877 downloadCallback.setDone();
880 /* unknown identifier? */
881 logger.warning("unknown identifier for GetFailed: " + identifier);
885 * @see net.pterodactylus.fcp.FcpListener#receivedIdentifierCollision(net.pterodactylus.fcp.FcpConnection,
886 * net.pterodactylus.fcp.IdentifierCollision)
888 public void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
893 * @see net.pterodactylus.fcp.FcpListener#receivedMessage(net.pterodactylus.fcp.FcpConnection,
894 * net.pterodactylus.fcp.FcpMessage)
896 public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
901 * @see net.pterodactylus.fcp.FcpListener#receivedNodeData(net.pterodactylus.fcp.FcpConnection,
902 * net.pterodactylus.fcp.NodeData)
904 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
909 * @see net.pterodactylus.fcp.FcpListener#receivedNodeHello(net.pterodactylus.fcp.FcpConnection,
910 * net.pterodactylus.fcp.NodeHello)
912 @SuppressWarnings("synthetic-access")
913 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
914 if (fcpConnection != HighLevelClient.this.fcpConnection) {
917 synchronized (syncObject) {
918 connectCallback.getIntermediaryResult().setFailed(false);
919 connectCallback.setDone();
920 connectCallback = null;
922 fireClientConnected();
926 * @see net.pterodactylus.fcp.FcpListener#receivedPeer(net.pterodactylus.fcp.FcpConnection,
927 * net.pterodactylus.fcp.Peer)
929 @SuppressWarnings("synthetic-access")
930 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
931 if (fcpConnection != HighLevelClient.this.fcpConnection) {
934 String identifier = peer.getIdentifier();
935 if (identifier == null) {
938 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.get(identifier);
939 if (peerListCallback != null) {
940 peerListCallback.getIntermediaryResult().addPeer(peer);
943 HighLevelCallback<PeerResult> peerResult = peerCallbacks.remove(identifier);
944 if (peerResult != null) {
945 peerResult.getIntermediaryResult().setPeer(peer);
946 peerResult.setDone();
949 logger.warning("got Peer message with unknown identifier: " + identifier);
953 * @see net.pterodactylus.fcp.FcpListener#receivedPeerNote(net.pterodactylus.fcp.FcpConnection,
954 * net.pterodactylus.fcp.PeerNote)
956 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
961 * @see net.pterodactylus.fcp.FcpListener#receivedPeerRemoved(net.pterodactylus.fcp.FcpConnection,
962 * net.pterodactylus.fcp.PeerRemoved)
964 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
969 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentGet(net.pterodactylus.fcp.FcpConnection,
970 * net.pterodactylus.fcp.PersistentGet)
972 @SuppressWarnings("synthetic-access")
973 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
974 if (fcpConnection != HighLevelClient.this.fcpConnection) {
977 synchronized (syncObject) {
978 if (requestListCallback != null) {
979 RequestListResult requestListResult = requestListCallback.getIntermediaryResult();
980 requestListResult.addRequestResult(new GetRequestResult(persistentGet));
984 String identifier = persistentGet.getIdentifier();
985 if (downloadCallbacks.containsKey(identifier)) {
992 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
993 * net.pterodactylus.fcp.PersistentPut)
995 @SuppressWarnings("synthetic-access")
996 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
997 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1000 synchronized (syncObject) {
1001 if (requestListCallback != null) {
1002 RequestListResult requestListResult = requestListCallback.getIntermediaryResult();
1003 requestListResult.addRequestResult(new PutRequestResult(persistentPut));
1010 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPutDir(net.pterodactylus.fcp.FcpConnection,
1011 * net.pterodactylus.fcp.PersistentPutDir)
1013 @SuppressWarnings("synthetic-access")
1014 public void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
1015 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1018 synchronized (syncObject) {
1019 if (requestListCallback != null) {
1020 RequestListResult requestListResult = requestListCallback.getIntermediaryResult();
1021 requestListResult.addRequestResult(new PutDirRequestResult(persistentPutDir));
1028 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestModified(net.pterodactylus.fcp.FcpConnection,
1029 * net.pterodactylus.fcp.PersistentRequestModified)
1031 public void receivedPersistentRequestModified(FcpConnection fcpConnection, PersistentRequestModified persistentRequestModified) {
1036 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestRemoved(net.pterodactylus.fcp.FcpConnection,
1037 * net.pterodactylus.fcp.PersistentRequestRemoved)
1039 public void receivedPersistentRequestRemoved(FcpConnection fcpConnection, PersistentRequestRemoved persistentRequestRemoved) {
1044 * @see net.pterodactylus.fcp.FcpListener#receivedPluginInfo(net.pterodactylus.fcp.FcpConnection,
1045 * net.pterodactylus.fcp.PluginInfo)
1047 public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
1052 * @see net.pterodactylus.fcp.FcpListener#receivedProtocolError(net.pterodactylus.fcp.FcpConnection,
1053 * net.pterodactylus.fcp.ProtocolError)
1055 @SuppressWarnings("synthetic-access")
1056 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
1057 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1060 String identifier = protocolError.getIdentifier();
1061 if (identifier == null) {
1064 cancelIdentifier(identifier);
1068 * @see net.pterodactylus.fcp.FcpListener#receivedPutFailed(net.pterodactylus.fcp.FcpConnection,
1069 * net.pterodactylus.fcp.PutFailed)
1071 @SuppressWarnings("synthetic-access")
1072 public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
1073 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1076 String identifier = putFailed.getIdentifier();
1077 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
1078 if (downloadCallback != null) {
1079 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
1080 downloadResult.setFailed(true);
1081 downloadResult.setFinished(true);
1082 downloadCallback.progressUpdated();
1083 downloadCallback.setDone();
1085 /* TODO - check inserts */
1086 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, true);
1087 fireProgressReceived(identifier, highLevelProgress);
1091 * @see net.pterodactylus.fcp.FcpListener#receivedPutFetchable(net.pterodactylus.fcp.FcpConnection,
1092 * net.pterodactylus.fcp.PutFetchable)
1094 @SuppressWarnings("synthetic-access")
1095 public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
1096 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1099 String identifier = putFetchable.getIdentifier();
1100 /* TODO - check inserts */
1101 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier);
1102 highLevelProgress.setFetchable(true);
1103 fireProgressReceived(identifier, highLevelProgress);
1107 * @see net.pterodactylus.fcp.FcpListener#receivedPutSuccessful(net.pterodactylus.fcp.FcpConnection,
1108 * net.pterodactylus.fcp.PutSuccessful)
1110 @SuppressWarnings("synthetic-access")
1111 public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
1112 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1115 String identifier = putSuccessful.getIdentifier();
1116 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
1117 if (downloadCallback != null) {
1118 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
1119 downloadResult.setFinished(true);
1120 downloadResult.setFailed(false);
1121 downloadCallback.progressUpdated();
1123 /* TODO - check inserts */
1124 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, true);
1125 fireProgressReceived(identifier, highLevelProgress);
1129 * @see net.pterodactylus.fcp.FcpListener#receivedSSKKeypair(net.pterodactylus.fcp.FcpConnection,
1130 * net.pterodactylus.fcp.SSKKeypair)
1132 @SuppressWarnings("synthetic-access")
1133 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
1134 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1137 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(sskKeypair.getIdentifier());
1138 if (keyGenerationCallback == null) {
1141 KeyGenerationResult keyGenerationResult = keyGenerationCallback.getIntermediaryResult();
1142 keyGenerationResult.setInsertURI(sskKeypair.getInsertURI());
1143 keyGenerationResult.setRequestURI(sskKeypair.getRequestURI());
1144 keyGenerationCallback.setDone();
1148 * @see net.pterodactylus.fcp.FcpListener#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
1149 * net.pterodactylus.fcp.SimpleProgress)
1151 @SuppressWarnings("synthetic-access")
1152 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
1153 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1156 String identifier = simpleProgress.getIdentifier();
1157 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
1158 if (downloadCallback != null) {
1159 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
1160 downloadResult.setTotalBlocks(simpleProgress.getTotal());
1161 downloadResult.setRequiredBlocks(simpleProgress.getRequired());
1162 downloadResult.setSuccessfulBlocks(simpleProgress.getSucceeded());
1163 downloadResult.setFailedBlocks(simpleProgress.getFailed());
1164 downloadResult.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
1165 downloadResult.setTotalFinalized(simpleProgress.isFinalizedTotal());
1166 downloadCallback.progressUpdated();
1168 /* TODO - check inserts */
1169 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, simpleProgress.getTotal(), simpleProgress.getRequired(), simpleProgress.getSucceeded(), simpleProgress.getFailed(), simpleProgress.getFatallyFailed(), simpleProgress.isFinalizedTotal());
1170 fireProgressReceived(identifier, highLevelProgress);
1174 * @see net.pterodactylus.fcp.FcpListener#receivedStartedCompression(net.pterodactylus.fcp.FcpConnection,
1175 * net.pterodactylus.fcp.StartedCompression)
1177 public void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
1182 * @see net.pterodactylus.fcp.FcpListener#receivedSubscribedUSKUpdate(net.pterodactylus.fcp.FcpConnection,
1183 * net.pterodactylus.fcp.SubscribedUSKUpdate)
1185 public void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
1190 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAComplete(net.pterodactylus.fcp.FcpConnection,
1191 * net.pterodactylus.fcp.TestDDAComplete)
1193 @SuppressWarnings("synthetic-access")
1194 public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
1195 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1198 String directory = testDDAComplete.getDirectory();
1199 if (directory == null) {
1202 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = directDiskAccessCallbacks.remove(directory);
1203 DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallback.getIntermediaryResult();
1204 cleanFiles(directDiskAccessResult);
1205 directDiskAccessResult.setReadAllowed(testDDAComplete.isReadDirectoryAllowed());
1206 directDiskAccessResult.setWriteAllowed(testDDAComplete.isWriteDirectoryAllowed());
1207 directDiskAccessCallback.setDone();
1211 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAReply(net.pterodactylus.fcp.FcpConnection,
1212 * net.pterodactylus.fcp.TestDDAReply)
1214 @SuppressWarnings("synthetic-access")
1215 public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
1216 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1219 String directory = testDDAReply.getDirectory();
1220 if (directory == null) {
1223 DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallbacks.get(directory).getIntermediaryResult();
1224 String readFilename = testDDAReply.getReadFilename();
1225 String readContent = readContent(readFilename);
1226 String writeFilename = testDDAReply.getWriteFilename();
1227 String writeContent = testDDAReply.getContentToWrite();
1228 writeContent(directDiskAccessResult, writeFilename, writeContent);
1229 TestDDAResponse testDDAResponse = new TestDDAResponse(directory, readContent);
1231 fcpConnection.sendMessage(testDDAResponse);
1232 } catch (IOException e) {
1233 /* swallow. I’m verry unhappy about this. */
1238 * @see net.pterodactylus.fcp.FcpListener#receivedURIGenerated(net.pterodactylus.fcp.FcpConnection,
1239 * net.pterodactylus.fcp.URIGenerated)
1241 @SuppressWarnings("synthetic-access")
1242 public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
1243 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1246 String identifier = uriGenerated.getIdentifier();
1247 /* TODO - check inserts */
1248 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, uriGenerated.getURI());
1249 fireProgressReceived(identifier, highLevelProgress);
1253 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownNodeIdentifier(net.pterodactylus.fcp.FcpConnection,
1254 * net.pterodactylus.fcp.UnknownNodeIdentifier)
1256 public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
1261 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownPeerNoteType(net.pterodactylus.fcp.FcpConnection,
1262 * net.pterodactylus.fcp.UnknownPeerNoteType)
1264 public void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
1269 * @see net.pterodactylus.fcp.FcpListener#receviedFinishedCompression(net.pterodactylus.fcp.FcpConnection,
1270 * net.pterodactylus.fcp.FinishedCompression)
1272 public void receviedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {