Beispiel #1
0
        /// <summary>
        /// Creates an ICacheSession given an ISession and CacheSessionConfiguration
        /// </summary>
        /// <param name="jcsmpSession"></param>
        /// <param name="sc"></param>
        /// <returns></returns>
        public static ICacheSession newCacheSession(ISession session, CacheSessionConfiguration sc)
        {
            CacheSessionProperties cacheProps = new CacheSessionProperties();

            cacheProps.CacheName                  = sc.CacheName;
            cacheProps.MaxMessagesPerTopic        = sc.MaxMsgs;
            cacheProps.MaxMessageAgeInSecs        = sc.MaxAge;
            cacheProps.CacheRequestTimeoutInMsecs = sc.Timeout;
            return(session.CreateCacheSession(cacheProps));
        }
Beispiel #2
0
        public bool ParseCacheSampleArgs(string[] args)
        {
            CacheSessionConfiguration cf = new CacheSessionConfiguration();

            this.sc = cf;
            Parse(args, this.sc);             //Parse common arguments

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-c":
                    i++;
                    if (i >= args.Length)
                    {
                        return(false);
                    }
                    cf.CacheName = args[i];
                    break;

                case "-m":
                    i++;
                    if (i >= args.Length)
                    {
                        return(false);
                    }
                    cf.MaxMsgs = System.Int32.Parse(args[i]);
                    break;

                case "-a":
                    i++;
                    if (i >= args.Length)
                    {
                        return(false);
                    }
                    cf.MaxAge = Int32.Parse(args[i]);
                    break;

                case "-l":
                    i++;
                    setLogLevel(args[i]);
                    break;

                case "-o":
                    i++;
                    if (i >= args.Length)
                    {
                        return(false);
                    }
                    cf.Timeout = System.Int32.Parse(args[i]);
                    break;

                case "-s":
                    i++;
                    if (i >= args.Length)
                    {
                        return(false);
                    }
                    cf.Subscribe = Boolean.Parse(args[i]);
                    break;

                case "-d":
                    i++;
                    if (i >= args.Length)
                    {
                        return(false);
                    }
                    cf.Action = ParseCacheLiveDataAction(args[i]);
                    break;

                default:
                    break;
                }
            }

            if (cf.CacheName == null)
            {
                System.Console.Error.WriteLine("No cache name specified");
                return(false);
            }
            return(true);
        }
        /// <summary>
        /// Main function in the sample.
        /// </summary>
        /// <param name="args"></param>
        public override void SampleCall(string[] args)
        {
            #region Parse Arguments
            ArgParser cmdLineParser = new ArgParser();

            if (!cmdLineParser.ParseCacheSampleArgs(args))
            {
                Console.WriteLine("Exception: " + INVALID_ARGUMENTS_ERROR);
                Console.Write(ArgParser.CacheArgUsage);
                return;
            }
            if (!SampleParseArgs(cmdLineParser))
            {
                // Parse failed for sample's arguments.
                Console.WriteLine("Exception: " + INVALID_ARGUMENTS_ERROR);
                Console.Write(ArgParser.CacheArgUsage);
                return;
            }
            #endregion

            #region Initialize properties from command line
            // Initialize the properties.
            ContextProperties contextProps = new ContextProperties();
            SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config);
            #endregion

            // Context and session.
            IContext context = null;
            ISession session = null;
            try
            {
                InitContext(cmdLineParser.LogLevel);
                Console.WriteLine("About to create the context ...");
                context = ContextFactory.Instance.CreateContext(contextProps, null);
                Console.WriteLine("Context successfully created. ");

                Console.WriteLine("About to create the session ...");
                session = context.CreateSession(sessionProps,
                                                SampleUtils.HandleMessageEvent,
                                                SampleUtils.HandleSessionEvent);
                Console.WriteLine("Session successfully created.");

                Console.WriteLine("About to connect the session ...");
                if (session.Connect() == ReturnCode.SOLCLIENT_OK)
                {
                    Console.WriteLine("Session successfully connected");
                    Console.WriteLine(GetRouterInfo(session));
                }
                if (session.IsCapable(CapabilityType.SUPPORTS_XPE_SUBSCRIPTIONS))
                {
                    Console.WriteLine("This sample requires a SolOS-TR version of the appliance, aborting");
                    return;
                }
                #region PUBLISH A MESSAGE (just to make sure there is one cached)
                ITopic topic = null;
                topic = ContextFactory.Instance.CreateTopic(SampleUtils.SAMPLE_TOPIC);
                IMessage message = ContextFactory.Instance.CreateMessage();
                message.Destination = topic;
                if (cmdLineParser.Config.startSequenceId != null && cmdLineParser.Config.endSequenceId != null)
                {
                    message.DeliveryMode = MessageDeliveryMode.Persistent;
                }
                else
                {
                    message.DeliveryMode = MessageDeliveryMode.Direct;
                }
                message.BinaryAttachment = Encoding.ASCII.GetBytes(SampleUtils.MSG_ATTACHMENTTEXT);
                session.Send(message);
                #endregion

                CacheSessionConfiguration cacheConfig  = (CacheSessionConfiguration)cmdLineParser.Config;
                ICacheSession             cacheSession = SampleUtils.newCacheSession(session, cacheConfig);
                ReturnCode rc;
                if (cmdLineParser.Config.startSequenceId != null && cmdLineParser.Config.endSequenceId != null)
                {
                    rc = cacheSession.SendCacheRequest(1, topic, cacheConfig.Subscribe, cacheConfig.Action, HandleCacheRequestResponse,
                                                       cmdLineParser.Config.startSequenceId.Value, cmdLineParser.Config.endSequenceId.Value);
                }
                else
                {
                    rc = cacheSession.SendCacheRequest(1, topic, cacheConfig.Subscribe, cacheConfig.Action, HandleCacheRequestResponse);
                }
                Console.WriteLine("Cache Response: " + rc.ToString());
                Console.WriteLine(string.Format("Waiting for async event or {0} secs (Whichever comes first)...", Timeout / 1000));
                waitForEvent.WaitOne(Timeout, false);
                IDictionary <Stats_Rx, Int64> stats = session.GetRxStats();
                SampleUtils.PrintRxStats(stats);
                Console.WriteLine("\nDone");
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
                if (context != null)
                {
                    context.Dispose();
                }
                // Must cleanup after
                CleanupContext();
            }
        }
        /// <summary>
        /// The main function in the sample.
        /// </summary>
        /// <param name="args"></param>
        public override void SampleCall(string[] args)
        {
            #region Parse Arguments
            ArgParser cmdLineParser = new ArgParser();
            if (!cmdLineParser.ParseCacheSampleArgs(args))
            {
                // Parse failed.
                Console.WriteLine("Exception: " + INVALID_ARGUMENTS_ERROR);
                Console.Write(ArgParser.CacheArgUsage);
                return;
            }
            #endregion

            #region Initialize properties from command line
            // Initialize the properties.
            ContextProperties contextProps = new ContextProperties();
            SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config);
            #endregion

            // Context and session.
            IContext context = null;
            ISession session = null;
            try
            {
                InitContext(cmdLineParser.LogLevel);
                Console.WriteLine("About to create the context ...");
                context = ContextFactory.Instance.CreateContext(contextProps, null);
                Console.WriteLine("Context successfully created. ");

                Console.WriteLine("About to create the Session ...");
                session = context.CreateSession(sessionProps,
                                                SampleUtils.HandleMessageEvent,
                                                SampleUtils.HandleSessionEvent);
                Console.WriteLine("Session successfully created.");

                Console.WriteLine("About to connect the session ...");
                if (session.Connect() == ReturnCode.SOLCLIENT_OK)
                {
                    Console.WriteLine("Session successfully connected");
                    Console.WriteLine(GetRouterInfo(session));
                }
                #region PUBLISH A MESSAGE (just to make sure there is one cached)

                ITopic topic = null;
                topic = ContextFactory.Instance.CreateTopic(SampleUtils.SAMPLE_TOPIC);
                IMessage message = ContextFactory.Instance.CreateMessage();
                message.Destination      = topic;
                message.DeliveryMode     = MessageDeliveryMode.Direct;
                message.BinaryAttachment = Encoding.ASCII.GetBytes(SampleUtils.MSG_ATTACHMENTTEXT);
                session.Send(message);
                #endregion

                CacheSessionConfiguration cacheConfig  = (CacheSessionConfiguration)cmdLineParser.Config;
                ICacheSession             cacheSession = SampleUtils.newCacheSession(session, cacheConfig);
                ReturnCode rc = cacheSession.SendCacheRequest(1, topic, cacheConfig.Subscribe, cacheConfig.Action);
                Console.WriteLine("Cache Response: " + rc.ToString());
                IDictionary <Stats_Rx, Int64> stats = session.GetRxStats();
                SampleUtils.PrintRxStats(stats);
                Console.WriteLine("\nDone");
            }
            catch (Exception ex)
            {
                PrintException(ex);
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
                if (context != null)
                {
                    context.Dispose();
                }
                // Must cleanup after.
                CleanupContext();
            }
        }