Example #1
0
        public void Setup()
        {
            topicRouter        = mockTopicRouter.Object;
            monitorCache       = mockMonitorCache.Object;
            subscriptionsCache = mockSubscriptionsCache.Object;
            subscriberFactory  = mockSubscriberFactory.Object;
            subscriber         = mockSubscriber.Object;
            monitor            = mockMonitor.Object;
            address            = TcpAddress.Localhost(5555);
            routing            = SubscriberRouting.For <ISubscriber>(address);

            mockTopicRouter
            .Setup(m => m.RoutingFor <Topic>(out routing))
            .Returns(true);

            mockSubscriberFactory
            .Setup(m => m.CreateSubscriber(It.IsAny <IAddress>()))
            .Returns(subscriber);

            mockSubscriberFactory
            .SetupGet(m => m.SubscriberType)
            .Returns(typeof(ISubscriber));

            mockSubscriberFactory
            .SetupGet(m => m.SubscriberMonitor)
            .Returns(monitor);
        }
Example #2
0
        public Subscriber()
        {
            router = UnityBuilder.Named("Subscriber")
                     .WithTransport <NetMQTransport>(t => t.WithSubscriber(TcpAddress.Localhost(5556)))
                     .Build();

            throw new NotImplementedException();
            //router.Subscribe<Observation>(AsyncHandler);
        }
Example #3
0
        public void CreateNewSubscriber_ReturnsSubscriber()
        {
            // Arrange
            var factory = new NetMQFactory(requestDispatcher, topicDispatcher, monitor, messageFactory);

            // Act
            var subscriber = factory.CreateSubscriber(TcpAddress.Localhost(5555));

            // Assert
            Assert.That(subscriber, Is.Not.Null);
        }
Example #4
0
        public async Task SenderReceiver_WhenBoundAndConnected_PassesMessage()
        {
            // Arrange
            var    responseStr     = "Hello, World!";
            var    requestStr      = "Hello";
            var    receivedRequest = string.Empty;
            ushort port            = 6555;
            var    called          = false;

            mockRequestDispatcher
            .Setup(m => m.Handle(ref It.Ref <RequestTask> .IsAny))
            .Callback(new Handler((ref RequestTask task) => {
                called          = true;
                receivedRequest = (string)task.Request;
                task.ResponseSender(responseStr);
            }));

            var packageFactory  = new PackageFactory();
            var serializer      = new DotNetSerializer();
            var serializerCache = new SerializerCache();
            var messageFactory  = new NetMQMessageFactory(serializerCache, packageFactory);
            var sender          = new NetMQSender(new DealerSocket(), messageFactory);
            var receiver        = new NetMQReceiver(new RouterSocket(), messageFactory, requestDispatcher);
            var poller          = new NetMQPoller();

            serializerCache.AddSerializer(serializer);

            sender.AddAddress(TcpAddress.Localhost(port));
            sender.InitializeConnection();

            receiver.AddAddress(TcpAddress.Wildcard(port));
            receiver.InitializeConnection();

            poller.Add(sender.PollableSocket);
            poller.Add(receiver.PollableSocket);
            poller.RunAsync();

            var package = new DataPackage <string>(new GuidPackageId(), requestStr);

            // Act
            var response = await sender.SendAndReceive(package, TimeSpan.FromSeconds(5));

            // Cleanup
            poller.StopAsync();
            sender.TerminateConnection();
            receiver.TerminateConnection();

            // Assert
            Assert.That(called, Is.True);
            Assert.That(receivedRequest, Is.EqualTo(requestStr));
            Assert.That((string)response, Is.EqualTo(responseStr));
        }
Example #5
0
        static void Main(string[] args)
        {
            var container = new UnityContainer();

            container.RegisterSingleton <UserMessageService>();

            var router =
                UnityBuilder
                .FromContainer(container)
                .WithName("ExampleServer")
                .WithSerializer <DotNetSerializer>()
                .WithSerializer <JsonSerializer>(true, serializer => serializer.Settings.SerializationBinder = new NetCoreSerializationBinder())
                .WithTransport <NetMQTransport>(t =>
            {
                t.WithReceiver(TcpAddress.Localhost(5555));
                t.WithPublisher(TcpAddress.Localhost(5556));
            })
                .WithHandlers(config =>
                              config
                              .WithRequestHandler <UserConnecting, Response <User>, UserMessageService>()
                              .WithRequestHandler <UserDisconecting, Response <User>, UserMessageService>()
                              .WithRequestHandler <ConnectedUsers, ConnectedUsersList, UserMessageService>()
                              .WithRequestHandler <PostMessage, Response <Message>, UserMessageService>())
                .BuildAndStart();

            Console.Title = "Chat Server";
            Console.WriteLine("Press enter to stop server");

            var server  = container.Resolve <UserMessageService>();
            var running = true;

            while (running)
            {
                var input = Console.ReadLine();

                switch (input)
                {
                case "reset":
                    server.Reset();
                    break;

                case "exit":
                    running = false;
                    break;
                }
            }

            router.Stop();
        }
Example #6
0
        public static void Main(string[] args)
        {
            var container = new UnityContainer();

            container
            .RegisterSingleton <UnityContainer>()
            .RegisterSingleton <App>()
            .RegisterSingleton <MessagingService>();

            var router =
                UnityBuilder
                .FromContainer(container)
                .WithName("ExampleClient")
                .WithSerializer <DotNetSerializer>()
                .WithSerializer <JsonSerializer>(true)
                .WithTransport <NetMQTransport>(t => {
                t.WithSender(TcpAddress.Localhost(5555))
                .For <UserConnecting>()
                .For <UserDisconecting>()
                .For <ConnectedUsers>()
                .For <PostMessage>();

                t.WithSubscriber(TcpAddress.Localhost(5556))
                .Handles <UserConnected>()
                .Handles <UserDisconnected>()
                .Handles <PostedMessage>();
            })
                .WithHandlers(c => {
                c.WithTopicHandler <UserConnected, MessagingService>()
                .WithTopicHandler <UserDisconnected, MessagingService>()
                .WithTopicHandler <PostedMessage, MessagingService>();
            })
                .BuildAndStart();

            using (container.Resolve <MessagingService>())
            {
                var app = container.Resolve <App>();
                app.Container = container;
                app.InitializeComponent();
                app.Run();
            }

            router.Stop();
        }