Esempio n. 1
0
        public void StartRecievingMsgs()
        {
            MessageQueue        m_msgSenderQueue = new MessageQueue(@".\private$\client");
            XmlMessageFormatter formatter        = new XmlMessageFormatter(new Type[] { typeof(Fix_OrderAcceptedResponse), typeof(SubscriberInitializationInfo), typeof(AreYouAlive), typeof(SubscriptionStatus), typeof(Fix_OrderRejectionResponse), typeof(Fix_ExecutionReport), typeof(LogOutResponse), typeof(LogOutResponse), typeof(Fix_BusinessMessageReject), typeof(Fix_OrderReplaceCancelReject) });

            if (!m_msgSenderQueue.CanRead)
            {
                // warning
            }
            while (true)
            {
                try
                {
                    //MessageQueueTransaction mqt = new MessageQueueTransaction();
                    //mqt.Begin();
                    System.Messaging.Message msg = (System.Messaging.Message)m_msgSenderQueue.Receive();
                    if (msg == null)
                    {
                        continue;
                    }

                    object recievedMsg = formatter.Read(msg);
                    if (recievedMsg.GetType() == typeof(SubscriptionStatus))
                    {
                        SubscriptionStatus status = (SubscriptionStatus)recievedMsg;
                        if (status.IsSubscribed)
                        {
                            clientKey = status.ClientKey;
                            this.BeginInvoke(new Action(() =>
                            {
                                lblSub.ForeColor = Color.Green;
                                lblSub.Text      = "Subscribed";
                                btnSub.BackColor = Color.Red;
                                btnSub.Text      = "Unsubscribe";
                                isSubscribed     = true;
                                btnSub.Enabled   = true;
                            }
                                                        ));
                        }
                        else
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                lblSub.ForeColor = Color.Red;
                                lblSub.Text      = "Unsubscribed";
                                btnSub.BackColor = Color.Green;
                                btnSub.Text      = "Subscribe";
                                isSubscribed     = false;
                                btnSub.Enabled   = true;
                            }
                                                        ));
                            //using (OrdersProxy proxy = new OrdersProxy())
                            //{
                            //    string path = string.Format(@"Formatname:DIRECT=TCP:{0}", SystemConfigurations.GetAppSetting("ResponseQueue"));
                            //    proxy.SubscribeSession(path);
                            //}
                        }
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderAcceptedResponse))
                    {
                        Fix_OrderAcceptedResponse resp = (Fix_OrderAcceptedResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} accepted", resp.RequesterOrderID), Color.Green)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_ExecutionReport))
                    {
                        Fix_ExecutionReport resp = (Fix_ExecutionReport)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} TotalExecuted: {1}, TradeExecuted: {2}, Remaining {3}, Status : {4}", resp.RequesterOrderID, resp.TotalExecutedQuantity, resp.TradeExecutedQuantity, resp.RemainingQuantity, resp.OrderStatus), Color.Blue)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderSuspensionResponse))
                    {
                        Fix_OrderSuspensionResponse resp = (Fix_OrderSuspensionResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} suspended : {1}", resp.RequesterOrderID, resp.Message), Color.Red)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderRejectionResponse))
                    {
                        Fix_OrderRejectionResponse resp = (Fix_OrderRejectionResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} rejected : {1}", resp.RequesterOrderID, resp.RejectionReason), Color.Red)));
                    }
                    else if (recievedMsg.GetType() == typeof(AreYouAlive))
                    {
                        AreYouAlive resp = (AreYouAlive)recievedMsg;
                        //m_msgSenderQueue.Send(new AmAlive() { ClientKey = clientKey });
                        using (OrdersProxy proxy = new OrdersProxy())
                        {
                            proxy.HandleRequest(new AmAlive()
                            {
                                ClientKey = clientKey
                            });
                        }
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderReplacedResponse))
                    {
                        Fix_OrderReplacedResponse replace = (Fix_OrderReplacedResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} replaced : Quantity {1} price {2}", replace.RequesterOrderID, replace.Quantity, replace.Price), Color.DarkOrange)));
                    }
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Error Sending Msg: " + ex.ToString());
                    Console.ResetColor();
                }
            }
        }
        public void StartRecievingMsgs()
        {
            // MessageQueue m_msgSenderQueue = new MessageQueue(string.Format(@"{0}\private$\{1}", SystemConfigurations.GetAppSetting("ResponseQueueIP"), SystemConfigurations.GetAppSetting("ResponseQueueName")));
            MessageQueue m_msgSenderQueue = new MessageQueue(string.Format(@"Formatname:DIRECT=TCP:{0}\private$\{1}", SystemConfigurations.GetAppSetting("ResponseQueueIP"), SystemConfigurations.GetAppSetting("ResponseQueueName")));
            //List<Type> types = System.Reflection.Assembly.Load("Beltone.Services.Fix.Contract").GetTypes().Where(x=>x.IsClass).ToList();
            //types.Add(typeof(Dictionary<string, object>));
            //XmlMessageFormatter formatter = new XmlMessageFormatter(types.ToArray());

            XmlMessageFormatter formatter = new XmlMessageFormatter
                                                (new Type[] { typeof(Fix_OrderReplaceRefusedByService), typeof(FixAdminMsg), typeof(FixAdmin_TestRequest),
                                                              typeof(Fix_OrderAcceptedResponse), typeof(Fix_OrderRejectionResponse), typeof(Fix_ExecutionReport),
                                                              typeof(LogOutResponse), typeof(LogOutResponse), typeof(Fix_BusinessMessageReject), typeof(Fix_OrderReplaceCancelReject),
                                                              typeof(Fix_OrderReplacedResponse),
                                                              typeof(Fix_PendingReplaceResponse), typeof(Fix_OrderRefusedByService), typeof(Fix_PendingNewResponse), typeof(Fix_PendingCancelResponse),
                                                              typeof(Fix_OrderCanceledResponse) });

            //if (!m_msgSenderQueue.CanRead)
            //{
            //    // warning
            //}
            while (true)
            {
                try
                {
                    //MessageQueueTransaction mqt = new MessageQueueTransaction();
                    //mqt.Begin();
                    System.Messaging.Message msg = (System.Messaging.Message)m_msgSenderQueue.Receive();
                    if (msg == null)
                    {
                        continue;
                    }

                    object recievedMsg = formatter.Read(msg);

                    this.BeginInvoke(new Action(() => SetReport(string.Format("Recieved {0}", recievedMsg.GetType().ToString()), Color.DarkGray)));

                    if (recievedMsg.GetType() == typeof(FixAdmin_TestRequest))
                    {
                        try
                        {
                            IToAdminMsg msgTo = new FixAdmin_TestResponse()
                            {
                                TestKey = ((FixAdmin_TestRequest)recievedMsg).TestKey
                            };
                            _client.HandleMsg(msgTo);
                            //_client.HandleRequest(new FixAdmin_TestResponse() { TestKey = "" });
                        }
                        catch (Exception ex)
                        {
                            this.BeginInvoke(new Action(() => MessageBox.Show(ex.Message)));
                        }
                        this.BeginInvoke(new Action(() => SetReport("sent FixAdmin_TestResponse", Color.DarkGray)));
                    }
                    if (recievedMsg.GetType() == typeof(Fix_OrderAcceptedResponse))
                    {
                        Fix_OrderAcceptedResponse resp = (Fix_OrderAcceptedResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} accepted", resp.ReqOrdID), Color.Green)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_ExecutionReport))
                    {
                        Fix_ExecutionReport resp = (Fix_ExecutionReport)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} TotalExecuted: {1}, TradeExecuted: {2}, Remaining {3}, Status : {4}", resp.RequesterOrderID, resp.TotalExecutedQuantity, resp.TradeExecutedQuantity, resp.RemainingQuantity, resp.OrderStatus), Color.Blue)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderSuspensionResponse))
                    {
                        Fix_OrderSuspensionResponse resp = (Fix_OrderSuspensionResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} suspended : {1}", resp.RequesterOrderID, resp.Message), Color.Red)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderRejectionResponse))
                    {
                        Fix_OrderRejectionResponse resp = (Fix_OrderRejectionResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} rejected : {1}", resp.RequesterOrderID, resp.RejectionReason), Color.Red)));
                    }
                    else if (recievedMsg.GetType() == typeof(Fix_OrderReplacedResponse))
                    {
                        Fix_OrderReplacedResponse replace = (Fix_OrderReplacedResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} replaced : Quantity {1} price {2}", replace.ReqOrdID, replace.Qty, replace.Prc), Color.DarkGreen)));
                    }

                    else if (recievedMsg.GetType() == typeof(Fix_OrderRefusedByService))
                    {
                        Fix_OrderRefusedByService refuseMsg = (Fix_OrderRefusedByService)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} refused by service : msessage : {1} ", refuseMsg.RequesterOrderID, refuseMsg.RefuseMessage), Color.DarkOrange)));
                    }

                    else if (recievedMsg.GetType() == typeof(Fix_OrderCanceledResponse))
                    {
                        Fix_OrderCanceledResponse msgCanceled = (Fix_OrderCanceledResponse)recievedMsg;
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order : {0} canceld successfully ! ", msgCanceled.RequesterOrderID), Color.DarkOrange)));
                    }


                    else if (recievedMsg.GetType() == typeof(Fix_PendingNewResponse) || recievedMsg.GetType() == typeof(Fix_PendingReplaceResponse) || recievedMsg.GetType() == typeof(Fix_PendingCancelResponse))
                    {
                        this.BeginInvoke(new Action(() => SetReport(string.Format("order pending"), Color.DarkOrange)));
                    }
                }
                catch (Exception ex)
                {
                    this.BeginInvoke(new Action(() => SetReport(string.Format("Error : {0}", ex.Message), Color.Red)));
                    Thread.Sleep(2000);
                }
            }
        }