So I am new to Java, I have done a bit of c programming.
I am trying to make a virtual network of nodes, each node would need to be a thread.
The nodes are only allowed to talk to their neighbor nodes.
there will be a master node that can talk to any node but the nodes would have to talk to each other to get back to the master node. the master nodes neighbors can talk to the master node.
I was originally going to keep an array list of the nodes, but then I realized all the nodes needed to be there own thread.
My question is how do I pass information back in forward between threads in Java.
I need to be able to have the master node give all the regular nodes position information.
and I need the regular nodes to be able to pass messages to their neighbor regular nodes.?
here are my git repos if you would like to look at the code I got going now.
https://github.com/fieldju/cs372_project
in C I made a program that used pipes for the children to talk to each other and a server connected the clients, but in this problem the nodes to to have p2p communication as most of them can not directly communicate to the master node / server
Just an Update for anyone who looks at this and wants to see the results. I got the nodes up and running and communicating you can check out the code at
https://github.com/fieldju/cs372_project
I am still working on the distance vector stuff and a few other things but by the end of next week the entire thing should be done.
You can keep an array of threads, it would still maintain a thread-per-node with the same logic structure.
If threads reside in the same process then definitely sockets are an overkill. I would use one or several ConcurrentLinkedQueue instances to push/pop messages.
One or several really depends on the kind of communication that you are implementing. Maybe one ConcurrentLinkedQueue per node, so nodes push messages to queues and every node knows where from to pop the message.
Few hints for implementation
Wrap up all the logic to en-route messages in a class – let’s call this class
VirtualNetwork.VirtualNetworkdeals with all the instances of ConcurrentLinkedQueue and offers an API of methods to all threads for sending/receiving messages. Make one instance of the classVirtualNetworkaccessible to all nodes – by passing a reference to it on the Thread constructor.This is an sketch of how your class
NodeThreadwould be. Notice that the classesVirtualNetworkandMessageare classes that you have to implement your self.