summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/SameController.java
blob: 16c4d80186baae497c7b28eeceb6bbfbc60bcc88 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.orbekk.same;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.orbekk.net.BroadcastListener;
import com.orbekk.net.HttpUtil;
import com.orbekk.paxos.PaxosService;
import com.orbekk.paxos.PaxosServiceImpl;
import com.orbekk.same.config.Configuration;
import com.orbekk.same.http.ServerBuilder;
import com.orbekk.same.http.ServerContainer;
import com.orbekk.same.http.StateServlet;

public class SameController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private int port;
    private ServerContainer server;
    private Master master;
    private Client client;
    private PaxosServiceImpl paxos;
    private DiscoveryService discoveryService;
    
    /**
     * Timeout for remote operations in milliseconds.
     */
    private static final int timeout = 10000;
    
    public static SameController create(Configuration configuration) {
        int port = configuration.getInt("port");
        ConnectionManagerImpl connections = new ConnectionManagerImpl(
                timeout, timeout);
        State state = new State("Default");
        Broadcaster broadcaster = BroadcasterImpl.getDefaultBroadcastRunner();
        
        String baseUrl = String.format("http://%s:%s/",
                configuration.get("localIp"), configuration.getInt("port"));
        
        String masterUrl = baseUrl + "MasterService.json";
        String clientUrl = baseUrl + "ClientService.json";
        
        Master master = Master.create(
                connections, broadcaster, masterUrl);
        
        Client client = new Client(state, connections,
                clientUrl);
        PaxosServiceImpl paxos = new PaxosServiceImpl("");
        
        DiscoveryService discoveryService = null;
        if ("true".equals(configuration.get("enableDiscovery"))) {
            BroadcastListener broadcastListener = new BroadcastListener(
                    configuration.getInt("discoveryPort"));
            discoveryService = new DiscoveryService(client, broadcastListener);
        }
        
        ServerContainer server = new ServerBuilder(port)
                .withServlet(new StateServlet(client.getInterface()), "/_/state")
                .withService(client.getService(), ClientService.class)
                .withService(master, MasterService.class)
                .withService(paxos, PaxosService.class)
                .build();
        
        SameController controller = new SameController(port, server, master, client,
                paxos, discoveryService);
        return controller;
    }
    
    public SameController(
            int port,
            ServerContainer server,
            Master master,
            Client client,
            PaxosServiceImpl paxos,
            DiscoveryService discoveryService) {
        this.port = port;
        this.server = server;
        this.master = master;
        this.client = client;
        this.paxos = paxos;
        this.discoveryService = discoveryService;
    }

    public void start() throws Exception {
        server.start();
        master.start();
        client.start();
        if (discoveryService != null) {
            discoveryService.start();
        }
    }
    
    public void stop() {
        try {
            client.interrupt();
            master.interrupt();
            server.stop();
            if (discoveryService != null) {
                discoveryService.interrupt();
            }
        } catch (Exception e) {
            logger.error("Failed to stop webserver", e);
        }
    }
    
    public void join() {
        try {
            server.join();
            master.join();
            if (discoveryService != null) {
                discoveryService.join();
            }
        } catch (InterruptedException e) {
            master.interrupt();
            try {
                server.stop();
            } catch (Exception e1) {
                logger.error("Failed to stop server", e);
            }
        }
    }
    
    public void joinNetwork(String url) {
        client.joinNetwork(url);
    }
    
    public Client getClient() {
        return client;
    }
    
    public Master getMaster() {
        return master;
    }
}