-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBEServer.java
More file actions
74 lines (65 loc) · 2.85 KB
/
Copy pathBEServer.java
File metadata and controls
74 lines (65 loc) · 2.85 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
import java.net.InetAddress;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.layered.TFramedTransport;
public class BEServer {
static Logger log;
public static void main(String [] args) throws Exception {
if (args.length != 3) {
System.err.println("Usage: java BENode FE_host FE_port BE_port");
System.exit(-1);
}
// initialize log4j
BasicConfigurator.configure();
log = Logger.getLogger(BEServer.class.getName());
String hostFE = args[0];
int portFE = Integer.parseInt(args[1]);
int portBE = Integer.parseInt(args[2]);
log.info("Launching BE node on port " + portBE + " at host " + getHostName());
// launch Thrift server in a separate thread
MiningPoolService.Processor processor = new MiningPoolService.Processor<MiningPoolService.Iface>(new MiningPoolServiceHandler(true));
TServerSocket socket = new TServerSocket(portBE);
TThreadPoolServer.Args sargs = new TThreadPoolServer.Args(socket);
sargs.protocolFactory(new TBinaryProtocol.Factory());
sargs.transportFactory(new TFramedTransport.Factory());
sargs.processorFactory(new TProcessorFactory(processor));
TThreadPoolServer server = new TThreadPoolServer(sargs);
Thread registrationThread = new Thread(() -> {
while (true) {
try {
TSocket beSocket = new TSocket(hostFE, portFE);
TTransport transport = new TFramedTransport(beSocket);
TProtocol protocol = new TBinaryProtocol(transport);
MiningPoolService.Client beClient = new MiningPoolService.Client(protocol);
transport.open();
beClient.registerBEServer(hostFE, portBE, Runtime.getRuntime().availableProcessors());
transport.close();
break;
} catch (Exception e) {
try {
Thread.sleep(50);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
});
registrationThread.start();
System.out.println("Starting BE server.");
server.serve();
}
static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (Exception e) {
return "localhost";
}
}
}