예제 #1
0
        static void Main(string[] args)
        {
            log.Debug("--> Main");

            SocketListener listener = new SocketListener();

            IMessageFactory factory = new MessageFactoryFIX(ConfigurationManager.AppSettings["BeginString"]);

            //IMessageFactory factory = new FIX_4_2.MessageFactoryFIX();
            _factory = factory;

            IEngine engine = new SocketEngine();

            engine.MessageCacheFactory = new MessageCacheFlatFileFactory();
            engine.MessageFactory      = factory;
            engine.Initialize(
                ConfigurationManager.AppSettings["SenderCompID"], null,
                ConfigurationManager.AppSettings["TargetCompID"], null);
            engine.Received += new MessageEventHandler(engine_Received);
            engine.Sent     += new MessageEventHandler(engine_Sent);

            listener.Add(engine);

            int port = int.Parse(ConfigurationManager.AppSettings["Port"]);

            listener.Init(port);

            Console.WriteLine("press <Enter> to exit...");
            Console.ReadLine();

            listener.Dispose();
            engine.Dispose();

            log.Debug("<-- Main");
        }
예제 #2
0
        public void ParseTest()
        {
            string            text    = "8=FIX.4.235=81=1331=111=1111=111111=11=112=11=1331=111=1111=111111=11=112=11=1331=111=1111=111111=1";
            MessageFactoryFIX factory = new MessageFactoryFIX("FIX.4.2");
            IMessage          message = factory.Parse(text);

            Assert.AreEqual(19, message.Fields.Count, "Message.Fields.Count");
        }
예제 #3
0
        private MessageCacheFlatFile Create()
        {
            MessageFactoryFIX factory = new MessageFactoryFIX("FIX.4.2");

            MessageCacheFlatFile cache = new MessageCacheFlatFile();

            cache.Initialize(factory);
            cache.Path = _path;
            cache.Open("TEST", null, "TEST", null, DateTime.MinValue);
            return(cache);
        }
예제 #4
0
        private static void SendMessagesFromFile(string fileName)
        {
            StreamReader fileReader = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);

            while (!fileReader.EndOfStream)
            {
                Console.WriteLine("press <Enter> to send next message");
                Console.ReadLine();

                IMessage message = null;

                string         line        = fileReader.ReadLine();
                FieldReaderFIX fieldReader = new FieldReaderFIX(line);
                IField         field;

                while ((field = fieldReader.GetNextField()) != null)
                {
                    if (message == null && field.Tag == 35)
                    {
                        message = _factory.CreateInstance(field.Value);
                        field   = null;
                    }
                    else if (message == null)
                    {
                        throw new Exception("MsgType(35) missing");
                    }
                    else if (string.Equals(field.Value, "{$ID}"))
                    {
                        field.Value = GetNextID();
                    }
                    else if (string.Equals(field.Value, "{$ID.LAST}"))
                    {
                        field.Value = GetLastID();
                    }
                    else if (string.Equals(field.Value, "{$UTCTimestamp}"))
                    {
                        field.Value = MessageFactoryFIX.ToFIXUTCTimestamp(DateTime.Now.ToUniversalTime());
                    }

                    if (field != null)
                    {
                        message.Fields.Add(field);
                    }
                }

                _engine.Send(message);
            }
        }
예제 #5
0
        public void ResetMsgSeqNumInWithExistingMessagesShouldPass()
        {
            _path = Path.GetTempPath();
            MessageFactoryFIX factory = new MessageFactoryFIX("FIX.4.2");

            if (File.Exists(_path + "TEST__TEST__00010101.LOG"))
            {
                File.Delete(_path + "TEST__TEST__00010101.LOG");
            }
            if (File.Exists(_path + "TEST__TEST__00010101.LDX"))
            {
                File.Delete(_path + "TEST__TEST__00010101.LDX");
            }

            MessageCacheFlatFile cache;

            cache = Create();
            for (int j = 1; j < 100; j++)
            {
                IMessage message = factory.CreateInstance("D");
                message.Direction = MessageDirection.In;
                message.MsgSeqNum = j;
                for (int i = 10000; i < 10100; i++)
                {
                    message.Fields.Add(new Field(i, "VALUE" + i));
                }
                factory.Build(message);
                cache.AddMessage(message);
            }
            Assert.AreEqual(99, cache.MsgSeqNumIn);
            cache.Close();

            cache = Create();
            cache.ResetMsgSeqNumIn(200);
            Assert.AreEqual(199, cache.MsgSeqNumIn);
            cache.Close();

            cache = Create();
            Assert.AreEqual(199, cache.MsgSeqNumIn);
            cache.ResetMsgSeqNumIn(300);
            Assert.AreEqual(299, cache.MsgSeqNumIn);
            cache.Close();

            cache = Create();
            Assert.AreEqual(299, cache.MsgSeqNumIn);
            cache.Close();
        }
예제 #6
0
        static void Main(string[] args)
        {
            log.Debug("--> Main");

            try
            {
                SocketLogonArgs logonArgs = new SocketLogonArgs();
                logonArgs.IPAddress = IPAddress.Parse(ConfigurationManager.AppSettings["Host"]);
                logonArgs.Port      = int.Parse(ConfigurationManager.AppSettings["Port"]);

                //set additional logon tags
                FIX4NET.FieldCollection fieldsLogon = (FIX4NET.FieldCollection)ConfigurationManager.GetSection("fixLogonFieldsSection");
                if (fieldsLogon != null)
                {
                    foreach (IField field in fieldsLogon)
                    {
                        logonArgs.AddField(field);
                    }
                }

                MessageFactoryFIX factory = new MessageFactoryFIX(ConfigurationManager.AppSettings["BeginString"]);
                //IMessageFactory factory = new FIX_4_4.MessageFactoryFIX();
                //IMessageFactory factory = new FIX_4_2.MessageFactoryFIX();
                _factory = factory;

                //additional tags for factory to append on every message
                FIX4NET.FieldCollection fields = (FIX4NET.FieldCollection)ConfigurationManager.GetSection("fixMessageFieldsSection");
                if (fields != null)
                {
                    foreach (FIX4NET.IField field in fields)
                    {
                        factory.AddField(field);
                    }
                }
                //create instance of FIX engine
                SocketEngine engine = new SocketEngine();

                //set message timeout
                string messageTimeout = ConfigurationManager.AppSettings["MessageTimeout"];
                if (!string.IsNullOrEmpty(messageTimeout))
                {
                    engine.MessageTimeout = int.Parse(messageTimeout);
                }

                //initialize FIX engine
                _engine = engine;
                _engine.MessageCacheFactory = new MessageCacheFlatFileFactory();
                _engine.MessageFactory      = factory;
                _engine.Initialize(
                    ConfigurationManager.AppSettings["SenderCompID"], ConfigurationManager.AppSettings["SenderSubID"],
                    ConfigurationManager.AppSettings["TargetCompID"], ConfigurationManager.AppSettings["TargetSubID"]);
                _engine.AllowOfflineSend = true;

                _engine.Received += new MessageEventHandler(engine_Received);
                _engine.Sent     += new MessageEventHandler(engine_Sent);

                //reset MsgSeqNum(Out)
                string resetMsgSeqNumOut = ConfigurationManager.AppSettings["ResetMsgSeqNumOut"];
                if (!string.IsNullOrEmpty(resetMsgSeqNumOut))
                {
                    _engine.ResetMsgSeqNumOut(int.Parse(resetMsgSeqNumOut));
                }

                //reset MsgSeqNum(In)
                string resetMsgSeqNumIn = ConfigurationManager.AppSettings["ResetMsgSeqNumIn"];
                if (!string.IsNullOrEmpty(resetMsgSeqNumIn))
                {
                    _engine.ResetMsgSeqNumIn(int.Parse(resetMsgSeqNumIn));
                }

                _engine.Logon(logonArgs);
                if (!_engine.IsConnected)
                {
                    throw new Exception("Logong failed");
                }

                Console.WriteLine("press <Enter> to continue...");
                Console.ReadLine();

                string sendMessageFromFile = ConfigurationManager.AppSettings["SendMessagesFromFile"];
                if (!string.IsNullOrEmpty(sendMessageFromFile))
                {
                    SendMessagesFromFile(sendMessageFromFile);
                }

                Console.WriteLine("press <Enter> to exit...");
                Console.ReadLine();

                _engine.Logout("USER LOGOUT REQUESTED");
                _engine.Dispose();
            }
            catch (Exception ex)
            {
                log.Error("Error running FIX test", ex);
            }

            log.Debug("<-- Main");
        }