/// <summary> /// Connects to the specified controller. /// </summary> /// <param name="def">The controller to connect to.</param> /// <returns>True if the connection failed or return a JoinSuccess, false if it returned a JoinFailure.</returns> private bool ConnectToController(NodeDefinition def) { Message message = new Message(def, new JoinAttempt(NodeType.Query, _settings.NodeName, _settings.Port, _settings.ToString()), true) { SendWithoutConfirmation = true }; SendMessage(message); message.BlockUntilDone(); if (message.Success) { if (message.Response.Data is JoinFailure) { Logger.Log("Failed to join controllers: " + ((JoinFailure)message.Response.Data).Reason, LogLevel.Error); AfterStop(); return(false); } // success JoinSuccess successData = (JoinSuccess)message.Response.Data; Connections[def].ConnectionEstablished(def, NodeType.Controller); if (successData.Data["PrimaryController"].ValueAsBoolean) { Logger.Log("Setting the primary controller to " + message.Address.ConnectionName, LogLevel.Info); Primary = message.Address; } SendMessage(new Message(message.Response, new Acknowledgement(), false)); } return(true); }
/// <inheritdoc /> public override void Run() { BeforeStart(); List <NodeDefinition> controllerNodes = NodeDefinition.ParseConnectionString(_connectionString); foreach (var def in controllerNodes) { Message message = new Message(def, new JoinAttempt(NodeType.Api, string.Empty, -1, _connectionString), true) { SendWithoutConfirmation = true }; SendMessage(message); message.BlockUntilDone(); if (message.Success) { if (message.Response.Data is JoinFailure) { _connected = false; AfterStop(); return; } JoinSuccess successData = (JoinSuccess)message.Response.Data; Connections[def].ConnectionEstablished(message.Address, NodeType.Controller); if (successData.Data["PrimaryController"].ValueAsBoolean) { Primary = message.Address; _connected = true; } SendMessage(new Message(message.Response, new Acknowledgement(), false)); } } }
/// <summary> /// Connects to a controller. /// </summary> /// <param name="target">The target controller to try to connect to.</param> /// <returns>A value indicating whether the target was connected to.</returns> private bool ConnectToController(NodeDefinition target) { Message message = new Message(target, new JoinAttempt(_self.Hostname, _self.Port, _settings.ToString(), Equals(Primary, Self)), true) { SendWithoutConfirmation = true }; SendMessage(message); message.BlockUntilDone(); if (message.Success) { if (message.Response.Data is JoinFailure) { Logger.Log("Failed to join other controllers: " + ((JoinFailure)message.Response.Data).Reason, LogLevel.Error); return(false); } // success Logger.Log("Connected to controller " + target.ConnectionName, LogLevel.Info); JoinSuccess success = (JoinSuccess)message.Response.Data; Connections[target].ConnectionEstablished(target, NodeType.Controller); if (success.Data["PrimaryController"].ValueAsBoolean) { Logger.Log("Setting the primary controller to " + target.ConnectionName, LogLevel.Info); Primary = target; } SendMessage(new Message(message.Response, new Acknowledgement(), false)); } else { Logger.Log("Timeout while trying to connect to " + target.ConnectionName, LogLevel.Info); } return(true); }