Exemplo n.º 1
0
        /*
         * Recursive stream printer for displaying a stream to the user.
         */
        public static string dumpStream(IStreamContainer s, int indent)
        {
            string    pad = getSpaces(indent);
            string    ret = pad + "(Dumping stream)\n";
            ISDTField e;

            while ((e = s.GetNext()) != null)
            {
                ret += pad + string.Format("[Type={0}, Val={1}]\n", e.Type, e.Value);
                if (e.Type == SDTFieldType.MAP)
                {
                    ret += dumpMap((IMapContainer)e.Value, indent + 4);
                }
                else if (e.Type == SDTFieldType.STREAM)
                {
                    ret += dumpStream((IStreamContainer)e.Value, indent + 4);
                }
            }
            s.Rewind();
            return(ret);
        }
        /// <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);
        }