backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / utils / evtlet.py
1 # Copyright (C) 2014 Nippon Telegraph and Telephone Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """
17  Concurrent networking library - Eventlet, based utilities classes.
18 """
19 from ryu.lib import hub
20 import logging
21
22 LOG = logging.getLogger('utils.evtlet')
23
24
25 class EventletIOFactory(object):
26
27     @staticmethod
28     def create_custom_event():
29         LOG.debug('Create CustomEvent called')
30         return hub.Event()
31
32     @staticmethod
33     def create_looping_call(funct, *args, **kwargs):
34         LOG.debug('create_looping_call called')
35         return LoopingCall(funct, *args, **kwargs)
36
37
38 # TODO: improve Timer service and move it into framework
39 class LoopingCall(object):
40     """Call a function repeatedly.
41     """
42
43     def __init__(self, funct, *args, **kwargs):
44         self._funct = funct
45         self._args = args
46         self._kwargs = kwargs
47         self._running = False
48         self._interval = 0
49         self._self_thread = None
50
51     @property
52     def running(self):
53         return self._running
54
55     @property
56     def interval(self):
57         return self._interval
58
59     def __call__(self):
60         if self._running:
61             # Schedule next iteration of the call.
62             self._self_thread = hub.spawn_after(self._interval, self)
63         self._funct(*self._args, **self._kwargs)
64
65     def start(self, interval, now=True):
66         """Start running pre-set function every interval seconds.
67         """
68         if interval < 0:
69             raise ValueError('interval must be >= 0')
70
71         if self._running:
72             self.stop()
73
74         self._running = True
75         self._interval = interval
76         if now:
77             self._self_thread = hub.spawn_after(0, self)
78         else:
79             self._self_thread = hub.spawn_after(self._interval, self)
80
81     def stop(self):
82         """Stop running scheduled function.
83         """
84         self._running = False
85         if self._self_thread is not None:
86             self._self_thread.cancel()
87             self._self_thread = None
88
89     def reset(self):
90         """Skip the next iteration and reset timer.
91         """
92         if self._self_thread is not None:
93             # Cancel currently scheduled call
94             self._self_thread.cancel()
95             self._self_thread = None
96         # Schedule a new call
97         self._self_thread = hub.spawn_after(self._interval, self)