Archived

This forum has been archived. Please start a new discussion on GitHub.

How to work the same with Class that is passed by value and by reference

For example I have this slice definition:
module Zk{
        interface Op {
                string getA();
        };

        class ExampleClass implements Op {
                string a;
        };

        interface Worker {
                void setExampleByReference(ExampleClass* cl);
                void setExampleByValue(ExampleClass cl);

                string doSomething();
        };
};

I would like to work in implementation of Worker with operation getA of class ExampleClass which can be send to me by value or by reference - my problem is that I dont know how to do it "transparently", something like (I know that it does not work):
public class WorkerI : _WorkerDisp {
  ExampleClassPrx _cl;
  public void setExampleByReference(ExampleClassPrx cl)
  {
    _cl=cl;
  }

  public void setExampleByValue(ExampleClass cl)
  {
    _cl=retypeToProxy(cl);
  }
  
  public string doSomething()
  {
    return cl.getA() + "aaaa";
  }
}

Comments

  • You can invoke on a class instance that is registered as a servant either locally or remotely. To invoke on it locally, you invoke via the Ptr to the class. To invoke on it remotely, you create a proxy to the class and and pass it to another address space; invocations via that Prx then go remote.

    There is (intentionally) no way to do this "transparently". When you invoke on the class, you must make a decision as to which copy (local or remote) the invocation is to be sent to.

    Note, however, that you can invoke on collocated servant via its proxy. That is, if you pass a Prx to a servant around and invoke via that, the invocations will always go to that servant, whether they originate locally or remotely.
    But, if you have passed a class by value out of your address space, you end up with two copies of the class, one local and one remote. In that case, you must decide to which copy of the class you want an invocation to go to.

    Cheers,

    Michi.
  • Thank you!

    Thank you!!!

    So, if I understand, this works?:

    (C#)
    public class WorkerI : _WorkerDisp {
      ExampleClassPrx _cl;
      Ice.ObjectAdapter _adapter;
    
      public WorkerI(Ice.ObjectAdapter adapter)
      {
        _adapter = adapter;
      }
    
      public void setExampleByReference(ExampleClassPrx cl)
      {
        _cl = cl;
      }
    
      public void setExampleByValue(ExampleClass cl)
      {
        _cl = ExampleClassPrxHelper.uncheckedCast( _adapter.addWithUUID(cl) );
      }
      
      public string doSomething()
      {
        return "This is result of getA:" + _cl.getA();
      }
    }
    

    thanks,
    Michal
  • benoit
    benoit Rennes, France
    Yes, this should work. Note that you will also have to take care of removing the servant from the object adapter. I also recommend you to take a look at the Ice manual for more information on collocation optimization.

    Benoit.
  • Thanks

    Thank you,
    great support here,

    Michal