Search in sources :

Example 1 with ReqHandshake1

use of org.aion.p2p.impl.zero.msg.ReqHandshake1 in project aion by aionnetwork.

the class P2pMgr method handleP2pMessage.

private void handleP2pMessage(final SelectionKey sk, byte act, final byte[] msgBytes) {
    ChannelBuffer rb = (ChannelBuffer) sk.attachment();
    switch(act) {
        case Act.REQ_HANDSHAKE:
            if (msgBytes.length > ReqHandshake.LEN) {
                ReqHandshake1 reqHandshake1 = ReqHandshake1.decode(msgBytes, p2pLOG);
                if (reqHandshake1 != null) {
                    handleHandshakeRequest(rb, sk.channel().hashCode(), reqHandshake1.getNodeId(), reqHandshake1.getNetId(), reqHandshake1.getPort(), reqHandshake1.getRevision());
                }
            }
            break;
        case Act.RES_HANDSHAKE:
            if (rb.getNodeIdHash() != 0) {
                if (msgBytes.length > ResHandshake.LEN) {
                    ResHandshake1 resHandshake1 = ResHandshake1.decode(msgBytes, p2pLOG);
                    if (resHandshake1 != null && resHandshake1.getSuccess()) {
                        handleHandshakeResponse(rb.getNodeIdHash(), resHandshake1.getBinaryVersion());
                    }
                }
            }
            break;
        case Act.REQ_ACTIVE_NODES:
            if (rb.getNodeIdHash() != 0) {
                INode node = nodeMgr.getActiveNode(rb.getNodeIdHash());
                if (node != null) {
                    ResActiveNodes resActiveNodes = new ResActiveNodes(p2pLOG, nodeMgr.getActiveNodesList());
                    send(node.getIdHash(), node.getIdShort(), resActiveNodes);
                }
            }
            break;
        case Act.RES_ACTIVE_NODES:
            if (isSyncSeedsOnly() || rb.getNodeIdHash() == 0) {
                break;
            }
            INode node = nodeMgr.getActiveNode(rb.getNodeIdHash());
            if (node != null) {
                node.refreshTimestamp();
                ResActiveNodes resActiveNodes = ResActiveNodes.decode(msgBytes, p2pLOG);
                if (resActiveNodes != null) {
                    List<INode> incomingNodes = resActiveNodes.getNodes();
                    for (INode incomingNode : incomingNodes) {
                        if (nodeMgr.tempNodesSize() >= getMaxTempNodes()) {
                            return;
                        }
                        if (validateNode(incomingNode)) {
                            nodeMgr.addTempNode(incomingNode);
                        }
                    }
                }
            }
            break;
        default:
            p2pLOG.debug("unknown-route act={}", act);
            break;
    }
}
Also used : INode(org.aion.p2p.INode) ReqHandshake1(org.aion.p2p.impl.zero.msg.ReqHandshake1) ResActiveNodes(org.aion.p2p.impl.zero.msg.ResActiveNodes) ResHandshake1(org.aion.p2p.impl.zero.msg.ResHandshake1)

Example 2 with ReqHandshake1

use of org.aion.p2p.impl.zero.msg.ReqHandshake1 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

ReqHandshake1 (org.aion.p2p.impl.zero.msg.ReqHandshake1)2 ArrayList (java.util.ArrayList)1 Handler (org.aion.p2p.Handler)1 Header (org.aion.p2p.Header)1 INode (org.aion.p2p.INode)1 ResActiveNodes (org.aion.p2p.impl.zero.msg.ResActiveNodes)1 ResHandshake1 (org.aion.p2p.impl.zero.msg.ResHandshake1)1