Exemple #1
0
        public MarketIncrementalProcessor(ChannelUMDFState state, ChannelUMDFConfig config, MonitorConfig monitorConfig) :
            base(config.MDIncrementalHost, config.MDIncrementalPorta, config.TemplateFile, config.ChannelID, config.LocalInterfaceAddress)
        {
            this.channelConfig = config;
            this.channelState  = state;
            this.processorType = ProcessorType.MarketIncremental;
            this.monitorConfig = monitorConfig;

            ProcessamentoMensagensEnabled    = false;
            ProcessamentoMensagensLockObject = new object();
            ReplayLockObject = new object();

            logger = LogManager.GetLogger("MarketIncrementalProcessor-" + config.ChannelID);

            MDSUtils.AddAppender("MarketIncrementalProcessor-" + config.ChannelID, logger.Logger);

            //if (int.Parse(channelConfig.ChannelID) <= 10)
            //    tcpReplayBMF = TCPReplayBMF.GetInstance(this, channelConfig, config.TemplateFile, qUdpPkt, ReplayLockObject);
            //else
            //    tcpReplayBovespa = new TCPReplayBovespa(this, channelConfig, config.TemplateFile, qUdpPkt, ReplayLockObject);

            //if (channelConfig.IsPuma)
            //    tcpReplayBMF = TCPReplayBMF.GetInstance(this, channelConfig, config.TemplateFile, qUdpPktReplay, ReplayLockObject);
            //else
            //    tcpReplayBovespa = new TCPReplayBovespa(this, channelConfig, config.TemplateFile, qUdpPktReplay, ReplayLockObject);

            fixInitiator = new FixInitiator(this, channelConfig, config.TemplateFile, qUdpPktReplay, ReplayLockObject);
        }
Exemple #2
0
        public virtual void OnLogout(QuickFix.SessionID session)
        {
            try
            {
                logger.Info("OnLogout()");


                string msg = String.Format("Logout da sessao: {0}-{1}-{2}-{3}", session.SenderCompID,
                                           session.SenderSubID,
                                           session.TargetCompID,
                                           session.TargetSubID);

                if (DateTime.Now.DayOfWeek != DayOfWeek.Sunday && DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
                {
                    if (DateTime.Now.Hour >= ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_INICIO &&
                        DateTime.Now.Hour < ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_FIM)
                    {
                        MDSUtils.EnviarEmail("FIX Logout", msg);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("OnLogout(): " + ex.Message, ex);
            }

            _bConectadoBolsa = false;
        }
        public SecurityListProcessor(ChannelUMDFState state, ChannelUMDFConfig config, MonitorConfig monitorConfig) :
            base(config.SecurityListHost, config.SecurityListPorta, config.SecurityListHostSec,
                 config.SecurityListPortaSec, config.TemplateFile, config.ChannelID, config.LocalInterfaceAddress)
        {
            this.channelConfig = config;
            this.channelState  = state;
            this.processorType = ProcessorType.SecurityList;
            this.monitorConfig = monitorConfig;

            logger = LogManager.GetLogger("SecurityList-" + config.ChannelID);

            MDSUtils.AddAppender("SecurityList-" + config.ChannelID, logger.Logger);
        }
Exemple #4
0
        private string GerarSonda()
        {
            EventoHttpSonda sonda = new EventoHttpSonda();

            sonda.cabecalho = MDSUtils.montaCabecalhoStreamer(ConstantesMDS.TIPO_REQUISICAO_HB_SONDA, null, 0, null, 2, null);
            sonda.sonda     = new Dictionary <string, string>();
            sonda.sonda.Add(ConstantesMDS.HTTP_SONDA_DATA, DateTime.Now.ToString("yyyyMMdd"));
            sonda.sonda.Add(ConstantesMDS.HTTP_SONDA_HORA, DateTime.Now.ToString("HHmmss"));

            string mensagem = Newtonsoft.Json.JsonConvert.SerializeObject(sonda);

            return(MDSUtils.montaMensagemHttp(ConstantesMDS.TIPO_REQUISICAO_HB_SONDA, null, null, mensagem));
        }
        public MarketRecoveryProcessor(ChannelUMDFState state, ChannelUMDFConfig config, MonitorConfig monitorConfig) :
            base(config.MDRecoveryHost, config.MDRecoveryPorta, config.MDRecoveryHostSec,
                 config.MDRecoveryPortaSec, config.TemplateFile, config.ChannelID, config.LocalInterfaceAddress)
        {
            this.channelConfig = config;
            this.channelState  = state;
            this.processorType = ProcessorType.MarketRecovery;
            this.monitorConfig = monitorConfig;

            logger = LogManager.GetLogger("MarketRecoveryProcessor-" + config.ChannelID);

            MDSUtils.AddAppender("MarketRecoveryProcessor-" + config.ChannelID, logger.Logger);
        }
        /// <summary>
        /// Thread de despacho final das mensagems
        /// </summary>
        private void mainOutboundProc()
        {
            logger.Info("Iniciando thread de despacho final");

            long lastLog = 0;

            while (_bKeepRunning)
            {
                try
                {
                    if (currentFeed < 0)
                    {
                        Thread.Sleep(250);
                        continue;
                    }

                    QuickFix.FIX44.Message message = null;
                    if (mainOutboundQ.TryDequeue(out message))
                    {
                        int    msgSeqNum = message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum);
                        string msgType   = message.Header.GetString(QuickFix.Fields.Tags.MsgType);

                        logger.Debug("Dispatching Message [" + message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum) + "] type [" + message.Header.GetString(QuickFix.Fields.Tags.MsgType) + "]");

                        if (UnderFIXMessageFire != null)
                        {
                            FIXMachineGunEventEventArgs args = new FIXMachineGunEventEventArgs();
                            args.Message = message;

                            UnderFIXMessageFire(this, args);
                        }

                        if (MDSUtils.shouldLog(lastLog))
                        {
                            logger.InfoFormat("Mensagens FIX na fila mainOutboundQ: {0}", mainOutboundQ.Count);
                            lastLog = DateTime.UtcNow.Ticks;
                        }

                        continue;
                    }

                    Thread.Sleep(25);
                }
                catch (Exception ex)
                {
                    logger.Error("mainOutboundProc: " + ex.Message, ex);
                }
            }

            logger.Info("Fim da thread mainOutboundProc");
        }
Exemple #7
0
        private void monitoraCanal()
        {
            logger.Info("Iniciando monitoracao do canal incremental [" + channelConfig.ChannelID + "]");
            long lastWatchDog = DateTime.UtcNow.Ticks;

            while (isRunning)
            {
                try
                {
                    if (mktIncrProc != null && !mktIncrProc.IsAlive() && shouldMonitor)
                    {
                        string subject = "REINICIANDO CANAL [" + channelConfig.ChannelID + "]";
                        string msg     = subject + "\r\n" + "reiniciando as " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss.fff");

                        logger.Warn(subject);

                        if (DateTime.Now.DayOfWeek != DayOfWeek.Sunday && DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
                        {
                            if (DateTime.Now.Hour >= ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_INICIO &&
                                DateTime.Now.Hour < ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_FIM)
                            {
                                MDSUtils.EnviarEmail(subject, msg);
                            }
                        }

                        finalizaCanal();
                        Thread.Sleep(10000);

                        iniciaCanal();
                        Thread.Sleep(10000);
                    }
                    else
                    {
                        TimeSpan ts = new TimeSpan(DateTime.UtcNow.Ticks - lastWatchDog);
                        if (ts.TotalMilliseconds > 30000)
                        {
                            logger.Info("Canal [" + channelConfig.ChannelID + "] ativo");
                            lastWatchDog = DateTime.UtcNow.Ticks;
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("monitoraCanal(): " + ex.Message, ex);
                }

                Thread.Sleep(250);
            }
        }
        private void splitterThreadWork()
        {
            logger.Info("Iniciando thread de splitter processor");

            long lastLog = 0;

            while (_bKeepRunning)
            {
                try
                {
                    QuickFix.FIX44.Message message = null;

                    if (queueToSplit.TryDequeue(out message))
                    {
                        logger.Debug("Splitting Message [" + message.Header.GetInt(34) + "] type [" + message.Header.GetString(35) + "]");

                        List <QuickFix.FIX44.Message> lstSplitted = FIXUtils.splitMessage(message, 0, null);

                        foreach (QuickFix.FIX44.Message splittedmsg in lstSplitted)
                        {
                            enqueueToProcess(splittedmsg);
                        }

                        if (MDSUtils.shouldLog(lastLog))
                        {
                            logger.Info("Mensagens para splitting FIX na fila: " + queueToSplit.Count);
                            lastLog = DateTime.UtcNow.Ticks;
                        }

                        continue;
                    }

                    Thread.Sleep(25);
                }
                catch (Exception ex)
                {
                    logger.Error("splitterProc: " + ex.Message, ex);
                }
            }

            logger.Info("Thread de splitter finalizada");
        }
Exemple #9
0
        /// <summary>
        ///
        ///
        /// </summary>
        private void procANGQueueNegocio()
        {
            long lstEvent = 0;

            logger.Info("Inicializando thread do processamento da fila de eventos de Negocios ANG");

            while (bKeepRunning)
            {
                try
                {
                    EventoNegocioANG e;
                    if (queueANG.TryDequeue(out e))
                    {
                        if (OnEventoNegocioAnaliseGrafica != null)
                        {
                            AnaliseGraficaNegocioEventArgs args = new AnaliseGraficaNegocioEventArgs();
                            args.Evento = e;
                            OnEventoNegocioAnaliseGrafica(this, args);
                        }

                        if (MDSUtils.shouldLog(lstEvent))
                        {
                            lstEvent = DateTime.UtcNow.Ticks;
                            logger.Info("Fila queueANG: " + queueANG.Count + " eventos.");
                        }

                        continue;
                    }

                    lock (syncQueueANG)
                    {
                        Monitor.Wait(syncQueueANG, QUEUE_WAIT_TIMEOUT);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("procANGQueueNegocio: " + ex.Message, ex);
                }
            }
        }
Exemple #10
0
        private void procHBQueueLOF()
        {
            long lstEvent = 0;

            logger.Info("Inicializando thread do processamento da fila de eventos de Livro de Ofertas HomeBroker");

            while (bKeepRunning)
            {
                try
                {
                    EventoHBLivroOfertas e;
                    if (queueHBLOF.TryDequeue(out e))
                    {
                        if (OnEventoHBLivroOfertas != null)
                        {
                            HBLivroOfertasEventArgs args = new HBLivroOfertasEventArgs();
                            args.Evento = e;
                            OnEventoHBLivroOfertas(this, args);
                        }

                        if (MDSUtils.shouldLog(lstEvent))
                        {
                            lstEvent = DateTime.UtcNow.Ticks;
                            logger.Info("Fila queueHBLOF: " + queueHBLOF.Count + " eventos.");
                        }

                        continue;
                    }

                    lock (syncQueueHBLOF)
                    {
                        Monitor.Wait(syncQueueHBLOF, QUEUE_WAIT_TIMEOUT);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("procHBQueueLOF: " + ex.Message, ex);
                }
            }
        }
        private void fetchSymbols(Message message)
        {
            try
            {
                SequenceValue sequence;

                if (message.IsDefined("RelatedSymbols"))
                {
                    sequence = message.GetSequence("RelatedSymbols");
                }
                else
                {
                    sequence = message.GetSequence("RelatedSym");
                }

                int ocorrencias = sequence.Length;

                logger.Debug("LISTA_INSTRUMENTO Sequencial[" +
                             message.GetInt("MsgSeqNum") +
                             "] Ocorrencias[" + ocorrencias + "]");

                List <Message> mensagens = UmdfUtils.splitMessage(message, channelConfig.MarketDepth, ConstantesUMDF.FAST_MSGTYPE_SECURITYLIST_SINGLE);

                foreach (Message newMessage in mensagens)
                {
                    MDSUtils.EnqueueEventoUmdf(newMessage,
                                               this.channelConfig.SecurityListTemplateID,
                                               this.channelConfig.ChannelID,
                                               this.channelConfig.Segment,
                                               ConstantesUMDF.FAST_MSGTYPE_SECURITYLIST_SINGLE,
                                               channelConfig.MarketDepth,
                                               StreamTypeEnum.STREAM_TYPE_SECURITY_DEFINITION);
                }
            }
            catch (Exception e)
            {
                logger.Error("Campo nao encontrado na mensagem: " + e.Message, e);
            }
        }
Exemple #12
0
 public ServidorConexaoHB()
 {
     logger       = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
     myThreadName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();
     MDSUtils.AddAppender("ServidorConexaoHB-", logger.Logger);
 }
        protected override void umdfMessageProcessor()
        {
            try
            {
                logger.Debug("Processando mensagens de recovery");

                string recoveryTemplateID = channelConfig.MDRecoveryTemplateID.ToString();
                listaRecoveryTemplateID = recoveryTemplateID.Split(",".ToCharArray());
                long lastLogTicks = 0;

                while (bKeepRunning)
                {
                    try
                    {
                        //Message message = this.filaMensagensUMDF.take();
                        Message message;
                        if (!queueToProcessor.TryDequeue(out message))
                        {
                            Thread.Sleep(50);
                            continue;
                        }

                        // Notifica o fim do snapshot para inicio do tratamento do incremental
                        if (listaRecoveryTemplateID.Any(message.Template.Id.Contains))
                        {
                            List <Message> mensagens = UmdfUtils.splitMessage(message, channelConfig.MarketDepth, ConstantesUMDF.FAST_MSGTYPE_SNAPSHOT_SINGLE);

                            foreach (Message newMessage in mensagens)
                            {
                                MDSUtils.EnqueueEventoUmdf(newMessage,
                                                           this.channelConfig.MDRecoveryTemplateID,
                                                           this.channelConfig.ChannelID,
                                                           this.channelConfig.Segment,
                                                           ConstantesUMDF.FAST_MSGTYPE_SNAPSHOT_SINGLE,
                                                           channelConfig.MarketDepth,
                                                           StreamTypeEnum.STREAM_TYPE_MARKET_RECOVERY);

                                if (MDSUtils.shouldLog(lastLogTicks))
                                {
                                    lastLogTicks = DateTime.UtcNow.Ticks;

                                    string msgTruncado = (message.ToString().Length < 200 ? message.ToString() : message.ToString().Substring(0, 200));
                                    monitorConfig.channels[channelConfig.ChannelID].dateTimeStartedStopped =
                                        "Snapshot - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " - " + msgTruncado;
                                    monitorConfig.channels[channelConfig.ChannelID].AddDetails(
                                        "2) Snapshot", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), msgTruncado);
                                }
                            }

                            if (message.IsDefined("MsgSeqNum") && message.IsDefined("TotNumReports"))
                            {
                                int seqNum        = message.GetInt("MsgSeqNum");     //34
                                int totNumReports = message.GetInt("TotNumReports"); //911

                                if (seqNum == 1)
                                {
                                    int LastMsgSeqNumProcessed = message.GetInt("LastMsgSeqNumProcessed");
                                    bCanProcess = true;

                                    logger.Info("Inicio do processamento do snapshot: " + totNumReports + " msgs");
                                    logger.Info("Notificando observers, LastMsgSeqNumProcessed:" + LastMsgSeqNumProcessed);

                                    RecoveryStartedEventArgs e = new RecoveryStartedEventArgs();
                                    e.LastMsgSeqNumProcessed = message.GetInt("LastMsgSeqNumProcessed");
                                    if (this.OnRecoveryStarted != null)
                                    {
                                        OnRecoveryStarted(this, e);
                                    }
                                }

                                if (seqNum == totNumReports && bCanProcess)
                                {
                                    int LastMsgSeqNumProcessed = message.GetInt("LastMsgSeqNumProcessed");

                                    logger.Info("Fim do processamento do snapshot: " + totNumReports + " msgs");
                                    logger.Info("Notificando observers, LastMsgSeqNumProcessed:" + LastMsgSeqNumProcessed);

                                    RecoveryCompletedEventArgs e = new RecoveryCompletedEventArgs();
                                    e.LastMsgSeqNumProcessed = message.GetInt("LastMsgSeqNumProcessed");

                                    monitorConfig.channels[channelConfig.ChannelID].dateTimeStartedStopped =
                                        "Snapshot - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ": Snapshot completo!";

                                    monitorConfig.channels[channelConfig.ChannelID].AddDetails(
                                        "2) Snapshot", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                                        "Snapshot completo! Ultimo SeqNum[" + e.LastMsgSeqNumProcessed + "]");

                                    if (this.OnRecoveryCompleted != null)
                                    {
                                        OnRecoveryCompleted(this, e);
                                    }
                                    bKeepRunning = false;
                                    break;
                                }
                            }
                            else
                            {
                                logger.Error("Template ID nao reconhecido [" + message.Template.Id + "]");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error("umdfMessageProcessor: " + ex.Message, ex);
                        bCanProcess = false;
                    }
                }

                Stop();

                logger.Debug("Fim");
            }
            catch (Exception ex)
            {
                logger.Error("Damn: " + ex.Message, ex);
                monitorConfig.channels[channelConfig.ChannelID].RemoveDetails("2) Snapshot");
                //bKeepRunning = false;
            }
        }
        protected override void umdfMessageProcessor()
        {
            logger.Debug("Templates carregados, processando pacotes");

            String securityListTemplateID = this.channelConfig.SecurityListTemplateID;

            listaSecurityListTemplateID = securityListTemplateID.Split(",".ToCharArray());
            long lastLogTicks = 0;

            bool ativaLeitura = false;

            while (bKeepRunning)
            {
                try
                {
                    Message message;
                    if (!queueToProcessor.TryDequeue(out message))
                    {
                        Thread.Sleep(50);
                        continue;
                    }

                    int seqNum = message.GetInt("MsgSeqNum");

                    // Despreza todas as mensagens ate reiniciar o SeqNum
                    if (!ativaLeitura)
                    {
                        if (seqNum == 1)
                        {
                            ativaLeitura = true;
                        }
                        else
                        {
                            logger.Debug("seqNum[" + seqNum + "] Desprezado");
                            continue;
                        }
                    }

                    if ("10".Equals(message.Template.Id))
                    {
                        logger.Debug("Sequence Reset");
                        continue;
                    }

                    logger.Debug(message);

                    if (MDSUtils.shouldLog(lastLogTicks))
                    {
                        lastLogTicks = DateTime.UtcNow.Ticks;

                        string msgTruncado = (message.ToString().Length < 200 ? message.ToString() : message.ToString().Substring(0, 200));
                        monitorConfig.channels[channelConfig.ChannelID].dateTimeStartedStopped =
                            "SecList - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " - " + msgTruncado;
                        monitorConfig.channels[channelConfig.ChannelID].AddDetails(
                            "1) Security List", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), msgTruncado);
                    }

                    if (listaSecurityListTemplateID.Any(message.Template.Id.Contains))
                    {
                        logger.Debug("Fetching symbols");
                        fetchSymbols(message);


                        if (message.IsDefined("LastFragment"))
                        {
                            String isLast = message.GetString("LastFragment");

                            if (isLast != null)
                            {
                                if (isLast.ToLowerInvariant().Equals("y") ||
                                    isLast.Equals("1"))
                                {
                                    logger.Debug("Fim da security list! isLast: " + isLast);
                                    bKeepRunning = false;

                                    monitorConfig.channels[channelConfig.ChannelID].dateTimeStartedStopped =
                                        "SecList - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + ": Security List completo!";

                                    monitorConfig.channels[channelConfig.ChannelID].AddDetails(
                                        "1) Security List", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"),
                                        "Security List completo! Ultimo SeqNum[" + message.GetInt("MsgSeqNum") + "]");

                                    if (OnSecurityListCompleted != null)
                                    {
                                        SecurityListCompletedEventArgs args = new SecurityListCompletedEventArgs();
                                        OnSecurityListCompleted(this, args);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("Damn, comecar tudo de novo: " + ex.Message, ex);
                    monitorConfig.channels[channelConfig.ChannelID].RemoveDetails("1) Security List");
                    break;
                    //bKeepRunning = false;
                }
            }

            Stop();

            logger.Info("Fim");
        }
Exemple #15
0
        protected override void udpPacketProcessor()
        {
            int  lastUdpReceived       = 0;
            long lastWatchDog          = 0;
            long seqNumInicialAnterior = 0;
            long seqNumFinalAnterior   = 0;
            long lastLogUDP            = 0;
            long lastForaOrdem         = 0;
            int  queueRead             = 0;

            CreateSocket(multicastServerPRI, multicastPortPRI);

            while (bKeepRunning)
            {
                try
                {
                    TimeSpan tsWatchDog = new TimeSpan(DateTime.UtcNow.Ticks - lastWatchDog);

                    if (tsWatchDog.TotalMilliseconds > 30000)
                    {
                        logger.Info("Aguardando recepcao pacotes UDP");
                        lastWatchDog = DateTime.UtcNow.Ticks;
                    }

                    UdpPacket udppacket = null;
                    if (qUdpPkt.TryDequeue(out udppacket) && queueRead < 1000)
                    {
                        queueRead++;

                        UmdfPacket umdfpacket = new UmdfPacket(udppacket.byteData, 0, udppacket.pktLength);

                        if (logger.IsDebugEnabled)
                        {
                            logger.DebugFormat("Recebeu pacote: {0} {1}/{2} {3}",
                                               umdfpacket.seqNum,
                                               umdfpacket.noChunks,
                                               umdfpacket.currChunk,
                                               umdfpacket.msgLength);
                        }

                        // ATP: O chunk pode vir fora de sequencia.... o teste abaixo eh furado
                        //if (umdfpacket.seqNum == 1 && umdfpacket.currChunk == 1)
                        //{
                        //    logger.Info("Limpa toda a fila de mensagens de fora de ordem!");
                        //    qUdpPktOut.Clear();
                        //    lastpkt = 0;
                        //}

                        // Se o pacote recebido for anterior ao ultimo processado, despreza
                        if (umdfpacket.seqNum < (lastpkt + 1) && umdfpacket.seqNum != 1 &&
                            umdfpacket.currChunk < (lastChunk + 1))
                        {
                            logger.Info("Despreza SeqNum[" + umdfpacket.seqNum + "] ja processado");
                            continue;
                        }

                        // Se o pacote recebido maior, mas nao consecutivo ao ultimo recebido,
                        // enfileira e solicita o intervalo
                        if (umdfpacket.seqNum > (lastUdpReceived + 1))
                        {
                            //if (lastpkt == 0 && umdfpacket.seqNum != 1)
                            //{
                            //    logger.Debug("Despreza pacote[" + umdfpacket.seqNum + "] ate o reinicio da lista");
                            //    continue;
                            //}

                            qUdpPktOut.Enqueue(udppacket);

                            //if (lastUdpReceived != 0 && channelConfig.IsPuma==true)
                            //{
                            //    logger.Debug("Perdeu pacote[" + (lastUdpReceived + 1) + "], reinfilera pacote[" + umdfpacket.seqNum + "] (Tam seqOut = " + qUdpPktOut.Count + ")");

                            //    packetTimeWindow(qUdpPktOut, lastUdpReceived + 1, umdfpacket.seqNum - 1);
                            //}

                            lastUdpReceived = umdfpacket.seqNum;
                            continue;
                        }

                        // Se for apenas maior que o ultimo processado, enfilera
                        // para reordenacao
                        if (umdfpacket.seqNum > (this.lastpkt + 1))
                        {
                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug("Reenfileira pacote[" + (lastUdpReceived + 1) + "] para fila out (Tam seqOut = " + qUdpPktOut.Count + ")");
                            }
                            qUdpPktOut.Enqueue(udppacket);
                            lastUdpReceived = umdfpacket.seqNum;

                            continue;
                        }

                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("Submetendo pacote [" + umdfpacket.seqNum + "] para fila de processamento");
                        }

                        //bool bsinaliza = qUmdfPacket.IsEmpty;
                        qUmdfPacket.Enqueue(umdfpacket);
                        //if (bsinaliza)
                        //{
                        //    lock (syncQueueUmdfPacket)
                        //    {
                        //        Monitor.Pulse(syncQueueUmdfPacket);
                        //    }
                        //}

                        lastUdpReceived = lastpkt = umdfpacket.seqNum;
                        if (umdfpacket.currChunk == umdfpacket.noChunks)
                        {
                            lastChunk = 0;
                        }
                        else
                        {
                            lastChunk = umdfpacket.currChunk;
                        }

                        if (MDSUtils.shouldLog(lastLogUDP))
                        {
                            logger.Info("Fila de pacotes UDP recebidos: " + qUdpPkt.Count);
                            lastLogUDP = DateTime.UtcNow.Ticks;
                        }

                        continue;
                    }

                    queueRead = 0;

                    if (MDSUtils.shouldLog(lastForaOrdem, 5))
                    {
                        logger.Info("Processando fila de pacotes fora de ordem (out) tamout=" + qUdpPktOut.Count);
                        lastForaOrdem = DateTime.UtcNow.Ticks;

                        if (qUdpPktOut.Count > 25000)
                        {
                            logger.Fatal("Holy shit, deve reiniciar o canal por excesso de pacotes aguardando processamento");

                            string msg = "Canal [" + this.channelConfig.ChannelID + "] precisa ser derrubado, pois nao fila esta acumulando.\r\n";
                            msg += "Ultima processada: " + lastMsgSeqNum;
                            string titulo = string.Format("Derrubando canal [{0}] por fila crescente", this.channelConfig.ChannelID);

                            bKeepRunning = false;
                        }
                    }

                    // Tenta processar pacotes recebidos via replay
                    int tentativa = 0;
                    while (qUdpPktReplay.Count > 0 && bKeepRunning && tentativa < qUdpPktReplay.Count)
                    {
                        tentativa++;
                        UdpPacket udppacket1 = null;
                        lock (this.qUdpPktReplay)
                        {
                            udppacket1 = qUdpPktReplay.Dequeue();
                        }

                        UmdfPacket umdfpacket = new UmdfPacket(udppacket1.byteData, 0, udppacket1.pktLength);

                        if (logger.IsDebugEnabled)
                        {
                            logger.DebugFormat("Recebeu pacote Replay : {0} {1}/{2} {3}",
                                               umdfpacket.seqNum,
                                               umdfpacket.noChunks,
                                               umdfpacket.currChunk,
                                               umdfpacket.msgLength);
                        }

                        if (umdfpacket.seqNum < (lastpkt + 1) && umdfpacket.seqNum != 1)
                        {
                            logger.Info("Despreza pacote replay SeqNum[" + umdfpacket.seqNum + "] ja processado");
                            continue;
                        }

                        if (umdfpacket.seqNum > (lastpkt + 1))
                        {
                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug("Pacote replay ainda nao eh o esperado, reinfilera pacote[" + umdfpacket.seqNum + "]");
                            }

                            lock (qUdpPktReplay)
                            {
                                qUdpPktReplay.Enqueue(udppacket1);
                            }

                            continue;
                        }

                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("Submetendo pacote replay [" + umdfpacket.seqNum + "] para fila de processamento");
                        }

                        //bool bsinaliza = qUmdfPacket.IsEmpty;
                        qUmdfPacket.Enqueue(umdfpacket);
                        //if (bsinaliza)
                        //{
                        //    lock (syncQueueUmdfPacket)
                        //    {
                        //        Monitor.Pulse(syncQueueUmdfPacket);
                        //    }
                        //}

                        lastpkt = umdfpacket.seqNum;

                        continue;
                    }

                    // Tenta processar pacotes fora de ordem....
                    //int tentativa = 0;
                    //tentativa = 0;
                    if (tentativa == 0)  // Se ja processou tudo via replay
                    {
                        while (qUdpPktOut.Count > 0 && bKeepRunning && tentativa < qUdpPktOut.Count)
                        {
                            tentativa++;
                            UdpPacket udppacket1 = null;
                            lock (qUdpPktOut)
                            {
                                udppacket1 = qUdpPktOut.Peek();
                            }

                            UmdfPacket umdfpacket = new UmdfPacket(udppacket1.byteData, 0, udppacket1.pktLength);

                            if (logger.IsDebugEnabled)
                            {
                                logger.DebugFormat("Recebeu pacote out: {0} {1}/{2} {3}",
                                                   umdfpacket.seqNum,
                                                   umdfpacket.noChunks,
                                                   umdfpacket.currChunk,
                                                   umdfpacket.msgLength);
                            }

                            if (umdfpacket.seqNum < (lastpkt + 1) && umdfpacket.seqNum != 1 && umdfpacket.noChunks == 1)
                            {
                                logger.Info("Despreza out SeqNum[" + umdfpacket.seqNum + "] ja processado");
                                lock (qUdpPktOut)
                                {
                                    udppacket1 = qUdpPktOut.Dequeue();
                                }
                                continue;
                            }

                            if (!this.channelConfig.IsNewsChannel)
                            {
                                if (!this.channelConfig.IsPuma || (DateTime.Now.Hour >= 9 && DateTime.Now.Hour <= 18))
                                {
                                    if (umdfpacket.seqNum > (lastpkt + 1) && ProcessamentoMensagensEnabled)
                                    {
                                        if (logger.IsDebugEnabled)
                                        {
                                            logger.Debug("Out: Perdeu pacote [" + (lastpkt + 1) + "], reinfilera pacote[" + umdfpacket.seqNum + "]");
                                        }

                                        //lock (qUdpPktOut)
                                        //{
                                        //    qUdpPktOut.Enqueue(udppacket);
                                        //}

                                        //Sanity check....
                                        int seqNumInicial = lastpkt + 1;
                                        int seqNumFinal   = umdfpacket.seqNum - 1;

                                        if (seqNumInicial <= seqNumFinal)
                                        {
                                            // Se expirou o TTL do pacote, descarta
                                            TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - udppacket1.pktTimestamp);
                                            if (ts.TotalMilliseconds > 20 && lastpkt != 0 &&
                                                (seqNumInicialAnterior == 0 || seqNumFinalAnterior == 0 || seqNumInicial < seqNumInicialAnterior || seqNumInicial > seqNumFinalAnterior))
                                            {
                                                if (!packetTimeWindow(qUdpPktOut, seqNumInicial, seqNumFinal))
                                                {
                                                    logger.Fatal("Holy shit, nao recebeu replay, deve reiniciar o canal");

                                                    string msg = "Canal [" + this.channelConfig.ChannelID + "] precisa ser derrubado, pois nao recebeu resposta do replay.\r\n";
                                                    msg += "Timeout aguardando tcpreplay, ultima processada: " + lastMsgSeqNum;
                                                    string titulo = string.Format("Derrubando canal [{0}] por timeout replay", this.channelConfig.ChannelID);

                                                    if (DateTime.Now.DayOfWeek != DayOfWeek.Sunday && DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
                                                    {
                                                        if (DateTime.Now.Hour >= ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_INICIO &&
                                                            DateTime.Now.Hour < ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_FIM)
                                                        {
                                                            MDSUtils.EnviarEmail(titulo, msg);
                                                        }
                                                    }

                                                    bKeepRunning = false;
                                                }
                                                seqNumInicialAnterior = seqNumInicial;
                                                seqNumFinalAnterior   = seqNumFinal;
                                                break;
                                            }
                                        }

                                        break;
                                    }
                                }
                            }

                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug("Submetendo pacote out[" + umdfpacket.seqNum + "] para fila de processamento");
                            }

                            lock (qUdpPktOut)
                            {
                                udppacket1 = qUdpPktOut.Dequeue();
                            }

                            //bool bsinaliza = qUmdfPacket.IsEmpty;
                            qUmdfPacket.Enqueue(umdfpacket);
                            //if (bsinaliza)
                            //{
                            //    lock (syncQueueUmdfPacket)
                            //    {
                            //        Monitor.Pulse(syncQueueUmdfPacket);
                            //    }
                            //}

                            lastpkt = umdfpacket.seqNum;
                        }
                    }

                    lock (syncQueueUdpPkt)
                    {
                        Monitor.Wait(syncQueueUdpPkt, 100);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("udpPacketProcessor: " + ex.Message, ex);
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        private void mainInboundQProc()
        {
            logger.Info("Iniciando thread de processamento da fila principal de entrada");
            long lastLog = 0;

            while (_bKeepRunning)
            {
                try
                {
                    if (currentFeed < 0)
                    {
                        Thread.Sleep(250);
                        continue;
                    }

                    QuickFix.FIX44.Message message = null;
                    if (mainInboundQ.TryDequeue(out message))
                    {
                        int msgSeqNum = message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum);
                        int lastDigit = msgSeqNum % 10;

                        // Se ha um intervalo entre as mensagens
                        // preenche com marcadores para forcar a
                        // troca da fila
                        if (lastMsgSeqNum > 0)
                        {
                            if (msgSeqNum > (lastMsgSeqNum + 1))
                            {
                                logger.InfoFormat("Filling gap [{0}]->[{1}]", lastMsgSeqNum + 1, msgSeqNum);

                                for (int fillSeqNum = lastMsgSeqNum + 1; fillSeqNum < msgSeqNum; fillSeqNum++)
                                {
                                    int markLastDigit = fillSeqNum % 10;

                                    // Envia o marcador de final da mensagem
                                    QuickFix.FIX44.TestRequest mark = new QuickFix.FIX44.TestRequest();
                                    mark.Header.SetField(new QuickFix.Fields.MsgSeqNum(fillSeqNum));

                                    inboundQueues[markLastDigit].Enqueue(mark);
                                }
                            }
                        }

                        inboundQueues[lastDigit].Enqueue(message);
                        lastMsgSeqNum = msgSeqNum;

                        if (MDSUtils.shouldLog(lastLog))
                        {
                            logger.Info("Mensagens FIX para processar na fila (mainInboudQ): " + mainInboundQ.Count);
                            lastLog = DateTime.UtcNow.Ticks;
                        }

                        continue;
                    }

                    Thread.Sleep(25);
                }
                catch (Exception ex)
                {
                    logger.Error("mainInboundQProc: " + ex.Message, ex);
                }
            }
        }
Exemple #17
0
        protected override void umdfMessageProcessor()
        {
            try
            {
                logger.Info("Carregando lista de Templates:");

                String securityListTemplateID = this.channelConfig.SecurityListTemplateID;
                listaSecurityListTemplateID = securityListTemplateID.Split(",".ToCharArray());

                String mdIncrementalTemplateID = this.channelConfig.MDIncrementalTemplateID;
                listaMDIncrementalTemplateID = mdIncrementalTemplateID.Split(",".ToCharArray());

                String securityStatusTemplateID = this.channelConfig.SecurityStatusTemplateID;
                listaSecurityStatusTemplateID = securityStatusTemplateID.Split(",".ToCharArray());

                String newsTemplateID = this.channelConfig.NewTemplateID;
                listaNewsTemplateID = newsTemplateID.Split(",".ToCharArray());

                foreach (string templateID in listaSecurityListTemplateID)
                {
                    logger.Info("SecurityList template [" + templateID + "]");
                }
                foreach (string templateID in listaMDIncrementalTemplateID)
                {
                    logger.Info("MDIncremental template [" + templateID + "]");
                }
                foreach (string templateID in listaSecurityStatusTemplateID)
                {
                    logger.Info("SecurityStatus template [" + templateID + "]");
                }
                foreach (string templateID in listaNewsTemplateID)
                {
                    logger.Info("News template [" + templateID + "]");
                }

                long lastLogTicks = 0;

                while (bKeepRunning)
                {
                    int seqNum    = 0;
                    int tcpSeqNum = 0;

                    if (ProcessamentoMensagensEnabled == false)
                    {
                        lock (ProcessamentoMensagensLockObject)
                        {
                            Monitor.Wait(ProcessamentoMensagensLockObject, 100);
                            continue;
                        }
                    }

                    Message message;
                    if (!queueToProcessor.TryDequeue(out message))
                    {
                        lock (syncQueueToProcessor)
                        {
                            Monitor.Wait(syncQueueToProcessor, 50);
                        }
                        continue;
                    }

                    //TODO: revisar o algoritmo de conciliacao das filas

                    /*
                     * if ( message == null )
                     * {
                     *  logger.Debug("Timeout aguardando pacote FAST");
                     *  continue;
                     * }*/

                    // Tratamento de Sequence Reset

                    //if (ConstantesMDSUMDF.FAST_MSGTYPE_SEQUENCERESET.equals(message.GetString("MsgType")))

                    string msgType;
                    if (message.Template.HasField("MsgType"))
                    {
                        msgType = message.GetString("MsgType");
                    }
                    else
                    {
                        msgType = message.GetString("MessageType");
                    }

                    if (msgType.Equals(ConstantesUMDF.FAST_MSGTYPE_SEQUENCERESET))
                    {
                        lastMsgSeqNum = 0;
                        if (message.Template.HasField("NewSeqNo"))
                        {
                            lastMsgSeqNum = message.GetInt("NewSeqNo");
                        }
                        logger.InfoFormat("Recebido SequenceReset - lastMsgSeqNum[{0}]", lastMsgSeqNum);

                        seqContador = lastMsgSeqNum;
                        continue;
                    }

                    if (msgType.Equals(ConstantesUMDF.FAST_MSGTYPE_HEARTBEAT))
                    {
                        logger.Info("Recebeu Hearbeat");
                        continue;
                    }

                    String sendingTime = message.GetString("SendingTime").Substring(0, 8);

                    // Descarta as mensagens ja recebidas no snapshot
                    if (message.IsDefined("MsgSeqNum"))
                    {
                        seqNum = message.GetInt("MsgSeqNum");

                        if (seqNum <= lastMsgSeqNum)
                        {
                            if (lastSendingTime.Equals(""))
                            {
                                lastSendingTime = sendingTime;
                            }

                            if (sendingTime.CompareTo(lastSendingTime) > 0)
                            {
                                logger.Info("Virou o dia! [" + sendingTime + "]");
                                lastMsgSeqNum = seqNum;
                                seqContador   = lastMsgSeqNum;
                            }
                            else
                            {
                                logger.Info("Descartando msg [" + seqNum + "]");
                                continue;
                            }
                        }
                    }

                    /*
                     * while ( this.filaTCPReplay.size() > 0 )
                     * {
                     *  logger.Debug("Mensagens na fila do TCP-Replay: " + this.filaTCPReplay.size());
                     *
                     *  messageTcp = this.filaTCPReplay.take();
                     *  tcpSeqNum = messageTcp.getInt("MsgSeqNum");
                     *
                     *  if ( tcpSeqNum <= this.lastMsgSeqNum )
                     *  {
                     *      logger.Error("Descartando msg replay [" + tcpSeqNum + "]");
                     *      continue;
                     *  }
                     *
                     *  if (tcpSeqNum >= seqNum )
                     *  {
                     *      logger.Debug("tcpSeqNum[" + tcpSeqNum + "] >= seqNum[" + seqNum + "] - saindo do loop da fila TCP-Replay");
                     *      break;
                     *  }
                     *
                     *  logger.Debug("Processando msg replay [" + tcpSeqNum + "]");
                     *
                     *  processMD(messageTcp);
                     *  this.lastMsgSeqNum = tcpSeqNum;
                     * }
                     */
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("Processando msg [" + message.ToString() + "]");
                    }

                    if (MDSUtils.shouldLog(lastLogTicks))
                    {
                        string convertedmsg = message.ToString();

                        lastLogTicks = DateTime.UtcNow.Ticks;
                        logger.Info("Tamanho da fila: " + this.queueToProcessor.Count);

                        string msgTruncado = (convertedmsg.Length < 200 ? convertedmsg : convertedmsg.Substring(0, 200));
                        monitorConfig.channels[channelConfig.ChannelID].dateTimeStartedStopped =
                            "Incr - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " - " + msgTruncado;
                        monitorConfig.channels[channelConfig.ChannelID].AddDetails(
                            "3) Incremental", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), msgTruncado);
                    }

                    if (listaMDIncrementalTemplateID.Any(message.Template.Id.Contains) ||
                        listaSecurityListTemplateID.Any(message.Template.Id.Contains))
                    {
                        List <Message> mensagens = UmdfUtils.splitMessage(message, channelConfig.MarketDepth, ConstantesUMDF.FAST_MSGTYPE_INCREMENTAL_SINGLE);

                        string templateID = ConstantesUMDF.FAST_MSGTYPE_INCREMENTAL_SINGLE;
                        if (listaSecurityListTemplateID.Any(message.Template.Id.Contains))
                        {
                            templateID = ConstantesUMDF.FAST_MSGTYPE_SECURITYLIST_SINGLE;
                        }

                        foreach (Message newMessage in mensagens)
                        {
                            MDSUtils.EnqueueEventoUmdf(newMessage,
                                                       newMessage.Template.Id,
                                                       channelConfig.ChannelID,
                                                       channelConfig.Segment,
                                                       templateID,
                                                       channelConfig.MarketDepth,
                                                       StreamTypeEnum.STREAM_TYPE_MARKET_INCREMENTAL);
                        }
                    }
                    else if (listaSecurityStatusTemplateID.Any(message.Template.Id.Contains))
                    {
                        MDSUtils.EnqueueEventoUmdf(message,
                                                   message.Template.Id,
                                                   channelConfig.ChannelID,
                                                   channelConfig.Segment,
                                                   channelConfig.MarketDepth,
                                                   StreamTypeEnum.STREAM_TYPE_MARKET_INCREMENTAL);
                    }
                    else if (listaNewsTemplateID.Any(message.Template.Id.Contains))
                    {
                        MDSUtils.EnqueueEventoUmdfNews(message,
                                                       message.Template.Id,
                                                       channelConfig.ChannelID,
                                                       channelConfig.Segment,
                                                       ConstantesUMDF.FAST_MSGTYPE_NEWS,
                                                       StreamTypeEnum.STREAM_TYPE_MARKET_INCREMENTAL);
                    }
                    else
                    {
                        logger.Error("Template ID nao reconhecido [" + message.Template.Id + "]");
                    }
                    lastMsgSeqNum   = seqNum;
                    lastSendingTime = sendingTime;
                }             // main loop

                logger.Info("Fim");
            }
            catch (Exception ex)
            {
                logger.Error("Damn: " + ex.Message, ex);
                bKeepRunning = false;
                monitorConfig.channels[channelConfig.ChannelID].RemoveDetails("3) Incremental");
            }
        }
Exemple #18
0
        public FixServerAcceptor(
            TCPReplayConfig tcpReplayConfig,
            Dictionary <string, SessionID> dctSessionsFixClients,
            Dictionary <string, SessionID> dctSessionsFixChannels)
        {
            _tcpReplayConfig        = tcpReplayConfig;
            _dctSessionsFixClients  = dctSessionsFixClients;
            _dctSessionsFixChannels = dctSessionsFixChannels;

            logger = LogManager.GetLogger("FixServerAcceptor");

            MDSUtils.AddAppender("FixServerAcceptor", logger.Logger);

            XMLMessageTemplateLoader loader = new XMLMessageTemplateLoader();

            registry = UmdfUtils.loadTemplates(tcpReplayConfig.TemplateFile);
            context  = new Context();
            context.TemplateRegistry = registry;

            try
            {
                // Cria dicionario da configuracao
                QuickFix.Dictionary mainDic = new QuickFix.Dictionary();
                mainDic.SetLong("SocketAcceptPort", tcpReplayConfig.SocketAcceptPort);
                mainDic.SetBool("ResetOnLogon", tcpReplayConfig.ResetOnLogon);
                mainDic.SetBool("ResetOnDisconnect", tcpReplayConfig.ResetOnDisconnect);
                mainDic.SetBool("PersistMessages", tcpReplayConfig.PersistMessages);
                mainDic.SetString("ConnectionType", tcpReplayConfig.ConnectionType);
                mainDic.SetString("FileStorePath", tcpReplayConfig.FileStorePath);
                mainDic.SetString("FileLogPath", tcpReplayConfig.FileLogPath);
                mainDic.SetString("StartTime", tcpReplayConfig.StartTime);
                mainDic.SetString("EndTime", tcpReplayConfig.EndTime);

                QuickFix.Dictionary sessDic = new QuickFix.Dictionary();
                sessDic.SetString("BeginString", tcpReplayConfig.BeginString);
                sessDic.SetString("SenderCompID", tcpReplayConfig.SenderCompID);
                sessDic.SetString("TargetCompID", tcpReplayConfig.TargetCompID);
                sessDic.SetString("DataDictionary", tcpReplayConfig.DataDictionary);
                sessDic.SetBool("UseDataDictionary", true);

                // Configure the session settings
                QuickFix.SessionSettings settings = new QuickFix.SessionSettings();

                settings.Set(mainDic);

                MemoryStoreFactory store   = new MemoryStoreFactory();
                FileLogFactory     log     = new FileLogFactory(settings);
                IMessageFactory    message = new DefaultMessageFactory();

                IEnumerable <int> rangeSenderSubID = Enumerable.Range(
                    tcpReplayConfig.SubIDStartSeq,
                    tcpReplayConfig.SubIDEndSeq);
                foreach (int item in rangeSenderSubID)
                {
                    string subID = tcpReplayConfig.SubIDPrefix + item.ToString("D3");

                    // Cria sessao FIX
                    SessionID sessionID = new QuickFix.SessionID(
                        tcpReplayConfig.BeginString,
                        tcpReplayConfig.SenderCompID,
                        subID,
                        tcpReplayConfig.TargetCompID,
                        subID);

                    sessDic.SetString("SenderSubID", subID);
                    sessDic.SetString("TargetSubID", subID);
                    settings.Set(sessionID, sessDic);
                }

                logger.InfoFormat("Start(): iniciando FIX ACCEPTOR na porta {0}...", tcpReplayConfig.SocketAcceptPort);
                _acceptor = new ThreadedSocketAcceptor(this, store, settings, log, message);
                _acceptor.Start();
            }
            catch (Exception ex)
            {
                logger.Error("Start():" + ex.Message, ex);
            }

            logger.Info("Start(): Sessao FIX iniciada!");
        }
        /// <summary>
        ///
        /// </summary>
        private void messageSplitterProc(object param)
        {
            int me = (int)param;

            logger.InfoFormat("Iniciando thread {0} de splitting", me);

            long lastLog = 0;

            while (_bKeepRunning)
            {
                try
                {
                    if (currentFeed < 0)
                    {
                        Thread.Sleep(250);
                        continue;
                    }

                    QuickFix.FIX44.Message message = null;
                    if (inboundQueues[me].TryDequeue(out message))
                    {
                        int    msgSeqNum = message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum);
                        string msgType   = message.Header.GetString(QuickFix.Fields.Tags.MsgType);
                        int    lastDigit = msgSeqNum % 10;

                        if (lastDigit != Convert.ToInt32(msgSeqNum.ToString().AsEnumerable().Last().ToString()))
                        {
                            logger.Error("PQP arrendondamento FDP");
                        }

                        if (!msgType.Equals(QuickFix.FIX44.TestRequest.MsgType))
                        {
                            logger.Debug("Splitting Message [" + message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum) + "] type [" + message.Header.GetString(QuickFix.Fields.Tags.MsgType) + "]");

                            List <QuickFix.FIX44.Message> lstSplitted = FIXUtils.splitMessage(message, 0, null);

                            foreach (QuickFix.FIX44.Message splittedmsg in lstSplitted)
                            {
                                enqueueToFire(me, splittedmsg);
                            }
                        }
                        else
                        {
                            logger.DebugFormat("inboundQueue[{0}] recebeu TestRequest, skipping", me);
                        }

                        // Envia o marcador de final da mensagem
                        QuickFix.FIX44.TestRequest mark = new QuickFix.FIX44.TestRequest();
                        mark.Header.SetField(new QuickFix.Fields.MsgSeqNum(msgSeqNum));
                        enqueueToFire(me, mark);

                        if (MDSUtils.shouldLog(lastLog))
                        {
                            logger.InfoFormat("Mensagens para splitting FIX na fila {0}: {1}", me, inboundQueues[me].Count);
                            lastLog = DateTime.UtcNow.Ticks;
                        }
                        continue;
                    }

                    Thread.Sleep(25);
                }
                catch (Exception ex)
                {
                    logger.Error("messageSplitterProc: " + ex.Message, ex);
                }
            }
        }
        /// <summary>
        /// Thread de convergencia das mensagems
        /// Pega as mensagens splitadas na sequencia correta a partir
        /// da fila intermediaria
        /// </summary>
        private void sendBulletsProc()
        {
            logger.Info("Iniciando thread de convergencia");

            long [] lastLogs = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            while (_bKeepRunning)
            {
                try
                {
                    if (currentFeed < 0)
                    {
                        Thread.Sleep(250);
                        continue;
                    }

                    QuickFix.FIX44.Message message = null;
                    if (intermediateQueues[currentFeed].TryDequeue(out message))
                    {
                        int    msgSeqNum = message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum);
                        string msgType   = message.Header.GetString(QuickFix.Fields.Tags.MsgType);

                        if (sentMsgSeqNum != 0)
                        {
                            if (msgSeqNum < sentMsgSeqNum || msgSeqNum > sentMsgSeqNum + 1)
                            {
                                logger.FatalFormat("Deu merrda, capitao {0}<=>{1}", msgSeqNum, sentMsgSeqNum);
                                continue;
                            }
                        }

                        sentMsgSeqNum = msgSeqNum;

                        // Qdo receber o marcador, troca a fila de leitura
                        if (msgType.Equals(QuickFix.FIX44.TestRequest.MsgType))
                        {
                            currentFeed++;
                            if (currentFeed > 9)
                            {
                                currentFeed = 0;
                            }

                            continue;
                        }

                        logger.Debug("Converging Message [" + message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum) + "] type [" + message.Header.GetString(QuickFix.Fields.Tags.MsgType) + "]");

                        enqueueToOutput(message);

                        if (MDSUtils.shouldLog(lastLogs[currentFeed]))
                        {
                            logger.InfoFormat("Mensagens FIX na fila intermediaria {0}: {1}", currentFeed, intermediateQueues[currentFeed].Count);
                            lastLogs[currentFeed] = DateTime.UtcNow.Ticks;
                        }

                        continue;
                    }

                    Thread.Sleep(25);
                }
                catch (Exception ex)
                {
                    logger.Error("sendBulletsProc: " + ex.Message, ex);
                }
            }

            logger.Info("Fim da thread sendBullets");
        }
Exemple #21
0
        protected virtual void umdfPacketAssembler()
        {
            //UmdfPacket packet = null;
            int lastSeqNum = 0;

            UmdfPacket []          chunks         = null;
            int                    receivedChunks = 0;
            Dictionary <int, long> dctSeqNum      = new Dictionary <int, long>();

            //StreamWriter tracewriter = new StreamWriter("d:\\turingoms\\servicos\\mdsnetpuma2\\logs\\Trace-" + this.channelID + "-" +  this.multicastPortPRI + ".txt", true);

            while (bKeepRunning)
            {
                Context    context = null;
                UmdfPacket packet  = null;

                if (qUmdfPacket.TryDequeue(out packet))
                {
                    int totalBytes = packet.data.Length;
                    try
                    {
                        //if (context != null)
                        //{
                        //    ((OpenFAST.Debug.BasicDecodeTrace)context.DecodeTrace).Writer.Close();
                        //    ((OpenFAST.Debug.BasicDecodeTrace)context.DecodeTrace).Writer.Dispose();
                        //    context = null;
                        //}

                        context = new Context();
                        context.TemplateRegistry = registry;
                        //context.TraceEnabled = true;
                        //context.StartTrace();
                        //((OpenFAST.Debug.BasicDecodeTrace)context.DecodeTrace).Writer = tracewriter;

                        if (packet.noChunks > 1)
                        {
                            logger.Debug("pktAss() noChunks: " + packet.noChunks + " currChunk: " + packet.currChunk + " seqNum: " + packet.seqNum + " lastSeqNum:" + lastSeqNum);
                            if (chunks == null || packet.seqNum != lastSeqNum)
                            {
                                logger.Debug("pktAss() Alocando array para [" + packet.noChunks + "] chunks: null ou seqNum!=");
                                chunks         = new UmdfPacket[packet.noChunks];
                                receivedChunks = 0;
                            }

                            // Verifica se ainda esta no mesmo chunk, e se ainda dentro da mesma
                            // rodada do snapshot
                            if (dctSeqNum.ContainsKey(packet.seqNum))
                            {
                                TimeSpan ts = new TimeSpan(DateTime.Now.Ticks - dctSeqNum[packet.seqNum]);
                                if (ts.TotalMilliseconds > PACKET_TIME_WINDOW && processorType.Equals(ProcessorType.MarketIncremental))
                                {
                                    logger.Debug("pktAss() REALOCANDO array para [" + packet.noChunks + "] chunks");
                                    chunks         = new UmdfPacket[packet.noChunks];
                                    receivedChunks = 0;
                                }
                                dctSeqNum[packet.seqNum] = DateTime.Now.Ticks;
                            }
                            else
                            {
                                logger.Debug("pktAss() Alocando array para [" + packet.noChunks + "] chunks");
                                dctSeqNum.Add(packet.seqNum, DateTime.Now.Ticks);
                                chunks         = new UmdfPacket[packet.noChunks];
                                receivedChunks = 0;
                            }


                            if ((packet.currChunk - 1) < chunks.Length)
                            {
                                if (chunks[packet.currChunk - 1] == null)
                                {
                                    receivedChunks++;
                                }
                                chunks[packet.currChunk - 1] = packet;
                            }
                            else
                            {
                                logger.Error("pktAss() Puts...[" + (packet.currChunk - 1) + "] <=> [" + chunks.Length + "] fdp furou!!!!");
                                continue;
                            }


                            logger.Debug("pktAss() noChunks: " + packet.noChunks + " currChunk: " + packet.currChunk);
                            lastSeqNum = packet.seqNum;

                            if (receivedChunks < packet.noChunks)
                            {
                                continue;
                            }

                            dctSeqNum.Remove(packet.seqNum);
                            logger.Debug("pktAss() Remontando pacote noChunks=" + packet.noChunks + " received=" + receivedChunks + " length=" + chunks.Length);

                            byte[] reassembled = UmdfUtils.reassemble(chunks);
                            totalBytes       = reassembled.Length;
                            chunks           = null;
                            receivedChunks   = 0;
                            packet.data      = reassembled;
                            packet.msgLength = totalBytes;

                            System.IO.MemoryStream byteIn = new System.IO.MemoryStream(packet.data, 0, packet.msgLength);

                            FastDecoder decoder = new FastDecoder(context, byteIn);

                            Message message = decoder.ReadMessage();

                            bool bsinaliza = queueToProcessor.IsEmpty;
                            queueToProcessor.Enqueue(message);
                            if (bsinaliza)
                            {
                                lock (syncQueueToProcessor)
                                {
                                    Monitor.Pulse(syncQueueToProcessor);
                                }
                            }

                            // testa se debug esta habilitado por conta do writegroup
                            // mais performatico
                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug(UmdfUtils.writeGroup(message));
                            }
                        }
                        else
                        {
                            lastSeqNum = packet.seqNum;
                            System.IO.MemoryStream byteIn = new System.IO.MemoryStream(packet.data, 0, packet.msgLength);

                            FastDecoder decoder = new FastDecoder(context, byteIn);

                            Message message = decoder.ReadMessage();

                            bool bsinaliza = queueToProcessor.IsEmpty;
                            queueToProcessor.Enqueue(message);
                            if (bsinaliza)
                            {
                                lock (syncQueueToProcessor)
                                {
                                    Monitor.Pulse(syncQueueToProcessor);
                                }
                            }

                            if (logger.IsDebugEnabled)
                            {
                                logger.Debug(UmdfUtils.writeGroup(message));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error("pktAss() Erro: " + ex.Message, ex);

                        // Bom, com uma exception dessas, acho melhor iniciar o canal, pode ser falha no decoder
                        string msg    = string.Format("Canal [{0}] precisa ser derrubado, pois deu falha grave no PacketAssembler().\r\n\r\n{1}\r\n\r\n{2}", channelID, ex.Message, ex);
                        string titulo = string.Format("Derrubando canal [{0}] por falha em pktAss()", channelID);
                        MDSUtils.EnviarEmail(titulo, msg);
                        bKeepRunning = false;

                        //lastpkt = 0;
                    }

                    continue;
                }

                lock (syncQueueUmdfPacket)
                {
                    Monitor.Wait(syncQueueUmdfPacket, 100);
                }
            }
        }
Exemple #22
0
        public ChannelTcpConflated(TCPConflatedConfig conflatedConfig)
        {
            logger = LogManager.GetLogger("ChannelTcpConflated-" + conflatedConfig.ChannelID);

            MDSUtils.AddAppender("ChannelTcpConflated-" + conflatedConfig.ChannelID, logger.Logger);

            _bKeepRunning = true;

            machineGun = new MachineGunFixSplitter();
            machineGun.UnderFIXMessageFire += new FIXMachineGunEventHandler(machineGun_UnderFIXMessageFire);
            machineGun.Start();

            thQueueProc = new Thread(new ThreadStart(queueProc));
            thQueueProc.Start();

            thSplitProc = new Thread(new ThreadStart(splitterThreadWork));
            thSplitProc.Start();

            _channelUmdfConfig = conflatedConfig;

            //if (!listaChannelQueues.ContainsKey(conflatedConfig.ChannelID))
            //    listaChannelQueues.Add(conflatedConfig.ChannelID, new ListChannelQueues(qUdpPkt, replayLockObject));

            logger.Info("Start(): iniciando sessao FIX...");
            try
            {
                // Cria sessao FIX
                _session = new QuickFix.SessionID(
                    conflatedConfig.BeginString,
                    conflatedConfig.SenderCompID,
                    conflatedConfig.TargetCompID);

                // Cria dicionario da configuracao
                QuickFix.Dictionary mainDic = new QuickFix.Dictionary();
                mainDic.SetLong("SocketConnectPort", conflatedConfig.ProxyPort);
                mainDic.SetLong("HeartBtInt", conflatedConfig.HeartBtInt);
                mainDic.SetLong("ReconnectInterval", conflatedConfig.ReconnectInterval);
                mainDic.SetBool("ResetOnLogon", conflatedConfig.ResetOnLogon);
                mainDic.SetBool("ResetOnLogout", conflatedConfig.ResetOnLogout);
                mainDic.SetBool("ResetOnDisconnect", conflatedConfig.ResetOnDisconnect);
                mainDic.SetBool("PersistMessages", conflatedConfig.PersistMessages);
                mainDic.SetString("ConnectionType", conflatedConfig.ConnectionType);
                mainDic.SetString("SocketConnectHost", conflatedConfig.ProxyHost);
                mainDic.SetString("FileStorePath", conflatedConfig.FileStorePath);
                mainDic.SetString("FileLogPath", conflatedConfig.FileLogPath);
                mainDic.SetString("StartTime", conflatedConfig.StartTime);
                mainDic.SetString("EndTime", conflatedConfig.EndTime);

                QuickFix.Dictionary sessDic = new QuickFix.Dictionary();
                sessDic.SetString("BeginString", conflatedConfig.BeginString);
                sessDic.SetString("SenderCompID", conflatedConfig.SenderCompID);

                sessDic.SetString("TargetCompID", conflatedConfig.TargetCompID);
                sessDic.SetString("DataDictionary", conflatedConfig.DataDictionary);
                sessDic.SetBool("CheckLatency", false);
                sessDic.SetBool("UseDataDictionary", true);
                sessDic.SetLong("SocketReceiveBufferSize", conflatedConfig.SocketReceiveBufferSize);

                // Configure the session settings
                QuickFix.SessionSettings settings = new QuickFix.SessionSettings();

                settings.Set(mainDic);
                settings.Set(_session, sessDic);

                MemoryStoreFactory store   = new MemoryStoreFactory();
                FileLogFactory     log     = new FileLogFactory(settings);
                IMessageFactory    message = new DefaultMessageFactory();

                // Cria o socket
                _initiator = new QuickFix.Transport.SocketInitiator(this, store, settings, this, message);
                _initiator.Start();

                QuickFix.Session mySession = QuickFix.Session.LookupSession(_session);
                QuickFix.Session.LookupSession(_session).ValidateLengthAndChecksum = false;
            }
            catch (Exception ex)
            {
                logger.Error("Start():" + ex.Message, ex);
            }

            logger.Info("Start(): Sessao FIX iniciado!");
        }
Exemple #23
0
        public virtual void OnMessage(QuickFix.FIX44.ApplicationMessageReport message, SessionID session)
        {
            try
            {
                logger.Info("*** Relatorio de Retransmissao");
                logger.Info("ApplReportID.....: " + message.GetString(QuickFix.Fields.Tags.ApplReportID));
                logger.Info("ApplReportType...: " + message.GetString(QuickFix.Fields.Tags.ApplReportType));

                int applReportType = Convert.ToInt32(message.ApplReportType.ToString());

                string lastSeqNum = "";
                if (message.IsSetField(QuickFix.Fields.Tags.NoApplIDs))
                {
                    int numApplIDs = message.GetInt(QuickFix.Fields.Tags.NoApplIDs);
                    logger.Info("ApplIDs..........: " + numApplIDs);
                    for (int numApplID = 1; numApplID <= numApplIDs; numApplID++)
                    {
                        QuickFix.FIX44.ApplicationMessageReport.NoApplIDsGroup groupApplID = new QuickFix.FIX44.ApplicationMessageReport.NoApplIDsGroup();
                        message.GetGroup(numApplID, groupApplID);

                        logger.Info("RefApplID[" + numApplID + "].....:" + groupApplID.GetString(QuickFix.Fields.Tags.RefApplID));
                        if (groupApplID.IsSetField(QuickFix.Fields.Tags.RefApplLastSeqNum))
                        {
                            lastSeqNum = groupApplID.GetString(QuickFix.Fields.Tags.RefApplLastSeqNum);
                            logger.Info("RefApplLastSeqNum[" + numApplID + "].:" + lastSeqNum);
                        }

                        if (groupApplID.IsSetField(QuickFix.Fields.Tags.ApplRespError))
                        {
                            logger.Info("ApplResponseError[" + numApplID + "].:" + groupApplID.GetString(QuickFix.Fields.Tags.ApplRespError));
                        }
                    }
                }
                string[] quebraApplReqID = message.GetString(QuickFix.Fields.Tags.ApplReqID).Split("-".ToCharArray());
                string   channelID       = quebraApplReqID[1];

                if (applReportType == 3)
                {
                    logger.Info("Libera ChannelID: [" + channelID + "]");
                    lock (listaChannelQueues[channelID].replayLockObject)
                    {
                        Monitor.Pulse(listaChannelQueues[channelID].replayLockObject);
                    }
                }
                else
                {
                    logger.Warn("Application Resend Error, nao libera channelID [" + channelID + "] devera reiniciar por timeout");

                    if (DateTime.Now.DayOfWeek != DayOfWeek.Sunday && DateTime.Now.DayOfWeek != DayOfWeek.Saturday)
                    {
                        if (DateTime.Now.Hour >= ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_INICIO &&
                            DateTime.Now.Hour < ConstantesMDS.HORARIO_NOTIFICACAO_EMAIL_FIM)
                        {
                            MDSUtils.EnviarEmail("Erro em recovery [" + channelID + "]", "Application Resend Error, nao libera channelID [" + channelID + "] devera reiniciar por timeout");
                        }
                    }
                }

                logger.Info("Final da Retransmissao ***");
            }
            catch (Exception ex)
            {
                logger.Error("onMessage(ApplicationMessageReport): " + ex.Message, ex);
            }
        }
Exemple #24
0
        private void queueProc()
        {
            logger.Info("Iniciando thread de processamento da fila de mensagens FIX");
            long lastLog = 0;

            while (_bKeepRunning)
            {
                try
                {
                    QuickFix.FIX44.Message message = null;

                    if (queueToProcess.TryDequeue(out message))
                    {
                        if (logger.IsDebugEnabled)
                        {
                            logger.Debug("Processing Message [" + message.Header.GetInt(34) + "] type [" + message.Header.GetString(35) + "]");
                        }

                        string channelID;
                        string msgtype = message.Header.GetString(QuickFix.Fields.Tags.MsgType);
                        switch (msgtype)
                        {
                        case QuickFix.FIX44.MarketDataIncrementalRefresh.MsgType:
                            if (message.GroupCount(QuickFix.Fields.Tags.NoMDEntries) > 0)
                            {
                                QuickFix.Group MDEntry    = message.GetGroup(1, QuickFix.Fields.Tags.NoMDEntries);
                                string         securityID = MDEntry.GetString(QuickFix.Fields.Tags.SecurityID);
                                channelID = dctChannel.GetOrAdd(securityID, FIX_DEFAULT_WORKER);
                            }
                            else
                            {
                                channelID = FIX_DEFAULT_WORKER;
                            }
                            break;

                        case QuickFix.FIX44.MarketDataSnapshotFullRefresh.MsgType:
                            if (message.IsSetField(QuickFix.Fields.Tags.SecurityID))
                            {
                                string securityID = message.GetString(QuickFix.Fields.Tags.SecurityID);
                                channelID = dctChannel.GetOrAdd(securityID, FIX_DEFAULT_WORKER);
                            }
                            else
                            {
                                channelID = FIX_DEFAULT_WORKER;
                            }
                            break;

                        case QuickFix.FIX44.SecurityList.MsgType:
                            if (message.GroupCount(QuickFix.Fields.Tags.NoRelatedSym) > 0)
                            {
                                QuickFix.Group MDEntry      = message.GetGroup(1, QuickFix.Fields.Tags.NoRelatedSym);
                                string         securityID   = MDEntry.GetString(QuickFix.Fields.Tags.SecurityID);
                                string         securityType = MDEntry.GetString(QuickFix.Fields.Tags.SecurityType);
                                channelID = dctChannel.GetOrAdd(securityID, securityType);
                            }
                            else
                            {
                                channelID = FIX_DEFAULT_WORKER;
                            }
                            break;

                        case QuickFix.FIX44.SecurityStatus.MsgType:
                            if (message.IsSetField(QuickFix.Fields.Tags.SecurityID))
                            {
                                string securityID = message.GetString(QuickFix.Fields.Tags.SecurityID);
                                channelID = dctChannel.GetOrAdd(securityID, FIX_DEFAULT_WORKER);
                            }
                            else
                            {
                                channelID = FIX_DEFAULT_WORKER;
                            }
                            break;

                        default:
                            channelID = FIX_DEFAULT_WORKER;
                            break;
                        }

                        EventoFIX eventoFIX = new EventoFIX();
                        eventoFIX.ChannelID   = channelID;
                        eventoFIX.MarketDepth = ConstantesUMDF.UMDF_MARKETDEPTH_MARKET_BY_ORDER;
                        eventoFIX.Message     = message;
                        eventoFIX.MsgSeqNum   = message.Header.GetInt(QuickFix.Fields.Tags.MsgSeqNum);
                        eventoFIX.MsgType     = msgtype;
                        eventoFIX.StreamType  = StreamTypeEnum.STREAM_TYPE_TCP_CONFLATED;


                        ContainerManager.Instance.EnqueueFIX(eventoFIX);

                        if (MDSUtils.shouldLog(lastLog))
                        {
                            logger.Info("Mensagens FIX para processar na fila: " + queueToProcess.Count);
                            lastLog = DateTime.UtcNow.Ticks;
                        }

                        continue;
                    }

                    Thread.Sleep(25);
                }
                catch (Exception ex)
                {
                    logger.Error("queueProc: " + ex.Message, ex);
                }
            }

            logger.Info("Thread de fila de mensagens FIX finalizada");
        }
Exemple #25
0
        public FixInitiator(MarketIncrementalProcessor mktIncProc, ChannelUMDFConfig channelUmdfConfig, string templateFile, Queue <UdpPacket> qUdpPkt, Object replayLockObject)
        {
            logger = LogManager.GetLogger("FixInitiator-" + channelUmdfConfig.ChannelID);

            MDSUtils.AddAppender("FixInitiator-" + channelUmdfConfig.ChannelID, logger.Logger);

            XMLMessageTemplateLoader loader = new XMLMessageTemplateLoader();

            registry = UmdfUtils.loadTemplates(templateFile);
            context  = new Context();
            context.TemplateRegistry = registry;

            _mktIncProc        = mktIncProc;
            _channelUmdfConfig = channelUmdfConfig;

            if (!listaChannelQueues.ContainsKey(channelUmdfConfig.ChannelID))
            {
                listaChannelQueues.Add(channelUmdfConfig.ChannelID, new ListChannelQueues(qUdpPkt, replayLockObject));
            }

            logger.Info("Start(): iniciando sessao FIX...");
            try
            {
                // Cria sessao FIX
                _session = new QuickFix.SessionID(
                    _channelUmdfConfig.TCPConfig.BeginString,
                    _channelUmdfConfig.TCPReplayConfig.SenderCompID,
                    _channelUmdfConfig.TCPReplayConfig.SubID,
                    _channelUmdfConfig.TCPReplayConfig.TargetCompID,
                    _channelUmdfConfig.TCPReplayConfig.SubID);

                // Cria dicionario da configuracao
                QuickFix.Dictionary mainDic = new QuickFix.Dictionary();
                mainDic.SetLong("SocketConnectPort", _channelUmdfConfig.TCPReplayConfig.SocketConnectPort);
                mainDic.SetLong("HeartBtInt", _channelUmdfConfig.TCPConfig.HeartBtInt);
                mainDic.SetLong("ReconnectInterval", _channelUmdfConfig.TCPConfig.ReconnectInterval);
                mainDic.SetBool("ResetOnLogon", _channelUmdfConfig.TCPConfig.ResetOnLogon);
                mainDic.SetBool("ResetOnLogout", _channelUmdfConfig.TCPConfig.ResetOnLogout);
                mainDic.SetBool("ResetOnDisconnect", _channelUmdfConfig.TCPConfig.ResetOnDisconnect);
                mainDic.SetBool("PersistMessages", _channelUmdfConfig.TCPConfig.PersistMessages);
                mainDic.SetString("ConnectionType", _channelUmdfConfig.TCPConfig.ConnectionType);
                mainDic.SetString("SocketConnectHost", _channelUmdfConfig.TCPReplayConfig.SocketConnectHost);
                mainDic.SetString("FileStorePath", _channelUmdfConfig.TCPConfig.FileStorePath);
                mainDic.SetString("FileLogPath", _channelUmdfConfig.TCPConfig.FileLogPath);
                mainDic.SetString("StartTime", _channelUmdfConfig.TCPReplayConfig.StartTime);
                mainDic.SetString("EndTime", _channelUmdfConfig.TCPReplayConfig.EndTime);

                QuickFix.Dictionary sessDic = new QuickFix.Dictionary();
                sessDic.SetString("BeginString", _channelUmdfConfig.TCPConfig.BeginString);
                sessDic.SetString("SenderCompID", _channelUmdfConfig.TCPReplayConfig.SenderCompID);
                sessDic.SetString("SenderSubID", _channelUmdfConfig.TCPReplayConfig.SubID);
                sessDic.SetString("TargetCompID", _channelUmdfConfig.TCPReplayConfig.TargetCompID);
                sessDic.SetString("TargetSubID", _channelUmdfConfig.TCPReplayConfig.SubID);
                sessDic.SetString("DataDictionary", _channelUmdfConfig.TCPConfig.DataDictionary);
                sessDic.SetBool("UseDataDictionary", true);

                // Configure the session settings
                QuickFix.SessionSettings settings = new QuickFix.SessionSettings();

                settings.Set(mainDic);
                settings.Set(_session, sessDic);

                MemoryStoreFactory store   = new MemoryStoreFactory();
                FileLogFactory     log     = new FileLogFactory(settings);
                IMessageFactory    message = new DefaultMessageFactory();

                // Cria o socket
                _initiator = new QuickFix.Transport.SocketInitiator(this, store, settings, log, message);
                _initiator.Start();
            }
            catch (Exception ex)
            {
                logger.Error("Start():" + ex.Message, ex);
            }

            logger.Info("Start(): Sessao FIX iniciado!");
        }
Exemple #26
0
        public FixServerInitiator(
            string channelsID,
            TCPReplayConfig tcpReplayConfig,
            Dictionary <string, SessionID> dctSessionsFixClients,
            Dictionary <string, SessionID> dctSessionsFixChannels)
        {
            _tcpReplayConfig        = tcpReplayConfig;
            _dctSessionsFixClients  = dctSessionsFixClients;
            _dctSessionsFixChannels = dctSessionsFixChannels;

            logger = LogManager.GetLogger("FixServerInitiator-" + tcpReplayConfig.ChannelID);

            MDSUtils.AddAppender("FixServerInitiator-" + tcpReplayConfig.ChannelID, logger.Logger);

            XMLMessageTemplateLoader loader = new XMLMessageTemplateLoader();

            registry = UmdfUtils.loadTemplates(tcpReplayConfig.TemplateFile);
            context  = new Context();
            context.TemplateRegistry = registry;

            try
            {
                // Cria dicionario da configuracao
                QuickFix.Dictionary mainDic = new QuickFix.Dictionary();
                mainDic.SetString("SocketConnectHost", tcpReplayConfig.SocketConnectHost);
                mainDic.SetLong("SocketConnectPort", tcpReplayConfig.SocketConnectPort);
                if (!String.IsNullOrEmpty(tcpReplayConfig.SocketConnectHost1))
                {
                    mainDic.SetString("SocketConnectHost1", tcpReplayConfig.SocketConnectHost1);
                    mainDic.SetLong("SocketConnectPort1", tcpReplayConfig.SocketConnectPort1);
                }
                mainDic.SetLong("HeartBtInt", tcpReplayConfig.HeartBtInt);
                mainDic.SetLong("ReconnectInterval", tcpReplayConfig.ReconnectInterval);
                mainDic.SetBool("ResetOnLogon", tcpReplayConfig.ResetOnLogon);
                mainDic.SetBool("PersistMessages", tcpReplayConfig.PersistMessages);
                mainDic.SetString("ConnectionType", tcpReplayConfig.ConnectionType);
                mainDic.SetString("FileStorePath", tcpReplayConfig.FileStorePath);
                mainDic.SetString("FileLogPath", tcpReplayConfig.FileLogPath);
                mainDic.SetString("StartTime", tcpReplayConfig.StartTime);
                mainDic.SetString("EndTime", tcpReplayConfig.EndTime);

                QuickFix.Dictionary sessDic = new QuickFix.Dictionary();
                sessDic.SetString("BeginString", tcpReplayConfig.BeginString);
                sessDic.SetString("SenderCompID", tcpReplayConfig.SenderCompID);
                sessDic.SetString("TargetCompID", tcpReplayConfig.TargetCompID);
                sessDic.SetString("DataDictionary", tcpReplayConfig.DataDictionary);
                sessDic.SetBool("UseDataDictionary", true);

                // Configure the session settings
                QuickFix.SessionSettings settings = new QuickFix.SessionSettings();

                settings.Set(mainDic);

                MemoryStoreFactory store   = new MemoryStoreFactory();
                FileLogFactory     log     = new FileLogFactory(settings);
                IMessageFactory    message = new DefaultMessageFactory();

                // Cria sessao FIX
                _sessionID = new QuickFix.SessionID(
                    tcpReplayConfig.BeginString,
                    tcpReplayConfig.SenderCompID,
                    tcpReplayConfig.TargetCompID);

                settings.Set(_sessionID, sessDic);

                string[] quebraChannelsID = channelsID.Split(",".ToCharArray());
                foreach (string channel in quebraChannelsID)
                {
                    dctSessionsFixChannels.Add(channel, _sessionID);
                }

                logger.InfoFormat("Start(): iniciando FIX com sessionID[{0}]...", _sessionID.ToString());
                _initiator = new QuickFix.Transport.SocketInitiator(this, store, settings, log, message);
                _initiator.Start();
            }
            catch (Exception ex)
            {
                logger.Error("Start():" + ex.Message, ex);
            }

            logger.Info("Start(): Sessao FIX iniciada!");
        }