Search in sources :

Example 21 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class ServerChannelHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    SocketChannel channel = (SocketChannel) ctx.channel();
    Node node = getNetworkService().getNode(channel.remoteAddress().getHostString());
    if (node != null && node.isAlive()) {
        ByteBuf buf = (ByteBuf) msg;
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        buf.release();
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        buffer.put(bytes);
        getNetworkService().receiveMessage(buffer, node);
    }
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer)

Example 22 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class ServerChannelHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    Log.debug("----------------------server channelActive ------------------------- ");
    String channelId = ctx.channel().id().asLongText();
    SocketChannel channel = (SocketChannel) ctx.channel();
    NioChannelMap.add(channelId, channel);
    Node node = new Node(getNetworkService().getNetworkParam(), Node.IN, channel.remoteAddress().getHostString(), channel.remoteAddress().getPort(), channelId);
    node.setStatus(Node.CONNECT);
    getNetworkService().addNodeToGroup(NetworkConstant.NETWORK_NODE_IN_GROUP, node);
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) Node(io.nuls.network.entity.Node)

Example 23 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class NodeEventHandler method process.

@Override
public NetworkEventResult process(BaseEvent networkEvent, Node node) {
    NodeEvent event = (NodeEvent) networkEvent;
    for (Node newNode : event.getEventBody().getNodes()) {
        newNode.setType(Node.OUT);
        newNode.setStatus(Node.WAIT);
        getNetworkService().addNodeToGroup(NetworkConstant.NETWORK_NODE_OUT_GROUP, newNode);
    }
    return null;
}
Also used : NodeEvent(io.nuls.network.message.entity.NodeEvent) Node(io.nuls.network.entity.Node)

Example 24 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class BroadcastHandler method broadcastToNode.

public BroadcastResult broadcastToNode(BaseEvent event, String nodeId, boolean asyn) {
    try {
        NulsMessage message = new NulsMessage(network.packetMagic(), event.serialize());
        Node node = nodesManager.getNode(nodeId);
        if (node == null) {
            return new BroadcastResult(false, "node not found");
        }
        return broadcast(message, node, asyn);
    } catch (IOException e) {
        return new BroadcastResult(false, "event.serialize() error");
    }
}
Also used : NulsMessage(io.nuls.core.mesasge.NulsMessage) BroadcastResult(io.nuls.network.entity.BroadcastResult) Node(io.nuls.network.entity.Node) IOException(java.io.IOException)

Example 25 with Node

use of io.nuls.network.entity.Node in project nuls by nuls-io.

the class NodeEventBody method size.

@Override
public int size() {
    int s = 0;
    s += VarInt.sizeOf(nodes.size());
    for (Node node : nodes) {
        s += node.size();
    }
    return s;
}
Also used : Node(io.nuls.network.entity.Node)

Aggregations

Node (io.nuls.network.entity.Node)25 SocketChannel (io.netty.channel.socket.SocketChannel)8 NodeGroup (io.nuls.network.entity.NodeGroup)5 ArrayList (java.util.ArrayList)4 BroadcastResult (io.nuls.network.entity.BroadcastResult)3 ByteBuf (io.netty.buffer.ByteBuf)2 NulsMessage (io.nuls.core.mesasge.NulsMessage)2 NetworkEventResult (io.nuls.network.message.NetworkEventResult)2 GetNodeEvent (io.nuls.network.message.entity.GetNodeEvent)2 GetNodesIpEvent (io.nuls.network.message.entity.GetNodesIpEvent)2 NodeEvent (io.nuls.network.message.entity.NodeEvent)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Block (io.nuls.core.chain.entity.Block)1 NodePo (io.nuls.db.entity.NodePo)1 GetVersionEvent (io.nuls.network.message.entity.GetVersionEvent)1 NodesIpEvent (io.nuls.network.message.entity.NodesIpEvent)1 InfoDto (io.nuls.rpc.entity.InfoDto)1 RpcResult (io.nuls.rpc.entity.RpcResult)1 HashSet (java.util.HashSet)1