Example #1
0
 def tworld_write(self, connid, msg):
     """Shortcut for writing to the tworld process. May raise exceptions.
     """
     if not self.tworldavailable:
         raise Exception('Tworld service is not available.')
     if type(msg) is dict:
         val = wcproto.message(connid, msg)
     else:
         val = wcproto.message(connid, msg, alreadyjson=True)
     self.tworld.write(val)
Example #2
0
    def monitor_tworld_status(self):
        """Check the status of the Tworld connection. If the socket is
        closed (or has never been opened), try to open it.

        This is called once when the app launches, to open the initial
        connection, and every few seconds thereafter.

        The tworldtimerbusy flag protects us from really slow connection
        attempts.
        
        This routine is *not* a coroutine, because it doesn't do anything
        yieldy. Instead, it has an old-fashioned (ugly) callback structure.
        """
        
        if (self.tworldtimerbusy):
            self.log.warning('monitor_tworld_status: already in flight; did a previous call jam?')
            return

        if (self.tworldavailable):
            # Nothing to do
            return

        # We're going to hold this "lock" until the connection attempt
        # fails or definitely succeeds.
        self.tworldtimerbusy = True

        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
            # We do a sync connect, because I don't understand how the
            # async version works. (IOStream.connect seems to hang forever
            # when the other process is down?)
            sock.connect(('localhost', self.app.twopts.tworld_port))
            sock.setblocking(0)
            tornado.platform.auto.set_close_exec(sock.fileno())
            self.tworld = tornado.iostream.IOStream(sock)
            self.twbuffer = bytearray()
        except Exception as ex:
            self.log.error('Could not open tworld socket: %s', ex)
            self.tworldavailable = False
            self.tworldtimerbusy = False
            return
            
        self.log.info('Tworld socket open')

        # But it won't count as available until we get a response from it.
        try:
            arr = []
            for (connid, conn) in self.app.twconntable.as_dict().items():
                arr.append( { 'connid':connid, 'uid':str(conn.uid), 'email':conn.email } )
            self.tworld.write(wcproto.message(0, {'cmd':'connect', 'connections':arr}))
        except Exception as ex:
            self.log.error('Could not write connect message to tworld socket: %s', ex)
            self.tworld = None
            self.twbuffer = None
            self.tworldavailable = False
            self.tworldtimerbusy = False
            return
        
        self.tworld.read_until_close(self.close_tworld, self.read_tworld_data)
Example #3
0
 def write(self, msg):
     """Shortcut to send a message to a player via this connection.
     """
     try:
         self.stream.write(wcproto.message(self.connid, msg))
         return True
     except Exception as ex:
         self.table.log.error('Unable to write to %d: %s', self.connid, ex)
         return False