public bool GetRemotePeerStatus(IPAddress ip, int port) { if (ip == null) { throw new ArgumentNullException("ip"); } return(GetRemotePeerStatus(PeerNode.Create(ip, port, 0))); }
public async Task <bool> GetRemotePeerStatus(string host, int port) { var peerNode = await PeerNode.Create(host, port, 0).ConfigureAwait(false); return(GetRemotePeerStatus(peerNode)); }
public Task Start() { if (this.State != ServiceState.Stopped) { throw new InvalidOperationException("Another instance of this service is already executing."); } if (this.Configuration == null) { throw new InvalidOperationException("The service must first be configured by calling LoadConfiguration."); } return(Task.Run( async() => { _stateSubject.OnNext(ServiceState.Starting); // initial setup // see https://stackoverflow.com/questions/2977630/wcf-instance-already-exists-in-counterset-error-when-reopening-servicehost GC.Collect(); GC.WaitForPendingFinalizers(); _localAgents.Clear(); _remoteAgents.Clear(); WcfFactory.BindingListenBacklog = this.Configuration.WcfBindingListenBacklog; WcfFactory.BindingMaxConnections = this.Configuration.WcfBindingMaxConnections; WcfFactory.BehaviorMaxConcurrentCalls = this.Configuration.WcfBehaviorMaxConcurrentCalls; WcfFactory.BehaviorMaxConcurrentSessions = this.Configuration.WcfBehaviorMaxConcurrentSessions; WcfFactory.BehaviorMaxConcurrentInstances = this.Configuration.WcfBehaviorMaxConcurrentInstances; this.MinOperationRetryDelay = TimeSpan.FromMilliseconds(this.Configuration.MinOperationRetryDelayInMs); this.MaxOperationRetryDelay = TimeSpan.FromMilliseconds(this.Configuration.MaxOperationRetryDelayInMs); foreach (var folder in this.Configuration.DependencyFolders) { _dependencyFolders.Add(folder); } // create local peer this.LocalPeerNode = await PeerNode.Create(this.Configuration.Host, this.Configuration.Port, this.Configuration.RxPort, this.Configuration.Description).ConfigureAwait(false); Log.Debug().Message("Adresse assignée au AgentBroker: '{0}'.", this.LocalPeerNode).Write(); _nextRxPort = this.Configuration.RxPort; // peers and agents creation // create local PeerCommunicationAgent var peerAgentInfo = await GetOrRegisterLocalAgent(PeerCommunicationAgent.Configuration, isInternal: true, forceRegister: true).ConfigureAwait(false); // create local agents await Task.WhenAll( this.Configuration.Agents .Where(agentConfig => agentConfig.Enabled) .Select(agentConfig => GetOrRegisterLocalAgent(agentConfig, isInternal: false, forceRegister: true))).ConfigureAwait(false); // activate internal agents await Task.WhenAll(GetLocalAgentInfos <IAgent>().Where(a => a.IsInternal).Select(a => a.Agent.Activate())).ConfigureAwait(false); // get remote peers list var remotePeers = ( await Task.WhenAll( this.Configuration.Peers .Where(p => p.Enabled) .Select(p => PeerNode.Create(p.Host, p.Port, p.RxPort, p.Description))) .ConfigureAwait(false) ).AsEnumerable(); // filter out this multiagent from the configuration's peer list by comparing their IP and port (host is resolved if required) // note that == and != operators are NOT overloaded by IPAddress, the Equals method must be used instead remotePeers = remotePeers .Where(p => p.Port != this.LocalPeerNode.Port || !p.Host.Equals(this.LocalPeerNode.Host)); // create remote peers foreach (var peer in remotePeers) { RegisterPeer(peer); } _heartbeatCts = new CancellationTokenSource(); _heartbeatTask = StartHeartbeat(_heartbeatCts.Token); _stateSubject.OnNext(ServiceState.Started); // activate all local agents if requested // must be done AFTER service state transition to Started because ObserveXXX are active only when the service is started if (this.Configuration.AutoActivateAgents) { await Task.WhenAll(GetLocalAgentInfos <IAgent>().Select(a => a.Agent.Activate())).ConfigureAwait(false); } }) .ContinueWith( async t => { if (t.IsCanceled) { throw (Exception)t.Exception ?? new OperationCanceledException(); } else if (t.IsFaulted) { await Stop(true).ConfigureAwait(false); throw t.Exception; } }) .Unwrap()); }