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.Logger;
38 import net.pterodactylus.fcp.AddPeer;
39 import net.pterodactylus.fcp.AllData;
40 import net.pterodactylus.fcp.ClientGet;
41 import net.pterodactylus.fcp.ClientHello;
42 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
43 import net.pterodactylus.fcp.ConfigData;
44 import net.pterodactylus.fcp.DataFound;
45 import net.pterodactylus.fcp.EndListPeerNotes;
46 import net.pterodactylus.fcp.EndListPeers;
47 import net.pterodactylus.fcp.EndListPersistentRequests;
48 import net.pterodactylus.fcp.FCPPluginReply;
49 import net.pterodactylus.fcp.FcpConnection;
50 import net.pterodactylus.fcp.FcpListener;
51 import net.pterodactylus.fcp.FcpMessage;
52 import net.pterodactylus.fcp.FcpUtils;
53 import net.pterodactylus.fcp.FinishedCompression;
54 import net.pterodactylus.fcp.GenerateSSK;
55 import net.pterodactylus.fcp.GetFailed;
56 import net.pterodactylus.fcp.IdentifierCollision;
57 import net.pterodactylus.fcp.ListPeers;
58 import net.pterodactylus.fcp.NodeData;
59 import net.pterodactylus.fcp.NodeHello;
60 import net.pterodactylus.fcp.NodeRef;
61 import net.pterodactylus.fcp.Peer;
62 import net.pterodactylus.fcp.PeerNote;
63 import net.pterodactylus.fcp.PeerRemoved;
64 import net.pterodactylus.fcp.PersistentGet;
65 import net.pterodactylus.fcp.PersistentPut;
66 import net.pterodactylus.fcp.PersistentPutDir;
67 import net.pterodactylus.fcp.PersistentRequestModified;
68 import net.pterodactylus.fcp.PersistentRequestRemoved;
69 import net.pterodactylus.fcp.PluginInfo;
70 import net.pterodactylus.fcp.ProtocolError;
71 import net.pterodactylus.fcp.PutFailed;
72 import net.pterodactylus.fcp.PutFetchable;
73 import net.pterodactylus.fcp.PutSuccessful;
74 import net.pterodactylus.fcp.ReturnType;
75 import net.pterodactylus.fcp.SSKKeypair;
76 import net.pterodactylus.fcp.SimpleProgress;
77 import net.pterodactylus.fcp.StartedCompression;
78 import net.pterodactylus.fcp.SubscribedUSKUpdate;
79 import net.pterodactylus.fcp.TestDDAComplete;
80 import net.pterodactylus.fcp.TestDDAReply;
81 import net.pterodactylus.fcp.TestDDARequest;
82 import net.pterodactylus.fcp.TestDDAResponse;
83 import net.pterodactylus.fcp.URIGenerated;
84 import net.pterodactylus.fcp.UnknownNodeIdentifier;
85 import net.pterodactylus.fcp.UnknownPeerNoteType;
88 * A high-level client that allows simple yet full-featured access to a Freenet
91 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
94 public class HighLevelClient {
97 private static final Logger logger = Logger.getLogger(HighLevelClient.class.getName());
99 /** Object for internal synchronization. */
100 private final Object syncObject = new Object();
102 /** The name of the client. */
103 private final String clientName;
105 /** The address of the node. */
106 private InetAddress address;
108 /** The port number of the node. */
111 /** The FCP connection to the node. */
112 private FcpConnection fcpConnection;
114 /** Listeners for high-level client events. */
115 private List<HighLevelClientListener> highLevelClientListeners = Collections.synchronizedList(new ArrayList<HighLevelClientListener>());
117 /** The listener for the connection. */
118 private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
120 /** The callback for {@link #connect()}. */
121 private HighLevelCallback<ConnectResult> connectCallback;
123 /** Mapping from request identifiers to callbacks. */
124 private Map<String, HighLevelCallback<KeyGenerationResult>> keyGenerationCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<KeyGenerationResult>>());
126 /** Mapping from request identifier to peer list callbacks. */
127 private Map<String, HighLevelCallback<PeerListResult>> peerListCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerListResult>>());
129 /** Mapping from request identifier to peer callbacks. */
130 private Map<String, HighLevelCallback<PeerResult>> peerCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerResult>>());
132 /** Mapping from directories to DDA callbacks. */
133 private Map<String, HighLevelCallback<DirectDiskAccessResult>> directDiskAccessCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<DirectDiskAccessResult>>());
135 /** Mapping from request identifiers to download callbacks. */
136 private Map<String, HighLevelProgressCallback<DownloadResult>> downloadCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelProgressCallback<DownloadResult>>());
139 * Creates a new high-level client that connects to a node on
140 * <code>localhost</code>.
143 * The name of the client
144 * @throws UnknownHostException
145 * if the hostname of the node can not be resolved.
147 public HighLevelClient(String clientName) throws UnknownHostException {
148 this(clientName, "localhost");
152 * Creates a new high-level client that connects to a node on the given
156 * The name of the client
158 * The hostname of the node
159 * @throws UnknownHostException
160 * if the hostname of the node can not be resolved.
162 public HighLevelClient(String clientName, String host) throws UnknownHostException {
163 this(clientName, host, FcpConnection.DEFAULT_PORT);
167 * Creates a new high-level client that connects to a node on the given
171 * The name of the client
173 * The hostname of the node
175 * The port number of the node
176 * @throws UnknownHostException
177 * if the hostname of the node can not be resolved.
179 public HighLevelClient(String clientName, String host, int port) throws UnknownHostException {
180 this(clientName, InetAddress.getByName(host), port);
184 * Creates a new high-level client that connects to a node at the given
188 * The name of the client
190 * The address of the node
192 * The port number of the node
194 public HighLevelClient(String clientName, InetAddress address, int port) {
195 this.clientName = clientName;
196 this.address = address;
205 * Adds the given high-level client listener to list of listeners.
207 * @param highLevelClientListener
208 * The listener to add
210 public void addHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
211 highLevelClientListeners.add(highLevelClientListener);
215 * Removes the given high-level client listener from the list of listeners.
217 * @param highLevelClientListener
218 * The listener to remove
220 public void removeHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
221 highLevelClientListeners.remove(highLevelClientListener);
225 * Notifies all listeners that a client has connected.
227 private void fireClientConnected() {
228 for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
229 highLevelClientListener.clientConnected(this);
234 * Notifies all listeners that a client has disconnected.
236 private void fireClientDisconnected() {
237 for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
238 highLevelClientListener.clientDisconnected(this);
247 * Returns the FCP connection that backs this high-level client. This method
248 * should be used with care as fiddling around with the FCP connection can
249 * easily break the high-level client if you don’t know what you’re doing!
251 * @return The FCP connection of this client
253 public FcpConnection getFcpConnection() {
254 return fcpConnection;
262 * Connects the client.
264 * @return A callback with a connection result
265 * @throws IOException
266 * if an I/O error occurs communicating with the node
268 public HighLevelCallback<ConnectResult> connect() throws IOException {
269 fcpConnection = new FcpConnection(address, port);
270 fcpConnection.addFcpListener(highLevelClientFcpListener);
271 ClientHello clientHello = new ClientHello(clientName);
272 connectCallback = new HighLevelCallback<ConnectResult>(new ConnectResult());
273 fcpConnection.sendMessage(clientHello);
274 return connectCallback;
278 * Disconnects the client from the node.
280 public void disconnect() {
281 fcpConnection.close();
282 fireClientDisconnected();
286 * Generates a new SSK keypair.
288 * @return A callback with the keypair
289 * @throws IOException
290 * if an I/O error occurs communicating with the node
292 public HighLevelCallback<KeyGenerationResult> generateKey() throws IOException {
293 String identifier = generateIdentifier("generateSSK");
294 GenerateSSK generateSSK = new GenerateSSK(identifier);
295 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>(new KeyGenerationResult(identifier));
296 keyGenerationCallbacks.put(identifier, keyGenerationCallback);
297 fcpConnection.sendMessage(generateSSK);
298 return keyGenerationCallback;
302 * Gets a list of all peers from the node.
304 * @return A callback with the peer list
305 * @throws IOException
306 * if an I/O error occurs with the node
308 public HighLevelCallback<PeerListResult> getPeers() throws IOException {
309 String identifier = generateIdentifier("listPeers");
310 ListPeers listPeers = new ListPeers(identifier, true, true);
311 HighLevelCallback<PeerListResult> peerListCallback = new HighLevelCallback<PeerListResult>(new PeerListResult(identifier));
312 peerListCallbacks.put(identifier, peerListCallback);
313 fcpConnection.sendMessage(listPeers);
314 return peerListCallback;
318 * Adds the peer whose noderef is stored in the given file.
321 * The name of the file the peer’s noderef is stored in
322 * @return A peer callback
323 * @throws IOException
324 * if an I/O error occurs communicating with the node
326 public HighLevelCallback<PeerResult> addPeer(String nodeRefFile) throws IOException {
327 String identifier = generateIdentifier("addPeer");
328 AddPeer addPeer = new AddPeer(nodeRefFile);
329 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
330 peerCallbacks.put(identifier, peerCallback);
331 fcpConnection.sendMessage(addPeer);
336 * Adds the peer whose noderef is stored in the given file.
339 * The URL where the peer’s noderef is stored
340 * @return A peer callback
341 * @throws IOException
342 * if an I/O error occurs communicating with the node
344 public HighLevelCallback<PeerResult> addPeer(URL nodeRefURL) throws IOException {
345 String identifier = generateIdentifier("addPeer");
346 AddPeer addPeer = new AddPeer(nodeRefURL);
347 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
348 peerCallbacks.put(identifier, peerCallback);
349 fcpConnection.sendMessage(addPeer);
354 * Adds the peer whose noderef is stored in the given file.
358 * @return A peer callback
359 * @throws IOException
360 * if an I/O error occurs communicating with the node
362 public HighLevelCallback<PeerResult> addPeer(NodeRef nodeRef) throws IOException {
363 String identifier = generateIdentifier("addPeer");
364 AddPeer addPeer = new AddPeer(nodeRef);
365 HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
366 peerCallbacks.put(identifier, peerCallback);
367 fcpConnection.sendMessage(addPeer);
372 * Checks whether direct disk access for the given directory is possible.
373 * You have to perform this check before you can upload or download anything
374 * from or the disk directly!
377 * The directory to check
379 * Whether you want to read the given directory
381 * Whether you want to write to the given directory
382 * @return A direct disk access callback
383 * @throws IOException
385 public HighLevelCallback<DirectDiskAccessResult> checkDirectDiskAccess(String directory, boolean wantRead, boolean wantWrite) throws IOException {
386 TestDDARequest testDDARequest = new TestDDARequest(directory, wantRead, wantWrite);
387 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = new HighLevelCallback<DirectDiskAccessResult>(new DirectDiskAccessResult(directory));
388 directDiskAccessCallbacks.put(directory, directDiskAccessCallback);
389 fcpConnection.sendMessage(testDDARequest);
390 return directDiskAccessCallback;
394 * Starts a download. Files can either be download to disk or streamed from
395 * the node. When downloading to disk you have to perform a direct disk
396 * access check for the directory you want to put the downloaded file in!
398 * @see #checkDirectDiskAccess(String, boolean, boolean)
402 * The filename to save the data to, or <code>null</code> to
403 * retrieve the data as InputStream from the
404 * {@link DownloadResult}
406 * Whether to put the download on the global queue
407 * @return A download result
408 * @throws IOException
409 * if an I/O error occurs communicating with the node
411 public HighLevelProgressCallback<DownloadResult> download(String uri, String filename, boolean global) throws IOException {
412 String identifier = generateIdentifier("download");
413 ClientGet clientGet = new ClientGet(uri, identifier, (filename == null) ? ReturnType.direct : ReturnType.disk);
414 clientGet.setGlobal(global);
415 HighLevelProgressCallback<DownloadResult> downloadCallback = new HighLevelProgressCallback<DownloadResult>(new DownloadResult(identifier));
416 downloadCallbacks.put(identifier, downloadCallback);
417 fcpConnection.sendMessage(clientGet);
418 return downloadCallback;
426 * Generates an identifier for the given function.
429 * The name of the function
430 * @return An identifier
432 private String generateIdentifier(String function) {
433 return "jFCPlib-" + function + "-" + System.currentTimeMillis();
437 * FCP listener for {@link HighLevelClient}.
439 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
442 private class HighLevelClientFcpListener implements FcpListener {
444 /** Mapping from directory to written file (for cleanup). */
445 private final Map<DirectDiskAccessResult, String> writtenFiles = new HashMap<DirectDiskAccessResult, String>();
448 * Creates a new FCP listener for {@link HighLevelClient}.
450 HighLevelClientFcpListener() {
459 * Searches all callback collections for a callback with the given
460 * identifier and cancels it.
463 * The identifier to search for, or <code>null</code> to
464 * cancel all pending requests
466 @SuppressWarnings("synthetic-access")
467 private void cancelIdentifier(String identifier) {
468 synchronized (syncObject) {
469 if (connectCallback != null) {
470 connectCallback.getIntermediaryResult().setFailed(true);
471 connectCallback.setDone();
472 connectCallback = null;
475 if (identifier == null) {
476 /* key generation callbacks */
477 for (Entry<String, HighLevelCallback<KeyGenerationResult>> keyGenerationEntry: keyGenerationCallbacks.entrySet()) {
478 keyGenerationEntry.getValue().getIntermediaryResult().setFailed(true);
479 keyGenerationEntry.getValue().setDone();
481 keyGenerationCallbacks.clear();
482 /* peer list callbacks. */
483 for (Entry<String, HighLevelCallback<PeerListResult>> peerListEntry: peerListCallbacks.entrySet()) {
484 peerListEntry.getValue().getIntermediaryResult().setFailed(true);
485 peerListEntry.getValue().setDone();
487 peerListCallbacks.clear();
488 /* peer callbacks. */
489 for (Entry<String, HighLevelCallback<PeerResult>> peerEntry: peerCallbacks.entrySet()) {
490 peerEntry.getValue().getIntermediaryResult().setFailed(true);
491 peerEntry.getValue().setDone();
493 peerCallbacks.clear();
494 /* direct disk access callbacks. */
495 for (Entry<String, HighLevelCallback<DirectDiskAccessResult>> directDiskAccessEntry: directDiskAccessCallbacks.entrySet()) {
496 directDiskAccessEntry.getValue().getIntermediaryResult().setFailed(true);
497 directDiskAccessEntry.getValue().setDone();
499 directDiskAccessCallbacks.clear();
500 /* download callbacks. */
501 for (Entry<String, HighLevelProgressCallback<DownloadResult>> downloadEntry: downloadCallbacks.entrySet()) {
502 downloadEntry.getValue().getIntermediaryResult().setFailed(true);
503 downloadEntry.getValue().setDone();
505 downloadCallbacks.clear();
507 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(identifier);
508 if (keyGenerationCallback != null) {
509 keyGenerationCallback.getIntermediaryResult().setFailed(true);
510 keyGenerationCallback.setDone();
513 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
514 if (peerListCallback != null) {
515 peerListCallback.getIntermediaryResult().setFailed(true);
516 peerListCallback.setDone();
519 HighLevelCallback<PeerResult> peerCallback = peerCallbacks.remove(identifier);
520 if (peerCallback != null) {
521 peerCallback.getIntermediaryResult().setFailed(true);
522 peerCallback.setDone();
525 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = directDiskAccessCallbacks.remove(identifier);
526 if (directDiskAccessCallback != null) {
527 directDiskAccessCallback.getIntermediaryResult().setFailed(true);
528 directDiskAccessCallback.setDone();
531 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
532 if (downloadCallback != null) {
533 downloadCallback.getIntermediaryResult().setFailed(true);
534 downloadCallback.setDone();
541 * Reads the given file and returns the first line of the file.
543 * @param readFilename
544 * The name of the file to read
545 * @return The content of the file
547 private String readContent(String readFilename) {
548 FileReader fileReader = null;
549 BufferedReader bufferedFileReader = null;
551 fileReader = new FileReader(readFilename);
552 bufferedFileReader = new BufferedReader(fileReader);
553 String content = bufferedFileReader.readLine();
555 } catch (IOException ioe1) {
558 FcpUtils.close(bufferedFileReader);
559 FcpUtils.close(fileReader);
565 * Writes the given content to the given file.
567 * @param directDiskAccessResult
569 * @param writeFilename
570 * The name of the file to write to
571 * @param writeContent
572 * The content to write to the file
574 private void writeContent(DirectDiskAccessResult directDiskAccessResult, String writeFilename, String writeContent) {
575 if ((writeFilename == null) || (writeContent == null)) {
578 writtenFiles.put(directDiskAccessResult, writeFilename);
579 FileWriter fileWriter = null;
581 fileWriter = new FileWriter(writeFilename);
582 fileWriter.write(writeContent);
583 } catch (IOException ioe1) {
586 FcpUtils.close(fileWriter);
591 * Cleans up any files that written for the given result.
593 * @param directDiskAccessResult
594 * The direct disk access result
596 @SuppressWarnings("synthetic-access")
597 private void cleanFiles(DirectDiskAccessResult directDiskAccessResult) {
598 String writeFilename = writtenFiles.remove(directDiskAccessResult);
599 if (writeFilename != null) {
600 if (!new File(writeFilename).delete()) {
601 logger.warning("could not delete " + writeFilename);
607 // INTERFACE FcpListener
611 * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection)
613 @SuppressWarnings("synthetic-access")
614 public void connectionClosed(FcpConnection fcpConnection) {
615 if (fcpConnection != HighLevelClient.this.fcpConnection) {
618 cancelIdentifier(null);
623 * @see net.pterodactylus.fcp.FcpListener#receivedAllData(net.pterodactylus.fcp.FcpConnection,
624 * net.pterodactylus.fcp.AllData)
626 public void receivedAllData(FcpConnection fcpConnection, AllData allData) {
631 * @see net.pterodactylus.fcp.FcpListener#receivedCloseConnectionDuplicateClientName(net.pterodactylus.fcp.FcpConnection,
632 * net.pterodactylus.fcp.CloseConnectionDuplicateClientName)
634 public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
639 * @see net.pterodactylus.fcp.FcpListener#receivedConfigData(net.pterodactylus.fcp.FcpConnection,
640 * net.pterodactylus.fcp.ConfigData)
642 public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) {
647 * @see net.pterodactylus.fcp.FcpListener#receivedDataFound(net.pterodactylus.fcp.FcpConnection,
648 * net.pterodactylus.fcp.DataFound)
650 public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) {
655 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeerNotes(net.pterodactylus.fcp.FcpConnection,
656 * net.pterodactylus.fcp.EndListPeerNotes)
658 public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) {
663 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeers(net.pterodactylus.fcp.FcpConnection,
664 * net.pterodactylus.fcp.EndListPeers)
666 @SuppressWarnings("synthetic-access")
667 public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) {
668 if (fcpConnection != HighLevelClient.this.fcpConnection) {
671 String identifier = endListPeers.getIdentifier();
672 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
673 if (peerListCallback == null) {
676 peerListCallback.setDone();
680 * @see net.pterodactylus.fcp.FcpListener#receivedEndListPersistentRequests(net.pterodactylus.fcp.FcpConnection,
681 * net.pterodactylus.fcp.EndListPersistentRequests)
683 public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) {
688 * @see net.pterodactylus.fcp.FcpListener#receivedFCPPluginReply(net.pterodactylus.fcp.FcpConnection,
689 * net.pterodactylus.fcp.FCPPluginReply)
691 public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) {
696 * @see net.pterodactylus.fcp.FcpListener#receivedGetFailed(net.pterodactylus.fcp.FcpConnection,
697 * net.pterodactylus.fcp.GetFailed)
699 @SuppressWarnings("synthetic-access")
700 public void receivedGetFailed(FcpConnection fcpConnection, GetFailed getFailed) {
701 if (fcpConnection != HighLevelClient.this.fcpConnection) {
704 String identifier = getFailed.getIdentifier();
705 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.remove(identifier);
706 if (downloadCallback != null) {
707 downloadCallback.getIntermediaryResult().setFailed(true);
708 downloadCallback.setDone();
711 /* unknown identifier? */
712 logger.warning("unknown identifier for GetFailed: " + identifier);
716 * @see net.pterodactylus.fcp.FcpListener#receivedIdentifierCollision(net.pterodactylus.fcp.FcpConnection,
717 * net.pterodactylus.fcp.IdentifierCollision)
719 public void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) {
724 * @see net.pterodactylus.fcp.FcpListener#receivedMessage(net.pterodactylus.fcp.FcpConnection,
725 * net.pterodactylus.fcp.FcpMessage)
727 public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) {
732 * @see net.pterodactylus.fcp.FcpListener#receivedNodeData(net.pterodactylus.fcp.FcpConnection,
733 * net.pterodactylus.fcp.NodeData)
735 public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) {
740 * @see net.pterodactylus.fcp.FcpListener#receivedNodeHello(net.pterodactylus.fcp.FcpConnection,
741 * net.pterodactylus.fcp.NodeHello)
743 @SuppressWarnings("synthetic-access")
744 public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) {
745 if (fcpConnection != HighLevelClient.this.fcpConnection) {
748 synchronized (syncObject) {
749 connectCallback.getIntermediaryResult().setFailed(false);
750 connectCallback.setDone();
751 connectCallback = null;
753 fireClientConnected();
757 * @see net.pterodactylus.fcp.FcpListener#receivedPeer(net.pterodactylus.fcp.FcpConnection,
758 * net.pterodactylus.fcp.Peer)
760 @SuppressWarnings("synthetic-access")
761 public void receivedPeer(FcpConnection fcpConnection, Peer peer) {
762 if (fcpConnection != HighLevelClient.this.fcpConnection) {
765 String identifier = peer.getIdentifier();
766 if (identifier == null) {
769 HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.get(identifier);
770 if (peerListCallback != null) {
771 peerListCallback.getIntermediaryResult().addPeer(peer);
774 HighLevelCallback<PeerResult> peerResult = peerCallbacks.remove(identifier);
775 if (peerResult != null) {
776 peerResult.getIntermediaryResult().setPeer(peer);
777 peerResult.setDone();
780 logger.warning("got Peer message with unknown identifier: " + identifier);
784 * @see net.pterodactylus.fcp.FcpListener#receivedPeerNote(net.pterodactylus.fcp.FcpConnection,
785 * net.pterodactylus.fcp.PeerNote)
787 public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) {
792 * @see net.pterodactylus.fcp.FcpListener#receivedPeerRemoved(net.pterodactylus.fcp.FcpConnection,
793 * net.pterodactylus.fcp.PeerRemoved)
795 public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
800 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentGet(net.pterodactylus.fcp.FcpConnection,
801 * net.pterodactylus.fcp.PersistentGet)
803 @SuppressWarnings("synthetic-access")
804 public void receivedPersistentGet(FcpConnection fcpConnection, PersistentGet persistentGet) {
805 if (fcpConnection != HighLevelClient.this.fcpConnection) {
808 String identifier = persistentGet.getIdentifier();
809 if (downloadCallbacks.containsKey(identifier)) {
810 /* ignore, because a download does not care about this. */
816 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection,
817 * net.pterodactylus.fcp.PersistentPut)
819 public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) {
824 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPutDir(net.pterodactylus.fcp.FcpConnection,
825 * net.pterodactylus.fcp.PersistentPutDir)
827 public void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) {
832 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestModified(net.pterodactylus.fcp.FcpConnection,
833 * net.pterodactylus.fcp.PersistentRequestModified)
835 public void receivedPersistentRequestModified(FcpConnection fcpConnection, PersistentRequestModified persistentRequestModified) {
840 * @see net.pterodactylus.fcp.FcpListener#receivedPersistentRequestRemoved(net.pterodactylus.fcp.FcpConnection,
841 * net.pterodactylus.fcp.PersistentRequestRemoved)
843 public void receivedPersistentRequestRemoved(FcpConnection fcpConnection, PersistentRequestRemoved persistentRequestRemoved) {
848 * @see net.pterodactylus.fcp.FcpListener#receivedPluginInfo(net.pterodactylus.fcp.FcpConnection,
849 * net.pterodactylus.fcp.PluginInfo)
851 public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) {
856 * @see net.pterodactylus.fcp.FcpListener#receivedProtocolError(net.pterodactylus.fcp.FcpConnection,
857 * net.pterodactylus.fcp.ProtocolError)
859 @SuppressWarnings("synthetic-access")
860 public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) {
861 if (fcpConnection != HighLevelClient.this.fcpConnection) {
864 String identifier = protocolError.getIdentifier();
865 if (identifier == null) {
868 cancelIdentifier(identifier);
872 * @see net.pterodactylus.fcp.FcpListener#receivedPutFailed(net.pterodactylus.fcp.FcpConnection,
873 * net.pterodactylus.fcp.PutFailed)
875 public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) {
880 * @see net.pterodactylus.fcp.FcpListener#receivedPutFetchable(net.pterodactylus.fcp.FcpConnection,
881 * net.pterodactylus.fcp.PutFetchable)
883 public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) {
888 * @see net.pterodactylus.fcp.FcpListener#receivedPutSuccessful(net.pterodactylus.fcp.FcpConnection,
889 * net.pterodactylus.fcp.PutSuccessful)
891 public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) {
896 * @see net.pterodactylus.fcp.FcpListener#receivedSSKKeypair(net.pterodactylus.fcp.FcpConnection,
897 * net.pterodactylus.fcp.SSKKeypair)
899 @SuppressWarnings("synthetic-access")
900 public void receivedSSKKeypair(FcpConnection fcpConnection, SSKKeypair sskKeypair) {
901 if (fcpConnection != HighLevelClient.this.fcpConnection) {
904 HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(sskKeypair.getIdentifier());
905 if (keyGenerationCallback == null) {
908 KeyGenerationResult keyGenerationResult = keyGenerationCallback.getIntermediaryResult();
909 keyGenerationResult.setInsertURI(sskKeypair.getInsertURI());
910 keyGenerationResult.setRequestURI(sskKeypair.getRequestURI());
911 keyGenerationCallback.setDone();
915 * @see net.pterodactylus.fcp.FcpListener#receivedSimpleProgress(net.pterodactylus.fcp.FcpConnection,
916 * net.pterodactylus.fcp.SimpleProgress)
918 @SuppressWarnings("synthetic-access")
919 public void receivedSimpleProgress(FcpConnection fcpConnection, SimpleProgress simpleProgress) {
920 if (fcpConnection != HighLevelClient.this.fcpConnection) {
923 String identifier = simpleProgress.getIdentifier();
924 HighLevelProgressCallback<DownloadResult> downloadCallback = downloadCallbacks.get(identifier);
925 if (downloadCallback != null) {
926 DownloadResult downloadResult = downloadCallback.getIntermediaryResult();
927 downloadResult.setTotalBlocks(simpleProgress.getTotal());
928 downloadResult.setRequiredBlocks(simpleProgress.getRequired());
929 downloadResult.setSuccessfulBlocks(simpleProgress.getSucceeded());
930 downloadResult.setFailedBlocks(simpleProgress.getFailed());
931 downloadResult.setFatallyFailedBlocks(simpleProgress.getFatallyFailed());
932 downloadResult.setTotalFinalized(simpleProgress.isFinalizedTotal());
933 downloadCallback.progressUpdated();
936 /* unknown identifier? */
937 logger.warning("unknown identifier for SimpleProgress: " + identifier);
941 * @see net.pterodactylus.fcp.FcpListener#receivedStartedCompression(net.pterodactylus.fcp.FcpConnection,
942 * net.pterodactylus.fcp.StartedCompression)
944 public void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) {
949 * @see net.pterodactylus.fcp.FcpListener#receivedSubscribedUSKUpdate(net.pterodactylus.fcp.FcpConnection,
950 * net.pterodactylus.fcp.SubscribedUSKUpdate)
952 public void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) {
957 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAComplete(net.pterodactylus.fcp.FcpConnection,
958 * net.pterodactylus.fcp.TestDDAComplete)
960 @SuppressWarnings("synthetic-access")
961 public void receivedTestDDAComplete(FcpConnection fcpConnection, TestDDAComplete testDDAComplete) {
962 if (fcpConnection != HighLevelClient.this.fcpConnection) {
965 String directory = testDDAComplete.getDirectory();
966 if (directory == null) {
969 HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = directDiskAccessCallbacks.remove(directory);
970 DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallback.getIntermediaryResult();
971 cleanFiles(directDiskAccessResult);
972 directDiskAccessResult.setReadAllowed(testDDAComplete.isReadDirectoryAllowed());
973 directDiskAccessResult.setWriteAllowed(testDDAComplete.isWriteDirectoryAllowed());
974 directDiskAccessCallback.setDone();
978 * @see net.pterodactylus.fcp.FcpListener#receivedTestDDAReply(net.pterodactylus.fcp.FcpConnection,
979 * net.pterodactylus.fcp.TestDDAReply)
981 @SuppressWarnings("synthetic-access")
982 public void receivedTestDDAReply(FcpConnection fcpConnection, TestDDAReply testDDAReply) {
983 if (fcpConnection != HighLevelClient.this.fcpConnection) {
986 String directory = testDDAReply.getDirectory();
987 if (directory == null) {
990 DirectDiskAccessResult directDiskAccessResult = directDiskAccessCallbacks.get(directory).getIntermediaryResult();
991 String readFilename = testDDAReply.getReadFilename();
992 String readContent = readContent(readFilename);
993 String writeFilename = testDDAReply.getWriteFilename();
994 String writeContent = testDDAReply.getContentToWrite();
995 writeContent(directDiskAccessResult, writeFilename, writeContent);
996 TestDDAResponse testDDAResponse = new TestDDAResponse(directory, readContent);
998 fcpConnection.sendMessage(testDDAResponse);
999 } catch (IOException e) {
1000 /* swallow. I’m verry unhappy about this. */
1005 * @see net.pterodactylus.fcp.FcpListener#receivedURIGenerated(net.pterodactylus.fcp.FcpConnection,
1006 * net.pterodactylus.fcp.URIGenerated)
1008 public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) {
1013 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownNodeIdentifier(net.pterodactylus.fcp.FcpConnection,
1014 * net.pterodactylus.fcp.UnknownNodeIdentifier)
1016 public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
1021 * @see net.pterodactylus.fcp.FcpListener#receivedUnknownPeerNoteType(net.pterodactylus.fcp.FcpConnection,
1022 * net.pterodactylus.fcp.UnknownPeerNoteType)
1024 public void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) {
1029 * @see net.pterodactylus.fcp.FcpListener#receviedFinishedCompression(net.pterodactylus.fcp.FcpConnection,
1030 * net.pterodactylus.fcp.FinishedCompression)
1032 public void receviedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) {