/// <summary>
        /// Process real time data subscription
        /// </summary>
        public IBusMessageOutbound ProcessSubscribe(IBusMessageInbound envelop, PinkoMsgCalculateExpression expression)
        {
            var response = (IBusMessageOutbound)envelop;
            var marketEnv = PinkoMarketEnvManager.GetMarketEnv(expression.DataFeedIdentifier.MaketEnvId);
            var resultMsg = new PinkoMsgCalculateExpressionResult().FromRequest(expression);

            response.Message = resultMsg;
            response.WebRoleId = expression.DataFeedIdentifier.WebRoleId;

            var finalFormExp = expression.GetExpression();

            Trace.TraceInformation("Calculating: {0}", expression.Verbose());

            //
            // Typed expressions supported are double and double[]
            //
            switch (resultMsg.ResultType)
            {
                // Single double result
                case PinkoCalculateExpressionDaoExtensions.ResultDouble:
                    {
                        var complExp = PinkoExpressionEngine.ParseAndCompile<double[]>(finalFormExp);
                        var results = PinkoExpressionEngine.Invoke(marketEnv, complExp);

                        if (null != results)
                            resultMsg.ResultsTupples = expression.ExpressionFormulas.GetTupleResult(results);
                    }
                    break;

                // 2-dim double[][]
                case PinkoCalculateExpressionDaoExtensions.ResultDoubleSeries:
                    {
                        var complExp = PinkoExpressionEngine.ParseAndCompile<double[][]>(finalFormExp);
                        var results = PinkoExpressionEngine.Invoke(marketEnv, complExp);

                        if (null != results)
                            resultMsg.ResultsTupples = expression.ExpressionFormulas.GetTupleResult(results);
                    }
                    break;

                default:
                    {
                        resultMsg.ResultType = PinkoErrorCode.FormulaTypeNotSupported;
                        response.ErrorCode = PinkoErrorCode.FormulaTypeNotSupported;
                        response.ErrorDescription = PinkoMessagesText.FormulaNotSupported;
                        response.ErrorSystem = PinkoMessagesText.FormulaNotSupported;
                    }
                    break;
            }

            return response;
        }
        /// <summary>
        /// Process real time data subscription
        /// </summary>
        public IBusMessageOutbound ProcessSubscribe(IBusMessageInbound envelop, PinkoMsgCalculateExpression expression)
        {
            var response = (IBusMessageOutbound)envelop;
            var resultMsg = new PinkoMsgCalculateExpressionResult().FromRequest(expression);

            response.Message = resultMsg;
            response.WebRoleId = expression.DataFeedIdentifier.WebRoleId;

            // Wait for calculation to end before processing standard request.
            // Avoid have to lock while performing parallel calculations.
            //
            // WARNING: Subscriber collection are NOT thread safe
            //
            using (CalculatingMutex.LockDisposible())
            {
                switch (expression.MsgAction)
                {
                    case PinkoMessageAction.ManagerSubscription:
                        {
                            //
                            // Typed expressions supported are double and double[]
                            //
                            switch (resultMsg.ResultType)
                            {
                                // Single double result
                                case PinkoCalculateExpressionDaoExtensions.ResultDouble:
                                    {
                                        SubscribersDouble.UpdateSubscriber(resultMsg, () => PinkoExpressionEngine.ParseAndCompile<double[]>(expression.GetExpression()));
                                    }
                                    break;

                                // 2-dim double[][]
                                case PinkoCalculateExpressionDaoExtensions.ResultDoubleSeries:
                                    {
                                        SubscribersDoubleDouble.UpdateSubscriber(resultMsg, () => PinkoExpressionEngine.ParseAndCompile<double[][]>(expression.GetExpression()));
                                    }
                                    break;

                                // Invalid result type
                                default:
                                    {
                                        resultMsg.ResultType = PinkoErrorCode.FormulaTypeNotSupported;
                                        response.ErrorCode = PinkoErrorCode.FormulaTypeNotSupported;
                                        response.ErrorDescription = PinkoMessagesText.FormulaNotSupported;
                                        response.ErrorSystem = PinkoMessagesText.FormulaNotSupported;
                                    }
                                    break;
                            }
                        }
                        break;

                    //case PinkoMessageAction.ReconnectSubscribe:
                    //    {
                    //        // TODO: Add unsubscribe
                    //    }
                    //    break;

                    default:
                        {
                            resultMsg.ResultType = PinkoErrorCode.ActionNotSupported;
                            response.ErrorCode = PinkoErrorCode.ActionNotSupported;
                            response.ErrorDescription = PinkoMessagesText.ActionNotSupported;
                            response.ErrorSystem = PinkoMessagesText.ActionNotSupported;
                        }
                        break;
                }
            }

            return response;
        }
Пример #3
0
        public void TestExpressionBuild()
        {
            var msgObj = new PinkoMsgCalculateExpression
            {
                ExpressionFormulas = SampleMockData.GetPinkoUserExpressionFormula(3),
            };

            var expression = msgObj.GetExpression();
            Assert.IsTrue(expression.Reduce() == "{ Lbl_0 = 0.1 * 0.2; Lbl_1 = 1.1 * 1.2; Lbl_2 = 2.1 * 2.2; [  Lbl_0, Lbl_1, Lbl_2 ]  }".Reduce());
        }