summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/SameController.java
blob: ac99197124597b9d01497e78b7fbd74b7cf881f4 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.orbekk.same;

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

import com.orbekk.net.BroadcasterInterface;
import com.orbekk.net.BroadcastListener;
import com.orbekk.net.BroadcasterFactory;
import com.orbekk.net.DefaultBroadcasterFactory;
import com.orbekk.paxos.PaxosService;
import com.orbekk.paxos.PaxosServiceImpl;
import com.orbekk.same.config.Configuration;
import com.orbekk.same.http.JettyServerBuilder;
import com.orbekk.same.http.JettyServerContainer;
import com.orbekk.same.http.StateServlet;

public class SameController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private JettyServerContainer server;
    private Master master;
    private Client client;
    private PaxosServiceImpl paxos;
    private DiscoveryService discoveryService;
    private BroadcasterFactory broadcasterFactory;
    private Configuration configuration;

    /**
     * Timeout for remote operations in milliseconds.
     */
    private static final int timeout = 10000;

    public static SameController create(BroadcasterFactory broadcasterFactory,
            Configuration configuration) {
        int port = configuration.getInt("port");
        ConnectionManagerImpl connections = new ConnectionManagerImpl(
                timeout, timeout);
        State clientState = new State(".InvalidClientNetwork");
        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, configuration.get("networkName"));

        Client client = new Client(clientState, 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);
        }

        StateServlet stateServlet = new StateServlet(client.getInterface(),
                new VariableFactory(client.getInterface()));

        JettyServerContainer server = new JettyServerBuilder(port)
            .withServlet(stateServlet, "/_/state")
            .withService(client.getService(), ClientService.class)
            .withService(master.getService(), MasterService.class)
            .withService(paxos, PaxosService.class)
            .build();

        SameController controller = new SameController(
                configuration, server, master, client,
                paxos, discoveryService, broadcasterFactory);
        return controller;
    }

    public static SameController create(Configuration configuration) {
        return create(new DefaultBroadcasterFactory(), configuration);
    }

    public SameController(
            Configuration configuration,
            JettyServerContainer server,
            Master master,
            Client client,
            PaxosServiceImpl paxos,
            DiscoveryService discoveryService,
            BroadcasterFactory broadcasterFactory) {
        this.configuration = configuration;
        this.server = server;
        this.master = master;
        this.client = client;
        this.paxos = paxos;
        this.discoveryService = discoveryService;
        this.broadcasterFactory = broadcasterFactory;
    }

    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();
            client.interrupt();
            master.interrupt();
            if (discoveryService != null) {
                discoveryService.join();
            }
        } catch (InterruptedException e) {
            try {
                server.stop();
            } catch (Exception e1) {
                logger.error("Failed to stop server", e);
            }
        }
    }

    public void searchNetworks() {
        BroadcasterInterface broadcaster = broadcasterFactory.create();
        String message = "Discover " + client.getUrl();
        broadcaster.sendBroadcast(configuration.getInt("discoveryPort"),
                message.getBytes());
    }

    public void joinNetwork(String url) {
        client.joinNetwork(url);
    }

    public Client getClient() {
        return client;
    }

    public Master getMaster() {
        return master;
    }

    public VariableFactory createVariableFactory() {
        return new VariableFactory(client.getInterface());
    }
}