summaryrefslogtreecommitdiff
path: root/pysnmp/proto/mpmod/cache.py
diff options
context:
space:
mode:
Diffstat (limited to 'pysnmp/proto/mpmod/cache.py')
-rw-r--r--pysnmp/proto/mpmod/cache.py36
1 files changed, 30 insertions, 6 deletions
diff --git a/pysnmp/proto/mpmod/cache.py b/pysnmp/proto/mpmod/cache.py
index 0cb343bd..265355a4 100644
--- a/pysnmp/proto/mpmod/cache.py
+++ b/pysnmp/proto/mpmod/cache.py
@@ -27,25 +27,37 @@ class Cache(object):
def pushByStateRef(self, stateReference, **msgInfo):
if stateReference in self.__stateReferenceIndex:
- raise error.ProtocolError('Cache dup for stateReference=%s at %s' % (stateReference, self))
+ raise error.ProtocolError(
+ 'Cache dup for stateReference=%s at %s' % (stateReference, self))
+
expireAt = self.__expirationTimer + 600
+
self.__stateReferenceIndex[stateReference] = msgInfo, expireAt
# Schedule to expire
if expireAt not in self.__expirationQueue:
self.__expirationQueue[expireAt] = {}
+
if 'stateReference' not in self.__expirationQueue[expireAt]:
self.__expirationQueue[expireAt]['stateReference'] = {}
+
self.__expirationQueue[expireAt]['stateReference'][stateReference] = 1
def popByStateRef(self, stateReference):
if stateReference in self.__stateReferenceIndex:
cacheInfo = self.__stateReferenceIndex[stateReference]
+
else:
- raise error.ProtocolError('Cache miss for stateReference=%s at %s' % (stateReference, self))
+ 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
@@ -56,9 +68,10 @@ class Cache(object):
def pushByMsgId(self, msgId, **msgInfo):
if msgId in self.__msgIdIndex:
raise error.ProtocolError(
- 'Cache dup for msgId=%s at %s' % (msgId, self)
- )
+ 'Cache dup for msgId=%s at %s' % (msgId, self))
+
expireAt = self.__expirationTimer + 600
+
self.__msgIdIndex[msgId] = msgInfo, expireAt
self.__sendPduHandleIdx[msgInfo['sendPduHandle']] = msgId
@@ -66,22 +79,29 @@ class Cache(object):
# Schedule to expire
if expireAt not in self.__expirationQueue:
self.__expirationQueue[expireAt] = {}
+
if 'msgId' not in self.__expirationQueue[expireAt]:
self.__expirationQueue[expireAt]['msgId'] = {}
+
self.__expirationQueue[expireAt]['msgId'][msgId] = 1
def popByMsgId(self, msgId):
if msgId in self.__msgIdIndex:
cacheInfo = self.__msgIdIndex[msgId]
+
else:
raise error.ProtocolError(
- 'Cache miss for msgId=%s at %s' % (msgId, self)
- )
+ 'Cache miss for msgId=%s at %s' % (msgId, self))
+
msgInfo, expireAt = cacheInfo
+
del self.__sendPduHandleIdx[msgInfo['sendPduHandle']]
del self.__msgIdIndex[msgId]
+
cacheEntry, expireAt = cacheInfo
+
del self.__expirationQueue[expireAt]['msgId'][msgId]
+
return cacheEntry
def popBySendPduHandle(self, sendPduHandle):
@@ -92,11 +112,15 @@ class Cache(object):
# Uses internal clock to expire pending messages
if self.__expirationTimer in self.__expirationQueue:
cacheInfo = self.__expirationQueue[self.__expirationTimer]
+
if 'stateReference' in cacheInfo:
for stateReference in cacheInfo['stateReference']:
del self.__stateReferenceIndex[stateReference]
+
if 'msgId' in cacheInfo:
for msgId in cacheInfo['msgId']:
del self.__msgIdIndex[msgId]
+
del self.__expirationQueue[self.__expirationTimer]
+
self.__expirationTimer += 1