Search in sources :

Example 1 with TaskUPnPManager

use of org.aion.p2p.impl.TaskUPnPManager 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)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 Handler (org.aion.p2p.Handler)1 Header (org.aion.p2p.Header)1 INode (org.aion.p2p.INode)1 TaskUPnPManager (org.aion.p2p.impl.TaskUPnPManager)1