예제 #1
0
        public List <TOMessageBackup> BuscarOrderSessionIDs(int idSessao)
        {
            try
            {
                List <TOMessageBackup> ret = new List <TOMessageBackup>();
                SqlDataAdapter         lAdapter;
                AbrirConexao(_strConnectionStringDefault);
                DataSet lDataSet = new DataSet();
                _sqlCommand             = new SqlCommand("prc_fix_ordersession_mngr_consultar", _sqlConn);
                _sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
                _sqlCommand.Parameters.Add(new SqlParameter("@IdSessao", idSessao));
                lAdapter = new SqlDataAdapter(_sqlCommand);
                lAdapter.Fill(lDataSet);
                foreach (DataRow lRow in lDataSet.Tables[0].Rows)
                {
                    TOMessageBackup to = new TOMessageBackup();

                    to.Key           = lRow["Chave"].DBToString();
                    to.BeginString   = lRow["BeginString"].DBToString();
                    to.SenderCompID  = lRow["SenderCompID"].DBToString();
                    to.SenderSubID   = lRow["SenderSubID"].DBToString();
                    to.TargetCompID  = lRow["TargetCompID"].DBToString();
                    to.TargetSubID   = lRow["TargetSubID"].DBToString();
                    to.TipoExpiracao = lRow["TipoExpiracao"].DBToString();
                    to.DataExpiracao = lRow["DataExpiracao"].DBToString();
                    to.DataEnvio     = lRow["DataEnvio"].DBToString();
                    to.MsgSeqNum     = lRow["MsgSeqNum"].DBToString();
                    to.ClOrdID       = lRow["ClOrdID"].DBToString();
                    to.OrigClOrdID   = lRow["OrigClOrdID"].DBToString();
                    to.Account       = lRow["Account"].DBToString();

                    // TODO[FF]: Efetuar a desconversao do base64, de-serealizar e montar o list
                    string partyIds = lRow["PartyIDs"].DBToString();
                    if (!string.IsNullOrEmpty(partyIds))
                    {
                        byte[]          bPartyId = Convert.FromBase64String(partyIds);
                        BinaryFormatter bs       = new BinaryFormatter();
                        MemoryStream    s        = new MemoryStream(bPartyId);
                        to.PartyIDs = bs.Deserialize(s) as List <PartyIDBackup>;
                        s.Dispose();
                        bs = null;
                    }
                    string orderInfo = lRow["OrderInfo"].DBToString();
                    if (!string.IsNullOrEmpty(orderInfo))
                    {
                        byte[]          bOrderInfo = Convert.FromBase64String(orderInfo);
                        BinaryFormatter bs         = new BinaryFormatter();
                        MemoryStream    s          = new MemoryStream(bOrderInfo);
                        to.Order = bs.Deserialize(s) as Gradual.OMS.RoteadorOrdens.Lib.Dados.OrdemInfo;
                        s.Dispose();
                        bs = null;
                    }

                    to.TipoLimite = lRow["TipoLimite"].DBToInt32();

                    //to.Order = item.Value.Order; // TODO [FF] - desconverter do base64, de-serealizar e montar o list
                    to.MensagemQF       = lRow["MensagemQF"].DBToString();
                    to.ExchangeNumberID = lRow["ExchangeNumberID"].DBToString();
                    to.ExchangeSeqNum   = lRow["ExchangeSeqNum"].DBToInt32();
                    ret.Add(to);
                    to = null;
                }
                FecharConexao();
                return(ret);
            }
            catch (Exception ex)
            {
                FecharConexao();
                logger.Error("BuscarOrderSessionIDs(): " + ex.Message, ex);
                return(null);
            }
        }
예제 #2
0
        public void SaveOrderSessionIDsToDB(int idSessao)
        {
            try
            {
                // Serialize Dictionary to dat file
                if (!_serializing)
                {
                    _serializing = true;
                    Dictionary <string, TOOrderSession> regs = new Dictionary <string, TOOrderSession>(_dicMsgsCl);
                    DbFix dbFix = new DbFix();
                    logger.Info("Inicio da Gravacao");

                    // Efetuar a limpeza para "re-gravar" os registros
                    if (!dbFix.LimparOrderSessionIDs(idSessao))
                    {
                        logger.Info("Problemas na limpeza dos OrderSessionIDs");
                        return;
                    }

                    foreach (KeyValuePair <string, TOOrderSession> item in regs)
                    {
                        TOMessageBackup to = new TOMessageBackup();

                        to.Key           = item.Key;
                        to.BeginString   = item.Value.Sessao.BeginString;
                        to.SenderCompID  = item.Value.Sessao.SenderCompID;
                        to.SenderSubID   = item.Value.Sessao.SenderSubID;
                        to.TargetCompID  = item.Value.Sessao.TargetCompID;
                        to.TargetSubID   = item.Value.Sessao.TargetSubID;
                        to.TipoExpiracao = item.Value.TipoExpiracao;
                        to.DataExpiracao = item.Value.DataExpiracao;
                        to.DataEnvio     = item.Value.DataEnvio;
                        to.MsgSeqNum     = item.Value.MsgSeqNum.ToString();
                        to.ClOrdID       = item.Value.ClOrdID;
                        to.OrigClOrdID   = item.Value.OrigClOrdID;
                        to.Account       = item.Value.Account.ToString();
                        int lenPid = item.Value.PartyIDs.Count;
                        for (int i = 0; i < lenPid; i++)
                        {
                            PartyIDBackup pId = new PartyIDBackup();
                            pId.PartyID       = item.Value.PartyIDs[i].GetField(Tags.PartyID);
                            pId.PartyIDSource = item.Value.PartyIDs[i].GetChar(Tags.PartyIDSource);
                            pId.PartyRole     = item.Value.PartyIDs[i].GetInt(Tags.PartyRole);
                            to.PartyIDs.Add(pId);
                        }
                        to.TipoLimite       = (int)item.Value.TipoLimite;
                        to.Order            = item.Value.Order;
                        to.MensagemQF       = item.Value.MensagemQF.ToString();
                        to.ExchangeNumberID = item.Value.ExchangeNumberID;
                        to.ExchangeSeqNum   = item.Value.ExchangeSeqNum;
                        dbFix.InserirOrderSessionItem(idSessao, to);
                        to = null;
                    }
                    logger.InfoFormat("Gravando OrderSessions da Sessao: [{0}] Registros: [{1}]", idSessao, regs.Count);
                    //BinaryFormatter bs = new BinaryFormatter();
                    //bs.Serialize(fs, lst);
                    //bs = null;
                    //fs.Close();
                    //fs = null;
                    regs.Clear();
                    regs         = null;
                    _serializing = false;
                }
                else
                {
                    if (_serializing)
                    {
                        logger.Debug("SaveSessionIDS(): Processo de serializacao já em execucao!!!");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("SaveSessionIDS(): Erro na serializacao dos registros do dicionario: " + ex.Message, ex);
                _serializing = false; // mudar para false para tentar backupear no proximo "ciclo"
                //fs.Close();
                //fs = null;
            }
        }
예제 #3
0
        public bool InserirOrderSessionItem(int idSessaoFix, TOMessageBackup to)
        {
            try
            {
                AbrirConexao(_strConnectionStringDefault);
                _sqlCommand             = new SqlCommand("prc_fix_ordersession_mngr_inserir", _sqlConn);
                _sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
                _sqlCommand.Parameters.Add(new SqlParameter("@id_session", idSessaoFix));
                _sqlCommand.Parameters.Add(new SqlParameter("@Chave", to.Key));
                _sqlCommand.Parameters.Add(new SqlParameter("@BeginString", to.BeginString));
                _sqlCommand.Parameters.Add(new SqlParameter("@TargetCompID", to.TargetCompID));
                _sqlCommand.Parameters.Add(new SqlParameter("@TargetSubID", to.TargetSubID));
                _sqlCommand.Parameters.Add(new SqlParameter("@SenderCompID", to.SenderCompID));
                _sqlCommand.Parameters.Add(new SqlParameter("@SenderSubID", to.SenderSubID));
                _sqlCommand.Parameters.Add(new SqlParameter("@TipoExpiracao", to.TipoExpiracao));
                _sqlCommand.Parameters.Add(new SqlParameter("@DataExpiracao", to.DataExpiracao));
                _sqlCommand.Parameters.Add(new SqlParameter("@DataEnvio", to.DataEnvio));
                _sqlCommand.Parameters.Add(new SqlParameter("@MsgSeqNum", to.MsgSeqNum));
                _sqlCommand.Parameters.Add(new SqlParameter("@OrigClOrdID", to.OrigClOrdID));
                _sqlCommand.Parameters.Add(new SqlParameter("@ClOrdID", to.ClOrdID));
                _sqlCommand.Parameters.Add(new SqlParameter("@Account", to.Account));

                // Serializar PartyIDs
                string partyids = string.Empty;
                if (to.PartyIDs.Count > 0)
                {
                    BinaryFormatter bs = new BinaryFormatter();
                    using (MemoryStream stream = new MemoryStream())
                    {
                        bs.Serialize(stream, to.PartyIDs);
                        partyids = Convert.ToBase64String(stream.ToArray());
                    }
                    bs = null;
                }
                _sqlCommand.Parameters.Add(new SqlParameter("@PartyIDs", partyids));
                // Serializar OrdemInfo
                string orderinfo = string.Empty;
                if (to.Order != null)
                {
                    BinaryFormatter bs = new BinaryFormatter();
                    using (MemoryStream stream = new MemoryStream())
                    {
                        bs.Serialize(stream, to.Order);
                        orderinfo = Convert.ToBase64String(stream.ToArray());
                    }
                    bs = null;
                }
                _sqlCommand.Parameters.Add(new SqlParameter("@OrderInfo", orderinfo));
                _sqlCommand.Parameters.Add(new SqlParameter("@TipoLimite", to.TipoLimite));
                _sqlCommand.Parameters.Add(new SqlParameter("@MensagemQF", to.MensagemQF));
                _sqlCommand.Parameters.Add(new SqlParameter("@ExchangeNumberID", to.ExchangeNumberID));
                _sqlCommand.Parameters.Add(new SqlParameter("@ExchangeSeqNum", to.ExchangeSeqNum));
                _sqlCommand.Parameters.Add(new SqlParameter("@ChaveDicionario", to.ChaveDicionario));

                int rows = _sqlCommand.ExecuteNonQuery();
                FecharConexao();
                return(true);
            }
            catch (Exception ex)
            {
                FecharConexao();
                logger.Error("InserirOrderSessionItem(): " + ex.Message, ex);
                return(false);
            }
        }
예제 #4
0
        /*
         * public void SaveSessionIDS(string filename)
         * {
         *  FileStream fs = File.Open(filename, FileMode.Create, FileAccess.Write);
         *  try
         *  {
         *      // Serialize Dictionary to dat file
         *      List<TOMessageBackup> lst = new List<TOMessageBackup>();
         *      if (!_serializing)
         *      {
         *          _serializing = true;
         *          Dictionary<string, TOOrderSession> regs = new Dictionary<string, TOOrderSession>(_dicMsgsCl);
         *
         *
         *          foreach (KeyValuePair<string, TOOrderSession> item in regs)
         *          {
         *              TOMessageBackup to = new TOMessageBackup();
         *
         *              to.Key = item.Key;
         *              to.BeginString = item.Value.Sessao.BeginString;
         *              to.SenderCompID = item.Value.Sessao.SenderCompID;
         *              to.SenderSubID = item.Value.Sessao.SenderSubID;
         *              to.TargetCompID = item.Value.Sessao.TargetCompID;
         *              to.TargetSubID = item.Value.Sessao.TargetSubID;
         *              to.TipoExpiracao = item.Value.TipoExpiracao;
         *              to.DataExpiracao = item.Value.DataExpiracao;
         *              to.DataEnvio = item.Value.DataEnvio;
         *              to.MsgSeqNum = item.Value.MsgSeqNum.ToString();
         *              to.ClOrdID = item.Value.ClOrdID;
         *              to.OrigClOrdID = item.Value.OrigClOrdID;
         *              to.Account = item.Value.Account.ToString();
         *              int lenPid = item.Value.PartyIDs.Count;
         *              for (int i = 0; i < lenPid; i++)
         *              {
         *                  PartyIDBackup pId = new PartyIDBackup();
         *                  pId.PartyID = item.Value.PartyIDs[i].GetField(Tags.PartyID);
         *                  pId.PartyIDSource = item.Value.PartyIDs[i].GetChar(Tags.PartyIDSource);
         *                  pId.PartyRole = item.Value.PartyIDs[i].GetInt(Tags.PartyRole);
         *                  to.PartyIDs.Add(pId);
         *              }
         *              // to.MensagemQF = item.Value.MensagemQF.ToString();
         *              to.TipoLimite = (int)item.Value.TipoLimite;
         *              to.Order = item.Value.Order;
         *              to.MensagemQF = item.Value.MensagemQF.ToString();
         *              to.ExchangeNumberID = item.Value.ExchangeNumberID;
         *              to.ExchangeSeqNum = item.Value.ExchangeSeqNum;
         *              //to.SecondaryOrderID = item.Value.SecondaryOrderID;
         *              //to.TradeDate = item.Value.TradeDate;
         *              lst.Add(to);
         *              to = null;
         *          }
         *
         *          BinaryFormatter bs = new BinaryFormatter();
         *          bs.Serialize(fs, lst);
         *          bs = null;
         *          logger.InfoFormat("SaveSessionIDS(): Registros serializados: [{0}] [{1}]", lst.Count, filename);
         *          // Efetuar limpeza da lista
         *          int len = lst.Count;
         *          for (int i = 0; i < len; i++)
         *          {
         *              TOMessageBackup aux = lst[i];
         *              aux = null;
         *          }
         *
         *          lst.Clear();
         *          lst = null;
         *          fs.Close();
         *          fs = null;
         *          regs.Clear();
         *          regs = null;
         *          _serializing = false;
         *      }
         *      else
         *      {
         *          if (_serializing)
         *              logger.Debug("SaveSessionIDS(): Processo de serializacao já em execucao!!!");
         *      }
         *  }
         *  catch (Exception ex)
         *  {
         *      logger.Error("SaveSessionIDS(): Erro na serializacao dos registros do dicionario: " + ex.Message, ex);
         *      _serializing = false; // mudar para false para tentar backupear no proximo "ciclo"
         *      fs.Close();
         *      fs = null;
         *  }
         * }
         */
        /*
         * public void LoadSessionIDs(string fileName, QuickFix.DataDictionary.DataDictionary dataDic)
         * {
         *  string msgQF = string.Empty;
         *  try
         *  {
         *      if (File.Exists(fileName))
         *      {
         *          List<TOMessageBackup> lst = new List<TOMessageBackup>();
         *          FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read);
         *          BinaryFormatter bformatter = new BinaryFormatter();
         *          lst = (List<TOMessageBackup>)bformatter.Deserialize(fs);
         *          int length = lst.Count;
         *          if (lst.Count > 0)
         *          {
         *              lock (_dicMsgsCl)
         *              {
         *                  for (int i = 0; i < length; i++)
         *                  {
         *                      TOMessageBackup to = lst[i];
         *                      TOOrderSession toOrder = new TOOrderSession();
         *                      SessionID ssID = null;
         *                      if (!string.IsNullOrEmpty(to.SenderSubID) && (!string.IsNullOrEmpty(to.TargetSubID)))
         *                          ssID = new SessionID(to.BeginString, to.SenderCompID, to.SenderSubID, to.TargetCompID, to.TargetSubID);
         *                      else
         *                          ssID = new SessionID(to.BeginString, to.SenderCompID, to.TargetCompID);
         *                      toOrder.Sessao = ssID;
         *                      toOrder.TipoExpiracao = to.TipoExpiracao;
         *                      toOrder.DataExpiracao = to.DataExpiracao;
         *                      toOrder.DataEnvio = to.DataEnvio;
         *                      toOrder.MsgSeqNum = Convert.ToInt32(to.MsgSeqNum);
         *                      toOrder.ClOrdID = to.ClOrdID;
         *                      toOrder.OrigClOrdID = to.OrigClOrdID;
         *                      toOrder.Account = Convert.ToInt32(to.Account);
         *                      int len = to.PartyIDs.Count;
         *                      for (int j = 0; j < len; j++)
         *                      {
         *                          QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup grp = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
         *                          grp.Set(new PartyID(to.PartyIDs[j].PartyID));
         *                          grp.Set(new PartyIDSource(to.PartyIDs[j].PartyIDSource));
         *                          grp.Set(new PartyRole(to.PartyIDs[j].PartyRole));
         *                          toOrder.PartyIDs.Add(grp);
         *                      }
         *
         *                      toOrder.TipoLimite = (TipoLimiteEnum)to.TipoLimite;
         *                      toOrder.Order = to.Order;
         *                      toOrder.MensagemQF = new QuickFix.Message(to.MensagemQF, dataDic, true);
         *                      toOrder.ExchangeNumberID = to.ExchangeNumberID;
         *                      toOrder.ExchangeSeqNum = to.ExchangeSeqNum;
         *                      //toOrder.SecondaryOrderID = to.SecondaryOrderID;
         *                      //toOrder.TradeDate = to.TradeDate;
         *                      _dicMsgsCl.Add(to.Key, toOrder);
         *                  }
         *              }
         *              logger.Info("LoadSessionIDs(): Registros recuperados: " + lst.Count);
         *              lst.Clear();
         *              lst = null;
         *          }
         *          if (fs != null)
         *          {
         *              fs.Close();
         *              fs = null;
         *          }
         *      }
         *  }
         *  catch (Exception ex)
         *  {
         *      logger.Error("LoadSessionIDs(): Erro na deserializacao dos registros do dicionario: MsgQF: " + msgQF + " " + ex.Message, ex);
         *  }
         * }
         */
        public void LoadOrderSessionIDsFromDB(int idSession, QuickFix.DataDictionary.DataDictionary dataDic)
        {
            try
            {
                DbFix dbFix = new DbFix();
                List <TOMessageBackup> lst = dbFix.BuscarOrderSessionIDs(idSession);

                int length = lst.Count;
                if (lst.Count > 0)
                {
                    lock (_dicMsgsCl)
                    {
                        for (int i = 0; i < length; i++)
                        {
                            TOMessageBackup to      = lst[i];
                            TOOrderSession  toOrder = new TOOrderSession();
                            SessionID       ssID    = null;
                            if (!string.IsNullOrEmpty(to.SenderSubID) && (!string.IsNullOrEmpty(to.TargetSubID)))
                            {
                                ssID = new SessionID(to.BeginString, to.SenderCompID, to.SenderSubID, to.TargetCompID, to.TargetSubID);
                            }
                            else
                            {
                                ssID = new SessionID(to.BeginString, to.SenderCompID, to.TargetCompID);
                            }
                            toOrder.Sessao        = ssID;
                            toOrder.TipoExpiracao = to.TipoExpiracao;
                            toOrder.DataExpiracao = to.DataExpiracao;
                            toOrder.DataEnvio     = to.DataEnvio;
                            toOrder.MsgSeqNum     = Convert.ToInt32(to.MsgSeqNum);
                            toOrder.ClOrdID       = to.ClOrdID;
                            toOrder.OrigClOrdID   = to.OrigClOrdID;
                            toOrder.Account       = Convert.ToInt32(to.Account);
                            int len = to.PartyIDs.Count;
                            for (int j = 0; j < len; j++)
                            {
                                QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup grp = new QuickFix.FIX44.NewOrderSingle.NoPartyIDsGroup();
                                grp.Set(new PartyID(to.PartyIDs[j].PartyID));
                                grp.Set(new PartyIDSource(to.PartyIDs[j].PartyIDSource));
                                grp.Set(new PartyRole(to.PartyIDs[j].PartyRole));
                                toOrder.PartyIDs.Add(grp);
                            }

                            toOrder.TipoLimite       = (TipoLimiteEnum)to.TipoLimite;
                            toOrder.Order            = to.Order;
                            toOrder.MensagemQF       = new QuickFix.Message(to.MensagemQF, dataDic, true);
                            toOrder.ExchangeNumberID = to.ExchangeNumberID;
                            toOrder.ExchangeSeqNum   = to.ExchangeSeqNum;
                            _dicMsgsCl.Add(to.Key, toOrder);
                        }
                    }
                    logger.Info("LoadOrderSessionIDsFromDB(): Registros recuperados: " + lst.Count);
                    lst.Clear();
                    lst = null;
                }
            }
            catch (Exception ex)
            {
                logger.Error("LoadOrderSessionIDsFromDB(): Erro na deserializacao dos registros a partir do banco de dados: " + ex.Message, ex);
            }
        }