private IPeerNeighbor GetNeighbor() { IPeerNeighbor neighbor = this.getNeighborCallback(OperationContext.Current.GetCallbackChannel <IPeerProxy>()); if ((neighbor == null) || (neighbor.State == PeerNeighborState.Closed)) { if (DiagnosticUtility.ShouldTraceWarning) { TraceUtility.TraceEvent(TraceEventType.Warning, 0x40036, System.ServiceModel.SR.GetString("TraceCodePeerNeighborNotFound"), new PeerNodeTraceRecord(this.config.NodeId), OperationContext.Current.IncomingMessage); } return(null); } if (DiagnosticUtility.ShouldTraceVerbose) { PeerNeighborState state = neighbor.State; PeerNodeAddress listenAddress = null; IPAddress connectIPAddress = null; if ((state >= PeerNeighborState.Opened) && (state <= PeerNeighborState.Connected)) { listenAddress = this.config.GetListenAddress(true); connectIPAddress = this.config.ListenIPAddress; } PeerNeighborTraceRecord extendedData = new PeerNeighborTraceRecord(neighbor.NodeId, this.config.NodeId, listenAddress, connectIPAddress, neighbor.GetHashCode(), neighbor.IsInitiator, state.ToString(), null, null, OperationContext.Current.IncomingMessage.Headers.Action); TraceUtility.TraceEvent(TraceEventType.Verbose, 0x4003a, System.ServiceModel.SR.GetString("TraceCodePeerNeighborMessageReceived"), extendedData, this, null); } return(neighbor); }
public virtual RegisterResponseInfo Register(Guid clientId, string meshId, PeerNodeAddress address) { Guid registrationId = Guid.NewGuid(); DateTime expires = DateTime.UtcNow + this.RefreshInterval; RegistrationEntry entry = null; MeshEntry meshEntry = null; lock (this.ThisLock) { entry = new RegistrationEntry(clientId, registrationId, meshId, expires, address); meshEntry = this.GetMeshEntry(meshId); if (meshEntry.Service2EntryTable.ContainsKey(address.ServicePath)) { PeerExceptionHelper.ThrowInvalidOperation_DuplicatePeerRegistration(address.ServicePath); } LiteLock liteLock = null; try { LiteLock.Acquire(out liteLock, meshEntry.Gate, true); meshEntry.EntryTable.Add(registrationId, entry); meshEntry.EntryList.Add(entry); meshEntry.Service2EntryTable.Add(address.ServicePath, entry); } finally { LiteLock.Release(liteLock); } } return(new RegisterResponseInfo(registrationId, this.RefreshInterval)); }
public RegisterInfo(Guid client, string meshId, PeerNodeAddress address) : this() { Body.ClientId = client; Body.MeshId = meshId; Body.NodeAddress = address; }
public RegisterInfo (Guid client, string meshId, PeerNodeAddress address) : this () { body.ClientId = client; body.MeshId = meshId; body.NodeAddress = address; }
// Register a node address for a given mesh ID public int Register(string meshId, PeerNodeAddress nodeAddress) { bool newMeshId = false; int registrationId; Registration registration = new Registration(meshId, nodeAddress); // Add the new registration to the registration table; update meshIdTable with the newly registered nodeAddress lock (registrationTable) { registrationId = nextRegistrationId++; lock (meshIdTable) { // Update the meshId table Dictionary <int, PeerNodeAddress> addresses; if (!meshIdTable.TryGetValue(meshId, out addresses)) { // MeshID doesn't exist and needs to be added to meshIdTable newMeshId = true; addresses = new Dictionary <int, PeerNodeAddress>(); meshIdTable[meshId] = addresses; } addresses[registrationId] = nodeAddress; // Add an entry to the registration table registrationTable[registrationId] = new Registration(meshId, nodeAddress); } } if (newMeshId) { Console.WriteLine("Registered new meshId {0}", meshId); } return(registrationId); }
public UpdateInfoDC(Guid registrationId, Guid client, string meshId, PeerNodeAddress address) { this.ClientId = client; this.MeshId = meshId; this.NodeAddress = address; this.RegistrationId = registrationId; }
public UpdateInfo (Guid registrationId, Guid client, string meshId, PeerNodeAddress address) : this () { Body.RegistrationId = registrationId; Body.ClientId = client; Body.MeshId = meshId; Body.NodeAddress = address; }
public UpdateInfo(Guid registrationId, Guid client, string meshId, PeerNodeAddress address) : this() { Body.RegistrationId = registrationId; Body.ClientId = client; Body.MeshId = meshId; Body.NodeAddress = address; }
public override object Register(string meshId, PeerNodeAddress nodeAddress, TimeSpan timeout) { if (this.opened) { long scopeId = -1L; bool flag = false; if (nodeAddress.IPAddresses.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.ServiceModel.SR.GetString("MustRegisterMoreThanZeroAddresses"))); } foreach (IPAddress address in nodeAddress.IPAddresses) { if (address.IsIPv6LinkLocal) { if (scopeId == -1L) { scopeId = address.ScopeId; } else if (scopeId != address.ScopeId) { flag = true; break; } } } List <IPAddress> list = new List <IPAddress>(); foreach (IPAddress address2 in nodeAddress.IPAddresses) { if (!flag || (!address2.IsIPv6LinkLocal && !address2.IsIPv6SiteLocal)) { list.Add(address2); } } if (list.Count == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("AmbiguousConnectivitySpec"))); } ReadOnlyCollection <IPAddress> ipAddresses = new ReadOnlyCollection <IPAddress>(list); this.meshId = meshId; this.nodeAddress = new PeerNodeAddress(nodeAddress.EndpointAddress, ipAddresses); RegisterInfo registerInfo = new RegisterInfo(this.clientId, meshId, this.nodeAddress); IPeerResolverClient proxy = this.GetProxy(); try { proxy.OperationTimeout = timeout; RegisterResponseInfo info2 = proxy.Register(registerInfo); this.registrationId = info2.RegistrationId; this.timer.Set(info2.RegistrationLifetime); this.defaultLifeTime = info2.RegistrationLifetime; proxy.Close(); } finally { proxy.Abort(); } } return(this.registrationId); }
public override void Update(object registrationId, PeerNodeAddress updatedNodeAddress, TimeSpan timeout) { if (this.opened) { UpdateInfo updateInfo = new UpdateInfo(this.registrationId, this.clientId, this.meshId, updatedNodeAddress); this.nodeAddress = updatedNodeAddress; this.SendUpdate(updateInfo, timeout); } }
void OnFailed(IPeerNeighbor neighbor) { lock (ThisLock) { this.state = PeerAuthState.Failed; this.timer.Cancel(); this.host = null; } if (DiagnosticUtility.ShouldTraceError) { PeerAuthenticationFailureTraceRecord record = null; String remoteUri = ""; PeerNodeAddress remoteAddress = neighbor.ListenAddress; if (remoteAddress != null) { remoteUri = remoteAddress.EndpointAddress.ToString(); } OperationContext opContext = OperationContext.Current; if (opContext != null) { remoteUri = opContext.IncomingMessageProperties.Via.ToString(); ServiceSecurityContext secContext = opContext.ServiceSecurityContext; if (secContext != null) { record = new PeerAuthenticationFailureTraceRecord( meshId, remoteUri, secContext.AuthorizationContext.ClaimSets[0], null); if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent( TraceEventType.Error, TraceCode.PeerNodeAuthenticationFailure, SR.GetString(SR.TraceCodePeerNodeAuthenticationFailure), record, this, null); } } } else { record = new PeerAuthenticationFailureTraceRecord(meshId, remoteUri); if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent(TraceEventType.Error, TraceCode.PeerNodeAuthenticationTimeout, SR.GetString(SR.TraceCodePeerNodeAuthenticationTimeout), record, this, null); } } } neighbor.Abort(PeerCloseReason.AuthenticationFailure, PeerCloseInitiator.LocalNode); }
public virtual ResolveResponseInfo Resolve(ResolveInfo resolveInfo) { if (resolveInfo == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("resolveInfo", System.ServiceModel.SR.GetString("PeerNullResolveInfo")); } this.ThrowIfClosed("Resolve"); if (!resolveInfo.HasBody()) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("resolveInfo", System.ServiceModel.SR.GetString("PeerInvalidMessageBody", new object[] { resolveInfo })); } int num = 0; int num2 = 0; int maxAddresses = resolveInfo.MaxAddresses; ResolveResponseInfo info = new ResolveResponseInfo(); List <PeerNodeAddress> list = new List <PeerNodeAddress>(); List <RegistrationEntry> entryList = null; MeshEntry meshEntry = this.GetMeshEntry(resolveInfo.MeshId, false); if (meshEntry != null) { LiteLock liteLock = null; try { LiteLock.Acquire(out liteLock, meshEntry.Gate); entryList = meshEntry.EntryList; if (entryList.Count <= maxAddresses) { foreach (RegistrationEntry entry3 in entryList) { list.Add(entry3.Address); } } else { Random random = new Random(); while (num < maxAddresses) { num2 = random.Next(entryList.Count); RegistrationEntry entry = entryList[num2]; PeerNodeAddress item = entry.Address; if (!list.Contains(item)) { list.Add(item); } num++; } } } finally { LiteLock.Release(liteLock); } } info.Addresses = list.ToArray(); return(info); }
public static bool ValidNodeAddress(PeerNodeAddress address) { return(address != null && address.EndpointAddress != null && address.EndpointAddress.Uri != null && address.IPAddresses != null && address.IPAddresses.Count > 0 && string.Compare(address.EndpointAddress.Uri.Scheme, Uri.UriSchemeNetTcp, StringComparison.OrdinalIgnoreCase) == 0 ); }
public PeerNeighborCloseTraceRecord(ulong remoteNodeId, ulong localNodeId, PeerNodeAddress listenAddress, IPAddress connectIPAddress, int hashCode, bool initiator, string state, string previousState, string attemptedState, string closeInitiator, string closeReason) : base(remoteNodeId, localNodeId, listenAddress, connectIPAddress, hashCode, initiator, state, previousState, attemptedState, null) { this.closeInitiator = closeInitiator; this.closeReason = closeReason; }
public override void Update(object registrationId, PeerNodeAddress updatedNodeAddress, TimeSpan timeout) { if (timeout <= TimeSpan.Zero) { throw new ArgumentException(String.Format("Timeout value must be positive value. It was {0}", timeout)); } client.OperationTimeout = timeout; client.Update(new UpdateInfo((Guid)registrationId, client_id, preserved_mesh_id, updatedNodeAddress)); }
private void UpdateEndpointsCollection(PeerNodeAddress address) { if (PeerValidateHelper.ValidNodeAddress(address)) { Uri endpointUri = GetEndpointUri(address); if (!this.nodeAddresses.ContainsKey(endpointUri) && (endpointUri != GetEndpointUri(this.maintainer.GetListenAddress()))) { this.nodeAddresses[endpointUri] = address; } } }
// Updates a node's registration with the resolver service. public override void Update(object registrationId, PeerNodeAddress updatedNodeAddress, TimeSpan timeout) { ChannelFactory <ICustomPeerResolverChannel> factory = new ChannelFactory <ICustomPeerResolverChannel>(config); ICustomPeerResolverChannel client = factory.CreateChannel(); MaskScopeId(updatedNodeAddress.IPAddresses); client.Update((int)registrationId, updatedNodeAddress); client.Close(); factory.Close(); }
void UpdateEndpointsCollection(PeerNodeAddress address) { // Don't accept invalid addresses if (PeerValidateHelper.ValidNodeAddress(address)) { Uri key = GetEndpointUri(address); if (!nodeAddresses.ContainsKey(key) && key != GetEndpointUri(maintainer.GetListenAddress())) { nodeAddresses[key] = address; } } }
IAsyncResult IPeerMaintainer.BeginOpenNeighbor(PeerNodeAddress address, TimeSpan timeout, AsyncCallback callback, object asyncState) { lock (this.ThisLock) { EndpointAddress endpointAddress = address.EndpointAddress; if (this.referralCache.ContainsKey(endpointAddress)) { this.referralCache.Remove(endpointAddress); } } return(this.neighborManager.BeginOpenNeighbor(address, timeout, callback, asyncState)); }
Node RegisterNode(Mesh mesh, Guid clientId, PeerNodeAddress addr) { lock (mesh) { var node = new Node() { ClientId = clientId, Address = addr }; mesh.Add(node); node.LastRefreshTime = DateTime.UtcNow; return(node); } }
private ReadOnlyCollection <PeerNodeAddress> ResolveNewAddresses(TimeSpan timeLeft, bool retryResolve) { TimeoutHelper helper = new TimeoutHelper(timeLeft); Dictionary <string, PeerNodeAddress> dictionary = new Dictionary <string, PeerNodeAddress>(); List <PeerNodeAddress> list = new List <PeerNodeAddress>(); PeerNodeAddress listenAddress = this.config.GetListenAddress(true); dictionary.Add(listenAddress.ServicePath, listenAddress); int num = retryResolve ? 2 : 1; if (DiagnosticUtility.ShouldTraceInformation) { PeerMaintainerTraceRecord extendedData = new PeerMaintainerTraceRecord("Resolving"); TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), extendedData, this, null); } for (int i = 0; ((i < num) && (list.Count < this.config.MaxResolveAddresses)) && (this.isOpen && (helper.RemainingTime() > TimeSpan.Zero)); i++) { ReadOnlyCollection <PeerNodeAddress> onlys; try { onlys = this.config.Resolver.Resolve(this.config.MeshId, this.config.MaxResolveAddresses, helper.RemainingTime()); } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } if (DiagnosticUtility.ShouldTraceInformation) { PeerMaintainerTraceRecord record2 = new PeerMaintainerTraceRecord("Resolve exception " + exception.Message); TraceUtility.TraceEvent(TraceEventType.Information, 0x40051, System.ServiceModel.SR.GetString("TraceCodePeerMaintainerActivity"), record2, this, null); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("ResolverException"), exception)); } if (onlys != null) { foreach (PeerNodeAddress address2 in onlys) { if (!dictionary.ContainsKey(address2.ServicePath)) { dictionary.Add(address2.ServicePath, address2); if (((IPeerMaintainer)this).FindDuplicateNeighbor(address2) == null) { list.Add(address2); } } } } } return(new ReadOnlyCollection <PeerNodeAddress>(list)); }
public PeerNeighborTraceRecord(ulong remoteNodeId, ulong localNodeId, PeerNodeAddress listenAddress, IPAddress connectIPAddress, int hashCode, bool initiator, string state, string previousState, string attemptedState, string action) { this.localNodeId = localNodeId; this.remoteNodeId = remoteNodeId; this.listenAddress = listenAddress; this.connectIPAddress = connectIPAddress; this.hashCode = hashCode; this.initiator = initiator; this.state = state; this.previousState = previousState; this.attemptedState = attemptedState; this.action = action; }
private void SendConnect(IPeerNeighbor neighbor) { if ((neighbor.State == PeerNeighborState.Connecting) && (this.state == State.Opened)) { PeerNodeAddress listenAddress = this.config.GetListenAddress(true); if (listenAddress != null) { ConnectInfo typedMessage = new ConnectInfo(this.config.NodeId, listenAddress); Message message = this.ConnectInfoMessageConverter.ToMessage(typedMessage, MessageVersion.Soap12WSAddressing10); this.SendMessageToNeighbor(neighbor, message, new PeerMessageHelpers.CleanupCallback(this.OnConnectFailure)); } } }
// Resolve addresses for a given mesh ID public PeerNodeAddress[] Resolve(string meshId, int maxAddresses) { if (maxAddresses <= 0) { throw new ArgumentOutOfRangeException("maxAddresses"); } PeerNodeAddress [] copyOfAddresses; lock (meshIdTable) { Dictionary <int, PeerNodeAddress> addresses; // Check if the meshId is known and if so, make a copy of the addresses to process outside of lock if (meshIdTable.TryGetValue(meshId, out addresses)) { // you may want to avoid the copy approach in case of a large mesh copyOfAddresses = new PeerNodeAddress[addresses.Count]; addresses.Values.CopyTo(copyOfAddresses, 0); } else { copyOfAddresses = new PeerNodeAddress[0]; } } // If there are <= maxAddresses for this meshId, return them all if (copyOfAddresses.Length <= maxAddresses) { return(copyOfAddresses); } else { // Otherwise, pick maxAddresses at random List <int> indices = new List <int>(maxAddresses); // may want to use a more performant data structure if maxAddresses is larger while (indices.Count < maxAddresses) { int listIndex = this.random.Next() % copyOfAddresses.Length; if (!indices.Contains(listIndex)) // check if we've already seen this index { indices.Add(listIndex); } } PeerNodeAddress[] randomAddresses = new PeerNodeAddress[maxAddresses]; for (int i = 0; i < randomAddresses.Length; i++) { randomAddresses[i] = copyOfAddresses[indices[i]]; } return(randomAddresses); } }
// Register address for a node participating in a mesh identified by meshId with the resolver service public override object Register(string meshId, PeerNodeAddress nodeAddress, TimeSpan timeout) { ChannelFactory <ICustomPeerResolverChannel> factory = new ChannelFactory <ICustomPeerResolverChannel>(config); ICustomPeerResolverChannel client = factory.CreateChannel(); MaskScopeId(nodeAddress.IPAddresses); int registrationId = client.Register(meshId, nodeAddress); client.Close(); factory.Close(); return(registrationId); }
public void ResolveResponseInfo() { var ser = new DataContractSerializer(typeof(ResolveResponseInfo)); var rri = new ResolveResponseInfo(); var pna = new PeerNodeAddress( new EndpointAddress("http://localhost:8080"), new ReadOnlyCollection <IPAddress> (new IPAddress [0])); rri.Addresses = new List <PeerNodeAddress> (); rri.Addresses.Add(pna); var sw = new StringWriter(); using (var xw = XmlWriter.Create(sw)) ser.WriteObject(xw, rri); rri = (ResolveResponseInfo)ser.ReadObject(XmlReader.Create(new StringReader(sw.ToString()))); }
void SendConnect(IPeerNeighbor neighbor) { // We do not attempt to send the message if PeerConnector is not open if (neighbor.State == PeerNeighborState.Connecting && this.state == State.Opened) { // Retrieve the local address. The retrieved address may be null if the node // is shutdown. In that case, don't bother to send connect message since the // node is closing... PeerNodeAddress listenAddress = this.config.GetListenAddress(true); if (listenAddress != null) { ConnectInfo connectInfo = new ConnectInfo(this.config.NodeId, listenAddress); Message message = ConnectInfoMessageConverter.ToMessage(connectInfo, MessageVersion.Soap12WSAddressing10); SendMessageToNeighbor(neighbor, message, OnConnectFailure); } } }
// Update an existing registration (can be called if the machine IP addresses change etc.) public void Update(int registrationId, PeerNodeAddress updatedNodeAddress) { Registration registration; lock (registrationTable) { // We expect the registration to exist. registration = registrationTable[registrationId]; // Update registrationTable and meshIdTable lock (meshIdTable) { Dictionary <int, PeerNodeAddress> addresses = meshIdTable[registration.MeshId]; addresses[registrationId] = updatedNodeAddress; } registrationTable[registrationId].NodeAddress = updatedNodeAddress; } }
private void OnFailed(IPeerNeighbor neighbor) { lock (this.ThisLock) { this.state = PeerAuthState.Failed; this.timer.Cancel(); this.host = null; } if (DiagnosticUtility.ShouldTraceError) { PeerAuthenticationFailureTraceRecord extendedData = null; string remoteAddress = ""; PeerNodeAddress listenAddress = neighbor.ListenAddress; if (listenAddress != null) { remoteAddress = listenAddress.EndpointAddress.ToString(); } OperationContext current = OperationContext.Current; if (current != null) { remoteAddress = current.IncomingMessageProperties.Via.ToString(); ServiceSecurityContext serviceSecurityContext = current.ServiceSecurityContext; if (serviceSecurityContext != null) { extendedData = new PeerAuthenticationFailureTraceRecord(this.meshId, remoteAddress, serviceSecurityContext.AuthorizationContext.ClaimSets[0], null); if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent(TraceEventType.Error, 0x4004d, System.ServiceModel.SR.GetString("TraceCodePeerNodeAuthenticationFailure"), extendedData, this, null); } } } else { extendedData = new PeerAuthenticationFailureTraceRecord(this.meshId, remoteAddress); if (DiagnosticUtility.ShouldTraceError) { TraceUtility.TraceEvent(TraceEventType.Error, 0x4004e, System.ServiceModel.SR.GetString("TraceCodePeerNodeAuthenticationTimeout"), extendedData, this, null); } } } neighbor.Abort(PeerCloseReason.AuthenticationFailure, PeerCloseInitiator.LocalNode); }
public override object Register(string meshId, PeerNodeAddress nodeAddress, TimeSpan timeout) { if (String.IsNullOrEmpty(meshId)) { throw new ArgumentNullException("meshId"); } if (nodeAddress == null) { throw new ArgumentNullException("nodeAddress"); } if (timeout <= TimeSpan.Zero) { throw new ArgumentException(String.Format("Timeout value must be positive value. It was {0}", timeout)); } client.OperationTimeout = timeout; preserved_mesh_id = meshId; return(client.Register(new RegisterInfo(client_id, meshId, nodeAddress)).RegistrationId); }
public static bool ValidReferralNodeAddress(PeerNodeAddress address) { long scopeId = -1L; foreach (IPAddress address2 in address.IPAddresses) { if (address2.IsIPv6LinkLocal) { if (scopeId == -1L) { scopeId = address2.ScopeId; } else if (scopeId != address2.ScopeId) { return(false); } } } return(true); }
// Query the resolver service for addresses associated with a mesh ID public override ReadOnlyCollection <PeerNodeAddress> Resolve(string meshId, int maxAddresses, TimeSpan timeout) { PeerNodeAddress[] addresses = null; ChannelFactory <ICustomPeerResolverChannel> factory = new ChannelFactory <ICustomPeerResolverChannel>(config); ICustomPeerResolverChannel client = factory.CreateChannel(); addresses = client.Resolve(meshId, maxAddresses); client.Close(); factory.Close(); // If addresses couldn't be obtained, return empty collection if (addresses == null) { addresses = new PeerNodeAddress[0]; } return(new ReadOnlyCollection <PeerNodeAddress>(addresses)); }
public virtual RegisterResponseInfo Register (Guid clientId, string meshId, PeerNodeAddress address) { return Register (new RegisterInfo (clientId, meshId, address)); }
public ResolveResponseInfo (PeerNodeAddress [] addresses) { Body.Addresses = new List<PeerNodeAddress> (addresses); }