my dog learned polymorphism*
The moose likes Distributed Java and the fly likes how to limit RMI clients? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "how to limit RMI clients?" Watch "how to limit RMI clients?" New topic
Author

how to limit RMI clients?

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Hi all.
We are writing code which includes control of a micro-positioner system via Java Native Interface. Because there is only one physical positioner, we would like to limit clients to one at a time.
We used to do this through sockets. The server kept track of whether or not there was currently a user. If there was a current user and additional users tried to log on, they received a "Server busy" reply and disconnected themselves.
What is the best way to achieve this behavior using RMI?
Thanks.
Stephanie
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
Hi,
You could have a static variable in the RMI server class that keeps track of the number of clients. Thus, when the client logs on / invokes a particular method, just increment the variable. In that method, check if the variable is incremented. If it is incremented, return false/ Throw an exception. Else, allow access.

Note that the sync block is better because the "clients++" statement may/may not be atomic.
Another way of doing this without Throwing an exception and have the client wait is by synchronizing all the methods of the class. Thus, effectively all requests are serialized and clients
will be served one at a time.
Hope this helps.
Ashwin.
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Ashwin,
Thank you for your detailed response!
Unfortunately, I don't think I explained the problem clearly enough. It is not a matter of clients taking turns. We would like the server to refuse to connect more than one client at a time. Here is why:
Suppose a person logs on and moves the positioner to the right 1000 steps. Now another person moves the positioner left 1000 steps. They have effectively canceled out the command of the first person.
What we really want is for the server to allow one and only one person to log in and process commands until they are completely done. At that time, they should log out and the next user should be able to log in.
While I appreciate your thoughtful response, I am a little afraid that it might not prevent the scenario I described above. If you have any other ideas or need more information, please let me know.
Thank you.
Stephanie
Ashwin Desai
Ranch Hand

Joined: Jul 17, 2000
Posts: 124
Hi Stephanie,
The first method should work.
Let us assume that your class has methods - login() and logoff()
login() allows a client to access other methods/functionality.
logoff() allows another client to logon.

Thus, the "clients" variable is the control variable.
If clients == 0, A client can logon and set that control var. to 1.
If another client tries to logon during this time, he gets an exception.
When the client logs off by using logoff(), "clients -- " makes clients = 0 & thus, a different client can log on now.
With this approach, I have assumed that the client will log off using logoff() method when done with the processing. If logoff() is not called, then clients would be 1 always and no one will be able to log on to the server.
Ashwin.
Suneel Setlur
Ranch Hand

Joined: Nov 26, 2000
Posts: 202
Hi Stephanie
You could use the singleton pattern for designing an application like that. This can be achieved by i) using a static method to issue and keep track of instances. We make constructor private so that the instance can be created from within the static method of the class.

One major advantage to this approach is that you don�t have to worry
about exception handling if the singleton already exists-- you simply get a
null return from the Instance method:

And, should you try to create instances of the iSpooler class directly,
this will fail at compile time because the constructor has been declared as
private.

The Second method is to throw an exception when the class is instantiated more than once.
Suneel


Suneel
Suneel Setlur
Ranch Hand

Joined: Nov 26, 2000
Posts: 202
You can check out design patterns here
[This message has been edited by Suneel Setlur (edited February 27, 2001).]
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Thanks Ashwin and Suneel for your suggestions!
Stephanie
Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
Suneel,
Thanks for your reply demonstrating the use of Singleton class. I had solicited info on singletons long back and the replies helped me getting a clear idea of such classes. However,I was waiting to see a practical solution using singletons. This is just that!!
Thanks to Stephanie too for her initial post.
-Rajendra.
Rajendra Deshpande
Ranch Hand

Joined: Nov 24, 2000
Posts: 40
Ashwin / Suneel / Stephanie,
I have found RMI to be very interesting and would like to do a small project incorporating most of the facets of RMI. However, I am short of ideas and request you for some useful suggestions/tips.
thanks,
-Rajendra.
 
Don't get me started about those stupid light bulbs.
 
subject: how to limit RMI clients?
 
Similar Threads
Error on RMI
FBN : StandAlone mode
DGC Question
JMS or RMI - CallBack
Help on lock and unlock methods design