Skip to content

Commit

Permalink
DhtShell put operation
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 4, 2016
1 parent cf82ee8 commit 07d62d2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/frostwire/jlibtorrent/Session.java
Expand Up @@ -84,8 +84,8 @@ public Session() {
this(new SettingsPack(), false, null);
}

public Session(String iface, int port, int retries) {
this(addSettings(new SettingsPack(), iface, port, retries), false, null);
public Session(String iface, int port, int retries, boolean logging, AlertListener listener) {
this(addSettings(new SettingsPack(), iface, port, retries), logging, listener);
}

public session getSwig() {
Expand Down
46 changes: 35 additions & 11 deletions src/test/java/com/frostwire/jlibtorrent/demo/DhtShell.java
@@ -1,11 +1,7 @@
package com.frostwire.jlibtorrent.demo;

import com.frostwire.jlibtorrent.AlertListener;
import com.frostwire.jlibtorrent.LibTorrent;
import com.frostwire.jlibtorrent.Session;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.AlertType;
import com.frostwire.jlibtorrent.alerts.ListenSucceededAlert;
import com.frostwire.jlibtorrent.*;
import com.frostwire.jlibtorrent.alerts.*;

import java.util.Scanner;

Expand All @@ -19,9 +15,7 @@ public static void main(String[] args) throws Throwable {

System.out.println("Using libtorrent version: " + LibTorrent.fullVersion());

Session s = new Session("0.0.0.0", 33123, 10);

s.addListener(new AlertListener() {
AlertListener l = new AlertListener() {
@Override
public int[] types() {
return null;
Expand All @@ -30,13 +24,27 @@ public int[] types() {
@Override
public void alert(Alert<?> alert) {
AlertType type = alert.getType();
log(alert.getMessage());

if (type == AlertType.LISTEN_SUCCEEDED) {
ListenSucceededAlert a = (ListenSucceededAlert) alert;
print(a.getMessage(), true);
log(a.getMessage());
}

if (type == AlertType.LISTEN_FAILED) {
ListenFailedAlert a = (ListenFailedAlert) alert;
log(a.getMessage());
}

if (type == AlertType.DHT_PUT) {
DhtPutAlert a = (DhtPutAlert) alert;
log(a.getMessage());
}
}
});
};

Session s = new Session("0.0.0.0", 33123, 10, false, l);
DHT dht = new DHT(s);

Scanner in = new Scanner(System.in);
while (true) {
Expand All @@ -46,6 +54,8 @@ public void alert(Alert<?> alert) {

if (is_quit(line)) {
quit(s);
} else if (is_put(line)) {
put(dht, line);
} else if (is_invalid(line)) {
invalid(line);
}
Expand All @@ -64,6 +74,10 @@ private static void print(String s) {
print(s, false);
}

private static void log(String s) {
print(s, true);
}

private static boolean is_quit(String s) {
s = s.split(" ")[0];
return s.equals("quit") || s.equals("exit") || s.equals("stop");
Expand All @@ -75,6 +89,16 @@ private static void quit(Session s) {
System.exit(0);
}

private static boolean is_put(String s) {
return s.startsWith("put ");
}

private static void put(DHT dht, String s) {
String data = s.split(" ")[1];
String key = dht.put(new Entry(data));
print("Wait for completion of put for key: " + key);
}

private static boolean is_invalid(String s) {
return !s.isEmpty();
}
Expand Down

0 comments on commit 07d62d2

Please sign in to comment.