Beispiel #1
0
        /// <summary>
        /// A Message receive callback delegate that prints the SDT content
        /// of received messages.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        public void PrintReceivedMessage(Object source, MessageEventArgs args)
        {
            IMessage      message   = args.Message;
            ISDTContainer container = SDTUtils.GetContainer(message);
            StringBuilder sb        = new StringBuilder();

            if (container is IMapContainer)
            {
                IMapContainer map = (IMapContainer)container;
                sb.Append("map={");
                while (map.HasNext())
                {
                    KeyValuePair <string, ISDTField> entry = map.GetNext();
                    sb.Append(string.Format("\n\tkey={0} value={1}", entry.Key, entry.Value.Value.ToString()));
                }
                sb.Append("}\n");
            }
            else if (container is IStreamContainer)
            {
                IStreamContainer stream = (IStreamContainer)container;
                sb.Append("stream={");
                while (stream.HasNext())
                {
                    ISDTField entry = stream.GetNext();
                    sb.Append(string.Format("\n\tvalue={0}", entry.Value.ToString()));
                }
                sb.Append("}\n");
            }
            SampleUtils.HandleMessageEvent(source, args);
            Console.WriteLine(sb.ToString());
        }
        /// <summary>
        /// Message event callback, this is where we handle message requests
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        private void HandleRequestMessage(Object source, MessageEventArgs args)
        {
            // Received a request message
            IMessage requestMessage = args.Message;

            Console.WriteLine("\nReceived request message, trying to parse it");

            // Parse it
            ISDTContainer sdt_data = SDTUtils.GetContainer(requestMessage);

            if (sdt_data is IStreamContainer)
            {
                IStreamContainer sdt_stream = (IStreamContainer)sdt_data;
                ISDTField        operation  = sdt_stream.GetNext();
                if (operation != null && operation.Type == SDTFieldType.INT8)
                {
                    ISDTField leftOperand = sdt_stream.GetNext();
                    if (leftOperand != null && leftOperand.Type == SDTFieldType.INT32)
                    {
                        ISDTField rightOperand = sdt_stream.GetNext();
                        if (rightOperand != null && rightOperand.Type == SDTFieldType.INT32)
                        {
                            int       enumVal = Int32.Parse(operation.Value.ToString());
                            Operation op      = (Operation)enumVal;
                            Console.WriteLine(ARITHMETIC_EXPRESSION,
                                              leftOperand.Value, op, rightOperand.Value, "?");
                            IMessage replyMessage =
                                ProcessArithmeticExpression((short)operation.Value, (Int32)leftOperand.Value, (Int32)rightOperand.Value);
                            try
                            {
                                Console.WriteLine("Sending replyMessage to requester...");
                                replyMessage.Destination = requestMessage.ReplyTo;
                                session.Send(replyMessage);
                                Console.WriteLine("Listening for request messages ... Press any key(except for Ctrl+C) to exit");
                                return;
                            }
                            catch (Exception ex)
                            {
                                PrintException(ex);
                                return;
                            }
                            finally
                            {
                                requestMessage.Dispose();
                                replyMessage.Dispose();
                            }
                        }
                    }
                }
            }
            // If we reach this point, the message format is not expected.
            Console.WriteLine("Failed to parse the request message, here's a message dump:\n{0}", requestMessage.Dump());
            requestMessage.Dispose(); /* A good practice*/
        }
        private void doRequest(Operation operation, int leftOperand, int rightOperand)
        {
            // Create the request message
            IMessage requestMessage = ContextFactory.Instance.CreateMessage();

            requestMessage.Destination  = ContextFactory.Instance.CreateTopic(requestTopicStr);
            requestMessage.DeliveryMode = MessageDeliveryMode.Direct; /* explicitly set to MessageDeliveryMode.Direct */
            IStreamContainer stream = SDTUtils.CreateStream(requestMessage, 256);

            stream.AddInt8((short)operation);
            stream.AddInt32(leftOperand);
            stream.AddInt32(rightOperand);

            // Send the request message to the service or RRDirectReplier
            IMessage replyMessage = null;
            int      timeout      = 2000; /* 2 secs*/

            Console.WriteLine("\nSending  request message, waiting for {0} msecs for a reply (make sure that RRDirectReply is running) ...", timeout);
            Console.WriteLine(ARITHMETIC_EXPRESSION,
                              leftOperand, operation.ToString(), rightOperand, "?");
            if (session.SendRequest(requestMessage, out replyMessage, 2000) == ReturnCode.SOLCLIENT_OK)
            {
                // Got a reply, format and print the response message
                Console.WriteLine("\nGot reply message");
                IStreamContainer respStream = (IStreamContainer)SDTUtils.GetContainer(replyMessage);
                if (respStream != null)
                {
                    ISDTField status = respStream.GetNext();
                    if (status.Type == SDTFieldType.BOOL)
                    {
                        if (((bool)status.Value))
                        {
                            Console.WriteLine(ARITHMETIC_EXPRESSION,
                                              leftOperand, operation.ToString(), rightOperand, respStream.GetNext().Value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(ARITHMETIC_EXPRESSION,
                                              leftOperand, operation.ToString(), rightOperand, "operation failed");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Request failed");
            }
            if (requestMessage != null)
            {
                // It is a good practice to dispose of messages once done using them
                requestMessage.Dispose();
            }
        }
Beispiel #4
0
        public void Convert(IMessage from, Message to)
        {
            var streamContainer = SDTUtils.GetContainer(from) as IStreamContainer;

            if (streamContainer != null)
            {
                using (streamContainer)
                {
                    var list = new List <object>();

                    while (streamContainer.HasNext())
                    {
                        list.Add(streamContainer.GetNext().Value);
                    }

                    if (list.Count > 0)
                    {
                        to.Properties["SdtStreamContainer"] = list;
                    }
                }
            }
        }
Beispiel #5
0
        private void doRequest(IDestination requestDestination, IDestination replyToQueue, Operation operation, int leftOperand, int rightOperand)
        {
            // Create the request message
            IMessage requestMessage = ContextFactory.Instance.CreateMessage();

            requestMessage.Destination  = requestDestination;
            requestMessage.DeliveryMode = MessageDeliveryMode.Persistent;
            requestMessage.ReplyTo      = replyToQueue;
            IStreamContainer stream = SDTUtils.CreateStream(requestMessage, 256);

            stream.AddInt8((short)operation);
            stream.AddInt32(leftOperand);
            stream.AddInt32(rightOperand);

            // Send the request message to the service or RRDirectReplier
            int timeout = 2000; /* 2 secs*/

            Console.WriteLine("\nSending  request message, waiting for {0} msecs for a reply (make sure that RRGuaranteedReplier is running) ...", timeout);
            session.Send(requestMessage);
            Console.WriteLine(ARITHMETIC_EXPRESSION,
                              leftOperand, operation.ToString(), rightOperand, "?");

            if (waitForReply.WaitOne(timeout))
            {
                // Got a reply, format and print the response message
                Console.WriteLine("\nGot reply message");
                IStreamContainer respStream = (IStreamContainer)SDTUtils.GetContainer(replyMessage);
                if (respStream != null)
                {
                    ISDTField status = respStream.GetNext();
                    if (status.Type == SDTFieldType.BOOL)
                    {
                        if (((bool)status.Value))
                        {
                            Console.WriteLine(ARITHMETIC_EXPRESSION,
                                              leftOperand, operation.ToString(), rightOperand, respStream.GetNext().Value.ToString());
                        }
                        else
                        {
                            Console.WriteLine(ARITHMETIC_EXPRESSION,
                                              leftOperand, operation.ToString(), rightOperand, "operation failed");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Failed to parse the request message, here's a message dump:\n{0}", replyMessage.Dump());
                    }
                }
                else
                {
                    Console.WriteLine("Failed to parse the request message, here's a message dump:\n{0}", replyMessage.Dump());
                }
            }
            else
            {
                Console.WriteLine(string.Format("Failed to receive a reply within {0} msecs", timeout));
            }
            // It is a good practice to dispose of messages once done using them
            if (requestMessage != null)
            {
                requestMessage.Dispose();
            }
            if (replyMessage != null)
            {
                replyMessage.Dispose();
            }
        }