/// <summary> /// Remove an observer for the given observable resource /// </summary> /// <param name="observableResourceURL">The observable URL for which the observer has to be removed</param> /// <param name="tokenValue">The Token value the observer</param> public void RemoveResourceObserver(string observableResourceURL, byte[] tokenValue) { if (!this.IsResourceBeingObserved(observableResourceURL)) { return; } this._observableListSync.WaitOne(); bool observerExists = false; ArrayList observers = (ArrayList)this._observers[observableResourceURL]; int count = 0; for (count = 0; count < observers.Count; count++) { CoAPRequest storedObserver = (CoAPRequest)observers[count]; if (AbstractByteUtils.AreByteArraysEqual(storedObserver.Token.Value, tokenValue)) { observerExists = true; break; } } if (observerExists && count < observers.Count) { observers.RemoveAt(count); } this._observableListSync.Set(); }
/// <summary> /// Check if the collection holds the given option number with the given value /// </summary> /// <param name="optionNumber">The option number whose existence has to be checked</param> /// <param name="optionValue">The option value to match</param> /// <returns>bool</returns> public bool HasOption(UInt16 optionNumber, byte[] optionValue) { foreach (CoAPHeaderOption headerOption in this) { if (headerOption.Number == optionNumber) { if (AbstractByteUtils.AreByteArraysEqual(headerOption.Value, optionValue)) { return(true); } } } return(false); }
/// <summary> /// Remove the given option that has the given value. If the option and value appears /// multiple times, all entries will be removed /// </summary> /// <param name="optionNumber">The option number to remove</param> /// <param name="optionValue">The option value that must match the option number to be removed</param> public virtual void RemoveOption(UInt16 optionNumber, byte[] optionValue) { while (this.HasOption(optionNumber, optionValue)) { foreach (CoAPHeaderOption option in this) { if (option.Number == optionNumber) { if (AbstractByteUtils.AreByteArraysEqual(option.Value, optionValue)) { this.Remove(option); break; } } } } }
/// <summary> /// Based on the token value, find which resource was being observed /// </summary> /// <param name="token">The CoAP token that is used for req/resp matching</param> /// <returns>The URL of the resource being observed</returns> protected string FindResourceBeingObserved(CoAPToken token) { if (token == null || token.Value == null) { throw new ArgumentNullException("Invalid token"); } bool observerFound = false; string observedResourceURL = null; CoAPRequest observerEntry = null; this._observableListSync.WaitOne(); foreach (string observedResURL in this._observers.Keys) { ArrayList observers = (ArrayList)this._observers[observedResURL]; foreach (CoAPRequest req in observers) { if (AbstractByteUtils.AreByteArraysEqual(req.Token.Value, token.Value)) { observerFound = true; observerEntry = req; break; } } if (observerFound) { break; } } this._observableListSync.Set(); if (observerFound && observerEntry != null) { observedResourceURL = observerEntry.GetURL(); } return(observedResourceURL); }