Home Help Center

Communicator from an object ?

flatmaxflatmax Member Matt FlaxOrganization: Sole TraderProject: Comms with embedded devices
Hi there,

Is it possible to access the communicator from the interface class ?

I have a class which inherits an interface :

class MyClassI : public MyClass {

void myMethod(void);

};

myMethod is not in the interface (it is custom/local to MyClassI).
I would like access to the communicator in myMethod(); so that I can create an adapter and so on.

This may not be the correct paradigm of operation, however I was going to test some ideas out.

The only way I can work out how to get access to the communicator is to manually store it in MyClassI when I am in the Ice::Application::run method :

Something like so :
Ice::Application::run(argc, argv){
MyClassI::storeCommunicator(CommunicatorPtr c){
communicator=c;
}
}

Is there a better way to get access to the communicator in the method MyClass::myMethod ?

thanks
Matt

Comments

  • xdmxdm La Coruña, SpainAdministrators, ZeroC Staff Jose Gutierrez de la ConchaOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    Hi Matt,

    You can access the communicator from a servant operation throw the Current object
    // Slice
    module Demo
    {
    
    interface Hello
    {
        void sayHello();
    };
    
    };
    
    // C++
    
    class HelloI : public Hello
    {
    public:
    
        void sayHello(const Ice::Current& c)
        {
            Ice::CommunicatorPtr ic = c.adapter->getCommunicator();
        }
    };
    

    Or you can store a reference to the communicator when you create your servant.
    class HelloI : public Hello
    {
    public:
    
        HelloI(const Ice::CommunicatorPtr& ic) :
            _ic(ic)
        {
        }
    
    private:
    
        Ice::CommunicatorPtr _ic;
    };
    
    class Server : public Ice::Application
    {
    public:
    
        virtual int run(int, char*[])
        {
            // ...
            adapter->add(new HelloI(communicator()), communicator()->stringToIdentity("hello"));
            // ...
        }
    };
    
Sign In or Register to comment.