Ejemplo n.º 1
0
def log_server(address):
    log = logging.getLogger(__name__)
    log.addHandler(SQLiteHandler())

    c = Channel()
    c.accept(address)

    while True:
        try:
            msg = c.recv()
            print('received', msg)
        except:
            print('reconnecting')
            c.reconnect()
            print('reconnected')
            continue

        if msg == b'':
            print('waiting')
            time.sleep(1)
            continue

        try:
            log.critical(msg)
        except DuplicateRecordError:
            c.send(b'nack')
            continue

        c.send(b'ack')
Ejemplo n.º 2
0
Archivo: node.py Proyecto: pylots/raft
class ServerQueue(Thread):
    def __init__(self, node, index, address):
        super().__init__()
        self.node = node
        self.index = index
        self.address = address
        self.queue = Queue()
        self.connections = {}

    def run(self):
        while True:
            logger.debug(f'{self.index}: Create new Accept channel')
            self.channel = Channel(self.index)
            self.queue.put(SystemMessage('Initialized'))
            running = True
            while running:
                try:
                    channel = self.channel.accept(self.address)
                except ChannelTimeout:
                    self.queue.put(TimeoutMessage(0))
                    continue
                except ChannelException as e:
                    logger.error(f'REINIT: ChannelException {e}')
                    self.queue.put(ExceptionMessage(e))
                    running = False
                    continue
                logger.debug(
                    f'{self.node.index}: Got a new channel: {channel}')
                sc = ServerChannel(self.node, self.index, self.queue, channel)
                self.connections[self.index] = sc
                sc.start()
                self.queue.put(
                    SystemMessage(f'NewClient connected to: {self.address}'))

    def receive(self, timeout):
        try:
            message = self.queue.get(timeout=timeout)
        except Empty:
            message = TimeoutMessage(timeout)
        logger.debug(
            f'{self.index}: Received message {message} from {message.msource}')
        return message

    def ack(self, message):
        self.channel.send(message)

    def send(self, message):
        self.channel.send(message)