views:

219

answers:

3

I am working on a JAVA project in which there are multiple terminals. These terminals act as client and servers. For example if there are 3 terminals A,B and C.Then at any given point in time one of them say A, will be a client making broadcast request. The other two terminals, B and C, will reply. I am using sockets to make them communicate. Once the reply is received from all the other terminals A will check the pool of channels to see if any one of the channel is free. It takes up the free channel and making it availabilty false.

The channelpool is implemented using HashMAp:

HashMap channelpool = new HashMap();

channelpool = 1=true, 2=true, 3=false, 4=true, 5=true, 6=true, 7=true, 8=true, 9=true, 10=true

So initially all the channels are true, any terminal can take any channel. But once the channel is taken it is set to false for the period of use and then reset to true.

Now this Hashmap has to be shared among the distributed terminals. Also it should be kept up to date. I can not used a shared resource among the terminals to store the HashMap.Can someone tell me an easy way to transfer the HashMap between the terminals. I will appreciate if someone could point me to a website which discusses this.

+2  A: 

Your problems start a lot earlier than with sending a HashMap over a Socket. How do you ensure only one process at a time modifies the map? If two processes modify the map, how do you consolidate changes in the same way for each process?

But to answer your question: Serializing the HashMap using java.io.ObjectOutputStream, and then reconstructing it on the other side using ObjectInputStream, should do the trick.

meriton
+1  A: 

Exactly. Transferring the HashMap around is subject to the same concurrency problems that you are trying to solve with it, so it is not the solution. Rather, it is part of the problem, and an unnecessary part. You need a central server to manage the allocations for you, which you could implement via RMI, or more probably a database rather than a HashMap.

EJP
Thank you Meriton and EJP for your answers. I get that transferring the hashmap is an easy task. I am using the java.io.ObjetOutputStream as suggested. As you pointed out, how will I ensure that only one process changes the HashMap at a time.? I am new to Distributed programming. I dont know how to use the JAVA RMI and all. Is using RMI the only solution or are there any easier solution too?
sar
RMI is the easiest solution I can think of.
EJP
A: 

No need for RMI or object serialization. Your server(s) should have Hazelcast distributed map. Your client JVMs should use Hazelcast Client to access the shared distributed map. Clients can listen for the distributed map events.

Hazelcast is an open source (Apache License), transactional, distributed implementation of map, multimap, queue, topic, lock and executor service for Java.

Talip Ozturk