/// <summary> /// Computes the results and stores it in a reply message /// </summary> /// <param name="operation"></param> /// <param name="leftOperand"></param> /// <param name="rightOperand"></param> /// <returns></returns> private IMessage ProcessArithmeticExpression(short operation, Int32 leftOperand, Int32 rightOperand) { // Create the reply message IMessage replyMessage = ContextFactory.Instance.CreateMessage(); replyMessage.DeliveryMode = MessageDeliveryMode.Direct; IStreamContainer stream = SDTUtils.CreateStream(replyMessage, 256); double? result = null; try { switch ((int)operation) { case (int)Operation.DIVIDE: result = (double)((double)leftOperand / (double)rightOperand); break; case (int)Operation.MINUS: result = (double)(leftOperand - rightOperand); break; case (int)Operation.PLUS: result = (double)(leftOperand + rightOperand); break; case (int)Operation.TIMES: result = (double)(leftOperand * rightOperand); break; default: result = null; break; } if (result == null) { stream.AddBool(false); } else { double val = result.Value; if (double.IsNaN(val) || double.IsInfinity(val)) { stream.AddBool(false); } else { stream.AddBool(true); stream.AddDouble(val); } } } catch (Exception) { stream.Rewind(); stream.AddBool(false); } Operation opEnum = (Operation)((int)operation); if (Enum.IsDefined(typeof(Operation), (Int32)operation)) { Console.WriteLine(ARITHMETIC_EXPRESSION, leftOperand, opEnum, rightOperand, (result == null ? "operation failed" : result.ToString())); } else { Console.WriteLine(ARITHMETIC_EXPRESSION, leftOperand, "UNKNOWN", rightOperand, (result == null ? "operation failed" : result.ToString())); } return(replyMessage); }
/// <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.Parse(args)) { // parse failed PrintUsage(INVALID_ARGUMENTS_ERROR); return; } #endregion #region Initialize properties from command line // Initialize the properties. ContextProperties contextProps = new ContextProperties(); SessionProperties sessionProps = SampleUtils.NewSessionPropertiesFromConfig(cmdLineParser.Config); #endregion // Define IContext and ISession and ITopic. IContext context = null; ISession session = null; ITopic topic = 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, PrintReceivedMessage, 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)); } topic = ContextFactory.Instance.CreateTopic(SampleUtils.SAMPLE_TOPIC); Console.WriteLine("About to subscribe to topic " + SampleUtils.SAMPLE_TOPIC); if (session.Subscribe(topic, true) == ReturnCode.SOLCLIENT_OK) { Console.WriteLine("Successfully added topic subscription"); } // Create the message independent stream. IStreamContainer stream = SDTUtils.CreateStream(1024); // Populate the stream. stream.AddDouble(3.141592654); stream.AddString("message"); // Create the message-independent map. IMapContainer map = SDTUtils.CreateMap(1024); // Add a well known integer to the map. map.AddInt32("mersenne", 43112609); // Create the message. IMessage message = ContextFactory.Instance.CreateMessage(); // Set the message delivery options. message.DeliveryMode = MessageDeliveryMode.Direct; message.Destination = topic; int numMsgsToSend = 10; Console.WriteLine(string.Format("About to send {0} messages ...", numMsgsToSend)); for (int i = 0; i < numMsgsToSend; i++) { // Overwrite the "message" field, set the container, and send. map.DeleteField("message"); map.AddString("message", "message" + (i + 1)); // Set the user property map to the map. message.UserPropertyMap = map; SDTUtils.SetSDTContainer(message, stream); session.Send(message); } // Dispose of the map, stream, and message. map.Dispose(); stream.Dispose(); message.Dispose(); Thread.Sleep(500); // wait for 0.5 seconds Console.WriteLine("\nDone"); } catch (Exception ex) { PrintException(ex); } finally { if (session != null) { if (topic != null) { session.Unsubscribe(topic, true); } session.Dispose(); } if (context != null) { context.Dispose(); } // Must cleanup after. CleanupContext(); } }