Example #1
0
 public void Send(ZreMessage msg)
 {
     foreach (var peer in _peers)
     {
         peer.Value.Send(msg.Duplicate());
     }
 }
Example #2
0
        private void OnPipeReady(object sender, NetMQSocketEventArgs e)
        {
            //  Get the whole message off the pipe in one go
            var message = e.Socket.ReceiveMultipartMessage();
            if (message == null)
            {
                return;
            }

            var command = message.Pop().ConvertToString();
            switch (command)
            {
                case Zre.StartCommand:
                if (Start())
                {
                    e.Socket.SignalOK();
                }
                else
                {
                    e.Socket.SignalError();
                }
                break;
                case Zre.StopCommand:
                if (Stop())
                {
                    e.Socket.SignalOK();
                }
                else
                {
                    e.Socket.SignalError();
                }
                break;
                case Zre.SetNameCommand:
                _name = message.Pop().ConvertToString();
                break;
                case Zre.GetNameCommand:
                _pipe.SendFrame(_name);
                break;
                case Zre.SetUuidCommand:
                _identity = new Guid(message.Pop().ToByteArray());
                break;
                case Zre.GetUuidCommand:
                _pipe.SendFrame(_identity.ToString());
                break;
                case Zre.SetIntervalCommand:
                _interval = TimeSpan.FromMilliseconds(message.Pop().ConvertToInt32());
                break;
                case Zre.SetVerboseCommand:
                _verbose = true;
                break;
                case Zre.SetHeaderCommand:
                {
                    var key = message.Pop().ConvertToString();
                    var value = message.Pop().ConvertToString();
                    _headers[key] = value;
                }
                break;
                case Zre.SetPortCommand:
                _beaconPort = message.Pop().ConvertToInt32();
                break;
                case Zre.WhisperCommand:
                {
                    //  Get peer to send message to
                    var identityString = message.Pop().ConvertToString();
                    Guid identity;
                    if (Guid.TryParse(identityString, out identity))
                    {
                        //  Send frame on out to peer's mailbox, drop message
                        //  if peer doesn't exist (may have been destroyed)
                        ZrePeer peer;
                        if (_peers.TryGetValue(identity, out peer))
                        {
                            var msg = new ZreMessage(ZreMessageType.Whisper);
                            msg.Content = message;
                            peer.Send(msg);
                        }
                    }
                }
                break;
                case Zre.ShoutCommand:
                {
                    //  Get group to send message to
                    var groupName = message.Pop().ConvertToString();
                    ZreGroup group;
                    if (_peerGroups.TryGetValue(groupName, out group))
                    {
                        var msg = new ZreMessage(ZreMessageType.Shout);
                        msg.Group = groupName;
                        msg.Content = message;
                        group.Send(msg);
                    }
                }
                break;
                case Zre.JoinCommand:
                {
                    var groupName = message.Pop().ConvertToString();
                    ZreGroup @group;
                    if (!_ownGroups.TryGetValue(groupName, out group))
                    {
                        @group = new ZreGroup(groupName);
                        _ownGroups[groupName] = @group;
                        var joinMsg = new ZreMessage(ZreMessageType.Join);
                        joinMsg.Group = groupName;
                        joinMsg.Status = ++_status;
                        foreach (var zrePeer in _peers)
                        {
                            zrePeer.Value.Send(joinMsg.Duplicate());
                        }

                        if (_verbose)
                        {
                            Trace.WriteLine(string.Format("({0}) JOIN group={1}", _name, groupName));
                        }
                    }
                }
                break;
                case Zre.LeaveCommand:
                {
                    var groupName = message.Pop().ConvertToString();
                    ZreGroup @group;
                    if (_ownGroups.TryGetValue(groupName, out group))
                    {
                        var leaveMsg = new ZreMessage(ZreMessageType.Leave);
                        leaveMsg.Group = groupName;
                        leaveMsg.Status = ++_status;
                        foreach (var zrePeer in _peers)
                        {
                            zrePeer.Value.Send(leaveMsg.Duplicate());
                        }

                        _ownGroups.TryRemove(groupName, out @group);

                        if (_verbose)
                        {
                            Trace.WriteLine(string.Format("({0}) LEAVE group={1}", _name, groupName));
                        }
                    }
                }
                break;
                case Zre.GetPeersCommand:
                {
                    var peerNames = _peers.Keys.ToArray();

                    using (var msgStream = new MemoryStream())
                    using (var writer = new BinaryWriter(msgStream))
                    {
                        foreach (var peerName in peerNames)
                        {
                            writer.PutString(peerName.ToString());
                        }

                        _pipe.SendFrame(msgStream.ToArray());
                    }
                }
                break;
                case Zre.GetPeerGroupsCommand:
                {
                }
                break;
                case Zre.GetOwnGroupsCommand:
                {
                }
                break;
                case Zre.GetPeerEndpointCommand:
                {
                    var identityString = message.Pop().ConvertToString();
                    Guid identity;
                    if (Guid.TryParse(identityString, out identity))
                    {
                        ZrePeer peer;
                        if (_peers.TryGetValue(identity, out peer))
                        {
                            _pipe.SendFrame(peer.Endpoint);
                        }
                    }
                }
                break;
                case Zre.GetPeerHeaderCommand:
                {
                    var identityString = message.Pop().ConvertToString();
                    var key = message.Pop().ConvertToString();
                    Guid identity;
                    if (Guid.TryParse(identityString, out identity))
                    {
                        ZrePeer peer;
                        if (_peers.TryGetValue(identity, out peer))
                        {
                            var header = peer.GetHeader(key, defaultValue: string.Empty);
                            _pipe.SendFrame(header);
                        }
                        else
                        {
                            _pipe.SendFrame(string.Empty);
                        }
                    }
                }
                break;
                case Zre.SetEndPointCommand:
                {
                    StartGossip();
                    var endpoint = message.Pop().ConvertToString();
                    try
                    {
                        _inbox.Bind(endpoint);
                        _endpoint = endpoint;
                        _pipe.SignalOK();
                    }
                    catch (Exception)
                    {
                        _pipe.SignalError();
                    }
                }
                break;
                case Zre.GossipBindCommand:
                {
                    StartGossip();
                    _gossipBind = message.Pop().ConvertToString();
                    _gossip.SendMoreFrame("BIND").SendFrame(_gossipBind);
                }
                break;
                case Zre.GossipConnectCommand:
                {
                    StartGossip();
                    _gossipConnect = message.Pop().ConvertToString();
                    _gossip.SendMoreFrame("CONNECT").SendFrame(_gossipConnect);
                }
                break;
                case Zre.SetInterfaceCommand:
                {
                    _beaconInterfaceName = message.Pop().ConvertToString();
                }
                break;
                case NetMQActor.EndShimMessage:
                _poller.Cancel();
                break;
            }
        }