private void Add(QuotationForBroadcast quotationForBroadcast)
 {
     lock (this._QuotationForBroadcastQueue)
     {
         this._QuotationForBroadcastQueue.Enqueue(quotationForBroadcast);
         this._QuotationArriveEvent.Set();
     }
 }
        public static void AddQuotation(QuotationForBroadcast quotationForBroadcast)
        {
            if (EnableLog)
            {
                AppDebug.LogEvent("StateServer.QuotationBroadcastHelper",
                                  string.Format("Quotation from QuotationServer: {0}", quotationForBroadcast), EventLogEntryType.Information);
            }

            foreach (QuotationBroadcastHelper helper in QuotationBroadcastHelper._QuotationBroadcastHeplers)
            {
                helper.Add(quotationForBroadcast);
            }
        }
        private void Broadcast()
        {
            while (true)
            {
                this._QuotationArriveEvent.WaitOne();
                try
                {
                    while (true)
                    {
                        QuotationForBroadcast    quotation = null;
                        AutoFillResultInString[] autoFillResults;
                        lock (this._QuotationForBroadcastQueue)
                        {
                            //if (this._QuotationForBroadcastQueue.Count == 4)
                            {
                                quotation = this._Merger.MergeAndGetQuotationToBroadcast(this._QuotationForBroadcastQueue);
                                this._QuotationForBroadcastQueue.Clear();
                            }
                        }
                        if (quotation == null)
                        {
                            break;
                        }
                        if (EnableLog)
                        {
                            AppDebug.LogEvent("StateServer.QuotationBroadcastHelper",
                                              string.Format("Quotation send to TransactionServer: {0}", quotation), EventLogEntryType.Information);
                        }

                        Stopwatch watch = new Stopwatch();
                        watch.Start();
                        //XmlNode xmlHitOrders = this._TransactionServerService.SetQuotation(quotation.Token, quotation.OriginQuotations, quotation.OverridedQuotations, out autoFillResults);
                        string  hitOrders    = this.realtimeQuotationProcessorService.SetQuotation(quotation.Token, quotation.OriginQuotations, quotation.OverridedQuotations, out autoFillResults);
                        XmlNode xmlHitOrders = null;
                        if (!string.IsNullOrEmpty(hitOrders))
                        {
                            XmlDocument document = new XmlDocument();
                            document.LoadXml(hitOrders);
                            xmlHitOrders = document.DocumentElement;
                        }
                        watch.Stop();
                        AppDebug.LogEvent("StateServer",
                                          string.Format("QuotationBroadcastHepler.Broadcast, call TransactionServer.SetQuotation consume time = {0} ms", watch.ElapsedMilliseconds), EventLogEntryType.Information);
                        AutoFillResult[] autoFillResults2 = autoFillResults == null ? null : new AutoFillResult[autoFillResults.Length];
                        if (autoFillResults != null)
                        {
                            int index = 0;
                            foreach (AutoFillResultInString source in autoFillResults)
                            {
                                autoFillResults2[index++] = source.ToAutoFillResult();
                            }
                        }
                        Token token = quotation.Token == null ? StateServer.Token: quotation.Token;
                        this._StateServer.AfterBroadcastQuotationToTransactionServer(token, xmlHitOrders, autoFillResults2, this._TransactionServerService);
                    }
                }
                catch (Exception ex)
                {
                    AppDebug.LogEvent("StateServer",
                                      string.Format("QuotationBroadcastHepler.Broadcast to {0} call TransactionServer.SetQuotation error {1}", this.realtimeQuotationProcessorServiceUrl, ex), EventLogEntryType.Error);
                    this.realtimeQuotationProcessorService = CreateRealtimeQuotationProcessorService(this.realtimeQuotationProcessorServiceUrl);
                }
            }
        }
        internal QuotationForBroadcast MergeAndGetQuotationToBroadcast(Queue <QuotationForBroadcast> pendingQuotations)
        {
            if (pendingQuotations.Count > 0)
            {
                if (pendingQuotations.Count > 3)
                {
                    string warning = string.Format("Too much {0} quotation wait for sending to TransactionServer", pendingQuotations.Count);
                    AppDebug.LogEvent("StateServer.QuotationBroadcastHelper", warning, EventLogEntryType.Warning);
                }

                foreach (QuotationForBroadcast pendingQuotation in pendingQuotations)
                {
                    if (pendingQuotation.OverridedQuotations == null)
                    {
                        continue;
                    }

                    foreach (OverridedQuotation overridedQuotation in pendingQuotation.OverridedQuotations)
                    {
                        if (string.IsNullOrEmpty(overridedQuotation.Ask) || string.IsNullOrEmpty(overridedQuotation.Bid))
                        {
                            continue;
                        }

                        string mergedQuotationKey = overridedQuotation.InstrumentID.ToString() + overridedQuotation.QuotePolicyID.ToString();

                        MergedQuotation mergedQuotation = null;
                        if (!this.mergedQuotaions.TryGetValue(mergedQuotationKey, out mergedQuotation))
                        {
                            mergedQuotation = new MergedQuotation(overridedQuotation.InstrumentID, overridedQuotation.QuotePolicyID);
                            this.mergedQuotaions.Add(mergedQuotationKey, mergedQuotation);
                        }

                        OriginQuotation originQuotation = null;
                        if (pendingQuotation.OriginQuotations != null)
                        {
                            originQuotation = pendingQuotation.OriginQuotations.FirstOrDefault <OriginQuotation>(t => { return(t.InstrumentID == overridedQuotation.InstrumentID); });
                        }
                        mergedQuotation.Merge(pendingQuotation.Token, overridedQuotation, originQuotation);
                    }
                }
            }


            List <OverridedQuotation> overridedQuotations = null;
            List <OriginQuotation>    originQuotations    = null;
            Token token = null;

            foreach (MergedQuotation mergedQuotation in this.mergedQuotaions.Values)
            {
                QuotationPair pair = mergedQuotation.Fetch();
                if (pair != null)
                {
                    if (overridedQuotations == null)
                    {
                        overridedQuotations = new List <OverridedQuotation>();
                    }
                    overridedQuotations.Add(pair.OverridedQuotation);
                    if (token != null)
                    {
                        token = pair.Token;
                    }

                    if (pair.OriginQuotation != null)
                    {
                        if (originQuotations == null)
                        {
                            originQuotations = new List <OriginQuotation>();
                        }
                        if (!originQuotations.Contains(pair.OriginQuotation))
                        {
                            originQuotations.Add(pair.OriginQuotation);
                        }
                    }
                }
            }

            if (overridedQuotations != null)
            {
                QuotationForBroadcast result = new QuotationForBroadcast();
                result.OverridedQuotations = overridedQuotations.ToArray();
                result.Token            = token;
                result.OriginQuotations = originQuotations == null ? null : originQuotations.ToArray();
                return(result);
            }
            else
            {
                return(null);
            }
        }