diff options
author | elie <elie> | 2005-06-14 10:43:02 +0000 |
---|---|---|
committer | elie <elie> | 2005-06-14 10:43:02 +0000 |
commit | 3be4e44e65c0ced55b3730af91b2b9b965408c36 (patch) | |
tree | 797a2c77dc1bd8bddfb8151ce786fb5038d144c7 /pysnmp/proto/mpmod/base.py | |
parent | da2a9b9c1b15edd229989711496ce73005e25a0c (diff) | |
download | pysnmp-git-3be4e44e65c0ced55b3730af91b2b9b965408c36.tar.gz |
renamed from msgproc
Diffstat (limited to 'pysnmp/proto/mpmod/base.py')
-rw-r--r-- | pysnmp/proto/mpmod/base.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/pysnmp/proto/mpmod/base.py b/pysnmp/proto/mpmod/base.py new file mode 100644 index 00000000..6b82de19 --- /dev/null +++ b/pysnmp/proto/mpmod/base.py @@ -0,0 +1,104 @@ +# MP-specific cache management +from pysnmp.proto import error + +class AbstractMessageProcessingModel: + messageProcessingModelID = None + __stateReference = __msgID = 0L + def __init__(self): + self.__msgIdIndex = {} + self.__stateReferenceIndex = {} + # Message expiration mechanics + self.__expirationQueue = {} + self.__expirationTimer = 0L + + def prepareOutgoingMessage(self, msgAndPduDsp, mibInstrumController, + **kwargs): pass + def prepareResponseMessage(self, msgAndPduDsp, mibInstrumController, + **kwargs): pass + def prepareDataElements(self, msgAndPduDsp, mibInstrumController, + wholeMsg): pass + + def _newStateReference(self): + AbstractMessageProcessingModel.__stateReference = ( + AbstractMessageProcessingModel.__stateReference + 1 + ) + return self.__stateReference + + # Server mode cache handling + + def _cachePushByStateRef(self, stateReference, **msgInfo): + if self.__stateReferenceIndex.has_key(stateReference): + raise error.ProtocolError( + 'Cache dup for stateReference=%s at %s' % + (stateReference, self) + ) + expireAt = self.__expirationTimer+50 + self.__stateReferenceIndex[stateReference] = ( msgInfo, expireAt ) + + # Schedule to expire + if not self.__expirationQueue.has_key(expireAt): + self.__expirationQueue[expireAt] = {} + if not self.__expirationQueue[expireAt].has_key('stateReference'): + self.__expirationQueue[expireAt]['stateReference'] = {} + self.__expirationQueue[expireAt]['stateReference'][stateReference] = 1 + self.__expireCaches() + + def _cachePopByStateRef(self, stateReference): + cacheInfo = self.__stateReferenceIndex.get(stateReference) + if cacheInfo is None: + raise error.ProtocolError( + 'Cache miss for stateReference=%s at %s' % + (stateReference, self) + ) + del self.__stateReferenceIndex[stateReference] + cacheEntry, expireAt = cacheInfo + del self.__expirationQueue[expireAt]['stateReference'][stateReference] + return cacheEntry + + # Client mode cache handling + + def _newMsgID(self): + AbstractMessageProcessingModel.__msgID = ( + AbstractMessageProcessingModel.__msgID + 1 + ) + return self.__msgID + + def _cachePushByMsgId(self, msgId, **msgInfo): + if self.__msgIdIndex.has_key(msgId): + raise error.ProtocolError( + 'Cache dup for msgId=%s at %s' % (msgId, self) + ) + expireAt = self.__expirationTimer+50 + self.__msgIdIndex[msgId] = ( msgInfo, expireAt ) + + # Schedule to expire + if not self.__expirationQueue.has_key(expireAt): + self.__expirationQueue[expireAt] = {} + if not self.__expirationQueue[expireAt].has_key('msgId'): + self.__expirationQueue[expireAt]['msgId'] = {} + self.__expirationQueue[expireAt]['msgId'][msgId] = 1 + self.__expireCaches() + + def _cachePopByMsgId(self, msgId): + cacheInfo = self.__msgIdIndex.get(msgId) + if cacheInfo is None: + raise error.ProtocolError( + 'Cache miss for msgId=%s at %s' % (msgId, self) + ) + del self.__msgIdIndex[msgId] + cacheEntry, expireAt = cacheInfo + del self.__expirationQueue[expireAt]['msgId'][msgId] + return cacheEntry + + def __expireCaches(self): + # Uses internal clock to expire pending messages + if self.__expirationQueue.has_key(self.__expirationTimer): + cacheInfo = self.__expirationQueue[self.__expirationTimer] + if cacheInfo.has_key('stateReference'): + for stateReference in cacheInfo['stateReference'].keys(): + del self.__stateReferenceIndex[stateReference] + if cacheInfo.has_key('msgId'): + for msgId in cacheInfo['msgId'].keys(): + del self.__msgIdIndex[msgId] + del self.__expirationQueue[self.__expirationTimer] + self.__expirationTimer = self.__expirationTimer + 1 |