Search in sources :

Example 1 with Handler

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);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) BroadcastNewBlockHandler(org.aion.zero.impl.sync.handler.BroadcastNewBlockHandler) Handler(org.aion.p2p.Handler) BroadcastNewBlockHandler(org.aion.zero.impl.sync.handler.BroadcastNewBlockHandler)

Example 2 with Handler

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);
    }
}
Also used : INode(org.aion.p2p.INode) Handler(org.aion.p2p.Handler)

Example 3 with Handler

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);
    }
}
Also used : SocketException(java.net.SocketException) INode(org.aion.p2p.INode) Header(org.aion.p2p.Header) TaskUPnPManager(org.aion.p2p.impl.TaskUPnPManager) InetSocketAddress(java.net.InetSocketAddress) Handler(org.aion.p2p.Handler) IOException(java.io.IOException)

Example 4 with Handler

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);
}
Also used : ReqStatusHandler(org.aion.zero.impl.sync.handler.ReqStatusHandler) ResBlocksBodiesHandler(org.aion.zero.impl.sync.handler.ResBlocksBodiesHandler) ArrayList(java.util.ArrayList) BroadcastNewBlockHandler(org.aion.zero.impl.sync.handler.BroadcastNewBlockHandler) Handler(org.aion.p2p.Handler) ResBlocksHeadersHandler(org.aion.zero.impl.sync.handler.ResBlocksHeadersHandler) ReqStatusHandler(org.aion.zero.impl.sync.handler.ReqStatusHandler) BroadcastTxHandler(org.aion.zero.impl.sync.handler.BroadcastTxHandler) ResStatusHandler(org.aion.zero.impl.sync.handler.ResStatusHandler) BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ReqBlocksBodiesHandler(org.aion.zero.impl.sync.handler.ReqBlocksBodiesHandler) ReqBlocksHeadersHandler(org.aion.zero.impl.sync.handler.ReqBlocksHeadersHandler) ResBlocksBodiesHandler(org.aion.zero.impl.sync.handler.ResBlocksBodiesHandler) ReqBlocksBodiesHandler(org.aion.zero.impl.sync.handler.ReqBlocksBodiesHandler) ReqBlocksHeadersHandler(org.aion.zero.impl.sync.handler.ReqBlocksHeadersHandler) ResBlocksHeadersHandler(org.aion.zero.impl.sync.handler.ResBlocksHeadersHandler) ResStatusHandler(org.aion.zero.impl.sync.handler.ResStatusHandler) BroadcastTxHandler(org.aion.zero.impl.sync.handler.BroadcastTxHandler) BroadcastNewBlockHandler(org.aion.zero.impl.sync.handler.BroadcastNewBlockHandler)

Example 5 with Handler

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));
}
Also used : Header(org.aion.p2p.Header) ReqHandshake1(org.aion.p2p.impl.zero.msg.ReqHandshake1) ArrayList(java.util.ArrayList) Handler(org.aion.p2p.Handler)

Aggregations

Handler (org.aion.p2p.Handler)5 ArrayList (java.util.ArrayList)2 Header (org.aion.p2p.Header)2 INode (org.aion.p2p.INode)2 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)2 BroadcastNewBlockHandler (org.aion.zero.impl.sync.handler.BroadcastNewBlockHandler)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 TaskUPnPManager (org.aion.p2p.impl.TaskUPnPManager)1 ReqHandshake1 (org.aion.p2p.impl.zero.msg.ReqHandshake1)1 BroadcastTxHandler (org.aion.zero.impl.sync.handler.BroadcastTxHandler)1 ReqBlocksBodiesHandler (org.aion.zero.impl.sync.handler.ReqBlocksBodiesHandler)1 ReqBlocksHeadersHandler (org.aion.zero.impl.sync.handler.ReqBlocksHeadersHandler)1 ReqStatusHandler (org.aion.zero.impl.sync.handler.ReqStatusHandler)1 ResBlocksBodiesHandler (org.aion.zero.impl.sync.handler.ResBlocksBodiesHandler)1 ResBlocksHeadersHandler (org.aion.zero.impl.sync.handler.ResBlocksHeadersHandler)1 ResStatusHandler (org.aion.zero.impl.sync.handler.ResStatusHandler)1