Ejemplo n.º 1
0
        /// <summary>
        /// Create reliable client
        /// </summary>
        /// <param name="context"></param>
        /// <param name="addresses">addresses of the reliable servers</param>
        public ReliableClient(NetMQContext context, params string[] addresses)
        {
            m_context   = context;
            m_addresses = addresses;

            m_actor = NetMQActor.Create(context, Run);
        }
Ejemplo n.º 2
0
        public void Simple()
        {
            using (var context = NetMQContext.Create())
            {
                using (NetMQActor actor = NetMQActor.Create(context, shim =>
                {
                    shim.SignalOK();

                    while (true)
                    {
                        NetMQMessage msg = shim.ReceiveMessage();

                        string command = msg[0].ConvertToString();

                        if (command == NetMQActor.EndShimMessage)
                        {
                            break;
                        }

                        else if (command == "Hello")
                        {
                            shim.Send("World");
                        }
                    }
                }))
                {
                    actor.SendMore("Hello");
                    actor.Send("Hello");
                    var result = actor.ReceiveString();
                    Assert.AreEqual("World", result);
                }
            }
        }
Ejemplo n.º 3
0
        public ReliableServer(string address)
        {
            m_address = address;

            // actor is like thread with builtin pair sockets connect the user thread with the actor thread
            m_actor = NetMQActor.Create(Run);
        }
Ejemplo n.º 4
0
        public void Simple()
        {
            void ShimAction(PairSocket shim)
            {
                shim.SignalOK();

                while (true)
                {
                    var msg     = shim.ReceiveMultipartMessage();
                    var command = msg[0].ConvertToString();

                    if (command == NetMQActor.EndShimMessage)
                    {
                        break;
                    }

                    if (command == "Hello")
                    {
                        Assert.AreEqual(2, msg.FrameCount);
                        Assert.AreEqual("Hello", msg[1].ConvertToString());
                        shim.SendFrame("World");
                    }
                }
            }

            using (var actor = NetMQActor.Create(ShimAction))
            {
                actor.SendMoreFrame("Hello").SendFrame("Hello");

                Assert.AreEqual("World", actor.ReceiveFrameString());
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        private void ServerLoop()
        {
            if (Thread.CurrentThread.Name == null)
            {
                Thread.CurrentThread.Name = "Nyx Hub Server Loop";
            }

            _disposables.Add(NyxMessageStream.Subscribe(RepSocketOnReceiveReady, x => _logger.Error("Error processing stream.", x)));
            _hubActor?.Dispose();
            _hubActor = NetMQActor.Create(new HubShimHandler(_inMessage, Port));

            _logger.Debug("Starting hub server loop...");
            _isStarted = true;
            _plugman.StartAllPlugins();
            _nodeStatusSubject.OnNext(NyxNodeStatus.Started);
            // Endless loop until request to stop, then shutdowns
            while (!_serverCancelToken.IsCancellationRequested)
            {
                _serverEvent.WaitOne(TimeSpan.FromSeconds(60));
            }
            _plugman.StopAllPlugins();
            Status = NyxNodeStatus.Stopped;
            _nodeStatusSubject.OnNext(Status);
            try
            {
                _hubActor?.SendFrame(NetMQActor.EndShimMessage);
                _hubActor?.Dispose();
            }
            catch (TerminatingException)
            {
                // Ignore and go
            }
            _isStarted = false;
        }
Ejemplo n.º 6
0
        public void Simple()
        {
            using (var context = NetMQContext.Create())
            {
                ShimAction shimAction = shim =>
                {
                    shim.SignalOK();

                    while (true)
                    {
                        NetMQMessage msg = shim.ReceiveMultipartMessage();

                        string command = msg[0].ConvertToString();

                        if (command == NetMQActor.EndShimMessage)
                        {
                            break;
                        }

                        if (command == "Hello")
                        {
                            shim.SendFrame("World");
                        }
                    }
                };

                using (var actor = NetMQActor.Create(context, shimAction))
                {
                    actor.SendMoreFrame("Hello").SendFrame("Hello");

                    Assert.AreEqual("World", actor.ReceiveFrameString());
                }
            }
        }
Ejemplo n.º 7
0
 public void Start()
 {
     if (_actor != null)
     {
         return;
     }
     _actor = NetMQActor.Create(new ShimHandler());
 }
Ejemplo n.º 8
0
        public ReliableServer(TimeSpan heartbeatInterval, string address)
        {
            _address           = address;
            _heartbeatInterval = heartbeatInterval;

            // actor is like thread with builtin pair sockets connect the user thread with the actor thread
            _actor = NetMQActor.Create(Run);
        }
Ejemplo n.º 9
0
 public void Start()
 {
     if (actor != null)
     {
         return;
     }
     actor = NetMQActor.Create(new ShimHandler(Address, (topic, value) => RaiseSubscriberMessageReceived(topic, value)));
 }
Ejemplo n.º 10
0
 public PeerBus(IHubContext <NotificationHub> hub, IOptions <WebServerOptions> options, ILogger <PeerBus> logger)
 {
     _id         = $"[{options.Value.ServerId}]";
     _info       = new Dictionary <PeerInfo, DateTimeOffset>();
     _beaconPort = options.Value.BeaconPort;
     _hub        = hub;
     _logger     = logger;
     _actor      = NetMQActor.Create(RunActor);
 }
Ejemplo n.º 11
0
        public ReliableServer(TimeSpan heartbeatInterval, string address, int receiveTimeout = 0, Action onReceiveTimeout = null)
        {
            _address           = address;
            _receiveTimeout    = receiveTimeout;
            _onReceiveTimeout  = onReceiveTimeout;
            _heartbeatInterval = heartbeatInterval;

            // actor is like thread with builtin pair sockets connect the user thread with the actor thread
            _actor = NetMQActor.Create(Run);
        }
Ejemplo n.º 12
0
 /// <summary>
 ///     Init or reinit all communications.
 /// </summary>
 public void Init()
 {
     if (Interlocked.CompareExchange(ref _requiresInitialisation, 0, 1) == 0)
     {
         return;
     }
     _actor?.SendFrame(NetMQActor.EndShimMessage);
     _actor?.Dispose();
     _actor = NetMQActor.Create(new ShimHandler(this, _connectionSubject, _address, _port));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Create reliable client
 /// </summary>
 /// <param name="reconnectInterval"></param>
 /// <param name="subscriberMessageHandler"></param>
 /// <param name="subscriberErrorHandler"></param>
 /// <param name="addresses">addresses of the reliable servers</param>
 /// <param name="heartbeatTimeOut"></param>
 public ReliableClient(IEnumerable <string> addresses, TimeSpan heartbeatTimeOut, TimeSpan reconnectInterval, Action <NetMQMessage> subscriberMessageHandler = null, Action <Exception, NetMQMessage> subscriberErrorHandler = null, Action welcomeMessageHandler = null)
 {
     _heartbeatTimeOut         = heartbeatTimeOut;
     _reconnectInterval        = reconnectInterval;
     _addresses                = addresses;
     _subscriberMessageHandler = subscriberMessageHandler;
     _subscriberErrorHandler   = subscriberErrorHandler;
     _welcomeMessageHandler    = welcomeMessageHandler;
     _actor = NetMQActor.Create(Run);
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="address"></param>
        /// <param name="shimCreator"></param>
        protected WsSocket(string address, Func <int, string, BaseShimHandler> shimCreator)
        {
            Id = Interlocked.Increment(ref _id);

            _messagesPipe = new PairSocket();
            _messagesPipe.Bind($"inproc://wsrouter-{Id}");
            _messagesPipe.ReceiveReady += OnMessagePipeReceiveReady;
            _actor = NetMQActor.Create(ShimHandler = shimCreator(Id, address));

            _messagesPipe.ReceiveSignal();
        }
Ejemplo n.º 15
0
        public SnapshotServer(string address, ISnapshotGenerator snapshotGenerator)
        {
            if (snapshotGenerator == null)
            {
                throw new ArgumentNullException(nameof(snapshotGenerator));
            }

            _address           = address;
            _snapshotGenerator = snapshotGenerator;
            _actor             = NetMQActor.Create(Run);
        }
Ejemplo n.º 16
0
        public void BadCommandTests(string command)
        {
            const string actorMessage = "whatever";

            using (var actor = NetMQActor.Create(new EchoShimHandler()))
            {
                actor.SendMoreFrame(command);
                actor.SendFrame(actorMessage);

                Assert.AreEqual("Error: invalid message to actor", actor.ReceiveFrameString());
            }
        }
        public void ShimExceptionTest()
        {
            using (var actor = NetMQActor.Create(new ExceptionShimHandler()))
            {
                actor.SendMoreFrame("SOME_COMMAND");
                actor.SendFrame("Whatever");

                Assert.AreEqual(
                    "Error: Exception occurred Actors Shim threw an Exception",
                    actor.ReceiveFrameString());
            }
        }
Ejemplo n.º 18
0
        public void EchoActorSendReceiveTests(string actorMessage)
        {
            using (var actor = NetMQActor.Create(new EchoShimHandler()))
            {
                actor.SendMoreFrame("ECHO");
                actor.SendFrame(actorMessage);

                Assert.AreEqual(
                    $"ECHO BACK : {actorMessage}",
                    actor.ReceiveFrameString());
            }
        }
Ejemplo n.º 19
0
        protected WSSocket(Func <int, IShimHandler> shimCreator)
        {
            int id = Interlocked.Increment(ref s_id);

            m_messagesPipe = new PairSocket();
            m_messagesPipe.Bind(string.Format("inproc://wsrouter-{0}", id));

            m_messagesPipe.ReceiveReady += OnMessagePipeReceiveReady;

            m_actor = NetMQActor.Create(shimCreator(id));

            m_messagesPipe.ReceiveSignal();
        }
Ejemplo n.º 20
0
        public void EchoActorSendReceiveTests(string actorMessage)
        {
            using (var context = NetMQContext.Create())
                using (var actor = NetMQActor.Create(context, new EchoShimHandler()))
                {
                    actor.SendMore("ECHO");
                    actor.Send(actorMessage);

                    Assert.AreEqual(
                        string.Format("ECHO BACK : {0}", actorMessage),
                        actor.ReceiveFrameString());
                }
        }
Ejemplo n.º 21
0
        private NetMQActor CreateActor()
        {
            return(NetMQActor.Create(shim =>
            {
                Shim = shim;
                Shim.ReceiveReady += ShimOnReceiveReady;
                Shim.SignalOK();

                Poller.Add(Shim);
                Poller.Add(ListeningSocket);
                Poller.Run();

                Log("Node closed");
            }));
        }
Ejemplo n.º 22
0
        private NetMQActor CreateActor()
        {
            return(NetMQActor.Create(shim =>
            {
                Shim = shim;
                Shim.ReceiveReady += ShimOnReceiveReady;

                Poller.Add(ListeningSocket);
                Poller.Add(InitTimer);
                Poller.Add(Shim);

                Shim.SignalOK();
                Poller.Run();
            }));
        }
Ejemplo n.º 23
0
        public void AccountActorJsonSendReceiveTests()
        {
            var account       = new Account(1, "Test Account", "11223", 0);
            var accountAction = new AccountAction(TransactionType.Credit, 10);

            using (var actor = NetMQActor.Create(new AccountShimHandler()))
            {
                actor.SendMoreFrame("AMEND ACCOUNT");
                actor.SendMoreFrame(JsonConvert.SerializeObject(accountAction));
                actor.SendFrame(JsonConvert.SerializeObject(account));

                var updatedAccount = JsonConvert.DeserializeObject <Account>(actor.ReceiveFrameString());

                Assert.Equal(10.0m, updatedAccount?.Balance ?? 0);
            }
        }
Ejemplo n.º 24
0
        private ZyreNode(PairSocket outbox, Action <string> loggerDelegate = null)
        {
            _outbox         = outbox;
            _loggerDelegate = loggerDelegate;

            _beaconPort = ZreDiscoveryPort;
            _interval   = TimeSpan.Zero; // Use default
            _uuid       = Guid.NewGuid();
            _peers      = new Dictionary <Guid, ZyrePeer>();
            _peerGroups = new Dictionary <string, ZyreGroup>();
            _ownGroups  = new Dictionary <string, ZyreGroup>();
            _headers    = new Dictionary <string, string>();

            //  Default name for node is first 6 characters of UUID:
            //  the shorter string is more readable in logs
            _name = _uuid.ToShortString6();

            //  Use ZMQ_ROUTER_HANDOVER so that when a peer disconnects and
            //  then reconnects, the new client connection is treated as the
            //  canonical one, and any old trailing commands are discarded.
            //  This RouterHandover option is currently not supported in NetMQ Feb 16 2016

            _actor = NetMQActor.Create(RunActor);
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Create new server
 /// </summary>
 /// <param name="serializer">Serializer to use to serialize messages</param>
 /// <param name="asyncHandler">Handler to handle messages from client</param>
 public AsyncServer(ISerializer serializer, IAsyncHandler asyncHandler)
 {
     m_actor = NetMQActor.Create(new AsyncServerEngine(serializer, asyncHandler));
 }
Ejemplo n.º 26
0
 private Bus(int broadcastPort)
 {
     m_nodes         = new Dictionary <NodeKey, DateTime>();
     m_broadcastPort = broadcastPort;
     m_actor         = NetMQActor.Create(RunActor);
 }
Ejemplo n.º 27
0
 public ReliableServer(string endpoint)
 {
     _address = endpoint;
     _actor   = NetMQActor.Create(Run);
 }
Ejemplo n.º 28
0
 public void Init() =>
 m_actor = NetMQActor.Create(Engine);
Ejemplo n.º 29
0
 /// <summary>
 /// Create new client
 /// </summary>
 /// <param name="serializer">Serialize to to use to serialize the message to byte array</param>
 /// <param name="address">Address of the server</param>
 public Client(ISerializer serializer, string address)
 {
     m_outgoingQueue = new NetMQQueue <ClientEngine.OutgoingMessage>();
     m_actor         = NetMQActor.Create(new ClientEngine(serializer, m_outgoingQueue, address));
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Create reliable client
        /// </summary>
        /// <param name="context"></param>
        /// <param name="addresses">addresses of the reliable servers</param>
        public ReliableClient(params string[] addresses)
        {
            m_addresses = addresses;

            m_actor = NetMQActor.Create(Run);
        }