Glacier2 - SessionNotExistException in session() after connected by factory

KevinHKevinH Member Kevin HitzlerOrganization: PersonalProject: Android Application

Good afternoon!

I am trying to implement a simple java client that communicates with a server via Glacier2+IceStorm and allows the server and the client to send messages to each other. The communication over the Glacier2 and the IceStorm service works well. I am already able to send messages from the client to the server by subscribing to topics. But the server is not able to send messages back. For that, I would like to use callbacks - like in the Chat Demo: https://doc.zeroc.com/display/Doc/Slice+Interfaces.

My problem is that the _session.session() method raises an Glacier2.SessionNotExistException. I don't know where the problem is, because the _session object itself returns a correct communicator() with which I am subscribing to my topics. The _session.isConnected() returns true.

Here are a few lines of code:

` public static String SERVER_IP = "192.168.1.172";
public static String SERVER_PORT = "4063";
public static String ICESTORM_IP = "192.168.1.172";
public static String ICESTORM_PORT = "80";

private Ice.Communicator _communicator;
private Glacier2.SessionHelper _session;
private Glacier2.SessionFactoryHelper _factory;
private Ice.InitializationData _initData;
private ChatSessionPrx _chat;

public Client()
{   
}

public void initialize(String[] args)
{
    // set initial properties (for Glacier2 and debugging)
    Ice.StringSeqHolder argsH = new Ice.StringSeqHolder(args);
    Ice.Properties properties = Ice.Util.createProperties(argsH);
    properties.setProperty("Ice.Default.Router", "Glacier2/router:tcp -h "+SERVER_IP+" -p "+SERVER_PORT);
    properties.setProperty("Ice.RetryIntervals", "-1"); 
    properties.setProperty("Ice.Trace.Network", "2");
    _initData = new Ice.InitializationData();
    _initData.properties = properties;

    // add SessionFactoryHelper for communicator intialization   
    _factory = new Glacier2.SessionFactoryHelper(_initData, new Glacier2.SessionCallback()
    {
        @Override
        public void connected(Glacier2.SessionHelper session)
            throws Glacier2.SessionNotExistException
        {
            // If the session has been reassigned avoid the spurious callback.
            if(session != _session)
            {
                System.out.println("Warning: Spurious callback. Exit.");
                return;
            }

            System.out.println("Connected to Glacier2 Service.");
            setAllCallbacks();
            //registerTopics();
        }

        @Override
        public void disconnected(Glacier2.SessionHelper session)
        {
            System.out.println("Disconnected from Glacier2 Service.");
        }

        @Override
        public void connectFailed(Glacier2.SessionHelper session, Throwable exception)
        {
            try
            {
                throw exception;
            }
            catch(final Glacier2.CannotCreateSessionException ex)
            {
                System.out.println("Login failed (Glacier2.CannotCreateSessionException):\n" + ex.reason);
            }
            catch(final Glacier2.PermissionDeniedException ex)
            {
                System.out.println("Login failed (Glacier2.PermissionDeniedException):\n" + ex.reason);
            }
            catch(Ice.Exception ex)
            {
                System.out.println("Login failed (" + ex.ice_name() + ").\n" +
                        "Please check your configuration.");
            }
            catch(Throwable ex)
            {
                ex.printStackTrace();
            }
        }

        @Override
        public void createdCommunicator(Glacier2.SessionHelper session)
        {
            //test(session.communicator() != null);
            if(session.communicator() != null)
            {
                System.out.println("Created communicater by session.");
            }
        }
    });

    // Connect to session
    System.out.println("Connection to Session...");
    _factory.setRouterHost(SERVER_IP);
    _session = _factory.connect("", "");  
}

private void setAllCallbacks()
{       
    try 
    {       
        System.out.print("Setting all Callbacks...\n");

        //variant 1
        /*
        ChatRoomCallback servant =  new ChatRoomCallbackI();
        ChatRoomCallbackPrx callback = ChatRoomCallbackPrxHelper.uncheckedCast(_session.addWithUUID(servant));
        _chat = ChatSessionPrxHelper.uncheckedCast(_session.session());
        _chat.setCallback(callback);*/

        //variant 2
        _chat = ChatSessionPrxHelper.uncheckedCast(_session.session());
        ChatRoomCallbackPrx callback = ChatRoomCallbackPrxHelper.uncheckedCast(_session.addWithUUID(new ChatRoomCallbackI()));
        _chat.begin_setCallback(callback);

        System.out.print("OK!\n");
    } 
    catch (SessionNotExistException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}`

The actual output looks like this:

Connection to Session... Created communicater by session. -- 26.05.17 14:27:33:383 Network: trying to establish tcp connection to 192.168.1.172:4063 -- 26.05.17 14:27:33:398 Network: established tcp connection local address = 192.168.1.188:14056 remote address = 192.168.1.172:4063 Connected to Glacier2 Service. Setting all Callbacks... Glacier2.SessionNotExistException at Glacier2.SessionHelper.session(SessionHelper.java:156) at Client.setAllCallbacks(Client.java:139) at Client.access$1(Client.java:124) at Client$1.connected(Client.java:71) at Glacier2.SessionHelper$8.run(SessionHelper.java:379) at Glacier2.SessionHelper.dispatchCallback(SessionHelper.java:590) at Glacier2.SessionHelper.connected(SessionHelper.java:372) at Glacier2.SessionHelper.access$600(SessionHelper.java:15) at Glacier2.SessionHelper$11.run(SessionHelper.java:556) at java.lang.Thread.run(Unknown Source)

Maybe you can help me on that?

Thank you very much in advance.

Best,
Kevin

Comments

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

    Sound like you have not configure a Glacier2::SessionManager, in this case the session will be nil and the call to session() will raise SessionNotExistException

    The Chat Demo includes a sample SessionManager implementation you should also review the Session Management documentation

  • KevinHKevinH Member Kevin HitzlerOrganization: PersonalProject: Android Application

    Hi, thank you for your fast response.

    I thought the Glacier2::SessionManager is only necessary when you are using the Glacier::Application helper class to initiate a session. Right now, I am trying to implement it with the SessionFactoryHelper in order to run it in a GUI application.

    https://doc.zeroc.com/display/Ice36/Glacier2+Helper+Classes

    Did I understand it wrong? Is there an example how to do it with the SessionFacetoryHelper class?

    I reviewed the code with the following example: https://github.com/zeroc-ice/ice-demos/blob/master/java/Chat/src/main/java/ChatDemoGUI/Coordinator.java

    Thank you very much.

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

    Glacier2::SessionManager is implemented in the Server side, if you configure glacier2router to use a SessionManager it will be resposible to create the users sessions and then session helper session method will return a proxy that reference the session object.

    see:
    ChatDemo session manager implementation and how it is configured in config.glacier2router

  • KevinHKevinH Member Kevin HitzlerOrganization: PersonalProject: Android Application

    Wow, now I got it. Thank you so much. It works!

Sign In or Register to comment.