コード例 #1
0
ファイル: NyxMessageExtensions.cs プロジェクト: ycdivfx/nyx
 /// <summary>
 ///     Sends a dummy message to the hub.
 /// </summary>
 /// <param name="sender">The borg to send from</param>
 /// <returns></returns>
 public static IObservable <MessageStatus> SendDummyMessage(this INyxBorg sender)
 {
     return(NyxMessage.Create("nyx", "dummy", sender.NodeId).SendMessage(sender, true));
 }
コード例 #2
0
ファイル: NyxMessageExtensions.cs プロジェクト: ycdivfx/nyx
 /// <summary>
 ///     Sends a simple message to the target.
 /// </summary>
 /// <param name="sender">The borg that sends.</param>
 /// <param name="target">The target of the message.</param>
 /// <param name="action">The message action.</param>
 /// <param name="skip">Skips the message if hub offline</param>
 /// <param name="timeout">Timeouts the message.</param>
 /// <returns></returns>
 public static IObservable <MessageStatus> SendMessage(this INyxBorg sender, string target, string action, bool skip = false, double?timeout = null)
 {
     return(NyxMessage.Create(target, action, sender.NodeId).SendMessage(sender, skip, timeout));
 }
コード例 #3
0
ファイル: NyxBorg.cs プロジェクト: ycdivfx/nyx
        /// <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);
        }