/// <summary>
        /// Add an observer for the given observable resource
        /// </summary>
        /// <param name="coapReq">A request message from client that is requesting resource observation</param>
        public void AddResourceObserver(CoAPRequest coapReq)
        {
            if (coapReq == null)
            {
                throw new ArgumentNullException("CoAP message requesting for observation is NULL");
            }
            if (!coapReq.IsObservable())
            {
                throw new ArgumentException("CoAP message requesting for observation is not marked as observable");
            }
            string observableURL = coapReq.GetURL().Trim().ToLower();

            //First, add this URL as an observable resource
            this.AddObservableResource(observableURL);

            //Now, add this observer for the given observable resource
            this._observableListSync.WaitOne();
            bool      observerAlreadyExists = false;
            ArrayList observers             = (ArrayList)this._observers[observableURL];

            for (int count = 0; count < observers.Count; count++)
            {
                CoAPRequest storedObserver = (CoAPRequest)observers[count];
                if (storedObserver.ID.Value == coapReq.ID.Value)
                {
                    observerAlreadyExists = true;
                    break;
                }
            }
            if (!observerAlreadyExists)
            {
                observers.Add(coapReq);
            }
            this._observableListSync.Set();
        }
Esempio n. 2
0
        /// <summary>
        /// Called when a CoAP request is received...we will only support CON requests
        /// of type GET... the path is sensors/temp
        /// </summary>
        /// <param name="coapReq">CoAPRequest object</param>
        static void OnCoAPRequestReceived(CoAPRequest coapReq)
        {
            string reqPath = (coapReq.GetPath() != null) ? coapReq.GetPath().ToLower() : "";

            /*We have skipped error handling in code below to just focus on observe option*/
            if (coapReq.MessageType.Value == CoAPMessageType.CON && coapReq.Code.Value == CoAPMessageCode.GET)
            {
                if (!coapReq.IsObservable() /*Does the request have "Observe" option*/)
                {
                    /*Request is not to observe...we do not support anything no-observable*/
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.NOT_IMPLEMENTED,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    coapServer.Send(resp);
                }
                else if (!coapServer.ObserversList.IsResourceBeingObserved(coapReq.GetURL()) /*do we support observation on this path*/)
                {
                    //Observation is not supported on this path..just to tell you how to check
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.NOT_FOUND,
                                                         coapReq /*Copy all necessary values from request in the response*/);
                    coapServer.Send(resp);
                }
                else
                {
                    //This is a request to observe this resource...register this client
                    coapServer.ObserversList.AddResourceObserver(coapReq);

                    /*Request contains observe option and path is correct*/
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK,
                                                         CoAPMessageCode.EMPTY,
                                                         coapReq /*Copy all necessary values from request in the response*/);

                    //send it..tell client we registered it's request to observe
                    coapServer.Send(resp);
                }
            }
        }