summaryrefslogtreecommitdiff
path: root/same-android/src/main/java/com/orbekk/PingServer.java
blob: 5df23467647c228a760183d7a976d9dc44773db6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.orbekk;

import org.eclipse.jetty.server.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.rpc.PingService;
import com.orbekk.rpc.PingServiceImpl;
import com.orbekk.rpc.RpcHandler;

public class PingServer {
    private Server server;
    private Logger logger = LoggerFactory.getLogger(getClass());

    public PingServer(Server server) {
        this.server = server;
    }

    public static PingServer createPingServer(int port) {
        PingService service = new PingServiceImpl();
        JsonRpcServer jsonServer = new JsonRpcServer(service, PingService.class);   

        Server server = new Server(port);
        RpcHandler rpcHandler = new RpcHandler(jsonServer);
        server.setHandler(rpcHandler);

        return new PingServer(server);
    }

    public void start() throws Exception {
        logger.info("Starting server.");
        server.start();
    }

    public void join() {
        try {
            server.join();
        } catch (InterruptedException e) {
            logger.info("Received InterruptException while waiting for server.", e.fillInStackTrace());
        }
    }
    
    public void stop() {
        try {
            server.stop();
        } catch (Exception e) {
            logger.warn("Exception when stopping server.", e.fillInStackTrace());
        }
    }
}