+ fcpConnection.close();
+ fireClientDisconnected();
+ }
+
+ /**
+ * Generates a new SSK keypair.
+ *
+ * @return A callback with the keypair
+ * @throws IOException
+ * if an I/O error occurs communicating with the node
+ */
+ public HighLevelCallback<KeyGenerationResult> generateKey() throws IOException {
+ String identifier = generateIdentifier("generateSSK");
+ GenerateSSK generateSSK = new GenerateSSK(identifier);
+ HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>(new KeyGenerationResult(identifier));
+ keyGenerationCallbacks.put(identifier, keyGenerationCallback);
+ fcpConnection.sendMessage(generateSSK);
+ return keyGenerationCallback;
+ }
+
+ /**
+ * Gets a list of all peers from the node.
+ *
+ * @return A callback with the peer list
+ * @throws IOException
+ * if an I/O error occurs with the node
+ */
+ public HighLevelCallback<PeerListResult> getPeers() throws IOException {
+ String identifier = generateIdentifier("listPeers");
+ ListPeers listPeers = new ListPeers(identifier, true, true);
+ HighLevelCallback<PeerListResult> peerListCallback = new HighLevelCallback<PeerListResult>(new PeerListResult(identifier));
+ peerListCallbacks.put(identifier, peerListCallback);
+ fcpConnection.sendMessage(listPeers);
+ return peerListCallback;
+ }
+
+ /**
+ * Adds the peer whose noderef is stored in the given file.
+ *
+ * @param nodeRefFile
+ * The name of the file the peer’s noderef is stored in
+ * @return A peer callback
+ * @throws IOException
+ * if an I/O error occurs communicating with the node
+ */
+ public HighLevelCallback<PeerResult> addPeer(String nodeRefFile) throws IOException {
+ String identifier = generateIdentifier("addPeer");
+ AddPeer addPeer = new AddPeer(nodeRefFile);
+ HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
+ peerCallbacks.put(identifier, peerCallback);
+ fcpConnection.sendMessage(addPeer);
+ return peerCallback;
+ }
+
+ /**
+ * Adds the peer whose noderef is stored in the given file.
+ *
+ * @param nodeRefURL
+ * The URL where the peer’s noderef is stored
+ * @return A peer callback
+ * @throws IOException
+ * if an I/O error occurs communicating with the node
+ */
+ public HighLevelCallback<PeerResult> addPeer(URL nodeRefURL) throws IOException {
+ String identifier = generateIdentifier("addPeer");
+ AddPeer addPeer = new AddPeer(nodeRefURL);
+ HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
+ peerCallbacks.put(identifier, peerCallback);
+ fcpConnection.sendMessage(addPeer);
+ return peerCallback;
+ }
+
+ /**
+ * Adds the peer whose noderef is stored in the given file.
+ *
+ * @param nodeRef
+ * The peer’s noderef
+ * @return A peer callback
+ * @throws IOException
+ * if an I/O error occurs communicating with the node
+ */
+ public HighLevelCallback<PeerResult> addPeer(NodeRef nodeRef) throws IOException {
+ String identifier = generateIdentifier("addPeer");
+ AddPeer addPeer = new AddPeer(nodeRef);
+ HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult(identifier));
+ peerCallbacks.put(identifier, peerCallback);
+ fcpConnection.sendMessage(addPeer);
+ return peerCallback;
+ }
+
+ /**
+ * Checks whether direct disk access for the given directory is possible.
+ * You have to perform this check before you can upload or download anything
+ * from or the disk directly!
+ *
+ * @param directory
+ * The directory to check
+ * @param wantRead
+ * Whether you want to read the given directory
+ * @param wantWrite
+ * Whether you want to write to the given directory
+ * @return A direct disk access callback
+ * @throws IOException
+ */
+ public HighLevelCallback<DirectDiskAccessResult> checkDirectDiskAccess(String directory, boolean wantRead, boolean wantWrite) throws IOException {
+ TestDDARequest testDDARequest = new TestDDARequest(directory, wantRead, wantWrite);
+ HighLevelCallback<DirectDiskAccessResult> directDiskAccessCallback = new HighLevelCallback<DirectDiskAccessResult>(new DirectDiskAccessResult(directory));
+ directDiskAccessCallbacks.put(directory, directDiskAccessCallback);
+ fcpConnection.sendMessage(testDDARequest);
+ return directDiskAccessCallback;
+ }
+
+ /**
+ * Starts a download. Files can either be download to disk or streamed from
+ * the node. When downloading to disk you have to perform a direct disk
+ * access check for the directory you want to put the downloaded file in!
+ *
+ * @see #checkDirectDiskAccess(String, boolean, boolean)
+ * @param uri
+ * The URI to get
+ * @param filename
+ * The filename to save the data to, or <code>null</code> to
+ * retrieve the data as InputStream from the
+ * {@link DownloadResult}
+ * @param global
+ * Whether to put the download on the global queue
+ * @return A download result
+ * @throws IOException
+ * if an I/O error occurs communicating with the node
+ */
+ public HighLevelProgressCallback<DownloadResult> download(String uri, String filename, boolean global) throws IOException {
+ String identifier = generateIdentifier("download");
+ ClientGet clientGet = new ClientGet(uri, identifier, (filename == null) ? ReturnType.direct : ReturnType.disk);
+ clientGet.setGlobal(global);
+ HighLevelProgressCallback<DownloadResult> downloadCallback = new HighLevelProgressCallback<DownloadResult>(new DownloadResult(identifier));
+ downloadCallbacks.put(identifier, downloadCallback);
+ fcpConnection.sendMessage(clientGet);
+ return downloadCallback;
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Generates an identifier for the given function.
+ *
+ * @param function
+ * The name of the function
+ * @return An identifier
+ */
+ private String generateIdentifier(String function) {
+ return "jFCPlib-" + function + "-" + System.currentTimeMillis();