Search in sources :

Example 1 with Node

use of me.matoosh.undernet.p2p.node.Node in project UnderNet by itsMatoosh.

the class EntryNodeCache method getMostReliable.

/**
 * Returns a specific number of the most reliable nodes.
 * @param amount
 * @return
 */
public static ArrayList<Node> getMostReliable(int amount, ArrayList<Node> exclude) {
    // Skipping if there are no cached nodes.
    if (cachedNodes == null || cachedNodes.size() == 0) {
        return null;
    }
    ArrayList<Node> resultList = (ArrayList<Node>) cachedNodes.clone();
    // Excluding nodes.
    if (exclude != null) {
        resultList.removeAll(exclude);
    }
    // Adjusting the amount.
    if (amount > resultList.size()) {
        amount = resultList.size();
    }
    // Removing nodes with lowest reliability until reached the amount.
    for (int i = 0; i < amount; i++) {
        if (amount == resultList.size())
            break;
        // Getting the least reliable node in the remaining set.
        Node lowestRel = resultList.get(0);
        /*for(Node node : resultList) {
                if(node.reliability < lowestRel.reliability) {
                    lowestRel = node;
                }
            }*/
        resultList.remove(lowestRel);
    }
    return resultList;
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ArrayList(java.util.ArrayList)

Example 2 with Node

use of me.matoosh.undernet.p2p.node.Node in project UnderNet by itsMatoosh.

the class Client method start.

/**
 * Starts the client and connects to cached nodes based on the settings.
 */
public void start() {
    EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTING));
    // Attempting to connect to each of the 5 most reliable nodes.
    ArrayList<Node> nodesToConnectTo = EntryNodeCache.getMostReliable(5, null);
    if (nodesToConnectTo == null || nodesToConnectTo.size() == 0) {
        logger.warn("There are no cached nodes to connect to! The client will stop.");
        EventManager.callEvent(new ClientExceptionEvent(this, new ClientNoNodesCachedException(this)));
        return;
    }
    // Creating a new event loop group.
    workerEventLoopGroup = new NioEventLoopGroup();
    // Creating a list of client futures.
    closeFutures = new ArrayList<>();
    EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTED));
    // Connecting to the selected nodes.
    for (Node node : nodesToConnectTo) {
        connect(node);
    }
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ClientStatusEvent(me.matoosh.undernet.event.client.ClientStatusEvent) ClientExceptionEvent(me.matoosh.undernet.event.client.ClientExceptionEvent) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 3 with Node

use of me.matoosh.undernet.p2p.node.Node in project UnderNet by itsMatoosh.

the class ServerNetworkMessageHandler method channelActive.

/**
 * Called when the channel is ready for data transfer.
 *
 * @param ctx
 */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    // Adding the channel to the server list.
    server.channels.add(ctx.channel());
    // Adding a node object to the connection.
    Node clientNode = new Node();
    // Setting the node's address.
    clientNode.address = ctx.channel().remoteAddress();
    clientNode.channel = ctx.channel();
    ctx.channel().attr(ATTRIBUTE_KEY_CLIENT_NODE).set(clientNode);
    // Adding the client node to the connectedNodes list.
    server.router.connectedNodes.add(clientNode);
    // Calling the channel created event.
    EventManager.callEvent(new ChannelCreatedEvent(ctx.channel(), true));
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ChannelCreatedEvent(me.matoosh.undernet.event.channel.ChannelCreatedEvent)

Example 4 with Node

use of me.matoosh.undernet.p2p.node.Node in project UnderNet by itsMatoosh.

the class ServerNetworkMessageHandler method channelInactive.

/**
 * Called when the channel is no longer ready for data transfer.
 *
 * @param ctx
 */
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    // Removing the channel from the server list.
    server.channels.remove(ctx.channel());
    // Removing the client from the connectedNodes list.
    Node clientNode = ctx.channel().attr(ATTRIBUTE_KEY_CLIENT_NODE).get();
    clientNode.channel = null;
    server.router.connectedNodes.remove(clientNode);
    // Calling the channel closed event.
    EventManager.callEvent(new ChannelClosedEvent(ctx.channel(), true));
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ChannelClosedEvent(me.matoosh.undernet.event.channel.ChannelClosedEvent)

Example 5 with Node

use of me.matoosh.undernet.p2p.node.Node in project UnderNet by itsMatoosh.

the class ClientNetworkMessageHandler method channelInactive.

/**
 * Calls {@link ChannelHandlerContext#fireChannelInactive()} to forward
 * to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
 * <p>
 * Sub-classes may override this method to change behavior.
 *
 * @param ctx
 */
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    // Removing the channel from the client list.
    client.channels.remove(ctx.channel());
    // Removing the server node from the connectedNodes list.
    Node serverNode = ctx.channel().attr(ATTRIBUTE_KEY_SERVER_NODE).get();
    serverNode.channel = null;
    client.router.connectedNodes.remove(serverNode);
    // Calling the channel closed event.
    EventManager.callEvent(new ChannelClosedEvent(ctx.channel(), false));
}
Also used : Node(me.matoosh.undernet.p2p.node.Node) ChannelClosedEvent(me.matoosh.undernet.event.channel.ChannelClosedEvent)

Aggregations

Node (me.matoosh.undernet.p2p.node.Node)8 ChannelClosedEvent (me.matoosh.undernet.event.channel.ChannelClosedEvent)2 ChannelCreatedEvent (me.matoosh.undernet.event.channel.ChannelCreatedEvent)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SecureRandom (java.security.SecureRandom)1 ArrayList (java.util.ArrayList)1 ClientExceptionEvent (me.matoosh.undernet.event.client.ClientExceptionEvent)1 ClientStatusEvent (me.matoosh.undernet.event.client.ClientStatusEvent)1 ResourceFinalStopEvent (me.matoosh.undernet.event.resource.push.ResourceFinalStopEvent)1 ResourcePushSentEvent (me.matoosh.undernet.event.resource.push.ResourcePushSentEvent)1 NetworkConfig (me.matoosh.undernet.p2p.config.NetworkConfig)1 Router (me.matoosh.undernet.p2p.router.Router)1 NetworkMessage (me.matoosh.undernet.p2p.router.data.message.NetworkMessage)1