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>
97 public class HighLevelClient {
100 private static final Logger logger = Logger.getLogger(HighLevelClient.class.getName());
102 /** Object for internal synchronization. */
103 private final Object syncObject = new Object();
105 /** The name of the client. */
106 private final String clientName;
108 /** The address of the node. */
109 private InetAddress address;
111 /** The port number of the node. */
114 /** The FCP connection to the node. */
115 private FcpConnection fcpConnection;
117 /** Listeners for high-level client events. */
118 private List<HighLevelClientListener> highLevelClientListeners = Collections.synchronizedList(new ArrayList<HighLevelClientListener>());
120 /** The listener for the connection. */
121 private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
123 /** The listeners for progress events. */
124 private List<HighLevelProgressListener> highLevelProgressListeners = Collections.synchronizedList(new ArrayList<HighLevelProgressListener>());
126 /** The callback for {@link #connect()}. */
127 private HighLevelCallback<ConnectResult> connectCallback;
129 /** Mapping from request identifiers to callbacks. */
130 private Map<String, HighLevelCallback<KeyGenerationResult>> keyGenerationCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<KeyGenerationResult>>());
132 /** Mapping from request identifier to peer list callbacks. */
133 private Map<String, HighLevelCallback<PeerListResult>> peerListCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerListResult>>());
135 /** Mapping from request identifier to peer callbacks. */
136 private Map<String, HighLevelCallback<PeerResult>> peerCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerResult>>());
138 /** Mapping from directories to DDA callbacks. */
139 private Map<String, HighLevelCallback<DirectDiskAccessResult>> directDiskAccessCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<DirectDiskAccessResult>>());
141 /** Mapping from request identifiers to download callbacks. */
142 private Map<String, HighLevelProgressCallback<DownloadResult>> downloadCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelProgressCallback<DownloadResult>>());
144 /** The callback for {@link #getRequests()}. */
145 private HighLevelCallback<RequestListResult> requestListCallback;
148 * Creates a new high-level client that connects to a node on
149 * <code>localhost</code>.
152 * The name of the client
153 * @throws UnknownHostException
154 * if the hostname of the node can not be resolved.
156 public HighLevelClient(String clientName) throws UnknownHostException {
157 this(clientName, "localhost");
161 * Creates a new high-level client that connects to a node on the given
165 * The name of the client
167 * The hostname of the node
168 * @throws UnknownHostException
169 * if the hostname of the node can not be resolved.
171 public HighLevelClient(String clientName, String host) throws UnknownHostException {
172 this(clientName, host, FcpConnection.DEFAULT_PORT);
176 * Creates a new high-level client that connects to a node on the given
180 * The name of the client
182 * The hostname of the node
184 * The port number of the node
185 * @throws UnknownHostException
186 * if the hostname of the node can not be resolved.
188 public HighLevelClient(String clientName, String host, int port) throws UnknownHostException {
189 this(clientName, InetAddress.getByName(host), port);
193 * Creates a new high-level client that connects to a node at the given
197 * The name of the client
199 * The address of the node
201 * The port number of the node
203 public HighLevelClient(String clientName, InetAddress address, int port) {
204 this.clientName = clientName;
205 this.address = address;
214 * Adds the given high-level client listener to list of listeners.
216 * @param highLevelClientListener
217 * The listener to add
219 public void addHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
220 highLevelClientListeners.add(highLevelClientListener);
224 * Removes the given high-level client listener from the list of listeners.
226 * @param highLevelClientListener
227 * The listener to remove
229 public void removeHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
230 highLevelClientListeners.remove(highLevelClientListener);
234 * Notifies all listeners that a client has connected.
236 private void fireClientConnected() {
237 for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
238 highLevelClientListener.clientConnected(this);
243 * Notifies all listeners that a client has disconnected.
246 * The exception that caused the disconnect, or <code>null</code>
247 * if there was no exception
249 private void fireClientDisconnected(Throwable throwable) {
250 for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
251 highLevelClientListener.clientDisconnected(this, throwable);
256 * Adds a high-level progress listener.
258 * @param highLevelProgressListener
259 * The high-level progress listener to add
261 public void addHighLevelProgressListener(HighLevelProgressListener highLevelProgressListener) {
262 highLevelProgressListeners.add(highLevelProgressListener);
266 * Removes a high-level progress listener.
268 * @param highLevelProgressListener
269 * The high-level progress listener to remove
271 public void removeHighLevelProgressListener(HighLevelProgressListener highLevelProgressListener) {
272 highLevelProgressListeners.remove(highLevelProgressListener);
276 * Notifies all listeners that the request with the given identifier made
280 * The identifier of the request
281 * @param highLevelProgress
282 * The progress of the request
284 private void fireProgressReceived(String identifier, HighLevelProgress highLevelProgress) {
285 for (HighLevelProgressListener highLevelProgressListener: highLevelProgressListeners) {
286 highLevelProgressListener.progressReceived(identifier, highLevelProgress);
295 * Returns the FCP connection that backs this high-level client. This method
296 * should be used with care as fiddling around with the FCP connection can
297 * easily break the high-level client if you don’t know what you’re doing!
299 * @return The FCP connection of this client
301 public FcpConnection getFcpConnection() {
302 return fcpConnection;
310 * Connects the client.
312 * @return A callback with a connection result
313 * @throws IOException
314 * if an I/O error occurs communicating with the node
316 public HighLevelCallback<ConnectResult> connect() throws IOException {
317 fcpConnection = new FcpConnection(address, port);
318 fcpConnection.addFcpListener(highLevelClientFcpListener);
319 fcpConnection.connect();
320 ClientHello clientHello = new ClientHello(clientName);
321 connectCallback = new HighLevelCallback<ConnectResult>(new ConnectResult());
322 fcpConnection.sendMessage(clientHello);
323 return connectCallback;
327 * Disconnects the client from the node.
329 public void disconnect() {
334 * Generates a new SSK keypair.
336 * @return A callback with the keypair
337 * @throws IOException
338 * if an I/O error occurs communicating with the node
340 public HighLevelCallback<KeyGenerationResult> generateKey() throws IOException {
341 String identifier = generateIdentifier("generateSSK");
342 GenerateSSK generateSSK = new GenerateSSK(identifier);
343 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>(new KeyGenerationResult(identifier));
344 keyGenerationCallbacks.put(identifier, keyGenerationCallback);
345 fcpConnection.sendMessage(generateSSK);
346 return keyGenerationCallback;
350 * Sets whether to watch the global queue.
353 * <code>true</code> to watch the global queue in addition to
354 * the client-local queue, <code>false</code> to only watch the
356 * @throws IOException
357 * if an I/O error occurs communicating with the node
359 public void setWatchGlobal(boolean enabled) throws IOException {
360 WatchGlobal watchGlobal = new WatchGlobal(enabled);
361 fcpConnection.sendMessage(watchGlobal);
365 * Gets a list of all peers from the node.
367 * @return A callback with the peer list
368 * @throws IOException
369 * if an I/O error occurs with the node
371 public HighLevelCallback<PeerListResult> getPeers() throws IOException {
372 String identifier = generateIdentifier("listPeers");
373 ListPeers listPeers = new ListPeers(identifier, true, true);
374 HighLevelCallback<PeerListResult> peerListCallback = new HighLevelCallback<PeerListResult>(new PeerListResult(identifier));
375 peerListCallbacks.put(identifier, peerListCallback);
376 fcpConnection.sendMessage(listPeers);
377 return peerListCallback;
381 * Adds the peer whose noderef is stored in the given file.
384 * The name of the file the peer’s noderef is stored in
385 * @return A peer callback
386 * @throws IOException
387 * if an I/O error occurs communicating with the node
389 public HighLevelCallback<PeerResult> addPeer(String nodeRefFile) throws IOException {
390 String identifier = generateIdentifier("addPeer");
391 AddPeer addPeer = new AddPeer(nodeRefFile);
392 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
393 peerCallbacks.put(identifier, peerCallback);
394 fcpConnection.sendMessage(addPeer);
399 * Adds the peer whose noderef is stored in the given file.
402 * The URL where the peer’s noderef is stored
403 * @return A peer callback
404 * @throws IOException
405 * if an I/O error occurs communicating with the node
407 public HighLevelCallback<PeerResult> addPeer(URL nodeRefURL) throws IOException {
408 String identifier = generateIdentifier("addPeer");
409 AddPeer addPeer = new AddPeer(nodeRefURL);
410 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
411 peerCallbacks.put(identifier, peerCallback);
412 fcpConnection.sendMessage(addPeer);
417 * Adds the peer whose noderef is stored in the given file.
421 * @return A peer callback
422 * @throws IOException
423 * if an I/O error occurs communicating with the node
425 public HighLevelCallback<PeerResult> addPeer(NodeRef nodeRef) throws IOException {
426 String identifier = generateIdentifier("addPeer");
427 AddPeer addPeer = new AddPeer(nodeRef);
428 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
429 peerCallbacks.put(identifier, peerCallback);
430 fcpConnection.sendMessage(addPeer);
435 * Checks whether direct disk access for the given directory is possible.
436 * You have to perform this check before you can upload or download anything
437 * from or the disk directly!
440 * The directory to check
442 * Whether you want to read the given directory
444 * Whether you want to write to the given directory
445 * @return A direct disk access callback
446 * @throws IOException
448 public HighLevelCallback<DirectDiskAccessResult> checkDirectDiskAccess(String directory, boolean wantRead, boolean wantWrite) throws IOException {
449 TestDDARequest testDDARequest = new TestDDARequest(directory, wantRead, wantWrite);
450 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = new HighLevelCallback<DirectDiskAccessResult>(new DirectDiskAccessResult(directory));
451 directDiskAccessCallbacks.put(directory, directDiskAccessCallback);
452 fcpConnection.sendMessage(testDDARequest);
453 return directDiskAccessCallback;
457 * Starts a download. Files can either be download to disk or streamed from
458 * the node. When downloading to disk you have to perform a direct disk
459 * access check for the directory you want to put the downloaded file in!
461 * @see #checkDirectDiskAccess(String, boolean, boolean)
465 * The filename to save the data to, or <code>null</code> to
466 * retrieve the data as InputStream from the
467 * {@link DownloadResult}
469 * Whether to put the download on the global queue
470 * @return A download result
471 * @throws IOException
472 * if an I/O error occurs communicating with the node
474 public HighLevelProgressCallback<DownloadResult> download(String uri, String filename, boolean global) throws IOException {
475 String identifier = generateIdentifier("download");
476 ClientGet clientGet = new ClientGet(uri, identifier, (filename == null) ? ReturnType.direct : ReturnType.disk);
477 clientGet.setGlobal(global);
478 HighLevelProgressCallback<DownloadResult> downloadCallback = new HighLevelProgressCallback<DownloadResult>(new DownloadResult(identifier));
479 downloadCallbacks.put(identifier, downloadCallback);
480 fcpConnection.sendMessage(clientGet);
481 return downloadCallback;
485 * Requests a list of all running requests from the node.
487 * @return The request list result
488 * @throws IOException
489 * if an I/O errors communicating with the node
491 public HighLevelCallback<RequestListResult> getRequests() throws IOException {
492 String identifier = generateIdentifier("list-persistent-requests");
493 ListPersistentRequests listPersistentRequests = new ListPersistentRequests();
494 synchronized (syncObject) {
495 if (requestListCallback != null) {
496 logger.log(Level.SEVERE, "getRequests() called with request still running!");
498 requestListCallback = new HighLevelCallback<RequestListResult>(new RequestListResult(identifier));
500 fcpConnection.sendMessage(listPersistentRequests);
501 return requestListCallback;
509 * Generates an identifier for the given function.
512 * The name of the function
513 * @return An identifier
515 private String generateIdentifier(String function) {
516 return "jFCPlib-" + function + "-" + System.currentTimeMillis();
520 * Disconnects the client from the node, handing the given Throwable to
521 * {@link #fireClientDisconnected(Throwable)}.
524 * The exception that caused the disconnect, or <code>null</code>
525 * if there was no exception
527 private void disconnect(Throwable throwable) {
528 fcpConnection.close();
529 fireClientDisconnected(throwable);
533 * FCP listener for {@link HighLevelClient}.
535 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
538 private class HighLevelClientFcpListener implements FcpListener {
540 /** Mapping from directory to written file (for cleanup). */
541 private final Map<DirectDiskAccessResult, String> writtenFiles = new HashMap<DirectDiskAccessResult, String>();
544 * Creates a new FCP listener for {@link HighLevelClient}.
546 HighLevelClientFcpListener() {
555 * Searches all callback collections for a callback with the given
556 * identifier and cancels it.
559 * The identifier to search for, or <code>null</code> to
560 * cancel all pending requests
562 @SuppressWarnings("synthetic-access")
563 private void cancelIdentifier(String identifier) {
564 synchronized (syncObject) {
565 if (connectCallback != null) {
566 connectCallback.getIntermediaryResult().setFailed(true);
567 connectCallback.setDone();
568 connectCallback = null;
570 if (requestListCallback != null) {
571 requestListCallback.getIntermediaryResult().setFailed(true);
572 requestListCallback.setDone();
573 requestListCallback = null;
576 if (identifier == null) {
577 /* key generation callbacks */
578 for (Entry<String, HighLevelCallback<KeyGenerationResult>> keyGenerationEntry: keyGenerationCallbacks.entrySet()) {
579 keyGenerationEntry.getValue().getIntermediaryResult().setFailed(true);
580 keyGenerationEntry.getValue().setDone();
582 keyGenerationCallbacks.clear();
583 /* peer list callbacks. */
584 for (Entry<String, HighLevelCallback<PeerListResult>> peerListEntry: peerListCallbacks.entrySet()) {
585 peerListEntry.getValue().getIntermediaryResult().setFailed(true);
586 peerListEntry.getValue().setDone();
588 peerListCallbacks.clear();
589 /* peer callbacks. */
590 for (Entry<String, HighLevelCallback<PeerResult>> peerEntry: peerCallbacks.entrySet()) {
591 peerEntry.getValue().getIntermediaryResult().setFailed(true);
592 peerEntry.getValue().setDone();
594 peerCallbacks.clear();
595 /* direct disk access callbacks. */
596 for (Entry<String, HighLevelCallback<DirectDiskAccessResult>> directDiskAccessEntry: directDiskAccessCallbacks.entrySet()) {
597 directDiskAccessEntry.getValue().getIntermediaryResult().setFailed(true);
598 directDiskAccessEntry.getValue().setDone();
600 directDiskAccessCallbacks.clear();
601 /* download callbacks. */
602 for (Entry<String, HighLevelProgressCallback<DownloadResult>> downloadEntry: downloadCallbacks.entrySet()) {
603 downloadEntry.getValue().getIntermediaryResult().setFailed(true);
604 downloadEntry.getValue().setDone();
606 downloadCallbacks.clear();
608 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(identifier);
609 if (keyGenerationCallback != null) {
610 keyGenerationCallback.getIntermediaryResult().setFailed(true);
611 keyGenerationCallback.setDone();
614 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
615 if (peerListCallback != null) {
616 peerListCallback.getIntermediaryResult().setFailed(true);
617 peerListCallback.setDone();
620 HighLevelCallback<PeerResult> peerCallback = peerCallbacks.remove(identifier);
621 if (peerCallback != null) {
622 peerCallback.getIntermediaryResult().setFailed(true);
623 peerCallback.setDone();
626 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = directDiskAccessCallbacks.remove(identifier);
627 if (directDiskAccessCallback != null) {
628 directDiskAccessCallback.getIntermediaryResult().setFailed(true);
629 directDiskAccessCallback.setDone();
632 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
633 if (downloadCallback != null) {
634 downloadCallback.getIntermediaryResult().setFailed(true);
635 downloadCallback.setDone();
642 * Reads the given file and returns the first line of the file.
644 * @param readFilename
645 * The name of the file to read
646 * @return The content of the file
648 private String readContent(String readFilename) {
649 FileReader fileReader = null;
650 BufferedReader bufferedFileReader = null;
652 fileReader = new FileReader(readFilename);
653 bufferedFileReader = new BufferedReader(fileReader);
654 String content = bufferedFileReader.readLine();
656 } catch (IOException ioe1) {
659 FcpUtils.close(bufferedFileReader);
660 FcpUtils.close(fileReader);
666 * Writes the given content to the given file.
668 * @param directDiskAccessResult
670 * @param writeFilename
671 * The name of the file to write to
672 * @param writeContent
673 * The content to write to the file
675 private void writeContent(DirectDiskAccessResult directDiskAccessResult, String writeFilename, String writeContent) {
676 if ((writeFilename == null) || (writeContent == null)) {
679 writtenFiles.put(directDiskAccessResult, writeFilename);
680 FileWriter fileWriter = null;
682 fileWriter = new FileWriter(writeFilename);
683 fileWriter.write(writeContent);
684 } catch (IOException ioe1) {
687 FcpUtils.close(fileWriter);
692 * Cleans up any files that written for the given result.
694 * @param directDiskAccessResult
695 * The direct disk access result
697 @SuppressWarnings("synthetic-access")
698 private void cleanFiles(DirectDiskAccessResult directDiskAccessResult) {
699 String writeFilename = writtenFiles.remove(directDiskAccessResult);
700 if (writeFilename != null) {
701 if (!new File(writeFilename).delete()) {
702 logger.warning("could not delete " + writeFilename);
708 // INTERFACE FcpListener
712 * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection,
715 @SuppressWarnings("synthetic-access")
716 public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) {
717 if (fcpConnection != HighLevelClient.this.fcpConnection) {
720 cancelIdentifier(null);
721 disconnect(throwable);
725 * @see net.pterodactylus.fcp.FcpListener#receivedAllData(net.pterodactylus.fcp.FcpConnection,
726 * net.pterodactylus.fcp.AllData)
728 public void receivedAllData(FcpConnection fcpConnection, AllData allData) {
733 * @see net.pterodactylus.fcp.FcpListener#receivedCloseConnectionDuplicateClientName(net.pterodactylus.fcp.FcpConnection,
734 * net.pterodactylus.fcp.CloseConnectionDuplicateClientName)
736 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
741 * @see net.pterodactylus.fcp.FcpListener#receivedConfigData(net.pterodactylus.fcp.FcpConnection,
742 * net.pterodactylus.fcp.ConfigData)
744 public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
749 * @see net.pterodactylus.fcp.FcpListener#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
750 * net.pterodactylus.fcp.DataFound)
752 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
757 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeerNotes(net.pterodactylus.fcp.FcpConnection,
758 * net.pterodactylus.fcp.EndListPeerNotes)
760 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
765 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeers(net.pterodactylus.fcp.FcpConnection,
766 * net.pterodactylus.fcp.EndListPeers)
768 @SuppressWarnings("synthetic-access")
769 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
770 if (fcpConnection != HighLevelClient.this.fcpConnection) {
773 String identifier = endListPeers.getIdentifier();
774 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
775 if (peerListCallback == null) {
778 peerListCallback.setDone();
782 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPersistentRequests(net.pterodactylus.fcp.FcpConnection,
783 * net.pterodactylus.fcp.EndListPersistentRequests)
785 @SuppressWarnings("synthetic-access")
786 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
787 if (fcpConnection != HighLevelClient.this.fcpConnection) {
790 synchronized (syncObject) {
791 if (HighLevelClient.this.requestListCallback == null) {
792 logger.log(Level.WARNING, "got EndListPersistentRequests without running request!");
795 requestListCallback.setDone();
796 requestListCallback = null;
801 * @see net.pterodactylus.fcp.FcpListener#receivedFCPPluginReply(net.pterodactylus.fcp.FcpConnection,
802 * net.pterodactylus.fcp.FCPPluginReply)
804 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
809 * @see net.pterodactylus.fcp.FcpListener#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
810 * net.pterodactylus.fcp.GetFailed)
812 @SuppressWarnings("synthetic-access")
813 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
814 if (fcpConnection != HighLevelClient.this.fcpConnection) {
817 String identifier = getFailed.getIdentifier();
818 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
819 if (downloadCallback != null) {
820 downloadCallback.getIntermediaryResult().setFailed(true);
821 downloadCallback.setDone();
824 /* unknown identifier? */
825 logger.warning("unknown identifier for GetFailed: " + identifier);
829 * @see net.pterodactylus.fcp.FcpListener#receivedIdentifierCollision(net.pterodactylus.fcp.FcpConnection,
830 * net.pterodactylus.fcp.IdentifierCollision)
832 public void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
837 * @see net.pterodactylus.fcp.FcpListener#receivedMessage(net.pterodactylus.fcp.FcpConnection,
838 * net.pterodactylus.fcp.FcpMessage)
840 public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
845 * @see net.pterodactylus.fcp.FcpListener#receivedNodeData(net.pterodactylus.fcp.FcpConnection,
846 * net.pterodactylus.fcp.NodeData)
848 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
853 * @see net.pterodactylus.fcp.FcpListener#receivedNodeHello(net.pterodactylus.fcp.FcpConnection,
854 * net.pterodactylus.fcp.NodeHello)
856 @SuppressWarnings("synthetic-access")
857 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
858 if (fcpConnection != HighLevelClient.this.fcpConnection) {
861 synchronized (syncObject) {
862 connectCallback.getIntermediaryResult().setFailed(false);
863 connectCallback.setDone();
864 connectCallback = null;
866 fireClientConnected();
870 * @see net.pterodactylus.fcp.FcpListener#receivedPeer(net.pterodactylus.fcp.FcpConnection,
871 * net.pterodactylus.fcp.Peer)
873 @SuppressWarnings("synthetic-access")
874 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
875 if (fcpConnection != HighLevelClient.this.fcpConnection) {
878 String identifier = peer.getIdentifier();
879 if (identifier == null) {
882 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.get(identifier);
883 if (peerListCallback != null) {
884 peerListCallback.getIntermediaryResult().addPeer(peer);
887 HighLevelCallback<PeerResult> peerResult = peerCallbacks.remove(identifier);
888 if (peerResult != null) {
889 peerResult.getIntermediaryResult().setPeer(peer);
890 peerResult.setDone();
893 logger.warning("got Peer message with unknown identifier: " + identifier);
897 * @see net.pterodactylus.fcp.FcpListener#receivedPeerNote(net.pterodactylus.fcp.FcpConnection,
898 * net.pterodactylus.fcp.PeerNote)
900 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
905 * @see net.pterodactylus.fcp.FcpListener#receivedPeerRemoved(net.pterodactylus.fcp.FcpConnection,
906 * net.pterodactylus.fcp.PeerRemoved)
908 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
913 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentGet(net.pterodactylus.fcp.FcpConnection,
914 * net.pterodactylus.fcp.PersistentGet)
916 @SuppressWarnings("synthetic-access")
917 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
918 if (fcpConnection != HighLevelClient.this.fcpConnection) {
921 synchronized (syncObject) {
922 if (requestListCallback != null) {
923 RequestListResult requestListResult = requestListCallback.getIntermediaryResult();
924 requestListResult.addRequestResult(new GetRequestResult(persistentGet));
928 String identifier = persistentGet.getIdentifier();
929 if (downloadCallbacks.containsKey(identifier)) {
936 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
937 * net.pterodactylus.fcp.PersistentPut)
939 @SuppressWarnings("synthetic-access")
940 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
941 if (fcpConnection != HighLevelClient.this.fcpConnection) {
944 synchronized (syncObject) {
945 if (requestListCallback != null) {
946 RequestListResult requestListResult = requestListCallback.getIntermediaryResult();
947 requestListResult.addRequestResult(new PutRequestResult(persistentPut));
954 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPutDir(net.pterodactylus.fcp.FcpConnection,
955 * net.pterodactylus.fcp.PersistentPutDir)
957 @SuppressWarnings("synthetic-access")
958 public void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
959 if (fcpConnection != HighLevelClient.this.fcpConnection) {
962 synchronized (syncObject) {
963 if (requestListCallback != null) {
964 RequestListResult requestListResult = requestListCallback.getIntermediaryResult();
965 requestListResult.addRequestResult(new PutDirRequestResult(persistentPutDir));
972 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestModified(net.pterodactylus.fcp.FcpConnection,
973 * net.pterodactylus.fcp.PersistentRequestModified)
975 public void receivedPersistentRequestModified(FcpConnection fcpConnection, PersistentRequestModified persistentRequestModified) {
980 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestRemoved(net.pterodactylus.fcp.FcpConnection,
981 * net.pterodactylus.fcp.PersistentRequestRemoved)
983 public void receivedPersistentRequestRemoved(FcpConnection fcpConnection, PersistentRequestRemoved persistentRequestRemoved) {
988 * @see net.pterodactylus.fcp.FcpListener#receivedPluginInfo(net.pterodactylus.fcp.FcpConnection,
989 * net.pterodactylus.fcp.PluginInfo)
991 public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
996 * @see net.pterodactylus.fcp.FcpListener#receivedProtocolError(net.pterodactylus.fcp.FcpConnection,
997 * net.pterodactylus.fcp.ProtocolError)
999 @SuppressWarnings("synthetic-access")
1000 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
1001 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1004 String identifier = protocolError.getIdentifier();
1005 if (identifier == null) {
1008 cancelIdentifier(identifier);
1012 * @see net.pterodactylus.fcp.FcpListener#receivedPutFailed(net.pterodactylus.fcp.FcpConnection,
1013 * net.pterodactylus.fcp.PutFailed)
1015 public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
1020 * @see net.pterodactylus.fcp.FcpListener#receivedPutFetchable(net.pterodactylus.fcp.FcpConnection,
1021 * net.pterodactylus.fcp.PutFetchable)
1023 public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
1028 * @see net.pterodactylus.fcp.FcpListener#receivedPutSuccessful(net.pterodactylus.fcp.FcpConnection,
1029 * net.pterodactylus.fcp.PutSuccessful)
1031 public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
1036 * @see net.pterodactylus.fcp.FcpListener#receivedSSKKeypair(net.pterodactylus.fcp.FcpConnection,
1037 * net.pterodactylus.fcp.SSKKeypair)
1039 @SuppressWarnings("synthetic-access")
1040 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
1041 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1044 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(sskKeypair.getIdentifier());
1045 if (keyGenerationCallback == null) {
1048 KeyGenerationResult keyGenerationResult = keyGenerationCallback.getIntermediaryResult();
1049 keyGenerationResult.setInsertURI(sskKeypair.getInsertURI());
1050 keyGenerationResult.setRequestURI(sskKeypair.getRequestURI());
1051 keyGenerationCallback.setDone();
1055 * @see net.pterodactylus.fcp.FcpListener#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
1056 * net.pterodactylus.fcp.SimpleProgress)
1058 @SuppressWarnings("synthetic-access")
1059 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
1060 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1063 String identifier = simpleProgress.getIdentifier();
1064 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
1065 if (downloadCallback != null) {
1066 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
1067 downloadResult.setTotalBlocks(simpleProgress.getTotal());
1068 downloadResult.setRequiredBlocks(simpleProgress.getRequired());
1069 downloadResult.setSuccessfulBlocks(simpleProgress.getSucceeded());
1070 downloadResult.setFailedBlocks(simpleProgress.getFailed());
1071 downloadResult.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
1072 downloadResult.setTotalFinalized(simpleProgress.isFinalizedTotal());
1073 downloadCallback.progressUpdated();
1076 HighLevelProgress highLevelProgress = new HighLevelProgress(identifier, simpleProgress.getTotal(), simpleProgress.getRequired(), simpleProgress.getSucceeded(), simpleProgress.getFailed(), simpleProgress.getFatallyFailed(), simpleProgress.isFinalizedTotal());
1077 fireProgressReceived(identifier, highLevelProgress);
1081 * @see net.pterodactylus.fcp.FcpListener#receivedStartedCompression(net.pterodactylus.fcp.FcpConnection,
1082 * net.pterodactylus.fcp.StartedCompression)
1084 public void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
1089 * @see net.pterodactylus.fcp.FcpListener#receivedSubscribedUSKUpdate(net.pterodactylus.fcp.FcpConnection,
1090 * net.pterodactylus.fcp.SubscribedUSKUpdate)
1092 public void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
1097 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAComplete(net.pterodactylus.fcp.FcpConnection,
1098 * net.pterodactylus.fcp.TestDDAComplete)
1100 @SuppressWarnings("synthetic-access")
1101 public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
1102 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1105 String directory = testDDAComplete.getDirectory();
1106 if (directory == null) {
1109 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = directDiskAccessCallbacks.remove(directory);
1110 DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallback.getIntermediaryResult();
1111 cleanFiles(directDiskAccessResult);
1112 directDiskAccessResult.setReadAllowed(testDDAComplete.isReadDirectoryAllowed());
1113 directDiskAccessResult.setWriteAllowed(testDDAComplete.isWriteDirectoryAllowed());
1114 directDiskAccessCallback.setDone();
1118 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAReply(net.pterodactylus.fcp.FcpConnection,
1119 * net.pterodactylus.fcp.TestDDAReply)
1121 @SuppressWarnings("synthetic-access")
1122 public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
1123 if (fcpConnection != HighLevelClient.this.fcpConnection) {
1126 String directory = testDDAReply.getDirectory();
1127 if (directory == null) {
1130 DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallbacks.get(directory).getIntermediaryResult();
1131 String readFilename = testDDAReply.getReadFilename();
1132 String readContent = readContent(readFilename);
1133 String writeFilename = testDDAReply.getWriteFilename();
1134 String writeContent = testDDAReply.getContentToWrite();
1135 writeContent(directDiskAccessResult, writeFilename, writeContent);
1136 TestDDAResponse testDDAResponse = new TestDDAResponse(directory, readContent);
1138 fcpConnection.sendMessage(testDDAResponse);
1139 } catch (IOException e) {
1140 /* swallow. I’m verry unhappy about this. */
1145 * @see net.pterodactylus.fcp.FcpListener#receivedURIGenerated(net.pterodactylus.fcp.FcpConnection,
1146 * net.pterodactylus.fcp.URIGenerated)
1148 public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
1153 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownNodeIdentifier(net.pterodactylus.fcp.FcpConnection,
1154 * net.pterodactylus.fcp.UnknownNodeIdentifier)
1156 public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
1161 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownPeerNoteType(net.pterodactylus.fcp.FcpConnection,
1162 * net.pterodactylus.fcp.UnknownPeerNoteType)
1164 public void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
1169 * @see net.pterodactylus.fcp.FcpListener#receviedFinishedCompression(net.pterodactylus.fcp.FcpConnection,
1170 * net.pterodactylus.fcp.FinishedCompression)
1172 public void receviedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {