mirror of https://github.com/zlatinb/muwire
wip on tracker
parent
5fc0283da7
commit
4aed958319
|
@ -1,6 +1,8 @@
|
|||
package com.muwire.tracker
|
||||
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
import com.github.arteam.simplejsonrpc.server.JsonRpcServer
|
||||
import com.muwire.core.Core
|
||||
|
@ -12,6 +14,8 @@ class Tracker {
|
|||
|
||||
private static final String VERSION = "0.6.12"
|
||||
|
||||
private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool()
|
||||
|
||||
public static void main(String [] args) {
|
||||
println "Launching MuWire Tracker version $VERSION"
|
||||
|
||||
|
@ -49,6 +53,8 @@ class Tracker {
|
|||
def i2cpProps = new Properties()
|
||||
i2cpProps['i2cp.tcp.port'] = props['i2cp.tcp.port']
|
||||
i2cpProps['i2cp.tcp.host'] = props['i2cp.tcp.host']
|
||||
i2cpProps['inbound.nickname'] = "MuWire Tracker"
|
||||
i2cpProps['outbound.nickname'] = "MuWire Tracker"
|
||||
|
||||
i2pProps.withPrintWriter { i2cpProps.store(it, "") }
|
||||
|
||||
|
@ -69,13 +75,17 @@ class Tracker {
|
|||
InetAddress toBind = InetAddress.getByName(p['jsonrpc.iface'])
|
||||
int port = Integer.parseInt(p['jsonrpc.port'])
|
||||
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
|
||||
TrackerService trackerService = new TrackerService()
|
||||
JsonRpcServer rpcServer = new JsonRpcServer()
|
||||
core.eventBus.with {
|
||||
register(UILoadedEvent.class, trackerService)
|
||||
}
|
||||
|
||||
Thread coreStarter = new Thread({
|
||||
core.startServices()
|
||||
|
@ -83,26 +93,29 @@ class Tracker {
|
|||
} 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()
|
||||
println "accepted connection from " + s.getInetAddress()
|
||||
EXECUTOR_SERVICE.submit {
|
||||
try {
|
||||
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()
|
||||
}
|
||||
} catch (Exception bad) {
|
||||
bad.printStackTrace()
|
||||
} finally {
|
||||
s.close()
|
||||
}
|
||||
} finally {
|
||||
s.close()
|
||||
}
|
||||
} as Runnable
|
||||
}
|
||||
} catch (Exception bad) {
|
||||
bad.printStackTrace()
|
||||
|
|
|
@ -2,18 +2,28 @@ package com.muwire.tracker
|
|||
|
||||
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod
|
||||
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcService
|
||||
import com.muwire.core.Core
|
||||
import com.muwire.core.UILoadedEvent
|
||||
|
||||
@JsonRpcService
|
||||
class TrackerService {
|
||||
|
||||
private final TrackerStatus status = new TrackerStatus()
|
||||
private final Core core
|
||||
|
||||
TrackerService() {
|
||||
TrackerService(Core core) {
|
||||
this.core = core
|
||||
status.status = "Starting"
|
||||
}
|
||||
|
||||
@JsonRpcMethod
|
||||
public TrackerStatus status() {
|
||||
status.connections = core.getConnectionManager().getConnections().size()
|
||||
status
|
||||
}
|
||||
|
||||
|
||||
void onUILoadedEvent(UILoadedEvent e) {
|
||||
status.status = "Running"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.muwire.tracker
|
||||
|
||||
class TrackerStatus {
|
||||
String status
|
||||
volatile String status
|
||||
int connections
|
||||
int swarms
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue