summaryrefslogtreecommitdiff
path: root/pysnmp/carrier/twisted/dispatch.py
blob: 77f5c16804bccd194609efcd4cfdb8e2bcd6e665 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
# Copyright (C) 2008 Truelite Srl <info@truelite.it>
# Author: Filippo Giunchedi <filippo@truelite.it>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the BSD 2-Clause License as shipped with pysnmp.
#
# Description: Transport dispatcher based on twisted.internet.reactor
#
import sys
import time
import traceback

from twisted.internet import reactor
from twisted.internet import task

from pysnmp.carrier.base import AbstractTransportDispatcher
from pysnmp.error import PySnmpError


class TwistedDispatcher(AbstractTransportDispatcher):
    """TransportDispatcher based on twisted.internet.reactor"""

    def __init__(self, *args, **kwargs):
        AbstractTransportDispatcher.__init__(self)
        self.__transportCount = 0
        if 'timeout' in kwargs:
            self.setTimerResolution(kwargs['timeout'])
        self.loopingcall = task.LoopingCall(
            lambda self=self: self.handleTimerTick(time.time())
        )

    def runDispatcher(self, timeout=0.0):
        if not reactor.running:
            try:
                reactor.run()

            except KeyboardInterrupt:
                raise

            except Exception:
                raise PySnmpError('reactor error: %s' % ';'.join(traceback.format_exception(*sys.exc_info())))

    # jobstarted/jobfinished might be okay as-is

    def registerTransport(self, tDomain, transport):
        if not self.loopingcall.running and self.getTimerResolution() > 0:
            self.loopingcall.start(self.getTimerResolution(), now=False)
        AbstractTransportDispatcher.registerTransport(
            self, tDomain, transport
        )
        self.__transportCount += 1

    def unregisterTransport(self, tDomain):
        t = AbstractTransportDispatcher.getTransport(self, tDomain)
        if t is not None:
            AbstractTransportDispatcher.unregisterTransport(self, tDomain)
            self.__transportCount -= 1

        # The last transport has been removed, stop the timeout
        if self.__transportCount == 0 and self.loopingcall.running:
            self.loopingcall.stop()