• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to limit RMI clients?

 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Setlur
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check out design patterns here
[This message has been edited by Suneel Setlur (edited February 27, 2001).]
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ashwin and Suneel for your suggestions!
Stephanie
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic