Beispiel #1
0
        private bool CreateRealTimeRequestSocket()
        {
            realTimeRequestSocket = new DealerSocket(realTimeRequestConnectionString);
            realTimeRequestSocket.Options.Identity = Encoding.UTF8.GetBytes(name);
            // Start off by sending a ping to make sure everything is regular
            byte[] reply = new byte[] { };
            try
            {
                realTimeRequestSocket.SendMoreFrame(string.Empty)
                .SendFrame(BitConverter.GetBytes((byte)DataRequestMessageType.Ping));

                if (realTimeRequestSocket.TryReceiveFrameBytes(TimeSpan.FromSeconds(1), out reply))
                {
                    realTimeRequestSocket.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(50), out reply);
                }
            }
            catch
            {
                Disconnect();
            }

            if (reply?.Length > 0)
            {
                DataRequestMessageType type = (DataRequestMessageType)BitConverter.ToInt16(reply, 0);

                if (type != DataRequestMessageType.Pong)
                {
                    try
                    {
                        realTimeRequestSocket.Disconnect(realTimeRequestConnectionString);
                    }
                    finally
                    {
                        realTimeRequestSocket.Close();
                        realTimeRequestSocket = null;
                    }

                    RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server."));

                    return(true);
                }
            }
            else
            {
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not connect to server."));
            }

            realTimeRequestSocket.ReceiveReady += RealTimeRequestSocketReceiveReady;
            return(false);
        }
        private Task <StateReply> GetStateOfTheWorld()
        {
            var policyResult = _getStateOfTheWorldRetyPolicy.ExecuteAndCapture <StateReply>(() =>
            {
                using (var dealer = new DealerSocket())
                {
                    dealer.Connect(_configuration.StateOfTheWorldEndpoint);

                    var request = new StateRequest(_configuration.Subject);

                    var requestBytes = _eventSerializer.Serializer.Serialize(request);

                    dealer.SendFrame(requestBytes);

                    var hasResponse = dealer.TryReceiveFrameBytes(_configuration.StateCatchupTimeout, out var responseBytes);

                    if (!hasResponse)
                    {
                        throw new UnreachableBrokerException($"Unable to reach broker @{_configuration.StateOfTheWorldEndpoint}");
                    }

                    return(_eventSerializer.Serializer.Deserialize <StateReply>(responseBytes));
                }
            });

            return(Task.FromResult(policyResult.Result));
        }
Beispiel #3
0
        public async Task ShouldHandleStateOfTheWorldRequest()
        {
            using (var publisher = new PublisherSocket())
            {
                publisher.Connect(ToPublishersEndpoint);

                var command1 = new ChangeCcyPairState("EUR/USD", "TEST2", CcyPairState.Passive);
                var command2 = new ChangeCcyPairState("EUR/USD", "TEST2", CcyPairState.Active);

                var message1 = _eventSerializer.ToProducerMessage(command1);
                var message2 = _eventSerializer.ToProducerMessage(command2);

                await Task.Delay(200);

                publisher.SendMoreFrame(message1.Subject)
                .SendFrame(_eventSerializer.Serializer.Serialize(message1));

                await Task.Delay(200);

                publisher.SendMoreFrame(message2.Subject)
                .SendFrame(_eventSerializer.Serializer.Serialize(message2));

                await Task.Delay(200);

                var cacheItems = await _eventCache.GetAllStreams();

                Assert.AreEqual(2, cacheItems.Count());

                using (var dealer = new DealerSocket())
                {
                    var request = new StateRequest()
                    {
                        Subject = "EUR/USD"
                    };

                    var requestBytes = _eventSerializer.Serializer.Serialize(request);

                    dealer.Connect(StateOfTheWorldEndpoint);
                    dealer.SendFrame(requestBytes);

                    var hasResponse = dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(1000), out var responseBytes);

                    var response = _eventSerializer.Serializer.Deserialize <StateReply>(responseBytes);

                    Assert.AreEqual(2, response.Events.Count());
                }
            }
        }
        private IStateReply GetStateOfTheWorld()
        {
            using (var dealer = new DealerSocket())
            {
                var request = new StateRequest()
                {
                    Subject = _configuration.Subject,
                };

                var requestBytes = _eventSerializer.Serializer.Serialize(request);

                dealer.Connect(_configuration.StateOfTheWorldEndpoint);
                dealer.SendFrame(requestBytes);

                var hasResponse = dealer.TryReceiveFrameBytes(_configuration.StateCatchupTimeout, out var responseBytes);

                if (!hasResponse)
                {
                    throw new Exception("unable to reach broker");
                }

                return(_eventSerializer.Serializer.Deserialize <StateReply>(responseBytes));
            }
        }