/// <summary> /// Start listening for requests. /// </summary> public void Start() { if (this.SensorChooserMap.Count <= 0) { if (this.listener != null) { this.listener.Stop(); } return; } bool snapshotMatch = true; // Check if the sensor chooser map, the allowed origins list or the collection // of stream handler factories have changed since last snapshot was taken. // If any of them has changed we need to create a new ThreadHostedHttpListener, // since the listener handler factory map is immutable data per instance. if (!SensorMapEquals(this.sensorChooserMap, this.sensorChooserMapSnapshot)) { snapshotMatch = false; this.sensorChooserMapSnapshot = new Dictionary <string, KinectSensorChooser>(this.sensorChooserMap); } if (!this.allowedOrigins.SequenceEqual(this.allowedOriginsSnapshot)) { snapshotMatch = false; this.allowedOriginsSnapshot = new List <Uri>(this.allowedOrigins); } if (!this.streamHandlerFactoriesSnapshot.SequenceEqual(this.streamHandlerFactories)) { snapshotMatch = false; this.streamHandlerFactoriesSnapshot = new Collection <ISensorStreamHandlerFactory>(this.streamHandlerFactories); } if (!snapshotMatch || this.hasFileServerChanged || this.hasOriginChanged || (this.listener == null)) { if (this.listener != null) { // we need to stop the current listener before creating a new one with // diferent parameters this.listener.Stop(true); this.listener.Started -= this.ListenerOnStarted; this.listener.Stopped -= this.ListenerOnStopped; this.listener = null; } var factoryMap = new Dictionary <string, IHttpRequestHandlerFactory>(); foreach (var entry in this.sensorChooserMapSnapshot) { factoryMap.Add( string.Format(CultureInfo.InvariantCulture, "{0}/{1}", KinectEndpointBasePath, entry.Key), new KinectRequestHandlerFactory(entry.Value, this.streamHandlerFactoriesSnapshot)); } if (!string.IsNullOrEmpty(this.fileServerRootDirectory)) { factoryMap.Add( this.fileServerBasePath, new FileRequestHandlerFactory(this.fileServerRootDirectory)); } var origins = new[] { this.OriginUri }; this.listener = new ThreadHostedHttpListener(origins, this.allowedOriginsSnapshot, factoryMap); this.listener.Started += this.ListenerOnStarted; this.listener.Stopped += this.ListenerOnStopped; this.listener.Start(); this.hasFileServerChanged = false; this.hasOriginChanged = false; } else if (!this.listener.IsListening) { this.listener.Start(); } }
/// <summary> /// Start listening for requests. /// </summary> public void Start() { if (this.SensorChooserMap.Count <= 0) { if (this.listener != null) { this.listener.Stop(); } return; } bool snapshotMatch = true; // Check if the sensor chooser map, the allowed origins list or the collection // of stream handler factories have changed since last snapshot was taken. // If any of them has changed we need to create a new ThreadHostedHttpListener, // since the listener handler factory map is immutable data per instance. if (!SensorMapEquals(this.sensorChooserMap, this.sensorChooserMapSnapshot)) { snapshotMatch = false; this.sensorChooserMapSnapshot = new Dictionary<string, KinectSensorChooser>(this.sensorChooserMap); } if (!this.allowedOrigins.SequenceEqual(this.allowedOriginsSnapshot)) { snapshotMatch = false; this.allowedOriginsSnapshot = new List<Uri>(this.allowedOrigins); } if (!this.streamHandlerFactoriesSnapshot.SequenceEqual(this.streamHandlerFactories)) { snapshotMatch = false; this.streamHandlerFactoriesSnapshot = new Collection<ISensorStreamHandlerFactory>(this.streamHandlerFactories); } if (!snapshotMatch || this.hasFileServerChanged || this.hasOriginChanged || (this.listener == null)) { if (this.listener != null) { // we need to stop the current listener before creating a new one with // diferent parameters this.listener.Stop(true); this.listener.Started -= this.ListenerOnStarted; this.listener.Stopped -= this.ListenerOnStopped; this.listener = null; } var factoryMap = new Dictionary<string, IHttpRequestHandlerFactory>(); foreach (var entry in this.sensorChooserMapSnapshot) { factoryMap.Add( string.Format(CultureInfo.InvariantCulture, "{0}/{1}", KinectEndpointBasePath, entry.Key), new KinectRequestHandlerFactory(entry.Value, this.streamHandlerFactoriesSnapshot)); } if (!string.IsNullOrEmpty(this.fileServerRootDirectory)) { factoryMap.Add( this.fileServerBasePath, new FileRequestHandlerFactory(this.fileServerRootDirectory)); } var origins = new[] { this.OriginUri }; this.listener = new ThreadHostedHttpListener(origins, this.allowedOriginsSnapshot, factoryMap); this.listener.Started += this.ListenerOnStarted; this.listener.Stopped += this.ListenerOnStopped; this.listener.Start(); this.hasFileServerChanged = false; this.hasOriginChanged = false; } else if (!this.listener.IsListening) { this.listener.Start(); } }