summaryrefslogtreecommitdiff
path: root/pysnmp/carrier/asyncore/dispatch.py
blob: ae6025499b0e5101c4a36c4166a794fe4800b701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2016, Ilya Etingof <ilya@glas.net>
# License: http://pysnmp.sf.net/license.html
#
from time import time
from sys import exc_info
from traceback import format_exception
from asyncore import socket_map
from asyncore import loop
from pysnmp.carrier.base import AbstractTransportDispatcher
from pysnmp.error import PySnmpError

class AsyncoreDispatcher(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():
            try:
                loop(timeout and timeout or self.timeout,
                     use_poll=True, map=self.__sockMap, count=1)
            except KeyboardInterrupt:
                raise
            except:
                raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info())))
            self.handleTimerTick(time())