2017-07-21 05:47:18 +00:00
|
|
|
import errno
|
2017-05-25 21:04:33 +00:00
|
|
|
import Queue
|
2017-07-21 05:47:18 +00:00
|
|
|
import socket
|
2017-06-21 10:16:33 +00:00
|
|
|
import sys
|
2017-05-25 21:04:33 +00:00
|
|
|
import threading
|
2017-05-27 17:09:21 +00:00
|
|
|
import time
|
2017-05-25 21:04:33 +00:00
|
|
|
|
2017-05-27 17:09:21 +00:00
|
|
|
import addresses
|
2017-05-25 21:04:33 +00:00
|
|
|
from bmconfigparser import BMConfigParser
|
|
|
|
from debug import logger
|
|
|
|
from helper_threading import StoppableThread
|
|
|
|
from inventory import Inventory
|
|
|
|
from network.connectionpool import BMConnectionPool
|
2017-05-27 17:09:21 +00:00
|
|
|
from network.bmproto import BMProto
|
2018-02-19 20:27:38 +00:00
|
|
|
from network.advanceddispatcher import UnknownStateError
|
2017-07-06 17:45:36 +00:00
|
|
|
from queues import receiveDataQueue
|
2017-05-25 21:04:33 +00:00
|
|
|
import protocol
|
2017-05-29 12:35:08 +00:00
|
|
|
import state
|
2017-05-25 21:04:33 +00:00
|
|
|
|
|
|
|
class ReceiveQueueThread(threading.Thread, StoppableThread):
|
2017-07-10 05:08:10 +00:00
|
|
|
def __init__(self, num=0):
|
|
|
|
threading.Thread.__init__(self, name="ReceiveQueue_%i" %(num))
|
2017-05-25 21:04:33 +00:00
|
|
|
self.initStop()
|
2017-07-10 05:08:10 +00:00
|
|
|
self.name = "ReceiveQueue_%i" % (num)
|
|
|
|
logger.info("init receive queue thread %i", num)
|
2017-05-25 21:04:33 +00:00
|
|
|
|
|
|
|
def run(self):
|
2017-05-29 12:35:08 +00:00
|
|
|
while not self._stopped and state.shutdown == 0:
|
2017-07-06 17:45:36 +00:00
|
|
|
try:
|
2017-07-08 04:54:25 +00:00
|
|
|
dest = receiveDataQueue.get(block=True, timeout=1)
|
2017-07-06 17:45:36 +00:00
|
|
|
except Queue.Empty:
|
|
|
|
continue
|
|
|
|
|
2017-10-19 07:11:34 +00:00
|
|
|
if self._stopped or state.shutdown:
|
2017-07-06 17:45:36 +00:00
|
|
|
break
|
2017-07-08 04:54:25 +00:00
|
|
|
|
2017-07-06 17:45:36 +00:00
|
|
|
# cycle as long as there is data
|
|
|
|
# methods should return False if there isn't enough data, or the connection is to be aborted
|
2017-07-08 04:54:25 +00:00
|
|
|
|
2017-07-08 05:33:29 +00:00
|
|
|
# state_* methods should return False if there isn't enough data,
|
|
|
|
# or the connection is to be aborted
|
2017-07-08 04:54:25 +00:00
|
|
|
|
|
|
|
try:
|
2018-02-19 20:27:38 +00:00
|
|
|
connection = BMConnectionPool().getConnectionByAddr(dest)
|
2017-07-08 16:02:47 +00:00
|
|
|
# KeyError = connection object not found
|
2018-02-19 20:27:38 +00:00
|
|
|
except KeyError:
|
|
|
|
receiveDataQueue.task_done()
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
connection.process()
|
|
|
|
# UnknownStateError = state isn't implemented
|
|
|
|
except (UnknownStateError):
|
2017-07-08 04:54:25 +00:00
|
|
|
pass
|
2017-07-21 05:47:18 +00:00
|
|
|
except socket.error as err:
|
|
|
|
if err.errno == errno.EBADF:
|
2018-02-19 20:27:38 +00:00
|
|
|
connection.set_state("close", 0)
|
2017-07-21 05:47:18 +00:00
|
|
|
else:
|
|
|
|
logger.error("Socket error: %s", str(err))
|
2018-02-19 20:27:38 +00:00
|
|
|
except:
|
|
|
|
logger.error("Error processing", exc_info=True)
|
2017-07-10 05:08:10 +00:00
|
|
|
receiveDataQueue.task_done()
|