wip on tracker

pull/53/head
Zlatin Balevsky 2020-04-26 19:31:21 +01:00
parent 5fc0283da7
commit 4aed958319
3 changed files with 42 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package com.muwire.tracker package com.muwire.tracker
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import com.github.arteam.simplejsonrpc.server.JsonRpcServer import com.github.arteam.simplejsonrpc.server.JsonRpcServer
import com.muwire.core.Core import com.muwire.core.Core
@ -12,6 +14,8 @@ class Tracker {
private static final String VERSION = "0.6.12" private static final String VERSION = "0.6.12"
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool()
public static void main(String [] args) { public static void main(String [] args) {
println "Launching MuWire Tracker version $VERSION" println "Launching MuWire Tracker version $VERSION"
@ -49,6 +53,8 @@ class Tracker {
def i2cpProps = new Properties() def i2cpProps = new Properties()
i2cpProps['i2cp.tcp.port'] = props['i2cp.tcp.port'] i2cpProps['i2cp.tcp.port'] = props['i2cp.tcp.port']
i2cpProps['i2cp.tcp.host'] = props['i2cp.tcp.host'] i2cpProps['i2cp.tcp.host'] = props['i2cp.tcp.host']
i2cpProps['inbound.nickname'] = "MuWire Tracker"
i2cpProps['outbound.nickname'] = "MuWire Tracker"
i2pProps.withPrintWriter { i2cpProps.store(it, "") } i2pProps.withPrintWriter { i2cpProps.store(it, "") }
@ -69,13 +75,17 @@ class Tracker {
InetAddress toBind = InetAddress.getByName(p['jsonrpc.iface']) InetAddress toBind = InetAddress.getByName(p['jsonrpc.iface'])
int port = Integer.parseInt(p['jsonrpc.port']) int port = Integer.parseInt(p['jsonrpc.port'])
ServerSocket ss = new ServerSocket(port, Integer.MAX_VALUE, toBind) ServerSocket ss = new ServerSocket(port, Integer.MAX_VALUE, toBind)
println "json rpc listening on $toBind:$port"
JsonRpcServer rpcServer = new JsonRpcServer()
Core core = new Core(muSettings, home, VERSION, "MuWire Tracker") Core core = new Core(muSettings, home, VERSION)
// init json service object // init json service object
TrackerService trackerService = new TrackerService() TrackerService trackerService = new TrackerService()
JsonRpcServer rpcServer = new JsonRpcServer() core.eventBus.with {
register(UILoadedEvent.class, trackerService)
}
Thread coreStarter = new Thread({ Thread coreStarter = new Thread({
core.startServices() core.startServices()
@ -83,26 +93,29 @@ class Tracker {
} as Runnable) } as Runnable)
coreStarter.start() coreStarter.start()
println "json rpc listening on $toBind:$port"
try { try {
while(true) { while(true) {
Socket s = ss.accept() Socket s = ss.accept()
try { println "accepted connection from " + s.getInetAddress()
println "accepted connection from " + s.getInetAddress() EXECUTOR_SERVICE.submit {
def reader = new BufferedReader(new InputStreamReader(s.getInputStream())) try {
String request; def reader = new BufferedReader(new InputStreamReader(s.getInputStream()))
while((request = reader.readLine()) != null) { String request;
println "got request \"$request\"" while((request = reader.readLine()) != null) {
String response = rpcServer.handle(request, trackerService) println "got request \"$request\""
println "sending response \"$response\"" String response = rpcServer.handle(request, trackerService)
s.getOutputStream().newWriter("UTF-8").write(response) println "sending response \"$response\""
s.getOutputStream().write("\n".getBytes(StandardCharsets.US_ASCII)) s.getOutputStream().newWriter("UTF-8").write(response)
s.getOutputStream().flush() s.getOutputStream().write("\n".getBytes(StandardCharsets.US_ASCII))
s.getOutputStream().flush()
}
} catch (Exception bad) {
bad.printStackTrace()
} finally {
s.close()
} }
} finally { } as Runnable
s.close()
}
} }
} catch (Exception bad) { } catch (Exception bad) {
bad.printStackTrace() bad.printStackTrace()

View File

@ -2,18 +2,28 @@ package com.muwire.tracker
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcService import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcService
import com.muwire.core.Core
import com.muwire.core.UILoadedEvent
@JsonRpcService @JsonRpcService
class TrackerService { class TrackerService {
private final TrackerStatus status = new TrackerStatus() private final TrackerStatus status = new TrackerStatus()
private final Core core
TrackerService() { TrackerService(Core core) {
this.core = core
status.status = "Starting" status.status = "Starting"
} }
@JsonRpcMethod @JsonRpcMethod
public TrackerStatus status() { public TrackerStatus status() {
status.connections = core.getConnectionManager().getConnections().size()
status status
} }
void onUILoadedEvent(UILoadedEvent e) {
status.status = "Running"
}
} }

View File

@ -1,7 +1,7 @@
package com.muwire.tracker package com.muwire.tracker
class TrackerStatus { class TrackerStatus {
String status volatile String status
int connections int connections
int swarms int swarms
} }