Example #1
0
File: Zyre.cs Project: NetMQ/Zyre
        /// <summary>
        /// Create a Zyre API that communicates with a node on the ZRE bus.
        /// </summary>
        /// <param name="name">The name of the node</param>
        /// <param name="useEvents">Set this to true to disable Receive() and instead subscribe to events for getting messages from peers. Default is true.</param>
        /// <param name="loggerDelegate">An action to take for logging when _verbose is true. Default is null.</param>
        public Zyre (string name, bool useEvents = true, Action<string> loggerDelegate = null)
        {
            _useEvents = useEvents;
            // Create front-to-back pipe pair for data traffic
            // outbox is passed to ZyreNode for sending Zyre message traffic back to _inbox
            PairSocket outbox;
            PairSocket.CreateSocketPair(out outbox, out _inbox);

            // Start node engine and wait for it to be ready
            // All node control is done through _actor

            _actor = ZyreNode.Create(outbox, loggerDelegate);

            if (useEvents)
            {
                _inboxPoller = new NetMQPoller();
                _inbox.ReceiveReady += InboxReceiveReady;
                _inboxPoller.RunAsync();
            }

            // Send name, if any, to node ending
            if (!string.IsNullOrEmpty(name))
            {
                _actor.SendMoreFrame("SET NAME").SendFrame(name);
            }
        }
        /// <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);
        }
        public ReliableServer(NetMQContext context, string address)
        {
            m_context = context;
            m_address = address;

            // actor is like thread with builtin pair sockets connect the user thread with the actor thread
            m_actor = NetMQActor.Create(context, Run);
        }
Example #4
0
        /// <summary>
        /// Create a new NetMQBeacon, contained within the given context.
        /// </summary>
        /// <param name="context">the NetMQContext to contain this new socket</param>
        public NetMQBeacon([NotNull] NetMQContext context)
        {
            m_actor = NetMQActor.Create(context, new Shim());

            m_receiveEventHelper = new EventDelegatorHelper<NetMQBeaconEventArgs>(
                () => m_actor.ReceiveReady += OnReceiveReady,
                () => m_actor.ReceiveReady -= OnReceiveReady);
        }
Example #5
0
        /// <summary>
        /// Create a new NetMQBeacon, contained within the given context.
        /// </summary>
        /// <param name="context">the NetMQContext to contain this new socket</param>
        public NetMQBeacon([NotNull] NetMQContext context)
        {
            m_actor = NetMQActor.Create(context, new Shim());

            EventHandler<NetMQActorEventArgs> onReceive = (sender, e) =>
                m_receiveEvent.Fire(this, new NetMQBeaconEventArgs(this));

            m_receiveEvent = new EventDelegator<NetMQBeaconEventArgs>(
                () => m_actor.ReceiveReady += onReceive,
                () => m_actor.ReceiveReady -= onReceive);
        }
Example #6
0
        /// <summary>
        /// Create a new ZreBeacon, contained within the given context.
        /// </summary>
        /// <param name="context">the NetMQContext to contain this new socket</param>
        public ZreBeacon(NetMQContext context)
        {
            _actor = NetMQActor.Create(context, new Shim());

            EventHandler<NetMQActorEventArgs> onReceive = (sender, e) =>
                                                          _receiveEvent.Fire(this, new ZreBeaconEventArgs(this));

            _receiveEvent = new EventDelegator<ZreBeaconEventArgs>(
                () => _actor.ReceiveReady += onReceive,
                () => _actor.ReceiveReady -= onReceive);
        }
Example #7
0
        private Zre(NetMQContext context, string name)
        {
            _name = name;
            _actor = NetMQActor.Create(context, new ZreNode(context));
            if (!string.IsNullOrEmpty(name))
            {
                Name = name;
            }

            EventHandler<NetMQActorEventArgs> onReceive = (sender, e) =>
                                              _receiveEvent.Fire(this, new ZreEventArgs(this));

            _receiveEvent = new EventDelegator<ZreEventArgs>(
                () => _actor.ReceiveReady += onReceive,
                () => _actor.ReceiveReady -= onReceive);
        }
Example #8
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);
        }
Example #9
0
File: Bus.cs Project: vadian/netmq
 private Bus(int broadcastPort)
 {
     m_nodes = new Dictionary<NodeKey, DateTime>();
     m_broadcastPort = broadcastPort;
     m_actor = NetMQActor.Create(RunActor);
 }
Example #10
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));
 }
Example #11
0
        public Zyre (string name)
        {
            // Create front-to-back pipe pair for data traffic
            // outbox is passed to ZreNode for sending Zyre message traffic back to _inbox
            PairSocket outbox;
            PairSocket.CreateSocketPair(out outbox, out _inbox);

            // Start node engine and wait for it to be ready
            // All node control is done through _actor
            _actor = ZreNode.Create(outbox);

            // Send name, if any, to node ending
            if (!string.IsNullOrEmpty(name))
            {
                _actor.SendMoreFrame("SET NAME").SendFrame(name);
            }
        }
Example #12
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));
 }
Example #13
0
 /// <summary>
 /// Create new wokrer
 /// </summary>
 /// <param name="serializer">Serializer to use to serialize messages</param>
 /// <param name="handler">Handler to handle messages from client</param>
 /// <param name="loadBalancerAddress">Address of load balancer to connect to</param>
 public Worker(ISerializer serializer, IHandler handler, string loadBalancerAddress)
 {
     m_actor = NetMQActor.Create(new WorkerEngine(serializer, handler, loadBalancerAddress));
 }
Example #14
0
        private void StartGossip()
        {
            //  If we haven't already set-up the gossip network, do so
            if (_gossip == null)
            {
                _beaconPort = 0; //  Disable UDP beaconing
                _gossip = NetMQActor.Create(
                    _context,
                    shim =>
                    {
                    });

                if (_verbose)
                {
                    _gossip.SendFrame(Zre.SetVerboseCommand);
                }
            }
        }
Example #15
0
 public LoadBalancer(string frontendAddress, string backendAddress)
 {
     m_actor = NetMQActor.Create(new LoadBalancerEngine(frontendAddress, backendAddress));
 }
Example #16
0
        private ZreNode(PairSocket outbox)
        {
            _inbox = new RouterSocket();

            //  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.
            // NOTE: This RouterHandover option apparently doesn't exist in NetMQ 
            //      so I IGNORE it for now. DaleBrubaker Feb 1 2016

            _outbox = outbox;
            //_beaconPort = ZreDiscoveryPort;
            _interval = TimeSpan.Zero; // Use default
            _uuid = Guid.NewGuid();
            _peers = new Dictionary<Guid, ZrePeer>();
            _peerGroups = new Dictionary<string, ZreGroup>();
            _ownGroups = new Dictionary<string, ZreGroup>();
            _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.ToString().ToUpper().Substring(0, 6);

            _actor = NetMQActor.Create(RunActor);
        }
Example #17
0
 /// <summary>
 /// Create NetMQProactor and start dedicate thread to handle incoming messages.
 /// </summary>
 /// <param name="receiveSocket">Socket to handle messages from</param>
 /// <param name="handler">Handler to handle incoming messages</param>
 public NetMQProactor(NetMQSocket receiveSocket, Action<NetMQSocket, NetMQMessage> handler)
 {
     m_receiveSocket = receiveSocket;
     m_handler = handler;
     m_actor = NetMQActor.Create(Run);
 }
Example #18
0
        /// <summary>
        /// Release any contained resources.
        /// </summary>
        /// <param name="disposing">true if managed resources are to be released</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;

            if (_actor != null)
            {
                _actor.Dispose();
                _actor = null;
            }
            if (_inbox != null)
            {
                _inbox.Dispose();
                _inbox = null;
            }
        }