Archived

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

client creating objects

Hello,
I am a CORBA newbie. Firstly i transited to Ice recently after some frustrations with other open source CORBA implementations. I could get basic \demo\Ice\Hello\ examples working well.
So, referring to "demo\Ice\Hello" example, it seems like the client can only access an existing object at the server by getting a proxyString. I extended this so that this server object can create more objects and return strings to the client on the same adapter.
My question is, is this how the client can create new objects or is there a known way that most experts use. Once again, the question is " how does the client create new objects at the server dynamically without referring to it in the configuration file".

thanks
bks

Comments

  • Use the factory pattern. Make an object that has a create method and that returns a proxy to the newly created object. The operation implementation instantiates a new servant, adds it to the ASM, and returns the proxy. In Slice, something like
    interface Widget {
        // ...
    };
    
    interface WidgetFactory {
        Widget* create(/* params here */);
    };
    
    The create implementation would do something like
    WidgetPrx
    WidgetFactoryI::create(/* ... */, const Ice::Current& c)
    {
        WidgetPtr p = new WidgetI(/* */);
        return WidgetPrx::uncheckedCast(c.adapter.addWithUUID(p));
    }
    
    The client simply calls create() to create a new object and receive its proxy.

    Cheers,

    Michi.
  • Michi,
    thanks that helped. As an extension to the above discussion, what is the appropriate way for the client to destroy this object.
    I am thinking, for every create method, a destroy method that passes the Proxy as a parameter. In the implementation do proxy->ice_getIdentity() and then remove(identity) from the ASM. I beleive once the object is gone from the ASM it is subject to garbage collection, rite? Am I mising somethign here?

    thanks
    bks
  • bks wrote:
    I am thinking, for every create method, a destroy method that passes the Proxy as a parameter. In the implementation do proxy->ice_getIdentity() and then remove(identity) from the ASM.

    I would advise against doing it this way. If you put a destroy() method on the factory, then, to destroy an object, you must know which factory created it. But, in a complex system with lots of factories and objects, that can rapidly become complex. A better option is to put the destroy() method on the object itself. That way, if you have created an object and passed it around to various other parties, anyone can destroy() the object by asking it to commit suicide:
    interface Widget {
        // Operations here...
        void destroy(); // Depending on the application, you might have user exception here too...
    };
    
    bks wrote:
    I beleive once the object is gone from the ASM it is subject to garbage collection, rite?
    It depends on whether your application code still holds a smart pointer to the servant or not. If it doesn't, then the entry in the ASM is the last smart pointer to the servant and, when you remove the servant's registration from the ASM, the Ice runtime decrements the ref count, which causes the servant to be deleted.

    Cheers,

    Michi.