Revision 3660fcf5 lib/daemon.py
b/lib/daemon.py | ||
---|---|---|
92 | 92 |
# differ and treat all messages equally. |
93 | 93 |
pass |
94 | 94 |
|
95 |
def do_read(self): |
|
95 |
# this method is overriding an asyncore.dispatcher method |
|
96 |
def handle_read(self): |
|
96 | 97 |
try: |
97 | 98 |
payload, address = self.recvfrom(constants.MAX_UDP_DATA_SIZE) |
98 | 99 |
except socket.error, err: |
... | ... | |
105 | 106 |
ip, port = address |
106 | 107 |
self.handle_datagram(payload, ip, port) |
107 | 108 |
|
108 |
# this method is overriding an asyncore.dispatcher method |
|
109 |
def handle_read(self): |
|
110 |
try: |
|
111 |
self.do_read() |
|
112 |
except: # pylint: disable-msg=W0702 |
|
113 |
# we need to catch any exception here, log it, but proceed, because even |
|
114 |
# if we failed handling a single request, we still want to continue. |
|
115 |
logging.error("Unexpected exception", exc_info=True) |
|
116 |
|
|
117 | 109 |
def handle_datagram(self, payload, ip, port): |
118 | 110 |
"""Handle an already read udp datagram |
119 | 111 |
|
... | ... | |
128 | 120 |
|
129 | 121 |
# this method is overriding an asyncore.dispatcher method |
130 | 122 |
def handle_write(self): |
123 |
if not self._out_queue: |
|
124 |
logging.error("handle_write called with empty output queue") |
|
125 |
return |
|
126 |
(ip, port, payload) = self._out_queue[0] |
|
131 | 127 |
try: |
132 |
if not self._out_queue: |
|
133 |
logging.error("handle_write called with empty output queue") |
|
128 |
self.sendto(payload, 0, (ip, port)) |
|
129 |
except socket.error, err: |
|
130 |
if err.errno == errno.EINTR: |
|
131 |
# we got a signal while trying to write. no need to do anything, |
|
132 |
# handle_write will be called again because we haven't emptied the |
|
133 |
# _out_queue, and we'll try again |
|
134 | 134 |
return |
135 |
(ip, port, payload) = self._out_queue[0] |
|
136 |
try: |
|
137 |
self.sendto(payload, 0, (ip, port)) |
|
138 |
except socket.error, err: |
|
139 |
if err.errno == errno.EINTR: |
|
140 |
# we got a signal while trying to write. no need to do anything, |
|
141 |
# handle_write will be called again because we haven't emptied the |
|
142 |
# _out_queue, and we'll try again |
|
143 |
return |
|
144 |
else: |
|
145 |
raise |
|
146 |
self._out_queue.pop(0) |
|
147 |
except: # pylint: disable-msg=W0702 |
|
148 |
# we need to catch any exception here, log it, but proceed, because even |
|
149 |
# if we failed sending a single datagram we still want to continue. |
|
150 |
logging.error("Unexpected exception", exc_info=True) |
|
135 |
else: |
|
136 |
raise |
|
137 |
self._out_queue.pop(0) |
|
138 |
|
|
139 |
# this method is overriding an asyncore.dispatcher method |
|
140 |
def handle_error(self): |
|
141 |
"""Log an error in handling any request, and proceed. |
|
142 |
|
|
143 |
""" |
|
144 |
logging.exception("Error while handling asyncore request") |
|
151 | 145 |
|
152 | 146 |
def enqueue_send(self, ip, port, payload): |
153 | 147 |
"""Enqueue a datagram to be sent when possible |
... | ... | |
169 | 163 |
""" |
170 | 164 |
result = utils.WaitForFdCondition(self, select.POLLIN, timeout) |
171 | 165 |
if result is not None and result & select.POLLIN: |
172 |
self.do_read()
|
|
166 |
self.handle_read()
|
|
173 | 167 |
return True |
174 | 168 |
else: |
175 | 169 |
return False |
Also available in: Unified diff