base.py :  » Network » Python-SNMP » pysnmp-4.1.13a » pysnmp » proto » mpmod » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Python SNMP 
Python SNMP » pysnmp 4.1.13a » pysnmp » proto » mpmod » base.py
# MP-specific cache management
from pysnmp.proto import error

class AbstractMessageProcessingModel:
    messageProcessingModelID = None
    __stateReference = __msgID = 0L
    def __init__(self):
        self.__msgIdIndex = {}
        self.__stateReferenceIndex = {}
        self.__sendPduHandleIdx = {}
        # Message expiration mechanics
        self.__expirationQueue = {}
        self.__expirationTimer = 0L
    
    def prepareOutgoingMessage(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        pdu,
        expectResponse,
        sendPduHandle
        ):
        raise error.ProtocolError('method not implemented')
        
    def prepareResponseMessage(
        self,
        snmpEngine,
        messageProcessingModel,
        securityModel,
        securityName,
        securityLevel,
        contextEngineId,
        contextName,
        pduVersion,
        pdu,
        maxSizeResponseScopedPDU,
        stateReference,
        statusInformation
        ):
        raise error.ProtocolError('method not implemented')

    def prepareDataElements(
        self,
        snmpEngine,
        transportDomain,
        transportAddress,
        wholeMsg
        ):
        raise error.ProtocolError('method not implemented')

    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 )

        self.__sendPduHandleIdx[msgInfo['sendPduHandle']] = msgId
        
        # 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)
                )
        msgInfo, expireAt = cacheInfo
        del self.__sendPduHandleIdx[msgInfo['sendPduHandle']]
        del self.__msgIdIndex[msgId]
        cacheEntry, expireAt = cacheInfo
        del self.__expirationQueue[expireAt]['msgId'][msgId]
        return cacheEntry

    def _cachePopBySendPduHandle(self, sendPduHandle):
        if self.__sendPduHandleIdx.has_key(sendPduHandle):
            self._cachePopByMsgId(self.__sendPduHandleIdx[sendPduHandle])
        
    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

    def releaseStateInformation(self, sendPduHandle):
        try:
            self._cachePopBySendPduHandle(sendPduHandle)
        except error.ProtocolError:
            pass # XXX maybe these should all follow some scheme?
    
    def receiveTimerTick(self, snmpEngine, timeNow):
        pass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.