5

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.

fieldju
  • 1,443
  • 1
  • 14
  • 20
  • Without a little bit more detail about what the network is supposed to do, its topology, and the intent and contents of the messages, I suspect the answers to this question will be too broad. – Dilum Ranatunga May 25 '11 at 18:51
  • About how many nodes You'd like to have? – Rekin May 25 '11 at 18:57
  • https://github.com/fieldju/cs372_project for the full read me but the sample input file has 10 nodes and a master node on a 1000 * 1000 grid and you use the distance forumla to calculate neighbors – fieldju May 25 '11 at 19:10
  • Right, so a node could be a thread with a P-C input queue, maybee with a timeout on the wait so that it could occasionally generate some 'sensor data' itself to send back to the master. The node would initially 'broadcast at maximum power' in an attempt to get its position from the master node, maybee it sends in some ID so the master knows which one it is? The master may get the registration message by more than one route - perhaps the reg. message should count hops and store the cumulative power used in the hops, so enabling the master to inform the sub-node of its cheapest route. – Martin James May 25 '11 at 19:58

3 Answers3

4

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.

You can keep an array of threads, it would still maintain a thread-per-node with the same logic structure.

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

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. VirtualNetwork deals with all the instances of ConcurrentLinkedQueue and offers an API of methods to all threads for sending/receiving messages. Make one instance of the class VirtualNetwork accessible to all nodes - by passing a reference to it on the Thread constructor.

This is an sketch of how your class NodeThread would be. Notice that the classes VirtualNetwork and Message are classes that you have to implement your self.

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's your decision to implement this in a blocking way or not */
    }
} 
Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56
  • i'm liking the sound of this, my main question with this approach is how would I push a message to a specific node? hopefully some reading will shed some light. – fieldju May 25 '11 at 19:04
  • your going to have to forgive my Java retardation as I am a c programmer. if i have an arraylist/array of threads and i push it to all of the nodes. is it just an array of pointers to the threads in memory, like in c. – fieldju May 25 '11 at 19:08
  • @fieldju You do it by posting a message to the queue that is associated with that thread (you can see the queue as an inbox), where each node/thread might have a separate queue. – Kaj May 25 '11 at 19:10
  • @fieldju. The array contains references to threads in the scenario described. A reference is a bit like a pointer. – Kaj May 25 '11 at 19:11
  • Forgive me if I make confusion worse, but what works for me to think of it, is absolutely everything (non-primitive) in java is a pointer and it's automatically dereferenced whenever you use it. You can't work with them (pointers) directly because it's just assumed you always use them the same way. So yes if you have a List myList you can do Thread thread = myList.get(i) and invoke methods on it and it's automatically dereferenced – Affe May 25 '11 at 19:16
  • @fieldju see the new edition of the question with some hints for a potential design. I hope it helps. – Manuel Salvadores May 25 '11 at 19:18
  • can you treat a thread like an object once you dereference it, and evoke a method that takes arguments and returns results? – fieldju May 25 '11 at 19:18
  • @fieldju yes. Threads in are objects and they take arguments mostly via the constructor or set methods. The `run` method doesn't allow parameters. – Manuel Salvadores May 25 '11 at 19:21
  • Thanks for all the help, I think i can hash this out now. – fieldju May 25 '11 at 19:22
  • 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 – fieldju May 27 '11 at 18:16
1

An easy (if not optimal) way to start is to create a BlockingQueue for each pair of threads that need to pass values in one direction (if it's bidirectional you need twice as many.)

finnw
  • 47,861
  • 24
  • 143
  • 221
0

You could definitely use Sockets. There are a couple of tutorials/descriptions here and here. Your description of the project suggests a client/server setup would work well since you have a central "master node". The master will be the server and the other threads will be able to connect to the master/server to receive their updates.

Haphazard
  • 10,900
  • 6
  • 43
  • 55