import net.pterodactylus.fcp.NodeRef;
import net.pterodactylus.fcp.Peer;
import net.pterodactylus.fcp.PeerNote;
+import net.pterodactylus.fcp.PeerNoteType;
import net.pterodactylus.fcp.PeerRemoved;
import net.pterodactylus.fcp.PersistentGet;
import net.pterodactylus.fcp.PersistentPut;
}
/**
+ * Replaces the private darknet comment peer note for the given peer.
+ *
+ * @param peer
+ * The peer
+ * @param noteText
+ * The new base64-encoded note text
+ * @throws IOException
+ * if an I/O error occurs
+ * @throws FcpException
+ * if an FCP error occurs
+ */
+ public void modifyPeerNote(final Peer peer, final String noteText) throws IOException, FcpException {
+ modifyPeerNote(peer, noteText, 1);
+ }
+
+ /**
* Replaces the peer note for the given peer.
*
* @param peer
* if an I/O error occurs
* @throws FcpException
* if an FCP error occurs
+ * @deprecated Use {@link #modifyPeerNote(Peer, String)} instead
*/
+ @Deprecated
public void modifyPeerNote(final Peer peer, final String noteText, final int noteType) throws IOException, FcpException {
new ExtendedFcpAdapter() {
@Override
@SuppressWarnings("synthetic-access")
public void run() throws IOException {
- sendMessage(new ModifyPeerNote(peer.getIdentity(), noteText, noteType));
+ ModifyPeerNote modifyPeerNote = new ModifyPeerNote(createIdentifier("modify-peer-note"), peer.getIdentity());
+ modifyPeerNote.setNoteText(noteText);
+ modifyPeerNote.setPeerNoteType(PeerNoteType.PRIVATE_DARKNET_COMMENT);
+ sendMessage(modifyPeerNote);
}
/**
* {@inheritDoc}
*/
@Override
- public void receivedPeer(FcpConnection fcpConnection, Peer receivedPeer) {
- if (receivedPeer.getIdentity().equals(peer.getIdentity())) {
+ public void receivedPeerNote(FcpConnection fcpConnection, PeerNote receivedPeerNote) {
+ if (receivedPeerNote.getNodeIdentifier().equals(peer.getIdentity())) {
complete();
}
}
import net.pterodactylus.fcp.NodeRef;
import net.pterodactylus.fcp.Peer;
import net.pterodactylus.fcp.PeerNote;
+import net.pterodactylus.fcp.PeerNoteType;
import net.pterodactylus.fcp.PeerRemoved;
import net.pterodactylus.fcp.ProtocolError;
import net.pterodactylus.fcp.SSKKeypair;
}
@Test
+ public void modifyPeerNoteSendsCorrectMessage() throws Exception {
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("ModifyPeerNote") && message.getField("NoteText").equals("bmV3IG5vdGU=")) {
+ return (listener, connection) -> {
+ listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", message.getField("NodeIdentifier"))));
+ };
+ }
+ return FcpClientTest::doNothing;
+ });
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ fcpClient.modifyPeerNote(createPeer(), "new note");
+ }
+ }
+
+ @Test
+ public void modifyPeerNoteWaitsForCorrectPeerNoteReturnMessage() throws Exception {
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("ModifyPeerNote")) {
+ return (listener, connection) -> {
+ listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", "different-node")));
+ try {
+ Thread.sleep(6000);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", message.getField("NodeIdentifier"))));
+ };
+ }
+ return FcpClientTest::doNothing;
+ });
+ Thread thread = new Thread(() -> {
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ fcpClient.modifyPeerNote(createPeer(), "new note");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ });
+ thread.start();
+ Thread.sleep(1000);
+ assertThat(thread.isAlive(), equalTo(true));
+ }
+
+ @Test
public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException {
FcpConnection fcpConnection = createFcpConnection(message -> {
if (message.getName().equals("GenerateSSK")) {
@Override
public void sendMessage(FcpMessage fcpMessage) {
BiConsumer<FcpListener, FcpConnection> listenerNotifier = messageConsumer.apply(fcpMessage);
- new Thread(() -> listeners.forEach(listener -> listenerNotifier.accept(listener, this))).start();
+ listeners.forEach(listener -> new Thread(() -> listenerNotifier.accept(listener, this)).start());
}
};
}