summaryrefslogtreecommitdiff
path: root/same/old/Client.java
blob: 11a7449ee4b9b23d2090464d06983510dd4b066a (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
52
53
54
55
56
57
58
59
60
61
62
package com.orbekk.same;

import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.net.HttpUtil;
import org.eclipse.jetty.server.Server;

public class Client {
    
    public static void main(String[] args) {
        if (args.length < 4) {
            System.err.println("Arguments: port clientId thisNetworkName " +
                    "remoteNetworkAddr");
            System.exit(1);
        }
        int port = Integer.parseInt(args[0]);
        String clientId = args[1];
        String networkName = args[2];
        String remoteAddr = args[3];

        ConnectionManagerImpl connections = new ConnectionManagerImpl();

        SameState sameState = new SameState(networkName, clientId,
                connections);
        sameState.start();

        SameServiceImpl service = new SameServiceImpl(sameState);
        JsonRpcServer jsonServer = new JsonRpcServer(service,
                SameService.class);   
    
        Server server = new Server(port);
        RpcHandler rpcHandler = new RpcHandler(jsonServer, sameState);
        server.setHandler(rpcHandler);

        try {
            server.start();
        } catch (Exception e) {
            System.out.println("Could not start jetty server.");
            e.printStackTrace();
        }

        while (sameState.getUrl() == null) {
            HttpUtil.sendHttpRequest(remoteAddr + "ping?port=" + port);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // Ignore interrupt in wait loop.
            }
        }
        
        SameService remoteService = connections.getConnection(remoteAddr);
        remoteService.notifyNetwork("NoNetwork");
        remoteService.participateNetwork("FirstNetwork",
                sameState.getClientId(), sameState.getUrl());

        try {
            server.join();
        } catch (InterruptedException e) {
            System.out.println("Interrupt");
        }

    }
}