Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DataServer"/> class.
 /// </summary>
 /// <param name="container">The componentry container.</param>
 /// <param name="messagingAdapter">The messaging adapter.</param>
 /// <param name="headerSerializer">The header serializer.</param>
 /// <param name="requestSerializer">The inbound message serializer.</param>
 /// <param name="responseSerializer">The outbound message serializer.</param>
 /// <param name="compressor">The data compressor.</param>
 /// <param name="encryption">The encryption configuration.</param>
 /// <param name="serviceName">The service name.</param>
 /// <param name="requestPort">The inbound port.</param>
 /// <param name="responsePort">The outbound port.</param>
 public DataServer(
     IComponentryContainer container,
     IMessageBusAdapter messagingAdapter,
     ISerializer <Dictionary <string, string> > headerSerializer,
     IMessageSerializer <Request> requestSerializer,
     IMessageSerializer <Response> responseSerializer,
     ICompressor compressor,
     EncryptionSettings encryption,
     Label serviceName,
     Port requestPort,
     Port responsePort)
     : base(
         container,
         messagingAdapter,
         headerSerializer,
         requestSerializer,
         responseSerializer,
         compressor,
         encryption,
         serviceName,
         ZmqNetworkAddress.AllInterfaces(requestPort),
         ZmqNetworkAddress.AllInterfaces(responsePort))
 {
     this.RegisterHandler <DataRequest>(this.OnMessage);
     this.RegisterHandler <DataResponse>(this.OnMessage);
     this.RegisterHandler <QueryFailure>(this.OnMessage);
 }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PublisherDataBus"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="dataBusAdapter">The data bus adapter.</param>
        /// <param name="compressor">The data compressor.</param>
        /// <param name="encryption">The encryption configuration.</param>
        /// <param name="networkAddress">The publishers network address.</param>
        protected PublisherDataBus(
            IComponentryContainer container,
            IDataBusAdapter dataBusAdapter,
            ICompressor compressor,
            EncryptionSettings encryption,
            ZmqNetworkAddress networkAddress)
            : base(container, dataBusAdapter)
        {
            this.compressor = compressor;

            this.socket = new PublisherSocket
            {
                Options =
                {
                    Identity = Encoding.UTF8.GetBytes($"{nameof(Nautilus)}-{this.Name.Value}"),
                    Linger   = TimeSpan.FromSeconds(1),
                },
            };

            this.networkAddress = networkAddress;

            if (encryption.UseEncryption)
            {
                EncryptionProvider.SetupSocket(encryption, this.socket);
                this.Logger.LogInformation(LogId.Network, $"{encryption.Algorithm} encryption setup for {this.networkAddress}");
            }
            else
            {
                this.Logger.LogWarning(LogId.Network, $"No encryption setup for {this.networkAddress}");
            }

            this.SentCount = 0;
        }
Example #3
0
        internal void InitializedPublisher_IsInCorrectState()
        {
            // Arrange
            // Act
            var publisher = new MockDataPublisher(
                this.container,
                DataBusFactory.Create(this.container),
                EncryptionSettings.None(),
                ZmqNetworkAddress.LocalHost(new Port(55555)));

            // Assert
            Assert.Equal(ComponentState.Initialized, publisher.ComponentState);
            Assert.Equal(0, publisher.SentCount);
        }
Example #4
0
        public MessageServerTests(ITestOutputHelper output)
            : base(output)
        {
            // Fixture Setup
            this.container          = TestComponentryContainer.Create(output);
            this.messagingAdapter   = new MockMessageBusProvider(this.container).Adapter;
            this.headerSerializer   = new MsgPackDictionarySerializer();
            this.requestSerializer  = new MsgPackRequestSerializer();
            this.responseSerializer = new MsgPackResponseSerializer();
            this.compressor         = new BypassCompressor();

            this.testRequestAddress  = ZmqNetworkAddress.LocalHost(new Port(55655));
            this.testResponseAddress = ZmqNetworkAddress.LocalHost(new Port(55656));
        }
Example #5
0
 public MockDataPublisher(
     IComponentryContainer container,
     IDataBusAdapter dataBusAdapter,
     EncryptionSettings encryption,
     ZmqNetworkAddress networkAddress)
     : base(
         container,
         dataBusAdapter,
         new BypassCompressor(),
         encryption,
         networkAddress)
 {
     this.RegisterHandler <(string, string)>(this.OnMessage);
 }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandServer"/> class.
        /// </summary>
        /// <param name="container">The component setup container.</param>
        /// <param name="messagingAdapter">The message bus adapter.</param>
        /// <param name="headerSerializer">The header serializer.</param>
        /// <param name="requestSerializer">The request serializer.</param>
        /// <param name="responseSerializer">The response serializer.</param>
        /// <param name="commandSerializer">The command serializer.</param>
        /// <param name="compressor">The message compressor.</param>
        /// <param name="encryption">The encryption configuration.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="config">The network configuration.</param>
        public CommandServer(
            IComponentryContainer container,
            IMessageBusAdapter messagingAdapter,
            ISerializer <Dictionary <string, string> > headerSerializer,
            IMessageSerializer <Request> requestSerializer,
            IMessageSerializer <Response> responseSerializer,
            IMessageSerializer <Command> commandSerializer,
            ICompressor compressor,
            EncryptionSettings encryption,
            Label serviceName,
            NetworkConfiguration config)
            : base(
                container,
                messagingAdapter,
                headerSerializer,
                requestSerializer,
                responseSerializer,
                compressor,
                encryption,
                serviceName,
                ZmqNetworkAddress.AllInterfaces(config.CommandReqPort),
                ZmqNetworkAddress.AllInterfaces(config.CommandResPort))
        {
            this.commandThrottler = new Throttler <Command>(
                container,
                this.SendToExecutionEngine,
                Duration.FromSeconds(1),
                config.CommandsPerSecond,
                nameof(Command));

            this.orderThrottler = new Throttler <Command>(
                container,
                this.commandThrottler.Endpoint.Send,
                Duration.FromSeconds(1),
                config.NewOrdersPerSecond,
                nameof(Order));

            this.RegisterSerializer(commandSerializer);
            this.RegisterHandler <SubmitOrder>(this.OnMessage);
            this.RegisterHandler <SubmitBracketOrder>(this.OnMessage);
            this.RegisterHandler <CancelOrder>(this.OnMessage);
            this.RegisterHandler <ModifyOrder>(this.OnMessage);
            this.RegisterHandler <AccountInquiry>(this.OnMessage);

            this.commandThrottler.Start();
            this.orderThrottler.Start();
        }
Example #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventPublisher"/> class.
 /// </summary>
 /// <param name="container">The component setup container.</param>
 /// <param name="serializer">The event serializer.</param>
 /// <param name="compressor">The event compressor.</param>
 /// <param name="encryption">The encryption configuration.</param>
 /// <param name="serviceName">The service name.</param>
 /// <param name="port">The publisher port.</param>
 public EventPublisher(
     IComponentryContainer container,
     ISerializer <Event> serializer,
     ICompressor compressor,
     EncryptionSettings encryption,
     Label serviceName,
     Port port)
     : base(
         container,
         serializer,
         compressor,
         encryption,
         serviceName,
         ZmqNetworkAddress.AllInterfaces(port))
 {
     this.RegisterHandler <TradeEvent>(this.OnEvent);
     this.RegisterHandler <AccountStateEvent>(this.OnEvent);
 }
Example #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataPublisher"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="dataBusAdapter">The data bus adapter.</param>
        /// <param name="instrumentSerializer">The instrument serializer.</param>
        /// <param name="compressor">The data compressor.</param>
        /// <param name="encryption">The encryption configuration.</param>
        /// <param name="port">The port.</param>
        public DataPublisher(
            IComponentryContainer container,
            IDataBusAdapter dataBusAdapter,
            IDataSerializer <Instrument> instrumentSerializer,
            ICompressor compressor,
            EncryptionSettings encryption,
            Port port)
            : base(
                container,
                dataBusAdapter,
                compressor,
                encryption,
                ZmqNetworkAddress.AllInterfaces(port))
        {
            this.instrumentSerializer = instrumentSerializer;

            this.RegisterHandler <Instrument>(this.OnMessage);

            this.Subscribe <BarData>();
            this.Subscribe <Instrument>();
        }
Example #9
0
        internal void GivenMessageToPublish_WhenMessageValid_PublishesToSubscriber()
        {
            // Arrange
            var publisher = new MockDataPublisher(
                this.container,
                DataBusFactory.Create(this.container),
                EncryptionSettings.None(),
                ZmqNetworkAddress.LocalHost(new Port(55555)));

            publisher.Start().Wait();

            const string testAddress = "tcp://localhost:55555";
            var          subscriber  = new SubscriberSocket(testAddress);

            subscriber.Connect(testAddress);
            subscriber.Subscribe(TestTopic);

            Task.Delay(100).Wait(); // Allow sockets to subscribe

            // Act
            const string toSend = "1234,1234";

            publisher.Endpoint.SendAsync((TestTopic, toSend));

            var topic   = subscriber.ReceiveFrameBytes();
            var message = subscriber.ReceiveFrameBytes();

            // Assert
            Assert.Equal(TestTopic, Encoding.UTF8.GetString(topic));
            Assert.Equal(toSend, Encoding.UTF8.GetString(message));
            Assert.Equal(ComponentState.Running, publisher.ComponentState);
            Assert.Equal(1, publisher.SentCount);

            // Tear Down
            subscriber.Disconnect(testAddress);
            subscriber.Dispose();
            publisher.Stop().Wait();
            publisher.Dispose();
        }
Example #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MessagePublisher{T}"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="serializer">The message serializer.</param>
        /// <param name="compressor">The message compressor.</param>
        /// <param name="encryption">The encryption configuration.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="networkAddress">The publishers network address.</param>
        protected MessagePublisher(
            IComponentryContainer container,
            ISerializer <T> serializer,
            ICompressor compressor,
            EncryptionSettings encryption,
            Label serviceName,
            ZmqNetworkAddress networkAddress)
            : base(container)
        {
            this.serializer = serializer;
            this.compressor = compressor;

            this.socket = new PublisherSocket
            {
                Options =
                {
                    Identity = Encoding.UTF8.GetBytes($"{serviceName.Value}-{this.Name.Value}"),
                    Linger   = TimeSpan.FromSeconds(1),
                },
            };

            this.networkAddress = networkAddress;

            if (encryption.UseEncryption)
            {
                EncryptionProvider.SetupSocket(encryption, this.socket);
                this.Logger.LogInformation(LogId.Network, $"{encryption.Algorithm} encryption setup for {this.networkAddress}");
            }
            else
            {
                this.Logger.LogWarning(LogId.Network, $"No encryption setup for {this.networkAddress}");
            }

            this.SentCount = 0;

            this.RegisterHandler <IEnvelope>(this.OnEnvelope);
        }
Example #11
0
        public MessageServerFacade(
            IComponentryContainer container,
            IMessageBusAdapter messagingAdapter,
            ICompressor compressor,
            EncryptionSettings encryption,
            ZmqNetworkAddress requestAddress,
            ZmqNetworkAddress responseAddress)
            : base(
                container,
                messagingAdapter,
                new MsgPackDictionarySerializer(),
                new MsgPackRequestSerializer(),
                new MsgPackResponseSerializer(),
                compressor,
                encryption,
                new Label("test-server"),
                requestAddress,
                responseAddress)
        {
            this.ReceivedMessages = new List <DataRequest>();
            this.ReceivedStrings  = new List <string>();

            this.RegisterHandler <DataRequest>(this.OnMessage);
        }
Example #12
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TickPublisher"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="dataBusAdapter">The data bus adapter.</param>
        /// <param name="quoteSerializer">The quote tick serializer.</param>
        /// <param name="tradeSerializer">The trade tick serializer.</param>
        /// <param name="compressor">The data compressor.</param>
        /// <param name="encryption">The encryption configuration.</param>
        /// <param name="port">The publisher port.</param>
        public TickPublisher(
            IComponentryContainer container,
            IDataBusAdapter dataBusAdapter,
            IDataSerializer <QuoteTick> quoteSerializer,
            IDataSerializer <TradeTick> tradeSerializer,
            ICompressor compressor,
            EncryptionSettings encryption,
            Port port)
            : base(
                container,
                dataBusAdapter,
                compressor,
                encryption,
                ZmqNetworkAddress.AllInterfaces(port))
        {
            this.quoteSerializer = quoteSerializer;
            this.tradeSerializer = tradeSerializer;

            this.RegisterHandler <QuoteTick>(this.OnMessage);
            this.RegisterHandler <TradeTick>(this.OnMessage);

            this.Subscribe <QuoteTick>();
            this.Subscribe <TradeTick>();
        }
Example #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MessageServer"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="messagingAdapter">The message bus adapter.</param>
        /// <param name="headerSerializer">The header serializer.</param>
        /// <param name="requestSerializer">The request serializer.</param>
        /// <param name="responseSerializer">The response serializer.</param>
        /// <param name="compressor">The message compressor.</param>
        /// <param name="encryption">The encryption configuration.</param>
        /// <param name="serviceName">The service name.</param>
        /// <param name="requestAddress">The inbound zmq network address.</param>
        /// <param name="responseAddress">The outbound zmq network address.</param>
        protected MessageServer(
            IComponentryContainer container,
            IMessageBusAdapter messagingAdapter,
            ISerializer <Dictionary <string, string> > headerSerializer,
            IMessageSerializer <Request> requestSerializer,
            IMessageSerializer <Response> responseSerializer,
            ICompressor compressor,
            EncryptionSettings encryption,
            Label serviceName,
            ZmqNetworkAddress requestAddress,
            ZmqNetworkAddress responseAddress)
            : base(container, messagingAdapter)
        {
            this.serverId           = new ServerId(this.Name.Value);
            this.headerSerializer   = headerSerializer;
            this.requestSerializer  = requestSerializer;
            this.responseSerializer = responseSerializer;
            this.compressor         = compressor;
            this.serviceName        = serviceName;

            this.socketInbound = new RouterSocket
            {
                Options =
                {
                    Identity             = Encoding.UTF8.GetBytes($"{this.serviceName.Value}-{this.Name.Value}"),
                    Linger               = TimeSpan.FromSeconds(1),
                    TcpKeepalive         = true,
                    TcpKeepaliveInterval = TimeSpan.FromSeconds(2),
                    RouterHandover       = true,
                },
            };

            this.socketOutbound = new RouterSocket
            {
                Options =
                {
                    Identity             = Encoding.UTF8.GetBytes($"{this.serviceName.Value}-{this.Name.Value}"),
                    Linger               = TimeSpan.FromSeconds(1),
                    TcpKeepalive         = true,
                    TcpKeepaliveInterval = TimeSpan.FromSeconds(2),
                    RouterMandatory      = true,
                    RouterHandover       = true,
                },
            };

            this.requestAddress   = requestAddress;
            this.responseAddress  = responseAddress;
            this.peers            = new Dictionary <ClientId, SessionId>();
            this.correlationIndex = new ConcurrentDictionary <Guid, Address>();

            this.queue = new MessageQueue(
                container,
                this.socketInbound,
                this.socketOutbound,
                this.HandlePayload);

            if (encryption.UseEncryption)
            {
                EncryptionProvider.SetupSocket(encryption, this.socketInbound);
                this.Logger.LogInformation(
                    LogId.Network,
                    $"{encryption.Algorithm} encryption setup for {this.requestAddress}");

                EncryptionProvider.SetupSocket(encryption, this.socketOutbound);
                this.Logger.LogInformation(
                    LogId.Network,
                    $"{encryption.Algorithm} encryption setup for {this.responseAddress}");
            }
            else
            {
                this.Logger.LogWarning(
                    LogId.Network,
                    $"No encryption setup for {this.requestAddress}");

                this.Logger.LogWarning(
                    LogId.Network,
                    $"No encryption setup for {this.responseAddress}");
            }

            this.ReceivedCount = 0;
            this.SentCount     = 0;
        }
Example #14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TestDealer"/> class.
        /// </summary>
        /// <param name="container">The componentry container.</param>
        /// <param name="headerSerializer">The header serializer.</param>
        /// <param name="requestSerializer">The outbound serializer.</param>
        /// <param name="responseSerializer">The inbound serializer.</param>
        /// <param name="compressor">The compressor.</param>
        /// <param name="encryption">The encryption settings.</param>
        /// <param name="serverReqAddress">The server request address to connect to.</param>
        /// <param name="serverResAddress">The server response address to connect to.</param>
        /// <param name="subName">The requesters sub-name.</param>
        public TestDealer(
            IComponentryContainer container,
            ISerializer <Dictionary <string, string> > headerSerializer,
            IMessageSerializer <Request> requestSerializer,
            IMessageSerializer <Response> responseSerializer,
            ICompressor compressor,
            EncryptionSettings encryption,
            ZmqNetworkAddress serverReqAddress,
            ZmqNetworkAddress serverResAddress,
            string subName = "")
            : base(container, subName)
        {
            this.headerSerializer   = headerSerializer;
            this.requestSerializer  = requestSerializer;
            this.responseSerializer = responseSerializer;
            this.compressor         = compressor;

            this.socketOutbound = new DealerSocket
            {
                Options =
                {
                    Identity = Encoding.UTF8.GetBytes(this.Name.Value),
                    Linger   = TimeSpan.FromSeconds(1),
                },
            };

            this.socketInbound = new DealerSocket
            {
                Options =
                {
                    Identity = Encoding.UTF8.GetBytes(this.Name.Value),
                    Linger   = TimeSpan.FromSeconds(1),
                },
            };

            this.ClientId         = new ClientId(this.Name.Value);
            this.ServerReqAddress = serverReqAddress;
            this.ServerResAddress = serverResAddress;

            if (encryption.UseEncryption)
            {
                EncryptionProvider.SetupSocket(encryption, this.socketOutbound);
                this.Logger.LogInformation(
                    LogId.Network,
                    $"{encryption.Algorithm} encryption setup for connections to {this.ServerReqAddress}");

                EncryptionProvider.SetupSocket(encryption, this.socketInbound);
                this.Logger.LogInformation(
                    LogId.Network,
                    $"{encryption.Algorithm} encryption setup for connections to {this.ServerResAddress}");
            }
            else
            {
                this.Logger.LogWarning(
                    LogId.Network,
                    $"No encryption setup for connections to {this.ServerReqAddress}");

                this.Logger.LogWarning(
                    LogId.Network,
                    $"No encryption setup for connections to {this.ServerResAddress}");
            }

            this.ReceivedCount = 0;
            this.SendCount     = 0;
        }