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;
}
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);
}
}
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));
}
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));
}
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));
}
Aggregations