/// <summary> /// Method to be called when a service has been discovered on the network. /// When listeners have been installed for this particular service, they will be correctly notified. /// </summary> /// <param name="sender">Caller of the event</param> /// <param name="eventArgs">Arguments attached to the event</param> public void OnServiceDiscovered(object sender, ServiceNotificationEventArgs eventArgs) { System.Diagnostics.Debug.WriteLine(String.Format("SERVICE DISCOVERED: {0}, {1}", eventArgs.typetag.tag, eventArgs.sourceAddress)); // Find out if ServiceListeners have been installed for a service of this particular type. If yes, call its "IsDiscovered" method // or "HandleDiscovery" on the ServiceListenerBase List<ServiceListenerBase> listeners = this.serviceManager.subscriptionManager.GetServiceListeners(eventArgs.typetag); foreach (ServiceListenerBase listener in listeners) listener.HandleDiscovery(eventArgs.service); }
/// <summary> /// Event to be triggered when a service reconnects to the network. /// When listeners have been installed, they will be correctly notified. /// </summary> /// <param name="sender">Caller of the event</param> /// <param name="eventArgs">Arguments linked to the event</param> public void OnServiceReconnected(object sender, ServiceNotificationEventArgs eventArgs) { System.Diagnostics.Debug.WriteLine(String.Format("SERVICE RECONNECTED: {0}, {1}", eventArgs.typetag.tag, eventArgs.sourceAddress)); // Notify the appropriate listeners List<ServiceListenerBase> listeners = this.serviceManager.subscriptionManager.GetServiceListeners(eventArgs.typetag); foreach (ServiceListenerBase listener in listeners) listener.HandleReconnection(eventArgs.service); }
/// <summary> /// Remove a previously imported service. Will move this service to the disconnected list. /// </summary> /// <param name="serviceIdentifier">Identifier of the disconnecting service</param> public void RemoveService(Guid serviceIdentifier) { // If we haven't discovered the service in the first place, ignore if (!this.IsImportedService(serviceIdentifier)) return; string sourceAddress = ""; // Remove the fact that we imported this service from "someone" (also grab the address while we're at it) foreach (string address in this.importedServiceAddresses.Keys) { List<Guid> serviceIdentifiers = this.importedServiceAddresses[address]; if (serviceIdentifiers.Contains(serviceIdentifier)) { sourceAddress = address; serviceIdentifiers.Remove(serviceIdentifier); if (serviceIdentifiers.Count == 0) this.importedServiceAddresses.Remove(address); // Break from the loop, because a service can only be exported by one client break; } } ServiceNotificationEventArgs eventArgs = new ServiceNotificationEventArgs(serviceIdentifier, sourceAddress, this.importedServiceTypeTags[serviceIdentifier], this.importedServices[serviceIdentifier]); // Remove the service implementation this.importedServices.Remove(serviceIdentifier); // Remove the service from the typetag dictionary this.importedServiceTypeTags.Remove(serviceIdentifier); // Add the service to the list of disconnected services this.diconnectedServices.Add(serviceIdentifier); if (this.ServiceDisconnectedEvent != null) this.ServiceDisconnectedEvent(this, eventArgs); }