Jini : single server with multiple clients
- by user200340
Hi all,
I have a question about how to make multiple clients can access a single file located on server side and keep the file consistent.
I have a simple PhoneBook server-client Jini program running at the moment, and server only provides some getter functions to clients, such as getName(String number), getNumber(String name) from a PhoneBook class(serializable), phonebook data are stored in a text file (phonebook.txt) at the moment. I have tried to implement some functions allowing to write a new records into the phonebook.txt file. If the writing record (name) is existing, an integer number will be added into the writing record.
for example the existing phonebook.txt is
....
John 01-01010101
....
if the writing record is "John 01-12345678",then "John_1 01-12345678" will be writen into phonebook.txt
However, if i start with two clients A and B (on the same machine using localhost), and A tries to write "John 01-11111111", B tries to write "John 01-22222222".
The early record will be overwritten later record.
So, there must be something i did complete wrong.
My client and server code are just like Jini HelloWorld example.
My server side code is .
1. LookupDiscovery with parameter new String[]{""};
2. DiscoveryListener for LookupDiscovery
3. registrations are saved into a HashTable
4. for every discovered lookup service, i use registrar to register the ServiceItem, ServiceItem contains a null attributeSets, a null serviceId, and a service.
The client code has:
1. LookupDiscovery with parameter new String[]{""};
2. DiscoveryListener for LookupDiscovery
3. a ServiceTemplate with null attributeSets, a null serviceId and a type, the type is the interface class.
4. for each found ServiceRegistrar, if it can find the looking for ServiceTemplate, the returned Object is cast into the type of the interface class.
I have tried to google more details, and i found JavaSpace could be the one i missed. But i am still not sure about it (i only start Jini for a very short time). So any help would be greatly appreciated.