/// <summary> /// Receive next message from network; the message may be a control. /// message (ENTER, EXIT, JOIN, LEAVE, STOP, EVASIVE) or data (WHISPER, SHOUT). /// </summary> /// <returns>message</returns> public NetMQMessage Receive() { if (_useEvents) { throw new Exception("Disallowed due to the constructor used. Receive via events instead."); } return(_inbox.ReceiveMultipartMessage()); }
private void OnShimReady(object sender, NetMQSocketEventArgs e) { var command = _shim.ReceiveFrameString(); switch (command) { case NetMQActor.EndShimMessage: { _poll.Stop(); break; } case PublishCommand: { _publisher.SendMultipartMessage(_shim.ReceiveMultipartMessage()); break; } case GetHostAddressCommand: { _shim.SendFrame($"{_beacon.BoundTo}:{_port}"); break; } } }
public void Run(PairSocket shim) { shim.SignalOK(); while (true) { try { NetMQMessage msg = shim.ReceiveMultipartMessage(); string command = msg[0].ConvertToString(); if (command == NetMQActor.EndShimMessage) { break; } //Simulate a failure that should be sent back to Actor throw new InvalidOperationException("Actors Shim threw an Exception"); } //You WILL need to decide what Exceptions should be caught here, this is for //demonstration purposes only, any unhandled fault will bubble up to caller's code catch (InvalidOperationException e) { shim.Send(string.Format("Error: Exception occurred {0}", e.Message)); } } }
private void OnShimReady(object sender, NetMQSocketEventArgs e) { // new actor command string command = m_shim.ReceiveFrameString(); // check if we received end shim command if (command == NetMQActor.EndShimMessage) { // we cancel the socket which dispose and exist the shim m_poller.Stop(); } else if (command == PublishCommand) { // it is a publish command // we just forward everything to the publisher until end of message NetMQMessage message = m_shim.ReceiveMultipartMessage(); m_publisher.SendMultipartMessage(message); } else if (command == GetHostAddressCommand) { var interfaceCollection = new InterfaceCollection(); var bindTo = interfaceCollection.Select(x => x.Address).First(); var address = bindTo + ":" + m_randomPort; m_shim.SendFrame(address); } }
public void Run(PairSocket shim) { shim.SignalOK(); while (true) { try { //Message for this actor/shim handler is expected to be //Frame[0] : Command //Frame[2] : AccountAction as JSON //Frame[1] : Account as JSON // //Result back to actor is a JSON message of the amended Account NetMQMessage msg = shim.ReceiveMultipartMessage(); string command = msg[0].ConvertToString(); if (command == NetMQActor.EndShimMessage) { break; } if (msg[0].ConvertToString() == "AMEND ACCOUNT") { string accountActionJson = msg[1].ConvertToString(); var accountAction = JsonConvert.DeserializeObject <AccountAction>(accountActionJson); string accountJson = msg[2].ConvertToString(); var account = JsonConvert.DeserializeObject <Account>(accountJson); if (account != null && accountAction != null) { AmendAccount(accountAction, account); shim.SendFrame(JsonConvert.SerializeObject(account)); } else { shim.SendFrame("Error: incorrectly formatted message to actor"); } } else { shim.SendFrame("Error: invalid message to actor"); } } // You WILL need to decide what Exceptions should be caught here, this is for // demonstration purposes only, any unhandled fault will bubble up to caller's code. catch (Exception e) { shim.SendFrame($"Error: Exception occurred {e.Message}"); } } }
public void Run(PairSocket shim) { shim.SignalOK(); while (true) { try { //Message for this actor/shim handler is expected to be //Frame[0] : Command //Frame[1] : Payload // //Result back to actor is a simple echoing of the Payload, where //the payload is prefixed with "ECHO BACK " NetMQMessage msg = shim.ReceiveMultipartMessage(); string command = msg[0].ConvertToString(); if (command == NetMQActor.EndShimMessage) { break; } if (command == "ECHO") { shim.Send(string.Format("ECHO BACK : {0}", msg[1].ConvertToString())); } else { shim.Send("Error: invalid message to actor"); } } // You WILL need to decide what Exceptions should be caught here, this is for // demonstration purposes only, any unhandled fault will bubble up to caller's code catch (Exception e) { shim.Send(string.Format("Error: Exception occurred {0}", e.Message)); } } }
private void OnShimReady(object sender, NetMQSocketEventArgs e) { // new actor command var command = shim.ReceiveFrameString(); // check if we received end shim command if (command == NetMQActor.EndShimMessage) { // we cancel the socket which dispose and exist the shim poller.Stop(); } else if (command == PublishCommand) { // it is a publish command // we just forward everything to the publisher until end of message var message = shim.ReceiveMultipartMessage(); publisher.SendMultipartMessage(message); } else if (command == GetHostAddressCommand) { var address = beacon.BoundTo + ":" + randomPort; shim.SendFrame(address); } }
private void OnShimReady(object sender, NetMQSocketEventArgs e) { // new actor command var command = _shim.ReceiveFrameString(); // check if we received end shim command if (command == NetMQActor.EndShimMessage) { // we cancel the socket which dispose and exist the shim _poller.Stop(); } else if (command == TaskSchedulerBusCommands.Publish.ToString()) { // it is a publish command // we just forward everything to the publisher until end of message var message = _shim.ReceiveMultipartMessage(); _publisher.SendMultipartMessage(message); } else if (command == TaskSchedulerBusCommands.GetHostAddress.ToString()) { var address = _beacon.HostName + ":" + _randomPort; _shim.SendFrame(address); } }
private void OnMessagePipeReady(object sender, NetMQSocketEventArgs e) { NetMQMessage request = m_messagesPipe.ReceiveMultipartMessage(); OnOutgoingMessage(request); }
/// <summary> /// Here we handle the different control messages from the front-end /// </summary> private void ReceiveApi() { // Get the whole message off the pipe in one go var request = _pipe.ReceiveMultipartMessage(); var command = request.Pop().ConvertToString(); switch (command) { case "UUID": _pipe.SendFrame(_uuid.ToByteArray()); break; case "NAME": _pipe.SendFrame(_name); break; case "ENDPOINT": _pipe.SendFrame(_endpoint ?? ""); break; case "SET NAME": _name = request.Pop().ConvertToString(); Debug.Assert(!String.IsNullOrEmpty(_name)); break; case "SET HEADER": var key = request.Pop().ConvertToString(); var value = request.Pop().ConvertToString(); _headers[key] = value; break; case "SET PORT": var str = request.Pop().ConvertToString(); Int32.TryParse(str, out _beaconPort); break; case "SET INTERVAL": var intervalStr = request.Pop().ConvertToString(); TimeSpan.TryParse(intervalStr, out _interval); break; case "START": var interfaceName = request.Pop().ConvertToString(); Start(interfaceName); break; case "STOP": Stop(); break; case "WHISPER": // Get peer to send message to var uuid = PopGuid(request); ZyrePeer peer; if (_peers.TryGetValue(uuid, out peer)) { // Send frame on out to peer's mailbox, drop message // if peer doesn't exist (may have been destroyed) var msg = new ZreMsg { Id = ZreMsg.MessageId.Whisper, Whisper = { Content = request } }; peer.Send(msg); } break; case "SHOUT": // Get group to send message to var groupNameShout = request.Pop().ConvertToString(); ZyreGroup group; if (_peerGroups.TryGetValue(groupNameShout, out group)) { var msg = new ZreMsg { Id = ZreMsg.MessageId.Shout, Shout = { Group = groupNameShout, Content = request } }; group.Send(msg); } break; case "JOIN": var groupNameJoin = request.Pop().ConvertToString(); ZyreGroup groupJoin; if (!_ownGroups.TryGetValue(groupNameJoin, out groupJoin)) { // Only send if we're not already in group ZyreGroup.NewGroup(groupNameJoin, _ownGroups); // Update status before sending command _status = _status == UbyteMax ? (byte)0 : ++_status; var msg = new ZreMsg { Id = ZreMsg.MessageId.Join, Join = { Group = groupNameJoin, Status = _status } }; foreach (var peerJoin in _peers.Values) { peerJoin.Send(msg); } } break; case "LEAVE": var groupNameLeave = request.Pop().ConvertToString(); ZyreGroup groupLeave; if (_ownGroups.TryGetValue(groupNameLeave, out groupLeave)) { // Only send if we are actually in group // Update status before sending command _status = _status == UbyteMax ? (byte)0 : ++_status; var msg = new ZreMsg { Id = ZreMsg.MessageId.Leave, Leave = { Group = groupNameLeave, Status = _status } }; foreach (var peerLeave in _peers.Values) { peerLeave.Send(msg); } _ownGroups.Remove(groupNameLeave); } break; case "PEERS": // Send the list of the _peers keys var peersKeyBuffer = Serialization.BinarySerialize(_peers.Keys.ToList()); _pipe.SendFrame(peersKeyBuffer); break; case "PEER ENDPOINT": var uuidForEndpoint = PopGuid(request); var peerForEndpoint = _peers[uuidForEndpoint]; // throw exception if not found _pipe.SendFrame(peerForEndpoint.Endpoint); break; case "PEER NAME": var uuidForName = PopGuid(request); var peerForName = _peers[uuidForName]; // throw exception if not found _pipe.SendFrame(peerForName.Name); break; case "PEER HEADER": var uuidForHeader = PopGuid(request); var keyForHeader = request.Pop().ConvertToString(); ZyrePeer peerForHeader; if (_peers.TryGetValue(uuidForHeader, out peerForHeader)) { string header; if (peerForHeader.Headers.TryGetValue(keyForHeader, out header)) { _pipe.SendFrame(header); } else { _loggerDelegate?.Invoke($"No header with key {keyForHeader} in peer uuidShort={uuidForHeader.ToShortString6()}"); _pipe.SendFrame(""); } } else { _loggerDelegate?.Invoke($"PEER HEADER requested for peer uuidShort={uuidForHeader.ToShortString6()} that is not a peer"); _pipe.SendFrame(""); } break; case "PEER GROUPS": // Send a list of the _peerGroups keys, comma-delimited var peerGroupsKeyBuffer = Serialization.BinarySerialize(_peerGroups.Keys.ToList()); _pipe.SendFrame(peerGroupsKeyBuffer); break; case "OWN GROUPS": // Send a list of the _ownGroups keys, comma-delimited var ownGroupsKeyBuffer = Serialization.BinarySerialize(_ownGroups.Keys.ToList()); _pipe.SendFrame(ownGroupsKeyBuffer); break; case "DUMP": Dump(); break; case NetMQActor.EndShimMessage: // API shut us down _poller?.Stop(); break; default: throw new ArgumentException(command); } }