diff --git a/tracker/build.gradle b/tracker/build.gradle index f7ee12c4..60c438cf 100644 --- a/tracker/build.gradle +++ b/tracker/build.gradle @@ -22,6 +22,6 @@ apply plugin : 'com.github.johnrengelman.shadow' dependencies { compile project(":core") - compile 'org.eclipse.lsp4j:org.eclipse.lsp4j.jsonrpc:0.9.0' + compile 'com.github.arteam:simple-json-rpc-server:1.0' } diff --git a/tracker/src/main/groovy/com/muwire/tracker/TrackRequest.java b/tracker/src/main/groovy/com/muwire/tracker/TrackRequest.java new file mode 100644 index 00000000..4e102c1b --- /dev/null +++ b/tracker/src/main/groovy/com/muwire/tracker/TrackRequest.java @@ -0,0 +1,10 @@ +package com.muwire.tracker; + +public class TrackRequest { + String infoHash; + + @Override + public String toString() { + return "infoHash: " +infoHash; + } +} diff --git a/tracker/src/main/groovy/com/muwire/tracker/Tracker.groovy b/tracker/src/main/groovy/com/muwire/tracker/Tracker.groovy index 7e662df9..3df3e864 100644 --- a/tracker/src/main/groovy/com/muwire/tracker/Tracker.groovy +++ b/tracker/src/main/groovy/com/muwire/tracker/Tracker.groovy @@ -1,6 +1,12 @@ package com.muwire.tracker +import java.nio.charset.StandardCharsets + +import com.github.arteam.simplejsonrpc.server.JsonRpcServer +import com.muwire.core.Core import com.muwire.core.MuWireSettings +import com.muwire.core.UILoadedEvent +import com.muwire.core.files.AllFilesLoadedEvent class Tracker { @@ -53,5 +59,54 @@ class Tracker { trackerProps.withPrintWriter { jsonProps.store(it, "") } } + + Properties p = new Properties() + mwProps.withReader("UTF-8", { p.load(it) } ) + MuWireSettings muSettings = new MuWireSettings(p) + p = new Properties() + trackerProps.withInputStream { p.load(it) } + + InetAddress toBind = InetAddress.getByName(p['jsonrpc.iface']) + int port = Integer.parseInt(p['jsonrpc.port']) + ServerSocket ss = new ServerSocket(port, Integer.MAX_VALUE, toBind) + + Core core = new Core(muSettings, home, VERSION, "MuWire Tracker") + + + // init json service object + TrackerService trackerService = new TrackerService() + JsonRpcServer rpcServer = new JsonRpcServer() + + Thread coreStarter = new Thread({ + core.startServices() + core.eventBus.publish(new UILoadedEvent()) + } as Runnable) + coreStarter.start() + + println "json rpc listening on $toBind:$port" + + try { + while(true) { + Socket s = ss.accept() + try { + println "accepted connection from " + s.getInetAddress() + def reader = new BufferedReader(new InputStreamReader(s.getInputStream())) + String request; + while((request = reader.readLine()) != null) { + println "got request \"$request\"" + String response = rpcServer.handle(request, trackerService) + println "sending response \"$response\"" + s.getOutputStream().newWriter("UTF-8").write(response) + s.getOutputStream().write("\n".getBytes(StandardCharsets.US_ASCII)) + s.getOutputStream().flush() + } + } finally { + s.close() + } + } + } catch (Exception bad) { + bad.printStackTrace() + } + } } diff --git a/tracker/src/main/groovy/com/muwire/tracker/TrackerService.groovy b/tracker/src/main/groovy/com/muwire/tracker/TrackerService.groovy new file mode 100644 index 00000000..a42ce9a7 --- /dev/null +++ b/tracker/src/main/groovy/com/muwire/tracker/TrackerService.groovy @@ -0,0 +1,19 @@ +package com.muwire.tracker + +import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod +import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcService + +@JsonRpcService +class TrackerService { + + private final TrackerStatus status = new TrackerStatus() + + TrackerService() { + status.status = "Starting" + } + + @JsonRpcMethod + public TrackerStatus status() { + status + } +} diff --git a/tracker/src/main/groovy/com/muwire/tracker/TrackerStatus.groovy b/tracker/src/main/groovy/com/muwire/tracker/TrackerStatus.groovy new file mode 100644 index 00000000..54baad7b --- /dev/null +++ b/tracker/src/main/groovy/com/muwire/tracker/TrackerStatus.groovy @@ -0,0 +1,7 @@ +package com.muwire.tracker + +class TrackerStatus { + String status + int connections + int swarms +}