/// <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"); }
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"); }
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); } } }
/// <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); } } }
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); } } }
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"); } }
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; } }
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"); }
/// <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"); }
/// <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> /// /// </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); } } }
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"); }