Java communication crosslink

So, I'm a beginner in Java, I programmed a little c. I'm trying to create a virtual network of nodes, each node must be flush. Nodes are allowed to talk only with their neighboring nodes. there will be a master node, which can talk to any node, but nodes have to talk to each other, to get back to the master node. Neighbors main nodes can talk to the master node.

I originally intended to keep a list of node arrays, but then I realized that all nodes must have their own stream.

My question is how to pass the information back between threads in Java. I need to master node has given all the information on the location of conventional units. and I need to ordinary nodes to send messages to its neighbor nodes regularly.

here are my git repository if you want to look at the code that I got right now.

https://github.com/fieldju/cs372_project

CI has been made in the program, which used channels for children to talk to each other, and connected the server clients, but this task nodes for a p2p connection, because most of them can not communicate directly with the master node / server


Just an update for those who look at it and want to see results. I launched the nodes and launched them, and you can check the code on

https://github.com/fieldju/cs372_project

I'm still working on things in the distance and a few other things, but by the end of next week everything should be done.

+4
source share
3 answers

I was originally going to list arrays of nodes, but then I realized that all the nodes have to be there own thread.

You can store an array of flows, it will still maintain thread-per- node with the same logical structure.

how do I pass information back forward between threads in Java.

If the flows are in the same process, the sockets are definitely overkill. I would use one or more copies ConcurrentLinkedQueue input / off messages.

One or more really depends on what kind of communication you are doing. Maybe one ConcurrentLinkedQueue per node, so the nodes forward messages in the queue, and each node knows where the message comes from.

A few implementation tips

Complete all of the logic for routing messages in the class - let this class VirtualNetwork . VirtualNetwork deals with all instances ConcurrentLinkedQueue and offers API methods for all streams to send / receive messages. Make one copy of the class VirtualNetwork accessible to all nodes - passing reference to it in the Thread constructor.

This is a sketch of how it will look your class NodeThread . Please note that classes VirtualNetwork and Message - classes that you need to implement your own.

 class NodeThread extends Thread { private int nodeId; private VirtualNetwork network; public NodeThread(int nodeId,VirtualNetwork network) { this.network = network; this.nodeId = nodeId; } public void run() { /* when you have to send */ int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */ Message m = new Message(this.nodeId,nodeReceptor); m.setContent(10); network.send(m); /* when you have to receive */ Message m = network.receive(this.nodeId); /* it your decision to implement this in a blocking way or not */ } } ) { class NodeThread extends Thread { private int nodeId; private VirtualNetwork network; public NodeThread(int nodeId,VirtualNetwork network) { this.network = network; this.nodeId = nodeId; } public void run() { /* when you have to send */ int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */ Message m = new Message(this.nodeId,nodeReceptor); m.setContent(10); network.send(m); /* when you have to receive */ Message m = network.receive(this.nodeId); /* it your decision to implement this in a blocking way or not */ } } * / class NodeThread extends Thread { private int nodeId; private VirtualNetwork network; public NodeThread(int nodeId,VirtualNetwork network) { this.network = network; this.nodeId = nodeId; } public void run() { /* when you have to send */ int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */ Message m = new Message(this.nodeId,nodeReceptor); m.setContent(10); network.send(m); /* when you have to receive */ Message m = network.receive(this.nodeId); /* it your decision to implement this in a blocking way or not */ } } nodeReceptor); class NodeThread extends Thread { private int nodeId; private VirtualNetwork network; public NodeThread(int nodeId,VirtualNetwork network) { this.network = network; this.nodeId = nodeId; } public void run() { /* when you have to send */ int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */ Message m = new Message(this.nodeId,nodeReceptor); m.setContent(10); network.send(m); /* when you have to receive */ Message m = network.receive(this.nodeId); /* it your decision to implement this in a blocking way or not */ } } / class NodeThread extends Thread { private int nodeId; private VirtualNetwork network; public NodeThread(int nodeId,VirtualNetwork network) { this.network = network; this.nodeId = nodeId; } public void run() { /* when you have to send */ int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */ Message m = new Message(this.nodeId,nodeReceptor); m.setContent(10); network.send(m); /* when you have to receive */ Message m = network.receive(this.nodeId); /* it your decision to implement this in a blocking way or not */ } } in a blocking way or not * / class NodeThread extends Thread { private int nodeId; private VirtualNetwork network; public NodeThread(int nodeId,VirtualNetwork network) { this.network = network; this.nodeId = nodeId; } public void run() { /* when you have to send */ int nodeReceptor = this.nodeId -1; /* neighbor in the array of threads */ Message m = new Message(this.nodeId,nodeReceptor); m.setContent(10); network.send(m); /* when you have to receive */ Message m = network.receive(this.nodeId); /* it your decision to implement this in a blocking way or not */ } } 
+4
source

Easy (if not the best) way to start - create BlockingQueue for each pair of streams that need to pass values in the same direction (if it is bi-directional, you need twice as much.)

+1
source

You can definitely use the Sockets . Here are some guidelines / descriptions here and here . Your description of the project suggests that the setting is a client / server will work fine, since you have a central "node master." The wizard will be the server, and other threads will be able to connect to the main / server to receive their updates.

0
source

All Articles