# Path: pysnmp/carrier/asynsock/dispatch.py
# class AsynsockDispatcher(AbstractTransportDispatcher):
#     def __init__(self):
#         self.__sockMap = {} # use own map for MT safety
#         self.timeout = 0.5
#         AbstractTransportDispatcher.__init__(self)
# 
#     def getSocketMap(self): return self.__sockMap
#     def setSocketMap(self, sockMap=socket_map): self.__sockMap = sockMap
#     
#     def registerTransport(self, tDomain, t):
#         AbstractTransportDispatcher.registerTransport(self, tDomain, t)
#         t.registerSocket(self.__sockMap)
# 
#     def unregisterTransport(self, tDomain):
#         self.getTransport(tDomain).unregisterSocket(self.__sockMap)
#         AbstractTransportDispatcher.unregisterTransport(self, tDomain)
# 
#     def transportsAreWorking(self):
#         for transport in self.__sockMap.values():
#             if transport.writable():
#                 return 1
#         return 0
#     
#     def runDispatcher(self, timeout=0.0):
#         while self.jobsArePending() or self.transportsAreWorking():
#             poll(timeout and timeout or self.timeout, self.__sockMap)
#             self.handleTimerTick(time())
# 
# Path: pysnmp/carrier/asynsock/dgram/udp.py
# class UdpSocketTransport(DgramSocketTransport):


from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp
from pyasn1.codec.ber import encoder, decoder
from pysnmp.proto import api
from time import time
startedAt = time()

def cbTimerFun(timeNow):
    if timeNow - startedAt > 3:
        raise Exception("Request timed out")
    
def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
              wholeMsg, reqPDU=reqPDU):
    while wholeMsg:
        rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=pMod.Message())
        rspPDU = pMod.apiMessage.getPDU(rspMsg)
        # Match response to request
        if pMod.apiPDU.getRequestID(reqPDU)==pMod.apiPDU.getRequestID(rspPDU):
            # Check for SNMP errors reported
            errorStatus = pMod.apiPDU.getErrorStatus(rspPDU)
            if errorStatus:
                print(errorStatus.prettyPrint())
            else:
                for oid, val in pMod.apiPDU.getVarBinds(rspPDU):
                    print('%s = %s' (oid.prettyPrint(), val.prettyPrint()))
            transportDispatcher.jobFinished(1)
    return wholeMsg

transportDispatcher = AsynsockDispatcher()

transportDispatcher.registerRecvCbFun(cbRecvFun)
transportDispatcher.registerTimerCbFun(cbTimerFun)

# UDP/IPv4
transportDispatcher.registerTransport(

# Path: pysnmp/carrier/asynsock/dispatch.py
# class AsynsockDispatcher(AbstractTransportDispatcher):
#     def __init__(self):
#         self.__sockMap = {} # use own map for MT safety
#         self.timeout = 0.5
#         AbstractTransportDispatcher.__init__(self)
# 
#     def getSocketMap(self): return self.__sockMap
#     def setSocketMap(self, sockMap=socket_map): self.__sockMap = sockMap
#     
#     def registerTransport(self, tDomain, t):
#         AbstractTransportDispatcher.registerTransport(self, tDomain, t)
#         t.registerSocket(self.__sockMap)
# 
#     def unregisterTransport(self, tDomain):
#         self.getTransport(tDomain).unregisterSocket(self.__sockMap)
#         AbstractTransportDispatcher.unregisterTransport(self, tDomain)
# 
#     def transportsAreWorking(self):
#         for transport in self.__sockMap.values():
#             if transport.writable():
#                 return 1
#         return 0
#     
#     def runDispatcher(self, timeout=0.0):
#         while self.jobsArePending() or self.transportsAreWorking():
#             poll(timeout and timeout or self.timeout, self.__sockMap)
#             self.handleTimerTick(time())
# 
# Path: pysnmp/carrier/asynsock/dgram/udp.py
# class UdpSocketTransport(DgramSocketTransport):


# Path: examples/v1arch/manager/setgen.py
from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp
from pyasn1.codec.ber import encoder, decoder
from pysnmp.proto import api
from time import time

startedAt = time()

def cbTimerFun(timeNow):
    if timeNow - startedAt > 3:
        raise Exception("Request timed out")
    
def cbRecvFun(transportDispatcher, transportDomain, transportAddress,
              wholeMsg, reqPDU=reqPDU):
    while wholeMsg:
        rspMsg, wholeMsg = decoder.decode(wholeMsg, asn1Spec=pMod.Message())
        rspPDU = pMod.apiMessage.getPDU(rspMsg)
        # Match response to request
        if pMod.apiPDU.getRequestID(reqPDU)==pMod.apiPDU.getRequestID(rspPDU):
            # Check for SNMP errors reported
            errorStatus = pMod.apiPDU.getErrorStatus(rspPDU)
            if errorStatus:
                print(errorStatus.prettyPrint())
            else:
                for oid, val in pMod.apiPDU.getVarBinds(rspPDU):
                    print('%s = %s' (oid.prettyPrint(), val.prettyPrint()))
            transportDispatcher.jobFinished(1)
    return wholeMsg

transportDispatcher = AsynsockDispatcher()

transportDispatcher.registerRecvCbFun(cbRecvFun)
transportDispatcher.registerTimerCbFun(cbTimerFun)

# UDP/IPv4
transportDispatcher.registerTransport(

    udp.domainName, udp.UdpSocketTransport().openClientMode()