private SpotterBase CreateNextSpotter(string contentType) { if (this._capacity != 0 && this._total >= this._capacity) { throw new Exception("Out of pool capacity."); } var i = SpotterFactory.GetSpotter(contentType); this._total++; this._inUse.Add(i); return(i); }
/// <summary> /// Gets spotter from pool. If no spotter is available and capacity is reached exeption is raised. /// </summary> /// <param name="contentType">Content type defining wanted spotter.</param> /// <returns></returns> public SpotterBase GetSpotter(string contentType) { lock (this._lock) { if (this.IsFreeOf(SpotterFactory.GetSpotterType(contentType))) { return(this.GetFreeSpotter(contentType)); } else { return(this.CreateNextSpotter(contentType)); } } }
/// <summary> /// Returns spotter from pool or waits if no free spotter is available. /// </summary> /// <param name="contentType">Content type defining wanted spotter.</param> /// <returns></returns> public SpotterBase GetSpotterOrWait(string contentType) { lock (this._lock) { if (this.IsFreeOf(SpotterFactory.GetSpotterType(contentType))) { // Free instances exists return(this.GetFreeSpotter(contentType)); } else if (this._total < this._capacity || this._capacity == 0) { // No free instances but free capacity return(this.CreateNextSpotter(contentType)); } else { do { Monitor.Wait(this._lock); } while(this.IsFreeOf(SpotterFactory.GetSpotterType(contentType))); return(this.GetFreeSpotter(contentType)); } } }
/// <summary> /// Creates new spotter pool with selected capacity. /// </summary> /// <param name="capacity">Pool capacity.</param> public SpotterPool(int capacity) { this._capacity = capacity; this._total = 0; /* initialize with spotters of all kind if capacity is not limitless */ if (capacity > 0) { foreach (var contentType in SpotterFactory.SupportedContent) { SpotterBase i = null; // have to create multipart spotter manualy because it has to be initialized with this pool but it is yet uninitialized in SpotterFactory i = contentType.StartsWith("multipart/form-data")? new SpotterMultipart(this) : SpotterFactory.GetSpotter(contentType); this._total++; this._free.Add(new KeyValuePair <Type, SpotterBase>(i.GetType(), i)); } } }