aboutsummaryrefslogtreecommitdiff
path: root/diplomacy/daide/server.py
blob: ceca1225050e33113b7a03675a4c510b7c6b0059 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ==============================================================================
# Copyright (C) 2019 - Philip Paquette
#
#  This program is free software: you can redistribute it and/or modify it under
#  the terms of the GNU Affero General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
#  details.
#
#  You should have received a copy of the GNU Affero General Public License along
#  with this program.  If not, see <https://www.gnu.org/licenses/>.
# ==============================================================================
""" Parallel server to receive DAIDE communications """
import logging
from tornado import gen
from tornado.iostream import StreamClosedError
from tornado.tcpserver import TCPServer
from diplomacy.daide.connection_handler import ConnectionHandler

# Constants
LOGGER = logging.getLogger(__name__)

class Server(TCPServer):
    """ Represents a server to receive DAIDE communications """
    def __init__(self, master_server, game_id):
        """ Contructor
            :param master_server: the internal server
            :param game_id: the game id for which this server will receive communications
        """
        super(Server, self).__init__()
        self._master_server = master_server
        self._game_id = game_id
        self._registered_connections = {}

    @property
    def master_server(self):
        """ Return the master server """
        return self._master_server

    @property
    def game_id(self):
        """ Return the game id associated with the server """
        return self._game_id

    def stop(self):
        """ Stop the server and close all connections """
        for connection_handler in self._registered_connections.values():
            connection_handler.close_connection()
        super(Server, self).stop()

    @gen.coroutine
    def handle_stream(self, stream, address):
        """ Handle an open stream
            :param stream: the stream to handle
            :param address: the address of the client
        """
        LOGGER.info('Connection from client [%s]', str(address))

        handler = ConnectionHandler()
        handler.initialize(stream, self._master_server, self._game_id)
        self._registered_connections[stream] = handler

        try:
            while not handler.stream.closed():
                yield handler.read_stream()
        except StreamClosedError:
            LOGGER.error('[%s] disconnected', str(address))

        del self._registered_connections[stream]