Пример #1
0
        /// <summary>
        /// Subscribe to operations on this source accounts. Needed if we are to place orders properly etc.
        /// </summary>
        /// <returns></returns>
        public bool SubscribeToAccounts()
        {
            // Send a subscribe for this account too.
            SubscribeToSourceAccountsUpdatesMessage subscribeRequest = new SubscribeToSourceAccountsUpdatesMessage(true);
            ResponceMessage subscribeResponce = this.SendAndReceiveResponding <ResponceMessage>(
                SourceTransportInfo, subscribeRequest);

            if (subscribeResponce == null || subscribeResponce.OperationResult == false)
            {
                SystemMonitor.OperationError("Failed to subscribe to account.");
                return(false);
            }

            return(true);
        }
        ResponceMessage Receive(SubscribeToSourceAccountsUpdatesMessage message)
        {
            lock (this)
            {
                if (message.Subscribe)
                {
                    _subscribers.Add(message.TransportInfo);
                }
                else
                {
                    _subscribers.Remove(message.TransportInfo);
                }
            }

            return(new ResponceMessage(true));
        }
        ResponceMessage Receive(SubscribeToSourceAccountsUpdatesMessage message)
        {
            if (message.TransportInfo.OriginalSenderId.HasValue == false)
            {
                SystemMonitor.Error("Failed to establish original sender id.");
                return null;
            }

            if (message.Subscribe)
            {
                List<OrderInfo> orderInfos = new List<OrderInfo>();
                lock (this)
                {
                    _subscribers[message.TransportInfo.OriginalSenderId.Value] = message.TransportInfo;

                    // Send an update of current orderInfo to new subscriber.
                    foreach (KeyValuePair<string, OrderInfo?> pair in _orders)
                    {
                        if (pair.Value.HasValue)
                        {
                            orderInfos.Add(pair.Value.Value);
                        }
                    }
                }

                if (orderInfos.Count > 0)
                {
                    TransportInfo transportInfo = message.TransportInfo.Clone();

                    GeneralHelper.FireAndForget(delegate()
                    {// Make sure the result of the current call is returned before sending an initial update.
                        Thread.Sleep(500);

                        OrdersInformationUpdateResponceMessage updateMessage = new OrdersInformationUpdateResponceMessage(_accountInfo,
                            orderInfos.ToArray(), true);

                        this.SendResponding(transportInfo, updateMessage);
                    });
                }
            }
            else
            {
                lock (this)
                {
                    _subscribers.Remove(message.TransportInfo.OriginalSenderId.Value);
                }
            }

            if (message.RequestResponce)
            {
                return new ResponceMessage(true);
            }

            return null;
        }
        /// <summary>
        /// Subscribe to operations on this source accounts. Needed if we are to place orders properly etc.
        /// </summary>
        /// <returns></returns>
        public bool UpdateAccountsSubscription(bool subscribe)
        {
            // Send a subscribe for this account too.
            SubscribeToSourceAccountsUpdatesMessage subscribeRequest = new SubscribeToSourceAccountsUpdatesMessage(subscribe);

            ResponseMessage subscribeResponse = this.SendAndReceiveResponding<ResponseMessage>(
                SourceTransportInfo, subscribeRequest);

            if (subscribeResponse == null || subscribeResponse.OperationResult == false)
            {
                SystemMonitor.OperationError("Failed to subscribe to account.");
                return false;
            }

            return true;
        }
        ResponseMessage Receive(SubscribeToSourceAccountsUpdatesMessage message)
        {
            lock (this)
            {
                if (message.Subscribe)
                {
                    _subscribers.Add(message.TransportInfo);
                }
                else
                {
                    _subscribers.Remove(message.TransportInfo);
                }
            }

            return new ResponseMessage(true);
        }