use of org.aion.p2p.Handler in project aion by aionnetwork.
the class AionHub method registerCallback.
private void registerCallback() {
List<Handler> cbs = new ArrayList<>();
cbs.add(new ReqStatusHandler(syncLog, this.blockchain, this.p2pMgr, cfg.getGenesis().getHash()));
cbs.add(new ResStatusHandler(syncLog, this.p2pMgr, this.syncMgr));
cbs.add(new ReqBlocksHeadersHandler(syncLog, this.blockchain, this.p2pMgr, cfg.getSync().getBlocksImportMax()));
cbs.add(new ResBlocksHeadersHandler(syncLog, this.syncMgr));
cbs.add(new ReqBlocksBodiesHandler(syncLog, this.blockchain, this.p2pMgr, cfg.getSync().getBlocksImportMax()));
cbs.add(new ResBlocksBodiesHandler(syncLog, this.syncMgr));
cbs.add(new BroadcastTxHandler(syncLog, this.mempool, this.p2pMgr));
cbs.add(new BroadcastNewBlockHandler(syncLog, this.propHandler));
this.p2pMgr.register(cbs);
}
use of org.aion.p2p.Handler in project aion by aionnetwork.
the class P2pMgr method handleKernelMessage.
private void handleKernelMessage(int nodeIdHash, int route, final byte[] msgBytes) {
INode node = nodeMgr.getActiveNode(nodeIdHash);
if (node != null) {
String nodeDisplayId = node.getIdShort();
node.refreshTimestamp();
List<Handler> hs = handlers.get(route);
if (hs != null) {
scheduledWorkers.execute(() -> receiveMessage(nodeIdHash, nodeDisplayId, msgBytes, hs));
}
} else {
p2pLOG.debug("handleKernelMsg can't find hash{}", nodeIdHash);
}
}
use of org.aion.p2p.Handler in project aion by aionnetwork.
the class P2pMgr method run.
@Override
public void run() {
try {
selector = Selector.open();
// IO-bounded threads get max-gain from the double of the availableProcessor number
scheduledWorkers = Executors.newScheduledThreadPool(Math.min(32, 2 * Runtime.getRuntime().availableProcessors()));
inboundExecutor = Executors.newSingleThreadScheduledExecutor();
tcpServer = ServerSocketChannel.open();
tcpServer.configureBlocking(false);
tcpServer.socket().setReuseAddress(true);
/*
* Bigger RECV_BUFFER and BACKLOG can have a better socket read/write tolerance, can be a advanced p2p settings in the config file.
*/
tcpServer.socket().setReceiveBufferSize(SOCKET_RECV_BUFFER);
try {
tcpServer.socket().bind(new InetSocketAddress(Node.ipBytesToStr(selfIp), selfPort), SOCKET_BACKLOG);
} catch (IOException e) {
p2pLOG.error("Failed to connect to Socket Address: " + Node.ipBytesToStr(selfIp) + ":" + selfPort + ", please check your ip and port configration!", e);
}
tcpServer.register(selector, SelectionKey.OP_ACCEPT);
inboundExecutor.scheduleWithFixedDelay(this::checkSelector, DELAY_SELECT, DELAY_SELECT, TimeUnit.MILLISECONDS);
if (p2pLOG.isDebugEnabled()) {
this.handlers.forEach((route, callbacks) -> {
Handler handler = callbacks.get(0);
Header h = handler.getHeader();
p2pLOG.debug("handler route={} v-c-a={}-{}-{} name={}", route, h.getVer(), h.getCtrl(), h.getAction(), handler.getClass().getSimpleName());
});
}
if (upnpEnable) {
scheduledWorkers.scheduleWithFixedDelay(new TaskUPnPManager(p2pLOG, selfPort), 1, PERIOD_UPNP_PORT_MAPPING, TimeUnit.MILLISECONDS);
}
if (p2pLOG.isInfoEnabled()) {
scheduledWorkers.scheduleWithFixedDelay(() -> {
Thread.currentThread().setName("p2p-status");
p2pLOG.info(nodeMgr.dumpNodeInfo(selfShortId, p2pLOG.isDebugEnabled()));
}, DELAY_SHOW_P2P_STATUS, DELAY_SHOW_P2P_STATUS, TimeUnit.SECONDS);
}
if (!syncSeedsOnly) {
scheduledWorkers.scheduleWithFixedDelay(() -> {
Thread.currentThread().setName("p2p-reqNodes");
INode node = getRandom();
if (node != null) {
p2pLOG.trace("TaskRequestActiveNodes: {}", node.toString());
send(node.getIdHash(), node.getIdShort(), cachedReqActiveNodesMsg);
}
}, 5 * DELAY_REQUEST_ACTIVE_NODES, DELAY_REQUEST_ACTIVE_NODES, TimeUnit.SECONDS);
}
scheduledWorkers.scheduleWithFixedDelay(() -> {
Thread.currentThread().setName("p2p-clear");
nodeMgr.timeoutCheck(System.currentTimeMillis());
}, DELAY_CLEAR_PEERS, DELAY_CLEAR_PEERS, TimeUnit.SECONDS);
scheduledWorkers.scheduleWithFixedDelay(() -> connectPeers(), DELAY_CONNECT_OUTBOUND, DELAY_CONNECT_OUTBOUND, TimeUnit.SECONDS);
} catch (SocketException e) {
p2pLOG.error("tcp-server-socket-exception.", e);
} catch (IOException e) {
p2pLOG.error("tcp-server-io-exception.", e);
}
}
use of org.aion.p2p.Handler in project aion by aionnetwork.
the class AionHub method registerCallback.
private void registerCallback() {
List<Handler> cbs = new ArrayList<>();
cbs.add(new ReqStatusHandler(syncLOG, blockchain, mempool, p2pMgr, cfg.getGenesis().getHash(), apiVersion));
cbs.add(new ResStatusHandler(syncLOG, surveyLOG, p2pMgr, syncMgr));
boolean inSyncOnlyMode = cfg.getNet().getP2p().inSyncOnlyMode();
cbs.add(new ReqBlocksHeadersHandler(syncLOG, blockchain, p2pMgr, inSyncOnlyMode));
cbs.add(new ResBlocksHeadersHandler(syncLOG, surveyLOG, syncMgr));
cbs.add(new ReqBlocksBodiesHandler(syncLOG, blockchain, syncMgr, p2pMgr, inSyncOnlyMode));
cbs.add(new ResBlocksBodiesHandler(syncLOG, surveyLOG, syncMgr, p2pMgr));
cbs.add(new BroadcastTxHandler(syncLOG, mempool, p2pMgr, inSyncOnlyMode));
cbs.add(new BroadcastNewBlockHandler(syncLOG, surveyLOG, propHandler, p2pMgr));
this.p2pMgr.register(cbs);
}
use of org.aion.p2p.Handler in project aion by aionnetwork.
the class P2pMgr method register.
@Override
public void register(final List<Handler> _cbs) {
for (Handler _cb : _cbs) {
Header h = _cb.getHeader();
short ver = h.getVer();
byte ctrl = h.getCtrl();
if (Ver.filter(ver) != Ver.UNKNOWN && Ctrl.filter(ctrl) != Ctrl.UNKNOWN) {
versions.add(ver);
int route = h.getRoute();
List<Handler> routeHandlers = handlers.get(route);
if (routeHandlers == null) {
routeHandlers = new ArrayList<>();
routeHandlers.add(_cb);
handlers.put(route, routeHandlers);
} else {
routeHandlers.add(_cb);
}
}
}
cachedReqHandshake1 = new ReqHandshake1(selfNodeId, selfChainId, selfIp, selfPort, selfRevision.getBytes(), new ArrayList<>(versions));
}
Aggregations