public void Wait() { #region comment // The assumption for this section is the following. For 5 role instances in the development fabric, we will have 6 Mutex objects: // // The overall Mutex called // // MicrosoftCorpnetAuthenticationFixerWaiter // // The selfMutex for the current role instance called // // deployment(400).GenericWorkerRole.Cloud.WebRole.2 // // The peerMutex collection containing // // deployment(400).GenericWorkerRole.Cloud.WebRole.0 // deployment(400).GenericWorkerRole.Cloud.WebRole.1 // deployment(400).GenericWorkerRole.Cloud.WebRole.3 // deployment(400).GenericWorkerRole.Cloud.WebRole.4 // // Once an instance starts, it locks it's own selfMutex. Then, gated by the overallMutex, the an instance tests // whether all peerMutexes are claimed. If that's the case, we assume that all instances are now syncronized in // the development fabric and we can start configuring the application pools. // #endregion var selfMutexClaimed = _selfMutex.WaitOne(); if (!selfMutexClaimed) { throw new Exception("Cannot claim Mutex " + Current); } using (Mutex overallMutex = Open(GlobalMutexName)) { var allPeersRunning = false; while (!allPeersRunning) { overallMutex.WaitOne(); allPeersRunning = !Peers.Any(DoesNotExist); // all peers running when there are not any peers that do not exist overallMutex.ReleaseMutex(); if (!allPeersRunning) { Thread.Sleep(WaitingTime); // before trying again } } } _selfMutex.ReleaseMutex(); }
private void AddPeer(EndPoint address, Tracker tracker = null, bool withTracker = false) { if (!(Peers.Any(peer => peer.Address.Equals(address))) && !(address.Equals(CommonHelpers.GetLocalEndPoint(CommonHelpers.PeerPort, true)))) { var peer = new Peer(address, Peers, tracker); Peers.Add(peer); //перенаправляем события пира peer.OnRequestBlocksMessage += (s, e) => { OnRequestBlocksMessage?.Invoke(s, e); }; peer.OnRequestTransactionsMessage += (s, e) => { OnRequestTransactionsMessage?.Invoke(s, e); }; peer.OnBlocksMessage += (s, e) => { OnBlocksMessage?.Invoke(s, e); }; peer.OnTransactionsMessage += (s, e) => { OnTransactionsMessage?.Invoke(s, e); }; peer.OnPeersMessage += (s, e) => OnPeersMessage(s, e); peer.Connect(withTracker); //вызов события подключения пира Task.Run(() => { System.Threading.Thread.Sleep(CommonHelpers.MessagesInterval * 5); OnPeerConnected?.Invoke(this, new MessageEventArgs(new Message(), peer.Hash, peer.Address)); }); } }
public bool IsAnyConnected(string identity) => string.IsNullOrEmpty(identity) ? Peers.Count > 0 : Peers.Any(c => c.RemoteIdentity == identity);
private bool IsPeerExists(string url) { bool isExists = Peers.Any(x => x.Url == url); return(isExists); }