示例#1
0
        public void AddObserver(Request request, LocalResource resource)
        {
            request.IsObserving = true;
            Observationship shipToAdd = new Observationship(request);

            lock (_sync)
            {
                // get clients map for the given resource path
                IDictionary <String, Observationship> resourceObservers = SafeGet(_observersByResource, resource.Path);
                // get resource map for given client address
                IDictionary <String, Observationship> clientObservers = SafeGet(_observersByClient, request.PeerAddress.ToString());

                // save relationship for notifications triggered by resource
                resourceObservers[request.PeerAddress.ToString()] = shipToAdd;
                // save relationship for actions triggered by client
                clientObservers[resource.Path] = shipToAdd;
            }

            if (log.IsInfoEnabled)
            {
                log.Info(String.Format("Established observing relationship: {0} @ {1}",
                                       request.PeerAddress, resource.Path));
            }

            // update response
            PrepareResponse(request);
        }
示例#2
0
        public void RemoveObserver(String clientID, Int32 messageId)
        {
            Observationship shipToRemove = null;
            IDictionary <String, Observationship> clientObservers = null;

            if (_observersByClient.ContainsKey(clientID))
            {
                clientObservers = _observersByClient[clientID];
                foreach (Observationship ship in clientObservers.Values)
                {
                    if (messageId == ship.lastMessageID && clientID.Equals(ship.clientID))
                    {
                        shipToRemove = ship;
                        break;
                    }
                }
            }

            if (shipToRemove == null)
            {
                if (log.IsWarnEnabled)
                {
                    log.Warn(String.Format("Cannot find observing relationship: {0}|{1}", clientID, messageId));
                }
            }
            else
            {
                IDictionary <String, Observationship> resourceObservers = _observersByResource[shipToRemove.resourcePath];

                // FIXME Inconsistent state check
                if (resourceObservers == null)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error(String.Format("FIXME: ObservingManager has clientObservee, but no resourceObservers ({0} @ {1})", clientID, shipToRemove.resourcePath));
                    }
                }
                else if (resourceObservers.Remove(clientID) && clientObservers.Remove(shipToRemove.resourcePath))
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info(String.Format("Terminated observing relationship by RST: {0} @ {1}", clientID, shipToRemove.resourcePath));
                    }
                    return;
                }
            }

            if (log.IsWarnEnabled)
            {
                log.Warn(String.Format("Cannot find observing relationship by MID: {0}|{1}", clientID, messageId));
            }
        }
示例#3
0
 private void UpdateLastMessageID(String clientID, String path, Int32 id)
 {
     if (_observersByClient.ContainsKey(clientID))
     {
         IDictionary <String, Observationship> clientObservers = _observersByClient[clientID];
         if (clientObservers.ContainsKey(path))
         {
             Observationship shipToUpdate = clientObservers[path];
             shipToUpdate.lastMessageID = id;
             if (log.IsDebugEnabled)
             {
                 log.Debug(String.Format("Updated last message ID for observing relationship: {0} @ {1}",
                                         clientID, shipToUpdate.resourcePath));
             }
             return;
         }
     }
     if (log.IsWarnEnabled)
     {
         log.Warn(String.Format("Cannot find observing relationship to update message ID: {0} @ {1}",
                                clientID, path));
     }
 }