summaryrefslogtreecommitdiff
path: root/pysnmp/carrier/base.py
blob: 518f304b861b61443c2e15f34f8b0848cdd4ad87 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
from pysnmp.carrier import error


class TimerCallable(object):
    def __init__(self, cbFun, callInterval):
        self.__cbFun = cbFun
        self.__callInterval = callInterval
        self.__nextCall = 0

    def __call__(self, timeNow):
        if self.__nextCall <= timeNow:
            self.__cbFun(timeNow)
            self.__nextCall = timeNow + self.__callInterval

    def __eq__(self, cbFun):
        return self.__cbFun == cbFun

    def __ne__(self, cbFun):
        return self.__cbFun != cbFun

    def __lt__(self, cbFun):
        return self.__cbFun < cbFun

    def __le__(self, cbFun):
        return self.__cbFun <= cbFun

    def __gt__(self, cbFun):
        return self.__cbFun > cbFun

    def __ge__(self, cbFun):
        return self.__cbFun >= cbFun


class AbstractTransportDispatcher(object):
    def __init__(self):
        self.__transports = {}
        self.__transportDomainMap = {}
        self.__jobs = {}
        self.__recvCallables = {}
        self.__timerCallables = []
        self.__ticks = 0
        self.__timerResolution = 0.1
        self.__timerDelta = self.__timerResolution * 0.05
        self.__nextTime = 0
        self.__routingCbFun = None

    def _cbFun(self, incomingTransport, transportAddress, incomingMessage):
        if incomingTransport in self.__transportDomainMap:
            transportDomain = self.__transportDomainMap[incomingTransport]

        else:
            raise error.CarrierError(
                'Unregistered transport %s' % (incomingTransport,)
            )

        if self.__routingCbFun:
            recvId = self.__routingCbFun(
                transportDomain, transportAddress, incomingMessage
            )

        else:
            recvId = None

        if recvId in self.__recvCallables:
            self.__recvCallables[recvId](
                self, transportDomain, transportAddress, incomingMessage
            )

        else:
            raise error.CarrierError(
                'No callback for "%r" found - loosing incoming event' % (recvId,)
            )

    # Dispatcher API

    def registerRoutingCbFun(self, routingCbFun):
        if self.__routingCbFun:
            raise error.CarrierError(
                'Data routing callback already registered'
            )

        self.__routingCbFun = routingCbFun

    def unregisterRoutingCbFun(self):
        if self.__routingCbFun:
            self.__routingCbFun = None

    def registerRecvCbFun(self, recvCb, recvId=None):
        if recvId in self.__recvCallables:
            raise error.CarrierError(
                'Receive callback %r already registered' % (recvId is None and '<default>' or recvId,)
            )

        self.__recvCallables[recvId] = recvCb

    def unregisterRecvCbFun(self, recvId=None):
        if recvId in self.__recvCallables:
            del self.__recvCallables[recvId]

    def registerTimerCbFun(self, timerCbFun, tickInterval=None):
        if not tickInterval:
            tickInterval = self.__timerResolution

        self.__timerCallables.append(TimerCallable(timerCbFun, tickInterval))

    def unregisterTimerCbFun(self, timerCbFun=None):
        if timerCbFun:
            self.__timerCallables.remove(timerCbFun)

        else:
            self.__timerCallables = []

    def registerTransport(self, transportDomain, transport):
        if transportDomain in self.__transports:
            raise error.CarrierError(
                'Transport %s already registered' % (transportDomain,)
            )

        transport.registerCbFun(self._cbFun)

        self.__transports[transportDomain] = transport
        self.__transportDomainMap[transport] = transportDomain

    def unregisterTransport(self, transportDomain):
        if transportDomain not in self.__transports:
            raise error.CarrierError(
                'Transport %s not registered' % (transportDomain,)
            )

        self.__transports[transportDomain].unregisterCbFun()

        del self.__transportDomainMap[self.__transports[transportDomain]]
        del self.__transports[transportDomain]

    def getTransport(self, transportDomain):
        if transportDomain in self.__transports:
            return self.__transports[transportDomain]

        raise error.CarrierError(
            'Transport %s not registered' % (transportDomain,))

    def sendMessage(self, outgoingMessage, transportDomain,
                    transportAddress):
        if transportDomain in self.__transports:
            self.__transports[transportDomain].sendMessage(
                outgoingMessage, transportAddress
            )

        else:
            raise error.CarrierError('No suitable transport domain for '
                                     '%s' % (transportDomain,))

    def getTimerResolution(self):
        return self.__timerResolution

    def setTimerResolution(self, timerResolution):
        if timerResolution < 0.01 or timerResolution > 10:
            raise error.CarrierError('Impossible timer resolution')

        self.__timerResolution = timerResolution
        self.__timerDelta = timerResolution * 0.05

    def getTimerTicks(self):
        return self.__ticks

    def handleTimerTick(self, timeNow):
        if self.__nextTime == 0:  # initial initialization
            self.__nextTime = timeNow + self.__timerResolution - self.__timerDelta

        if self.__nextTime >= timeNow:
            return

        self.__ticks += 1
        self.__nextTime = timeNow + self.__timerResolution - self.__timerDelta

        for timerCallable in self.__timerCallables:
            timerCallable(timeNow)

    def jobStarted(self, jobId, count=1):
        if jobId in self.__jobs:
            self.__jobs[jobId] += count

        else:
            self.__jobs[jobId] = count

    def jobFinished(self, jobId, count=1):
        self.__jobs[jobId] -= count
        if self.__jobs[jobId] == 0:
            del self.__jobs[jobId]

    def jobsArePending(self):
        return bool(self.__jobs)

    def runDispatcher(self, timeout=0.0):
        raise error.CarrierError('Method not implemented')

    def closeDispatcher(self):
        for tDomain in list(self.__transports):
            self.__transports[tDomain].closeTransport()
            self.unregisterTransport(tDomain)

        self.__transports.clear()
        self.unregisterRecvCbFun()
        self.unregisterTimerCbFun()


class AbstractTransportAddress(object):
    _localAddress = None

    def setLocalAddress(self, s):
        self._localAddress = s
        return self

    def getLocalAddress(self):
        return self._localAddress

    def clone(self, localAddress=None):
        return self.__class__(self).setLocalAddress(
            localAddress is None and self.getLocalAddress() or localAddress)


class AbstractTransport(object):
    PROTO_TRANSPORT_DISPATCHER = None
    ADDRESS_TYPE = AbstractTransportAddress
    _cbFun = None

    @classmethod
    def isCompatibleWithDispatcher(cls, transportDispatcher):
        return isinstance(transportDispatcher, cls.PROTO_TRANSPORT_DISPATCHER)

    def registerCbFun(self, cbFun):
        if self._cbFun:
            raise error.CarrierError('Callback function %s already registered '
                                     'at %s' % (self._cbFun, self))
        self._cbFun = cbFun

    def unregisterCbFun(self):
        self._cbFun = None

    def closeTransport(self):
        self.unregisterCbFun()

    # Public API

    def openClientMode(self, iface=None):
        raise error.CarrierError('Method not implemented')

    def openServerMode(self, iface):
        raise error.CarrierError('Method not implemented')

    def sendMessage(self, outgoingMessage, transportAddress):
        raise error.CarrierError('Method not implemented')