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
|
package com.orbekk.same;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**
* 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("");
ServerContainer server = new ServerBuilder(port)
.withServlet(new StateServlet(), "/_/state")
.withService(client.getService(), ClientService.class)
.withService(master, MasterService.class)
.withService(paxos, PaxosService.class)
.build();
SameController controller = new SameController(port, server, master, client,
paxos);
return controller;
}
public SameController(
int port,
ServerContainer server,
Master master,
Client client,
PaxosServiceImpl paxos) {
this.port = port;
this.server = server;
this.master = master;
this.client = client;
this.paxos = paxos;
}
public void start() throws Exception {
server.start();
master.start();
client.start();
}
public void stop() {
try {
client.interrupt();
master.interrupt();
server.stop();
} catch (Exception e) {
logger.error("Failed to stop webserver", e);
}
}
public void join() {
try {
server.join();
master.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;
}
}
|