Exemplo n.º 1
0
        public override int run(string[] args)
        {
            String protocol = communicator().getProperties().getPropertyWithDefault("Ice.Default.Protocol", "tcp");
            String host     = communicator().getProperties().getPropertyWithDefault("Ice.Default.Host", "127.0.0.1");

            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback1());

            //
            // Test to create a session with wrong userid/password
            //
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
                Console.Out.Flush();

                _factory.setProtocol(protocol);
                _session = _factory.connect("userid", "xxx");
                while (true)
                {
#if COMPACT
                    System.Threading.Monitor.Wait(this);
                    break;
#else
                    try
                    {
                        System.Threading.Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }
            }

            _initData.properties.setProperty("Ice.Default.Router", "");
            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback4());
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect interrupt... ");
                Console.Out.Flush();
                _factory.setRouterHost(host);
                _factory.setPort(12011);
                _factory.setProtocol(protocol);
                _session = _factory.connect("userid", "abc123");

                System.Threading.Thread.Sleep(100);
                _session.destroy();

                while (true)
                {
#if COMPACT
                    System.Threading.Monitor.Wait(this);
                    break;
#else
                    try
                    {
                        System.Threading.Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }
            }

            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback2());
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect... ");
                Console.Out.Flush();
                _factory.setRouterHost(host);
                _factory.setPort(12347);
                _factory.setProtocol(protocol);
                _session = _factory.connect("userid", "abc123");
                while (true)
                {
#if COMPACT
                    System.Threading.Monitor.Wait(this);
                    break;
#else
                    try
                    {
                        System.Threading.Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }

                Console.Out.Write("testing SessionHelper isConnected after connect... ");
                Console.Out.Flush();
                test(_session.isConnected());
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper categoryForClient after connect... ");
                Console.Out.Flush();
                try
                {
                    test(!_session.categoryForClient().Equals(""));
                }
                catch (Glacier2.SessionNotExistException)
                {
                    test(false);
                }
                Console.Out.WriteLine("ok");

                //             try
                //             {
                //                 test(_session.session() != null);
                //             }
                //             catch(Glacier2.SessionNotExistException ex)
                //             {
                //                 test(false);
                //             }

                Console.Out.Write("testing stringToProxy for server object... ");
                Console.Out.Flush();
                Ice.ObjectPrx @base = _session.communicator().stringToProxy("callback:default -p 12010");
                Console.Out.WriteLine("ok");

                Console.Out.Write("pinging server after session creation... ");
                Console.Out.Flush();
                @base.ice_ping();
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing checked cast for server object... ");
                Console.Out.Flush();
                CallbackPrx twoway = CallbackPrxHelper.checkedCast(@base);
                test(twoway != null);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing server shutdown... ");
                Console.Out.Flush();
                twoway.shutdown();
                Console.Out.WriteLine("ok");

                test(_session.communicator() != null);
                Console.Out.Write("testing SessionHelper destroy... ");
                Console.Out.Flush();
                _session.destroy();
                while (true)
                {
#if COMPACT
                    System.Threading.Monitor.Wait(this);
                    break;
#else
                    try
                    {
                        System.Threading.Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }

                Console.Out.Write("testing SessionHelper isConnected after destroy... ");
                Console.Out.Flush();
                test(_session.isConnected() == false);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper categoryForClient after destroy... ");
                Console.Out.Flush();
                try
                {
                    test(!_session.categoryForClient().Equals(""));
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper session after destroy... ");
                try
                {
                    _session.session();
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper communicator after destroy... ");
                Console.Out.Flush();
                try
                {
                    test(_session.communicator() != null);
                    _session.communicator().stringToProxy("dummy");
                    test(false);
                }
                catch (Ice.CommunicatorDestroyedException)
                {
                }
                Console.Out.WriteLine("ok");


                Console.Out.Write("uninstalling router with communicator... ");
                Console.Out.Flush();
                communicator().setDefaultRouter(null);
                Console.Out.WriteLine("ok");

                Ice.ObjectPrx processBase;
                {
                    Console.Out.Write("testing stringToProxy for process object... ");
                    processBase = communicator().stringToProxy("Glacier2/admin -f Process:default -h \"" + host + "\" -p 12348");
                    Console.Out.WriteLine("ok");
                }


                Ice.ProcessPrx process;
                {
                    Console.Out.Write("testing checked cast for admin object... ");
                    process = Ice.ProcessPrxHelper.checkedCast(processBase);
                    test(process != null);
                    Console.Out.WriteLine("ok");
                }

                Console.Out.Write("testing Glacier2 shutdown... ");
                process.shutdown();
                try
                {
                    process.ice_ping();
                    test(false);
                }
                catch (Ice.LocalException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback3());
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect after router shutdown... ");
                Console.Out.Flush();

                _factory.setRouterHost(host);
                _factory.setPort(12347);
                _factory.setProtocol(protocol);
                _session = _factory.connect("userid", "abc123");
                while (true)
                {
#if COMPACT
                    System.Threading.Monitor.Wait(this);
                    break;
#else
                    try
                    {
                        System.Threading.Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }

                Console.Out.Write("testing SessionHelper isConnect after connect failure... ");
                Console.Out.Flush();
                test(_session.isConnected() == false);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper communicator after connect failure... ");
                Console.Out.Flush();
                try
                {
                    test(_session.communicator() != null);
                    _session.communicator().stringToProxy("dummy");
                    test(false);
                }
                catch (Ice.CommunicatorDestroyedException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper destroy after connect failure... ");
                Console.Out.Flush();
                _session.destroy();
                Console.Out.WriteLine("ok");
            }

            return(0);
        }
Exemplo n.º 2
0
        public override int runWithSession(string[] args)
        {
            if (args.Length > 0)
            {
                Console.Error.WriteLine(appName() + ": too many arguments");
                return(1);
            }


            Ice.Identity callbackReceiverIdent     = createCallbackIdentity("callbackReceiver");
            Ice.Identity callbackReceiverFakeIdent = new Ice.Identity("fake", "callbackReceiver");

            Ice.ObjectPrx @base       = communicator().propertyToProxy("Callback.Proxy");
            CallbackPrx   twoway      = CallbackPrxHelper.checkedCast(@base);
            CallbackPrx   oneway      = CallbackPrxHelper.uncheckedCast(twoway.ice_oneway());
            CallbackPrx   batchOneway = CallbackPrxHelper.uncheckedCast(twoway.ice_batchOneway());

            objectAdapter().add(new CallbackReceiverI(), callbackReceiverFakeIdent);

            CallbackReceiverPrx twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                objectAdapter().add(new CallbackReceiverI(), callbackReceiverIdent));

            CallbackReceiverPrx onewayR = CallbackReceiverPrxHelper.uncheckedCast(twowayR.ice_oneway());

            menu();

            string line      = null;
            string @override = null;
            bool   fake      = false;

            do
            {
                Console.Write("==> ");
                Console.Out.Flush();
                line = Console.In.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (line.Equals("t"))
                {
                    Dictionary <string, string> context = new Dictionary <string, string>();
                    context["_fwd"] = "t";
                    if (@override != null)
                    {
                        context["_ovrd"] = @override;
                    }
                    twoway.initiateCallback(twowayR, context);
                }
                else if (line.Equals("o"))
                {
                    Dictionary <string, string> context = new Dictionary <string, string>();
                    context["_fwd"] = "o";
                    if (@override != null)
                    {
                        context["_ovrd"] = @override;
                    }
                    oneway.initiateCallback(onewayR, context);
                }
                else if (line.Equals("O"))
                {
                    Dictionary <string, string> context = new Dictionary <string, string>();
                    context["_fwd"] = "O";
                    if (@override != null)
                    {
                        context["_ovrd"] = @override;
                    }
                    batchOneway.initiateCallback(onewayR, context);
                }
                else if (line.Equals("f"))
                {
                    batchOneway.ice_flushBatchRequests();
                }
                else if (line.Equals("v"))
                {
                    if (@override == null)
                    {
                        @override = "some_value";
                        Console.WriteLine("override context field is now `" + @override + "'");
                    }
                    else
                    {
                        @override = null;
                        Console.WriteLine("override context field is empty");
                    }
                }
                else if (line.Equals("F"))
                {
                    fake = !fake;

                    if (fake)
                    {
                        twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                            twowayR.ice_identity(callbackReceiverFakeIdent));
                        onewayR = CallbackReceiverPrxHelper.uncheckedCast(
                            onewayR.ice_identity(callbackReceiverFakeIdent));
                    }
                    else
                    {
                        twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                            twowayR.ice_identity(callbackReceiverIdent));
                        onewayR = CallbackReceiverPrxHelper.uncheckedCast(
                            onewayR.ice_identity(callbackReceiverIdent));
                    }

                    Console.WriteLine("callback receiver identity: " +
                                      Ice.Util.identityToString(twowayR.ice_getIdentity()));
                }
                else if (line.Equals("s"))
                {
                    twoway.shutdown();
                }
                else if (line.Equals("r"))
                {
                    restart();
                }
                else if (line.Equals("x"))
                {
                    // Nothing to do
                }
                else if (line.Equals("?"))
                {
                    menu();
                }
                else
                {
                    Console.WriteLine("unknown command `" + line + "'");
                    menu();
                }
            }while(!line.Equals("x"));

            return(0);
        }
Exemplo n.º 3
0
Arquivo: Client.cs Projeto: motuii/ice
    public override void run(string[] args)
    {
        Ice.Properties properties = createTestProperties(ref args);
        //
        // We must disable connection warnings, because we attempt to
        // ping the router before session establishment, as well as
        // after session destruction. Both will cause a
        // ConnectionLostException.
        //
        properties.setProperty("Ice.Warn.Connections", "0");
        using (var communicator = initialize(properties))
        {
            IObjectPrx routerBase;
            {
                Console.Out.Write("testing stringToProxy for router... ");
                Console.Out.Flush();
                routerBase = IObjectPrx.Parse($"Glacier2/router:{getTestEndpoint(50)}", communicator);
                Console.Out.WriteLine("ok");
            }

            Glacier2.RouterPrx router;
            {
                Console.Out.Write("testing checked cast for router... ");
                Console.Out.Flush();
                router = Glacier2.RouterPrx.CheckedCast(routerBase);
                test(router != null);
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing router finder... ");
                Console.Out.Flush();
                RouterFinderPrx finder = RouterFinderPrx.Parse($"Ice/RouterFinder:{getTestEndpoint(50)}", communicator);
                test(finder.getRouter().Identity.Equals(router.Identity));
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("installing router with communicator... ");
                Console.Out.Flush();
                communicator.setDefaultRouter(router);
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("getting the session timeout... ");
                Console.Out.Flush();
                long timeout = router.getSessionTimeout();
                test(timeout == 30);
                Console.Out.WriteLine("ok");
            }

            IObjectPrx @base;
            {
                Console.Out.Write("testing stringToProxy for server object... ");
                Console.Out.Flush();
                @base = IObjectPrx.Parse($"c1/callback:{getTestEndpoint(0)}", communicator);
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying to ping server before session creation... ");
                Console.Out.Flush();
                try
                {
                    @base.IcePing();
                    test(false);
                }
                catch (Ice.ConnectionLostException)
                {
                    Console.Out.WriteLine("ok");
                }
                catch (Ice.SocketException)
                {
                    test(false);
                }
            }

            {
                Console.Out.Write("trying to create session with wrong password... ");
                Console.Out.Flush();
                try
                {
                    router.createSession("userid", "xxx");
                    test(false);
                }
                catch (Glacier2.PermissionDeniedException)
                {
                    Console.Out.WriteLine("ok");
                }
                catch (Glacier2.CannotCreateSessionException)
                {
                    test(false);
                }
            }

            {
                Console.Out.Write("trying to destroy non-existing session... ");
                Console.Out.Flush();
                try
                {
                    router.destroySession();
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("creating session with correct password... ");
                Console.Out.Flush();
                try
                {
                    router.createSession("userid", "abc123");
                }
                catch (Glacier2.PermissionDeniedException)
                {
                    test(false);
                }
                catch (Glacier2.CannotCreateSessionException)
                {
                    test(false);
                }
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying to create a second session... ");
                Console.Out.Flush();
                try
                {
                    router.createSession("userid", "abc123");
                    test(false);
                }
                catch (Glacier2.PermissionDeniedException)
                {
                    test(false);
                }
                catch (Glacier2.CannotCreateSessionException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("pinging server after session creation... ");
                Console.Out.Flush();
                @base.IcePing();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("pinging object with client endpoint... ");
                IObjectPrx baseC = IObjectPrx.Parse($"collocated:{getTestEndpoint(50)}", communicator);
                try
                {
                    baseC.IcePing();
                }
                catch (Ice.ObjectNotExistException)
                {
                }
                Console.Out.WriteLine("ok");
            }

            CallbackPrx twoway;

            {
                Console.Out.Write("testing checked cast for server object... ");
                Console.Out.Flush();
                twoway = CallbackPrx.CheckedCast(@base);
                test(twoway != null);
                Console.Out.WriteLine("ok");
            }

            Ice.ObjectAdapter adapter;

            {
                Console.Out.Write("creating and activating callback receiver adapter... ");
                Console.Out.Flush();
                communicator.getProperties().setProperty("Ice.PrintAdapterReady", "0");
                adapter = communicator.createObjectAdapterWithRouter("CallbackReceiverAdapter", router);
                adapter.Activate();
                Console.Out.WriteLine("ok");
            }

            string category;

            {
                Console.Out.Write("getting category from router... ");
                Console.Out.Flush();
                category = router.getCategoryForClient();
                Console.Out.WriteLine("ok");
            }

            CallbackReceiverI   callbackReceiverImpl;
            CallbackReceiver    callbackReceiver;
            CallbackReceiverPrx twowayR;
            CallbackReceiverPrx fakeTwowayR;

            {
                Console.Out.Write("creating and adding callback receiver object... ");
                Console.Out.Flush();
                callbackReceiverImpl = new CallbackReceiverI();
                callbackReceiver     = callbackReceiverImpl;
                Ice.Identity callbackReceiverIdent = new Ice.Identity();
                callbackReceiverIdent.name     = "callbackReceiver";
                callbackReceiverIdent.category = category;
                twowayR = adapter.Add(callbackReceiver, callbackReceiverIdent);
                Ice.Identity fakeCallbackReceiverIdent = new Ice.Identity();
                fakeCallbackReceiverIdent.name     = "callbackReceiver";
                fakeCallbackReceiverIdent.category = "dummy";
                fakeTwowayR = adapter.Add(callbackReceiver, fakeCallbackReceiverIdent);
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing oneway callback... ");
                Console.Out.Flush();
                CallbackPrx                 oneway  = twoway.Clone(oneway: true);
                CallbackReceiverPrx         onewayR = twowayR.Clone(oneway: true);
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "o";
                oneway.initiateCallback(onewayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing twoway callback... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                twoway.initiateCallback(twowayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("ditto, but with user exception... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                try
                {
                    twoway.initiateCallbackEx(twowayR, context);
                    test(false);
                }
                catch (CallbackException ex)
                {
                    test(ex.someValue == 3.14);
                    test(ex.someString.Equals("3.14"));
                }
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying twoway callback with fake category... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                try
                {
                    twoway.initiateCallback(fakeTwowayR, context);
                    test(false);
                }
                catch (Ice.ObjectNotExistException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("testing whether other allowed category is accepted... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                CallbackPrx otherCategoryTwoway =
                    CallbackPrx.UncheckedCast(twoway.Clone(Ice.Util.stringToIdentity("c2/callback")));
                otherCategoryTwoway.initiateCallback(twowayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing whether disallowed category gets rejected... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                try
                {
                    CallbackPrx otherCategoryTwoway =
                        CallbackPrx.UncheckedCast(twoway.Clone(Ice.Util.stringToIdentity("c3/callback")));
                    otherCategoryTwoway.initiateCallback(twowayR, context);
                    test(false);
                }
                catch (Ice.ObjectNotExistException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("testing whether user-id as category is accepted... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                CallbackPrx otherCategoryTwoway =
                    CallbackPrx.UncheckedCast(twoway.Clone(Ice.Util.stringToIdentity("_userid/callback")));
                otherCategoryTwoway.initiateCallback(twowayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            if (args.Length >= 1 && args[0].Equals("--shutdown"))
            {
                Console.Out.Write("testing server shutdown... ");
                Console.Out.Flush();
                twoway.shutdown();
                // No ping, otherwise the router prints a warning message if it's
                // started with --Ice.Warn.Connections.
                Console.Out.WriteLine("ok");

                /*
                 * try
                 * {
                 * base.IcePing();
                 * test(false);
                 * }
                 * // If we use the glacier router, the exact exception reason gets
                 * // lost.
                 * catch(Ice.UnknownLocalException ex)
                 * {
                 * Console.Out.WriteLine("ok");
                 * }
                 */
            }

            {
                Console.Out.Write("destroying session... ");
                Console.Out.Flush();
                try
                {
                    router.destroySession();
                }
                catch (Ice.LocalException)
                {
                    test(false);
                }

                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying to ping server after session destruction... ");
                Console.Out.Flush();
                try
                {
                    @base.IcePing();
                    test(false);
                }
                catch (Ice.ConnectionLostException)
                {
                    Console.Out.WriteLine("ok");
                }
                catch (Ice.SocketException)
                {
                    test(false);
                }
            }

            if (args.Length >= 1 && args[0].Equals("--shutdown"))
            {
                {
                    Console.Out.Write("uninstalling router with communicator... ");
                    Console.Out.Flush();
                    communicator.setDefaultRouter(null);
                    Console.Out.WriteLine("ok");
                }

                IObjectPrx processBase;
                {
                    Console.Out.Write("testing stringToProxy for admin object... ");
                    processBase = IObjectPrx.Parse($"Glacier2/admin -f Process:{getTestEndpoint(51)}", communicator);
                    Console.Out.WriteLine("ok");
                }

                /*
                 * {
                 * Console.Out.Write("uninstalling router with process object... ");
                 * processBase.ice_router(null);
                 * Console.Out.WriteLine("ok");
                 * }
                 */

                ProcessPrx process;
                {
                    Console.Out.Write("testing checked cast for process object... ");
                    process = ProcessPrx.CheckedCast(processBase);
                    process.IcePing();
                    Console.Out.WriteLine("ok");
                }

                Console.Out.Write("testing Glacier2 shutdown... ");
                process.shutdown();
                try
                {
                    process.IcePing();
                    test(false);
                }
                catch (LocalException)
                {
                    Console.Out.WriteLine("ok");
                }
            }
        }
    }
Exemplo n.º 4
0
    private static int run(Ice.Communicator communicator)
    {
        var router = Glacier2.RouterPrxHelper.checkedCast(communicator.getDefaultRouter());

        Glacier2.SessionPrx session;
        while (true)
        {
            Console.WriteLine("This demo accepts any user-id / password combination.");

            string id;
            string pw;
            try
            {
                Console.Write("user id: ");
                Console.Out.Flush();
                id = Console.In.ReadLine();
                if (id == null)
                {
                    throw new Ice.CommunicatorDestroyedException();
                }
                id = id.Trim();

                Console.Write("password: "******"permission denied:\n" + ex.reason);
            }
            catch (Glacier2.CannotCreateSessionException ex)
            {
                Console.WriteLine("cannot create session:\n" + ex.reason);
            }
        }

        int acmTimeout = router.getACMTimeout();

        Ice.Connection connection = router.ice_getCachedConnection();
        Debug.Assert(connection != null);
        connection.setACM(acmTimeout, Ice.Util.None, Ice.ACMHeartbeat.HeartbeatAlways);
        connection.setCloseCallback(_ => Console.WriteLine("The Glacier2 session has been destroyed."));

        Ice.Identity callbackReceiverIdent     = new Ice.Identity("callbackReceiver", router.getCategoryForClient());
        Ice.Identity callbackReceiverFakeIdent = new Ice.Identity("fake", "callbackReceiver");

        Ice.ObjectPrx @base       = communicator.propertyToProxy("Callback.Proxy");
        CallbackPrx   twoway      = CallbackPrxHelper.checkedCast(@base);
        CallbackPrx   oneway      = CallbackPrxHelper.uncheckedCast(twoway.ice_oneway());
        CallbackPrx   batchOneway = CallbackPrxHelper.uncheckedCast(twoway.ice_batchOneway());

        var adapter = communicator.createObjectAdapterWithRouter("", router);

        adapter.add(new CallbackReceiverI(), callbackReceiverFakeIdent);

        CallbackReceiverPrx twowayR = CallbackReceiverPrxHelper.uncheckedCast(
            adapter.add(new CallbackReceiverI(), callbackReceiverIdent));

        adapter.activate();
        CallbackReceiverPrx onewayR = CallbackReceiverPrxHelper.uncheckedCast(twowayR.ice_oneway());

        menu();

        string line      = null;
        string @override = null;
        bool   fake      = false;

        do
        {
            Console.Write("==> ");
            Console.Out.Flush();
            line = Console.In.ReadLine();
            if (line == null)
            {
                break;
            }
            if (line.Equals("t"))
            {
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                if (@override != null)
                {
                    context["_ovrd"] = @override;
                }
                twoway.initiateCallback(twowayR, context);
            }
            else if (line.Equals("o"))
            {
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "o";
                if (@override != null)
                {
                    context["_ovrd"] = @override;
                }
                oneway.initiateCallback(onewayR, context);
            }
            else if (line.Equals("O"))
            {
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "O";
                if (@override != null)
                {
                    context["_ovrd"] = @override;
                }
                batchOneway.initiateCallback(onewayR, context);
            }
            else if (line.Equals("f"))
            {
                batchOneway.ice_flushBatchRequests();
            }
            else if (line.Equals("v"))
            {
                if (@override == null)
                {
                    @override = "some_value";
                    Console.WriteLine("override context field is now `" + @override + "'");
                }
                else
                {
                    @override = null;
                    Console.WriteLine("override context field is empty");
                }
            }
            else if (line.Equals("F"))
            {
                fake = !fake;

                if (fake)
                {
                    twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                        twowayR.ice_identity(callbackReceiverFakeIdent));
                    onewayR = CallbackReceiverPrxHelper.uncheckedCast(
                        onewayR.ice_identity(callbackReceiverFakeIdent));
                }
                else
                {
                    twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                        twowayR.ice_identity(callbackReceiverIdent));
                    onewayR = CallbackReceiverPrxHelper.uncheckedCast(
                        onewayR.ice_identity(callbackReceiverIdent));
                }

                Console.WriteLine("callback receiver identity: " +
                                  Ice.Util.identityToString(twowayR.ice_getIdentity()));
            }
            else if (line.Equals("s"))
            {
                twoway.shutdown();
            }
            else if (line.Equals("x"))
            {
                // Nothing to do
            }
            else if (line.Equals("?"))
            {
                menu();
            }
            else
            {
                Console.WriteLine("unknown command `" + line + "'");
                menu();
            }
        }while(!line.Equals("x"));

        return(0);
    }
Exemplo n.º 5
0
    public override void run(string[] args)
    {
        Ice.InitializationData initData = new Ice.InitializationData();
        initData.properties = createTestProperties(ref args);
        initData.properties.setProperty("Ice.Warn.Connections", "0");
        initData.properties.setProperty("Ice.Default.Router",
                                        "Glacier2/router:" + getTestEndpoint(initData.properties, 50));
        initData.dispatcher = delegate(Action action, Ice.Connection connection)
        {
            action();
        };

        using (var communicator = initialize(initData))
        {
            string protocol = getTestProtocol();
            string host     = getTestHost();

            Glacier2.SessionFactoryHelper factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback1(this));
            Glacier2.SessionHelper        session = null;

            //
            // Test to create a session with wrong userid/password
            //
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
                Console.Out.Flush();

                factory.setProtocol(protocol);
                session = factory.connect("userid", "xxx");
                while (true)
                {
                    try
                    {
                        Monitor.Wait(this, 30000);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }
                test(!session.isConnected());
            }

            initData.properties.setProperty("Ice.Default.Router", "");
            factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback4(this));
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect interrupt... ");
                Console.Out.Flush();
                factory.setRouterHost(host);
                factory.setPort(getTestPort(1));
                factory.setProtocol(protocol);
                session = factory.connect("userid", "abc123");

                Thread.Sleep(100);
                session.destroy();

                while (true)
                {
                    try
                    {
                        Monitor.Wait(this, 30000);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }
                test(!session.isConnected());
            }

            factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback2(this));
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect... ");
                Console.Out.Flush();
                factory.setRouterHost(host);
                factory.setPort(getTestPort(50));
                factory.setProtocol(protocol);
                session = factory.connect("userid", "abc123");
                while (true)
                {
                    try
                    {
                        Monitor.Wait(this, 30000);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                Console.Out.Write("testing SessionHelper isConnected after connect... ");
                Console.Out.Flush();
                test(session.isConnected());
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper categoryForClient after connect... ");
                Console.Out.Flush();
                try
                {
                    test(!session.categoryForClient().Equals(""));
                }
                catch (Glacier2.SessionNotExistException)
                {
                    test(false);
                }
                Console.Out.WriteLine("ok");

                test(session.session() == null);

                Console.Out.Write("testing stringToProxy for server object... ");
                Console.Out.Flush();
                Ice.ObjectPrx @base = session.communicator().stringToProxy("callback:" + getTestEndpoint(0));
                Console.Out.WriteLine("ok");

                Console.Out.Write("pinging server after session creation... ");
                Console.Out.Flush();
                @base.ice_ping();
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing checked cast for server object... ");
                Console.Out.Flush();
                CallbackPrx twoway = CallbackPrxHelper.checkedCast(@base);
                test(twoway != null);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing server shutdown... ");
                Console.Out.Flush();
                twoway.shutdown();
                Console.Out.WriteLine("ok");

                test(session.communicator() != null);
                Console.Out.Write("testing SessionHelper destroy... ");
                Console.Out.Flush();
                session.destroy();
                while (true)
                {
                    try
                    {
                        Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                Console.Out.Write("testing SessionHelper isConnected after destroy... ");
                Console.Out.Flush();
                test(session.isConnected() == false);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper categoryForClient after destroy... ");
                Console.Out.Flush();
                try
                {
                    test(!session.categoryForClient().Equals(""));
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper session after destroy... ");
                test(session.session() == null);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper communicator after destroy... ");
                Console.Out.Flush();
                try
                {
                    test(session.communicator() != null);
                    session.communicator().stringToProxy("dummy");
                    test(false);
                }
                catch (Ice.CommunicatorDestroyedException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("uninstalling router with communicator... ");
                Console.Out.Flush();
                communicator.setDefaultRouter(null);
                Console.Out.WriteLine("ok");

                Ice.ObjectPrx processBase;
                {
                    Console.Out.Write("testing stringToProxy for process object... ");
                    processBase = communicator.stringToProxy("Glacier2/admin -f Process:" + getTestEndpoint(51));
                    Console.Out.WriteLine("ok");
                }

                Ice.ProcessPrx process;
                {
                    Console.Out.Write("testing checked cast for admin object... ");
                    process = Ice.ProcessPrxHelper.checkedCast(processBase);
                    test(process != null);
                    Console.Out.WriteLine("ok");
                }

                Console.Out.Write("testing Glacier2 shutdown... ");
                process.shutdown();
                try
                {
                    process.ice_ping();
                    test(false);
                }
                catch (Ice.LocalException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            factory = new Glacier2.SessionFactoryHelper(initData, new SessionCallback3(this));
            lock (this)
            {
                Console.Out.Write("testing SessionHelper connect after router shutdown... ");
                Console.Out.Flush();

                factory.setRouterHost(host);
                factory.setPort(getTestPort(50));
                factory.setProtocol(protocol);
                session = factory.connect("userid", "abc123");
                while (true)
                {
                    try
                    {
                        Monitor.Wait(this);
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
                }

                Console.Out.Write("testing SessionHelper isConnect after connect failure... ");
                Console.Out.Flush();
                test(session.isConnected() == false);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper communicator after connect failure... ");
                Console.Out.Flush();
                try
                {
                    test(session.communicator() != null);
                    session.communicator().stringToProxy("dummy");
                    test(false);
                }
                catch (Ice.CommunicatorDestroyedException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper destroy after connect failure... ");
                Console.Out.Flush();
                session.destroy();
                Console.Out.WriteLine("ok");
            }
        }
    }
Exemplo n.º 6
0
        public override int run(string[] args)
        {
            Ice.ObjectPrx routerBase;

            {
                Console.Out.Write("testing stringToProxy for router... ");
                Console.Out.Flush();
                routerBase = communicator().stringToProxy("Glacier2/router:default -p 12347");
                Console.Out.WriteLine("ok");
            }

            Glacier2.RouterPrx router;

            {
                Console.Out.Write("testing checked cast for router... ");
                Console.Out.Flush();
                router = Glacier2.RouterPrxHelper.checkedCast(routerBase);
                test(router != null);
                Console.Out.WriteLine("ok");
            }


            {
                Console.Out.Write("testing router finder... ");
                Console.Out.Flush();
                Ice.RouterFinderPrx finder = Ice.RouterFinderPrxHelper.uncheckedCast(
                    communicator().stringToProxy("Ice/RouterFinder:default -p 12347"));
                test(finder.getRouter().ice_getIdentity().Equals(router.ice_getIdentity()));
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("installing router with communicator... ");
                Console.Out.Flush();
                communicator().setDefaultRouter(router);
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("getting the session timeout... ");
                Console.Out.Flush();
                long timeout = router.getSessionTimeout();
                test(timeout == 30);
                Console.Out.WriteLine("ok");
            }

            Ice.ObjectPrx @base;

            {
                Console.Out.Write("testing stringToProxy for server object... ");
                Console.Out.Flush();
                @base = communicator().stringToProxy("c1/callback:tcp -p 12010");
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying to ping server before session creation... ");
                Console.Out.Flush();
                try
                {
                    @base.ice_ping();
                    test(false);
                }
                catch (Ice.ConnectionLostException)
                {
                    Console.Out.WriteLine("ok");
                }
                catch (Ice.SocketException)
                {
                    test(false);
                }
            }


            {
                Console.Out.Write("trying to create session with wrong password... ");
                Console.Out.Flush();
                try
                {
                    router.createSession("userid", "xxx");
                    test(false);
                }
                catch (Glacier2.PermissionDeniedException)
                {
                    Console.Out.WriteLine("ok");
                }
                catch (Glacier2.CannotCreateSessionException)
                {
                    test(false);
                }
            }

            {
                Console.Out.Write("trying to destroy non-existing session... ");
                Console.Out.Flush();
                try
                {
                    router.destroySession();
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("creating session with correct password... ");
                Console.Out.Flush();
                try
                {
                    router.createSession("userid", "abc123");
                }
                catch (Glacier2.PermissionDeniedException)
                {
                    test(false);
                }
                catch (Glacier2.CannotCreateSessionException)
                {
                    test(false);
                }
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying to create a second session... ");
                Console.Out.Flush();
                try
                {
                    router.createSession("userid", "abc123");
                    test(false);
                }
                catch (Glacier2.PermissionDeniedException)
                {
                    test(false);
                }
                catch (Glacier2.CannotCreateSessionException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("pinging server after session creation... ");
                Console.Out.Flush();
                @base.ice_ping();
                Console.Out.WriteLine("ok");
            }

            CallbackPrx twoway;

            {
                Console.Out.Write("testing checked cast for server object... ");
                Console.Out.Flush();
                twoway = CallbackPrxHelper.checkedCast(@base);
                test(twoway != null);
                Console.Out.WriteLine("ok");
            }

            Ice.ObjectAdapter adapter;

            {
                Console.Out.Write("creating and activating callback receiver adapter... ");
                Console.Out.Flush();
                communicator().getProperties().setProperty("Ice.PrintAdapterReady", "0");
                adapter = communicator().createObjectAdapterWithRouter("CallbackReceiverAdapter", router);
                adapter.activate();
                Console.Out.WriteLine("ok");
            }

            String category;

            {
                Console.Out.Write("getting category from router... ");
                Console.Out.Flush();
                category = router.getCategoryForClient();
                Console.Out.WriteLine("ok");
            }

            CallbackReceiverI callbackReceiverImpl;

            Ice.Object          callbackReceiver;
            CallbackReceiverPrx twowayR;
            CallbackReceiverPrx fakeTwowayR;

            {
                Console.Out.Write("creating and adding callback receiver object... ");
                Console.Out.Flush();
                callbackReceiverImpl = new CallbackReceiverI();
                callbackReceiver     = callbackReceiverImpl;
                Ice.Identity callbackReceiverIdent = new Ice.Identity();
                callbackReceiverIdent.name     = "callbackReceiver";
                callbackReceiverIdent.category = category;
                twowayR = CallbackReceiverPrxHelper.uncheckedCast(adapter.add(callbackReceiver, callbackReceiverIdent));
                Ice.Identity fakeCallbackReceiverIdent = new Ice.Identity();
                fakeCallbackReceiverIdent.name     = "callbackReceiver";
                fakeCallbackReceiverIdent.category = "dummy";
                fakeTwowayR = CallbackReceiverPrxHelper.uncheckedCast(
                    adapter.add(callbackReceiver, fakeCallbackReceiverIdent));
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing oneway callback... ");
                Console.Out.Flush();
                CallbackPrx                 oneway  = CallbackPrxHelper.uncheckedCast(twoway.ice_oneway());
                CallbackReceiverPrx         onewayR = CallbackReceiverPrxHelper.uncheckedCast(twowayR.ice_oneway());
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "o";
                oneway.initiateCallback(onewayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing twoway callback... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                twoway.initiateCallback(twowayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("ditto, but with user exception... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                try
                {
                    twoway.initiateCallbackEx(twowayR, context);
                    test(false);
                }
                catch (CallbackException ex)
                {
                    test(ex.someValue == 3.14);
                    test(ex.someString.Equals("3.14"));
                }
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying twoway callback with fake category... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                try
                {
                    twoway.initiateCallback(fakeTwowayR, context);
                    test(false);
                }
                catch (Ice.ObjectNotExistException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("testing whether other allowed category is accepted... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                CallbackPrx otherCategoryTwoway = CallbackPrxHelper.uncheckedCast(
                    twoway.ice_identity(communicator().stringToIdentity("c2/callback")));
                otherCategoryTwoway.initiateCallback(twowayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing whether disallowed category gets rejected... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                try
                {
                    CallbackPrx otherCategoryTwoway = CallbackPrxHelper.uncheckedCast(
                        twoway.ice_identity(communicator().stringToIdentity("c3/callback")));
                    otherCategoryTwoway.initiateCallback(twowayR, context);
                    test(false);
                }
                catch (Ice.ObjectNotExistException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            {
                Console.Out.Write("testing whether user-id as category is accepted... ");
                Console.Out.Flush();
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "t";
                CallbackPrx otherCategoryTwoway = CallbackPrxHelper.uncheckedCast(
                    twoway.ice_identity(communicator().stringToIdentity("_userid/callback")));
                otherCategoryTwoway.initiateCallback(twowayR, context);
                callbackReceiverImpl.callbackOK();
                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("testing server shutdown... ");
                Console.Out.Flush();
                twoway.shutdown();
                // No ping, otherwise the router prints a warning message if it's
                // started with --Ice.Warn.Connections.
                Console.Out.WriteLine("ok");

                /*
                 * try
                 * {
                 *    base.ice_ping();
                 *    test(false);
                 * }
                 * // If we use the glacier router, the exact exception reason gets
                 * // lost.
                 * catch(Ice.UnknownLocalException ex)
                 * {
                 *    Console.Out.WriteLine("ok");
                 * }
                 */
            }

            {
                Console.Out.Write("destroying session... ");
                Console.Out.Flush();
                try
                {
                    router.destroySession();
                }
                catch (Ice.LocalException)
                {
                    test(false);
                }

                Console.Out.WriteLine("ok");
            }

            {
                Console.Out.Write("trying to ping server after session destruction... ");
                Console.Out.Flush();
                try
                {
                    @base.ice_ping();
                    test(false);
                }
                catch (Ice.ConnectionLostException)
                {
                    Console.Out.WriteLine("ok");
                }
                catch (Ice.SocketException)
                {
                    test(false);
                }
            }

            if (args.Length >= 1 && args[0].Equals("--shutdown"))
            {
                {
                    Console.Out.Write("uninstalling router with communicator... ");
                    Console.Out.Flush();
                    communicator().setDefaultRouter(null);
                    Console.Out.WriteLine("ok");
                }

                Ice.ObjectPrx processBase;

                {
                    Console.Out.Write("testing stringToProxy for admin object... ");
                    processBase = communicator().stringToProxy("Glacier2/admin -f Process:tcp -h 127.0.0.1 -p 12348");
                    Console.Out.WriteLine("ok");
                }

                /*
                 *          {
                 *              Console.Out.Write("uninstalling router with process object... ");
                 *              processBase.ice_router(null);
                 *              Console.Out.WriteLine("ok");
                 *          }
                 */

                Ice.ProcessPrx process;

                {
                    Console.Out.Write("testing checked cast for process object... ");
                    process = Ice.ProcessPrxHelper.checkedCast(processBase);
                    test(process != null);
                    Console.Out.WriteLine("ok");
                }

                Console.Out.Write("testing Glacier2 shutdown... ");
                process.shutdown();
                try
                {
                    process.ice_ping();
                    test(false);
                }
                catch (Ice.LocalException)
                {
                    Console.Out.WriteLine("ok");
                }
            }

            return(0);
        }
Exemplo n.º 7
0
    public override void run(string[] args)
    {
        var properties = createTestProperties(ref args);

        properties["Ice.Warn.Connections"] = "0";
        properties["Ice.Default.Router"]   = $"Glacier2/router:{getTestEndpoint(properties, 50)}";

        Action <Action, Connection?> dispatcher = (Action action, Connection? connection) =>
        {
            action();
        };

        using var communicator = initialize(properties, dispatcher);

        string protocol = getTestProtocol();
        string host     = getTestHost();

        Glacier2.SessionFactoryHelper factory = new Glacier2.SessionFactoryHelper(new SessionCallback1(this), properties, dispatcher: dispatcher);
        Glacier2.SessionHelper?       session = null;

        //
        // Test to create a session with wrong userid/password
        //
        lock (this)
        {
            Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
            Console.Out.Flush();

            factory.setProtocol(protocol);
            session = factory.connect("userid", "xxx");
            while (true)
            {
                try
                {
                    if (!Monitor.Wait(this, 30000))
                    {
                        test(false);
                    }
                    break;
                }
                catch (ThreadInterruptedException)
                {
                }
            }
            test(!session.isConnected());
        }

        properties.Remove("Ice.Default.Router");
        factory = new Glacier2.SessionFactoryHelper(new SessionCallback4(this), properties, dispatcher: dispatcher);
        lock (this)
        {
            Console.Out.Write("testing SessionHelper connect interrupt... ");
            Console.Out.Flush();
            factory.setRouterHost(host);
            factory.setPort(getTestPort(1));
            factory.setProtocol(protocol);
            session = factory.connect("userid", "abc123");

            Thread.Sleep(100);
            session.destroy();

            while (true)
            {
                try
                {
                    if (!Monitor.Wait(this, 30000))
                    {
                        test(false);
                    }
                    break;
                }
                catch (ThreadInterruptedException)
                {
                }
            }
            test(!session.isConnected());
        }

        factory = new Glacier2.SessionFactoryHelper(new SessionCallback2(this), properties, dispatcher: dispatcher);
        lock (this)
        {
            Console.Out.Write("testing SessionHelper connect... ");
            Console.Out.Flush();
            factory.setRouterHost(host);
            factory.setPort(getTestPort(50));
            factory.setProtocol(protocol);
            session = factory.connect("userid", "abc123");
            while (true)
            {
                try
                {
                    if (!Monitor.Wait(this, 30000))
                    {
                        test(false);
                    }
                    break;
                }
                catch (ThreadInterruptedException)
                {
                }
            }

            Console.Out.Write("testing SessionHelper isConnected after connect... ");
            Console.Out.Flush();
            test(session.isConnected());
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing SessionHelper categoryForClient after connect... ");
            Console.Out.Flush();
            try
            {
                test(!session.categoryForClient().Equals(""));
            }
            catch (Glacier2.SessionNotExistException)
            {
                test(false);
            }
            Console.Out.WriteLine("ok");

            test(session.session() == null);

            Console.Out.Write("testing stringToProxy for server object... ");
            Console.Out.Flush();
            var @base = IObjectPrx.Parse($"callback:{getTestEndpoint(0)}", session.communicator());
            Console.Out.WriteLine("ok");

            Console.Out.Write("pinging server after session creation... ");
            Console.Out.Flush();
            @base.IcePing();
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing checked cast for server object... ");
            Console.Out.Flush();
            CallbackPrx twoway = CallbackPrx.CheckedCast(@base);
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing server shutdown... ");
            Console.Out.Flush();
            twoway.shutdown();
            Console.Out.WriteLine("ok");

            test(session.communicator() != null);
            Console.Out.Write("testing SessionHelper destroy... ");
            Console.Out.Flush();
            session.destroy();
            while (true)
            {
                try
                {
                    if (!Monitor.Wait(this, 30000))
                    {
                        test(false);
                    }
                    break;
                }
                catch (ThreadInterruptedException)
                {
                }
            }

            Console.Out.Write("testing SessionHelper isConnected after destroy... ");
            Console.Out.Flush();
            test(session.isConnected() == false);
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing SessionHelper categoryForClient after destroy... ");
            Console.Out.Flush();
            try
            {
                test(!session.categoryForClient().Equals(""));
                test(false);
            }
            catch (Glacier2.SessionNotExistException)
            {
            }
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing SessionHelper session after destroy... ");
            test(session.session() == null);
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing SessionHelper communicator after destroy... ");
            Console.Out.Flush();
            try
            {
                test(session.communicator() != null);
                IObjectPrx.Parse("dummy", session.communicator());
                test(false);
            }
            catch (Ice.CommunicatorDestroyedException)
            {
            }
            Console.Out.WriteLine("ok");

            Console.Out.Write("uninstalling router with communicator... ");
            Console.Out.Flush();
            communicator.setDefaultRouter(null);
            Console.Out.WriteLine("ok");

            IObjectPrx processBase;
            {
                Console.Out.Write("testing stringToProxy for process object... ");
                processBase = IObjectPrx.Parse($"Glacier2/admin -f Process:{getTestEndpoint(51)}", communicator);
                Console.Out.WriteLine("ok");
            }

            ProcessPrx process;
            {
                Console.Out.Write("testing checked cast for admin object... ");
                process = Ice.ProcessPrx.CheckedCast(processBase);
                test(process != null);
                Console.Out.WriteLine("ok");
            }

            Console.Out.Write("testing Glacier2 shutdown... ");
            process.shutdown();
            try
            {
                process.IcePing();
                test(false);
            }
            catch (LocalException)
            {
                Console.Out.WriteLine("ok");
            }
        }

        factory = new Glacier2.SessionFactoryHelper(new SessionCallback3(this), properties, dispatcher: dispatcher);
        lock (this)
        {
            Console.Out.Write("testing SessionHelper connect after router shutdown... ");
            Console.Out.Flush();

            factory.setRouterHost(host);
            factory.setPort(getTestPort(50));
            factory.setProtocol(protocol);
            session = factory.connect("userid", "abc123");
            while (true)
            {
                try
                {
                    if (!Monitor.Wait(this, 30000))
                    {
                        test(false);
                    }
                    break;
                }
                catch (ThreadInterruptedException)
                {
                }
            }

            Console.Out.Write("testing SessionHelper isConnect after connect failure... ");
            Console.Out.Flush();
            test(session.isConnected() == false);
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing SessionHelper communicator after connect failure... ");
            Console.Out.Flush();
            try
            {
                test(session.communicator() != null);
                IObjectPrx.Parse("dummy", session.communicator());
                test(false);
            }
            catch (CommunicatorDestroyedException)
            {
            }
            Console.Out.WriteLine("ok");

            Console.Out.Write("testing SessionHelper destroy after connect failure... ");
            Console.Out.Flush();
            session.destroy();
            Console.Out.WriteLine("ok");
        }
    }
Exemplo n.º 8
0
    private static int run(Ice.Communicator communicator)
    {
        var router = Glacier2.RouterPrxHelper.checkedCast(communicator.getDefaultRouter());

        Glacier2.SessionPrx session;
        //
        // Loop until we have successfully create a session.
        //
        while (true)
        {
            //
            // Prompt the user for the credentials to create the session.
            //
            Console.WriteLine("This demo accepts any user-id / password combination.");

            string id;
            string pw;
            try
            {
                Console.Write("user id: ");
                Console.Out.Flush();
                id = Console.In.ReadLine();
                if (id == null)
                {
                    throw new Ice.CommunicatorDestroyedException();
                }
                id = id.Trim();

                Console.Write("password: "******"permission denied:\n" + ex.reason);
            }
            catch (Glacier2.CannotCreateSessionException ex)
            {
                Console.WriteLine("cannot create session:\n" + ex.reason);
            }
        }

        int acmTimeout = router.getACMTimeout();

        Ice.Connection connection = router.ice_getCachedConnection();
        Debug.Assert(connection != null);
        connection.setACM(acmTimeout, Ice.Util.None, Ice.ACMHeartbeat.HeartbeatAlways);
        connection.setCloseCallback(_ => Console.WriteLine("The Glacier2 session has been destroyed."));

        //
        // The Glacier2 router routes bidirectional calls to objects in the client only
        // when these objects have the correct Glacier2-issued category. The purpose of
        // the callbackReceiverFakeIdent is to demonstrate this.
        //
        // The Identity name is not checked by the server any value can be used.
        //
        Ice.Identity callbackReceiverIdent =
            new Ice.Identity(System.Guid.NewGuid().ToString(), router.getCategoryForClient());
        Ice.Identity callbackReceiverFakeIdent =
            new Ice.Identity(System.Guid.NewGuid().ToString(), "fake");

        Ice.ObjectPrx @base       = communicator.propertyToProxy("Callback.Proxy");
        CallbackPrx   twoway      = CallbackPrxHelper.checkedCast(@base);
        CallbackPrx   oneway      = CallbackPrxHelper.uncheckedCast(twoway.ice_oneway());
        CallbackPrx   batchOneway = CallbackPrxHelper.uncheckedCast(twoway.ice_batchOneway());

        var adapter = communicator.createObjectAdapterWithRouter("", router);

        //
        // Callback will never be called for a fake identity.
        //
        adapter.add(new CallbackReceiverI(), callbackReceiverFakeIdent);

        CallbackReceiverPrx twowayR = CallbackReceiverPrxHelper.uncheckedCast(
            adapter.add(new CallbackReceiverI(), callbackReceiverIdent));

        adapter.activate();
        CallbackReceiverPrx onewayR = CallbackReceiverPrxHelper.uncheckedCast(twowayR.ice_oneway());

        menu();

        //
        // Client REPL
        //
        string line      = null;
        string @override = null;
        bool   fake      = false;

        do
        {
            Console.Write("==> ");
            Console.Out.Flush();
            line = Console.In.ReadLine();
            if (line == null)
            {
                break;
            }
            if (line.Equals("t"))
            {
                twoway.initiateCallback(twowayR);
            }
            else if (line.Equals("o"))
            {
                Dictionary <string, string> context = new Dictionary <string, string>();
                if (@override != null)
                {
                    context["_ovrd"] = @override;
                }
                oneway.initiateCallback(onewayR, context);
            }
            else if (line.Equals("O"))
            {
                Dictionary <string, string> context = new Dictionary <string, string>();
                context["_fwd"] = "O";
                if (@override != null)
                {
                    context["_ovrd"] = @override;
                }
                batchOneway.initiateCallback(onewayR, context);
            }
            else if (line.Equals("f"))
            {
                batchOneway.ice_flushBatchRequests();
            }
            else if (line.Equals("v"))
            {
                if (@override == null)
                {
                    @override = "some_value";
                    Console.WriteLine("override context field is now `" + @override + "'");
                }
                else
                {
                    @override = null;
                    Console.WriteLine("override context field is empty");
                }
            }
            else if (line.Equals("F"))
            {
                fake = !fake;

                if (fake)
                {
                    twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                        twowayR.ice_identity(callbackReceiverFakeIdent));
                    onewayR = CallbackReceiverPrxHelper.uncheckedCast(
                        onewayR.ice_identity(callbackReceiverFakeIdent));
                }
                else
                {
                    twowayR = CallbackReceiverPrxHelper.uncheckedCast(
                        twowayR.ice_identity(callbackReceiverIdent));
                    onewayR = CallbackReceiverPrxHelper.uncheckedCast(
                        onewayR.ice_identity(callbackReceiverIdent));
                }

                Console.WriteLine("callback receiver identity: " +
                                  Ice.Util.identityToString(twowayR.ice_getIdentity()));
            }
            else if (line.Equals("s"))
            {
                twoway.shutdown();
            }
            else if (line.Equals("x"))
            {
                // Nothing to do
            }
            else if (line.Equals("?"))
            {
                menu();
            }
            else
            {
                Console.WriteLine("unknown command `" + line + "'");
                menu();
            }
        }while(!line.Equals("x"));

        return(0);
    }
Exemplo n.º 9
0
        public override int run(string[] args)
        {
            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback1());

            //
            // Test to create a session with wrong userid/password
            //
            lck.Lock();
            try
            {
                Console.Out.Write("testing SessionHelper connect with wrong userid/password... ");
                Console.Out.Flush();

                _factory.setRouterHost("127.0.0.1");
                _factory.setPort(12347);
                _factory.setRouterIdentity(Ice.Util.stringToIdentity("Glacier2/router"));
                _factory.setSecure(false);
                _session = _factory.connect("userid", "xxx");
                while (true)
                {
#if COMPACT
                    lck.Wait();
                    break;
#else
                    try
                    {
                        lck.Wait();
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }
            }
            finally
            {
                lck.Unlock();
            }

            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback2());
            lck.Lock();
            try
            {
                Console.Out.Write("testing SessionHelper connect... ");
                Console.Out.Flush();
                _factory.setRouterHost("127.0.0.1");
                _factory.setPort(12347);
                _factory.setRouterIdentity(Ice.Util.stringToIdentity("Glacier2/router"));
                _factory.setSecure(false);
                _session = _factory.connect("userid", "abc123");
                while (true)
                {
#if COMPACT
                    lck.Wait();
                    break;
#else
                    try
                    {
                        lck.Wait();
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }

                Console.Out.Write("testing SessionHelper isConnected after connect... ");
                Console.Out.Flush();
                test(_session.isConnected());
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper categoryForClient after connect... ");
                Console.Out.Flush();
                try
                {
                    test(!_session.categoryForClient().Equals(""));
                }
                catch (Glacier2.SessionNotExistException)
                {
                    test(false);
                }
                Console.Out.WriteLine("ok");

                //             try
                //             {
                //                 test(_session.session() != null);
                //             }
                //             catch(Glacier2.SessionNotExistException ex)
                //             {
                //                 test(false);
                //             }

                Console.Out.Write("testing stringToProxy for server object... ");
                Console.Out.Flush();
                Ice.ObjectPrx @base = _session.communicator().stringToProxy("callback:tcp -p 12010");
                Console.Out.WriteLine("ok");

                Console.Out.Write("pinging server after session creation... ");
                Console.Out.Flush();
                @base.ice_ping();
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing checked cast for server object... ");
                Console.Out.Flush();
                CallbackPrx twoway = CallbackPrxHelper.checkedCast(@base);
                test(twoway != null);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing server shutdown... ");
                Console.Out.Flush();
                twoway.shutdown();
                Console.Out.WriteLine("ok");

                test(_session.communicator() != null);
                Console.Out.Write("testing SessionHelper destroy... ");
                Console.Out.Flush();
                _session.destroy();
                while (true)
                {
#if COMPACT
                    lck.Wait();
                    break;
#else
                    try
                    {
                        lck.Wait();
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }

                Console.Out.Write("testing SessionHelper isConnected after destroy... ");
                Console.Out.Flush();
                test(_session.isConnected() == false);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper categoryForClient after destroy... ");
                Console.Out.Flush();
                try
                {
                    test(!_session.categoryForClient().Equals(""));
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper session after destroy... ");
                try
                {
                    _session.session();
                    test(false);
                }
                catch (Glacier2.SessionNotExistException)
                {
                }
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper communicator after destroy... ");
                Console.Out.Flush();
                test(_session.communicator() != null);
                Console.Out.WriteLine("ok");


                Console.Out.Write("uninstalling router with communicator... ");
                Console.Out.Flush();
                communicator().setDefaultRouter(null);
                Console.Out.WriteLine("ok");

                Ice.ObjectPrx processBase;
                {
                    Console.Out.Write("testing stringToProxy for process object... ");
                    processBase = communicator().stringToProxy("Glacier2/admin -f Process:tcp -h 127.0.0.1 -p 12348");
                    Console.Out.WriteLine("ok");
                }


                Ice.ProcessPrx process;
                {
                    Console.Out.Write("testing checked cast for admin object... ");
                    process = Ice.ProcessPrxHelper.checkedCast(processBase);
                    test(process != null);
                    Console.Out.WriteLine("ok");
                }

                Console.Out.Write("testing Glacier2 shutdown... ");
                process.shutdown();
                try
                {
                    process.ice_ping();
                    test(false);
                }
                catch (Ice.LocalException)
                {
                    Console.Out.WriteLine("ok");
                }
            }
            finally
            {
                lck.Unlock();
            }

            _factory = new Glacier2.SessionFactoryHelper(_initData, new SessionCalback3());
            lck.Lock();
            try
            {
                Console.Out.Write("testing SessionHelper connect after router shutdown... ");
                Console.Out.Flush();

                _factory.setRouterHost("127.0.0.1");
                _factory.setPort(12347);
                _factory.setRouterIdentity(Ice.Util.stringToIdentity("Glacier2/router"));
                _factory.setSecure(false);
                _session = _factory.connect("userid", "abc123");
                while (true)
                {
#if COMPACT
                    lck.Wait();
                    break;
#else
                    try
                    {
                        lck.Wait();
                        break;
                    }
                    catch (ThreadInterruptedException)
                    {
                    }
#endif
                }

                Console.Out.Write("testing SessionHelper isConnect after connect failure... ");
                Console.Out.Flush();
                test(_session.isConnected() == false);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper communicator after connect failure... ");
                Console.Out.Flush();
                test(_session.communicator() != null);
                Console.Out.WriteLine("ok");

                Console.Out.Write("testing SessionHelper destroy after connect failure... ");
                Console.Out.Flush();
                _session.destroy();
                Console.Out.WriteLine("ok");
            }
            finally
            {
                lck.Unlock();
            }

            return(0);
        }