tags:

views:

103

answers:

3

Hi,

I´m relative new on remoting (2.0 C#). Is there any/someway to lock the server side object/instance to one client?

I have up to 10 clients that will connect to the server. The server will offer 3 different task/operations/classes and if one client does a request and if the server is not working on that, I´ll like to lock this operation to that client. The reason for this is that the requests works with HW that only can handle on task at the time. Hope you understand what I like too do.

EDIT:

I´ll try to explain my problem again...

I have 3 classes that will have X number of methods/operations (operations that will trigger a external hardware to do some measuring). When a client "connects" to one class (at the time) and request a measuring to be performed I want to lock that class to the client, hence, the client will own this class and it shall be able to execute all methods. No other client shall be able/allowed to access this class while the first client has control. The other tow classes should be open for requests from other clients, but the same principle/rules shall apply to these classes. As soon as a client request a lock it shall have it as long as it requires it. I´ll will have an intreface that all clients must follow. Call a method called Lock() to require the control over the class and Unlock() to release the control. I/We will develop all the clients and the server!

Thanks for all the help, so far!

Regards

/Anders

A: 

You have to lock the task by using semaphores in order to ensure only one thread at a time. Look into the Semaphore and Mutex classes.


Edit: You can do many ways from locking to complex semaphores, here you have two samples:

This one only locks to ensure that one execution is being done at a time:

private static object lockObject=new object();
public void Test()
{
  lock (lockObject)
  {
    //your code here
  }
}

This one uses a Mutex to wait until it is released, but with a timeout that will return with some information to the client indicating that the method could not be executed.

private static Mutex mutex = new Mutex();
public bool Test2()
{
   if (!mutex.WaitOne(500))
   {
     return false;
   }
   try
   {
     //your code here
   }
   finally
   {
     mutex.ReleaseMutex();
   }
   return true;
}
jmservera
Shall i use WellKnownObjectMode.SingleCall or WellKnownObjectMode.Singleton with this solution?
anra
It looks like your object will be a Singleton. In this case you can use also a static counter and a lock on your method (look the Interlocked methods too).
jmservera
hi, thanks for the examples...but I don´t seems to get them to work the way I want too...
anra
A: 

Ok, now I see the point.

You can use the CAO approach instead: create a factory (can be a singleton) that gives you a CAO (Client Activated Object) if nobody else owns an instance. CAO is good for that because it will ensure that if the client dies the CAO would be released.

Explaining a CAO is too much for a simple answer, it is something like this: CAO is a class inherited from MarshalByRefObject that you will create from your factory and return the instance from one method (i.e.: your Lock method); the object lives in the server and the client receives only a proxy. The object will live into the server while it's lease is being refreshed by the client (done automatically while the object is referenced and client are alive).

You may take a look to the Ingo Rammer's articles and books on remoting.

jmservera
I have read a little about CAO before, and if I have understood correctly, all remoting code needs to be shared between the server and client, hence it limits the scalability and gives you tight coupling. Thanks for you suggestion and help so far, but I think I have to continue my search. If you do run in to a nice solution I would be glad to hear about it. Again, thanks for all your help!
anra
You don't need to share the code, just use an Interface to define your CAO's methods and properties and share the dll with the interface, it is the same than for SAO objects.
jmservera
A: 

Hi,

jmservera, thanks for all your help.

I have now found a solution that will work for me...I´m using the proxy pattern combined with the factory pattern. I do use the WellKnownObjectMode.Singleton method so I can control how many active instances I have on my server.

And by doing it this way, i don´t need to share my code with the client, only the interface (as you said before).

Regards

/Anders

anra
Nice to know it worked!Cheers!
jmservera