Repository navigation
Expand file tree
/
Copy pathServer.java
More file actions
78 lines (66 loc) · 2.92 KB
/
Copy pathServer.java
File metadata and controls
78 lines (66 loc) · 2.92 KB
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
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.consensus.server;
import io.atomix.catalyst.transport.Address;
import io.atomix.catalyst.transport.netty.NettyTransport;
import io.atomix.copycat.server.CopycatServer;
import io.atomix.copycat.server.storage.Storage;
import io.atomix.copycat.server.storage.StorageLevel;
import org.tron.consensus.common.GetQuery;
import org.tron.consensus.common.MapstateMachine;
import org.tron.consensus.common.PutCommand;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.CompletableFuture;
public class Server {
public static void serverRun() {
InetAddress localhost = null;
try {
localhost = InetAddress.getLocalHost();
System.out.println("Server localhost: " + localhost.getHostAddress
());
Address address = new Address(localhost.getHostAddress(), 5000);
CopycatServer server = CopycatServer.builder(address)
.withStateMachine(MapstateMachine::new)
.withTransport(NettyTransport.builder()
.withThreads(4)
.build())
.withStorage(Storage.builder()
.withDirectory(new File("consensus-logs"))
.withStorageLevel(StorageLevel.DISK)
.build())
.build();
server.serializer().register(PutCommand.class);
server.serializer().register(GetQuery.class);
CompletableFuture<CopycatServer> future = server.bootstrap();
future.join();
//Collection<Address> cluster = Collections.singleton(new Address
// ("192.16.50.129", 5000));
//server.join(cluster).join();
System.out.println("Server xxd: " + server.cluster().members());
CopycatServer.State state = server.state();
System.out.println("Server state: " + state);
server.onStateChange(state1 -> {
if (state == CopycatServer.State.LEADER) {
System.out.println("Server state: " + state);
}
});
server.context();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}