/// <summary> /// Return our node UUID string, after successful initialization /// </summary> /// <returns>our node UUID string</returns> public Guid Uuid() { _actor.SendFrame("UUID"); var uuidBytes = _actor.ReceiveFrameBytes(); Debug.Assert(uuidBytes.Length == 16); _uuid = new Guid(uuidBytes); return(_uuid); }
/// <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; }
/// <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)); }
/// <summary> /// Starts this instance. /// </summary> public void Start() { lock (_lockSocket) { _actor = _bus.Start(); } _currentTaskCount = 0; _running = true; try { lock (_lockSocket) { _actor.SendFrame(TaskSchedulerBusCommands.GetHostAddress.ToString()); } _hostAddress = _actor.ReceiveFrameString(); _hostPort = new Uri("http://" + _hostAddress).Port; //second beacon time, so we wait to ensure beacon has fired Thread.Sleep(1100); //let other nodes know we are here lock (_lockSocket) { _actor.SendMoreFrame(TaskSchedulerBusCommands.Publish.ToString()) .SendMoreFrame(TaskSchedulerBusCommands.BroadCast.ToString()) .SendFrame(_hostAddress); } // receive messages from other nodes on the bus while (!_stopRequested) { try { ProcessMessages(); } catch (Exception error) { _log.ErrorException("Failed to handle NetMCQ commands", error); } } } catch (Exception error) { _log.ErrorException("A fatal error occurred while processing NetMCQ commands", error); } finally { _running = false; } }
private void OnReceiveTimeout(object sender, NetMQTimerEventArgs e) { _actor.SendFrame(ReceiveTimeoutCommand); }
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); } } }
/*private void DropPublisherSubscriptions(object sender, NetMQSocketEventArgs e) * { * // just drop the subscription messages, we have to do that to Welcome message to work * _publisherSocket.SkipMultipartMessage(); * }*/ private void OnHeartbeatTimerElapsed(object sender, NetMQTimerEventArgs e) { _actor.SendFrame(HeartbeatMessage); }
public void Connected() { _actor.SendFrame("connected"); }
/// <summary> /// Default connect /// </summary> /// <param name="ipAddress"></param> /// <param name="port"></param> /// <returns></returns> public bool Connect(string ipAddress = "", int port = 4015) { if (!IsStarted) { _logger.Error("Connection failed. Server not running."); if (_autostart) { _logger.Info("AutoStart is on. Server starting."); if (!Start().Select(x => true).Amb(Observable.Return(false).Delay(TimeSpan.FromSeconds(5))).Wait()) { return(false); } } else { return(false); } } try { if (_isConnected && _lastHubIp == ipAddress) { _logger.Trace("Connection to {0} is active, not reconnecting.", ipAddress); return(true); } if (string.IsNullOrWhiteSpace(ipAddress)) { ipAddress = "127.0.0.1"; } Heartbeat.Instance.Disconnected(); _connectionDisposable?.Dispose(); // Stop previous actor is any. try { _actor?.SendFrame(NetMQActor.EndShimMessage); //_actor?.Dispose(); } catch (Exception ex) { _logger.Error("NetMQ error.", ex); } _actor = null; // Store port and hub address _port = port; _lastHubIp = ipAddress; Heartbeat.Instance.Setup(_lastHubIp, _port); // Create a new actor to handle the comunications from hub. _actor = NetMQActor.Create(BorgShimHandler.Create(_nyxMessageSubject, ipAddress, port)); Heartbeat.Instance.Connected(); CreateServerSocket(); _config.Set("borg", "hubIp", _lastHubIp); // Global channel AddToGroup("global"); // Custom channels SubscribersChannels.ForEach(c => _actor.SendMoreFrame("subscribe").SendFrame(c)); _actor.SendMoreFrame("subscribe").SendFrame(NodeId); _isConnected = true; _connectionDisposable = Disposable.Create(() => _isConnected = false); NyxMessage.Create(BasicHubAction.NyxId, BasicHubAction.Register, NodeId).Set("nodeID", NodeId).SendMessage(this); } catch (Exception ex) { _logger.Error("Error connecting...", ex); return(false); } return(true); }