2019-12-19 12:24:53 +01:00
|
|
|
"""
|
|
|
|
Process data incoming from network
|
|
|
|
"""
|
2017-07-21 07:47:18 +02:00
|
|
|
import errno
|
2017-05-25 23:04:33 +02:00
|
|
|
import Queue
|
2017-07-21 07:47:18 +02:00
|
|
|
import socket
|
2017-05-25 23:04:33 +02:00
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
import state
|
2018-02-19 21:27:38 +01:00
|
|
|
from network.advanceddispatcher import UnknownStateError
|
2020-01-24 15:16:05 +01:00
|
|
|
from network.connectionpool import BMConnectionPool
|
2017-07-06 19:45:36 +02:00
|
|
|
from queues import receiveDataQueue
|
2019-08-06 13:04:33 +02:00
|
|
|
from threads import StoppableThread
|
2017-05-25 23:04:33 +02:00
|
|
|
|
2019-08-01 13:37:26 +02:00
|
|
|
|
|
|
|
class ReceiveQueueThread(StoppableThread):
|
2019-12-19 12:24:53 +01:00
|
|
|
"""This thread processes data received from the network
|
|
|
|
(which is done by the asyncore thread)"""
|
2017-07-10 07:08:10 +02:00
|
|
|
def __init__(self, num=0):
|
2019-08-01 13:37:26 +02:00
|
|
|
super(ReceiveQueueThread, self).__init__(name="ReceiveQueue_%i" % num)
|
2017-05-25 23:04:33 +02:00
|
|
|
|
|
|
|
def run(self):
|
2017-05-29 14:35:08 +02:00
|
|
|
while not self._stopped and state.shutdown == 0:
|
2017-07-06 19:45:36 +02:00
|
|
|
try:
|
2017-07-08 06:54:25 +02:00
|
|
|
dest = receiveDataQueue.get(block=True, timeout=1)
|
2017-07-06 19:45:36 +02:00
|
|
|
except Queue.Empty:
|
|
|
|
continue
|
|
|
|
|
2017-10-19 09:11:34 +02:00
|
|
|
if self._stopped or state.shutdown:
|
2017-07-06 19:45:36 +02:00
|
|
|
break
|
2017-07-08 06:54:25 +02:00
|
|
|
|
2017-07-06 19:45:36 +02:00
|
|
|
# cycle as long as there is data
|
2019-08-06 13:04:33 +02:00
|
|
|
# methods should return False if there isn't enough data,
|
2017-07-08 07:33:29 +02:00
|
|
|
# or the connection is to be aborted
|
2017-07-08 06:54:25 +02:00
|
|
|
|
2019-08-06 13:04:33 +02:00
|
|
|
# state_* methods should return False if there isn't
|
|
|
|
# enough data, or the connection is to be aborted
|
|
|
|
|
2017-07-08 06:54:25 +02:00
|
|
|
try:
|
2018-02-19 21:27:38 +01:00
|
|
|
connection = BMConnectionPool().getConnectionByAddr(dest)
|
2019-12-19 12:24:53 +01:00
|
|
|
# connection object not found
|
|
|
|
except KeyError:
|
2018-02-19 21:27:38 +01:00
|
|
|
receiveDataQueue.task_done()
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
connection.process()
|
2019-12-19 12:24:53 +01:00
|
|
|
# state isn't implemented
|
|
|
|
except UnknownStateError:
|
2017-07-08 06:54:25 +02:00
|
|
|
pass
|
2017-07-21 07:47:18 +02:00
|
|
|
except socket.error as err:
|
|
|
|
if err.errno == errno.EBADF:
|
2018-02-19 21:27:38 +01:00
|
|
|
connection.set_state("close", 0)
|
2017-07-21 07:47:18 +02:00
|
|
|
else:
|
2019-08-06 13:04:33 +02:00
|
|
|
self.logger.error('Socket error: %s', err)
|
2018-02-19 21:27:38 +01:00
|
|
|
except:
|
2019-08-06 13:04:33 +02:00
|
|
|
self.logger.error('Error processing', exc_info=True)
|
2017-07-10 07:08:10 +02:00
|
|
|
receiveDataQueue.task_done()
|