summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/same/SameController.java
blob: b87e6f50451b03ab0a3bd2d8f9feda5667520975 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.orbekk.same;

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

import com.google.protobuf.RpcCallback;
import com.orbekk.paxos.PaxosService;
import com.orbekk.paxos.PaxosServiceImpl;
import com.orbekk.protobuf.Rpc;
import com.orbekk.protobuf.SimpleProtobufServer;
import com.orbekk.same.config.Configuration;
import com.orbekk.same.http.JettyServerBuilder;
import com.orbekk.same.http.ServerContainer;
import com.orbekk.same.http.StateServlet;

public class SameController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private ServerContainer server;
    private SimpleProtobufServer pServer;
    private MasterServiceProxy masterService;
    private Master master;
    private Client client;
    private PaxosServiceImpl paxos;
    private Configuration configuration;
    private ConnectionManager connections;
    private Broadcaster serviceBroadcaster;

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

    private MasterController masterController = new MasterController() {
        @Override
        public void enableMaster(State lastKnownState, int masterId) {
            String myLocation = configuration.get("localIp") + ":" +
                    configuration.get("pport");
            String masterUrl = configuration.get("baseUrl") +
                    "MasterService.json";
            master = Master.create(connections, serviceBroadcaster,
                    masterUrl, configuration.get("networkName"), myLocation);
            master.resumeFrom(lastKnownState, masterId);
            pServer.registerService(master.getNewService());
            master.start();
            masterService.setService(master.getService());
        }

        @Override
        public void disableMaster() {
            masterService.setService(null);
            if (master != null) {
                master.interrupt();
            }
        }
    };
    
    public static SameController create(Configuration configuration) {
        int port = configuration.getInt("port");
        int pport = configuration.getInt("pport");
        String myLocation = configuration.get("localIp") + ":" + pport;
        
        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 clientUrl = baseUrl + "ClientService.json";

        MasterServiceProxy master = new MasterServiceProxy();
        Client client = new Client(clientState, connections,
                clientUrl, myLocation,
                BroadcasterImpl.getDefaultBroadcastRunner());
        PaxosServiceImpl paxos = new PaxosServiceImpl("");
        StateServlet stateServlet = new StateServlet(client.getInterface(),
                new VariableFactory(client.getInterface()));
        ServerContainer server = new JettyServerBuilder(port)
            .withServlet(stateServlet, "/_/state")
            .withService(client.getService(), ClientService.class)
            .withService(master, MasterService.class)
            .withService(paxos, PaxosService.class)
            .build();
        
        SimpleProtobufServer pServer = SimpleProtobufServer.create(pport);
        
        SameController controller = new SameController(
                configuration, connections, server, master, client,
                paxos, broadcaster, pServer);
        return controller;
    }

    public SameController(
            Configuration configuration,
            ConnectionManager connections,
            ServerContainer server,
            MasterServiceProxy master,
            Client client,
            PaxosServiceImpl paxos,
            Broadcaster serviceBroadcaster,
            SimpleProtobufServer pServer) {
        this.configuration = configuration;
        this.connections = connections;
        this.server = server;
        this.masterService = master;
        this.client = client;
        this.paxos = paxos;
        this.serviceBroadcaster = serviceBroadcaster;
        this.pServer = pServer;
    }

    public void start() throws Exception {
        server.start();
        pServer.start();
        client.setMasterController(masterController);
        client.start();
    }

    public void stop() {
        try {
            client.interrupt();
            if (master != null) {
                master.interrupt();
            }
            server.stop();
            pServer.interrupt();
        } catch (Exception e) {
            logger.error("Failed to stop webserver", e);
        }
    }

    public void join() {
        server.join();
        client.interrupt();
        if (master != null) {
            master.interrupt();
        }
    }

    public void createNetwork(String networkName) {
        masterController.disableMaster();
        masterController.enableMaster(new State(networkName), 1);
        String masterUrl = configuration.get("baseUrl") +
                "MasterService.json";
        joinNetwork(master.getMasterInfo());
    }

    public void joinNetwork(Services.MasterState masterInfo) {
        client.joinNetwork(masterInfo);
    }

    public Client getClient() {
        return client;
    }

    public Master getMaster() {
        return master;
    }
    
    public void registerNetwork(Master master) {
        Services.Directory directory = getDirectory();
        if (directory == null) {
            return;
        }
        Services.MasterState request = Services.MasterState.newBuilder()
                .setNetworkName(master.getNetworkName())
                .setMasterUrl(master.getUrl())
                .setMasterLocation(master.getLocation())
                .build();
        final Rpc rpc = new Rpc();
        RpcCallback<Services.Empty> done = new RpcCallback<Services.Empty>() {
            @Override public void run(Services.Empty unused) {
                if (!rpc.isOk()) {
                    logger.warn("Failed to register network: {}", rpc);
                }
            }
        };
        directory.registerNetwork(rpc, request, done);
    }
    
    public Services.Directory getDirectory() {
        String directoryLocation = configuration.get("directoryLocation");
        if (directoryLocation != null) {
            return connections.getDirectory(directoryLocation);
        } else {
            return null;
        }
    }

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