Example #1
0
    def run(self):
        '''Fonction d'execution de serveur.'''

        while self.running:
            # suppression des sockets ferme ainsi que des socket inactifs
            for sock in self.inputs + self.outputs:
                if sock.closed():
                    self.disconnect(sock)
                elif not sock in self.always_alive:
                    if sock.time_alive() > self.keep_alive and self.keep_alive > 0:
                        self.disconnect(sock)

            # Obtention des sockets lisable/ecrivable
            try:
                readable, writable, exceptable = select.select(self.inputs, self.outputs, self.inputs, 0)
            except socket.error:
                    debug.wtf(self, 'Stop that shit dude')
            except:
                pass

            if self.running:
                for sock in readable:
                    self.read(sock)

            if self.running:
                for sock in writable:
                    self.write(sock)

            if self.running:
                for sock in exceptable:
                    self.exception(sock)

        self.stop()
Example #2
0
    def disconnect(self, sock):
        '''Ferme la connexion d'un socket et de tous les sockets lies a celui-ci.

        Parametre:
            sock -- socket a ferme
        '''

        if not sock.closed():
            debug.log(self, 'Disconnection of ' + str(sock), 1)

        sock.close()

        if sock in self.link:
            self.disconnect(self.link[sock])

            try:
                del self.link[sock]
            except KeyError:
                debug.wtf(self, 'Python fait toujours la meme merde.')
        if sock in self.inputs:
            self.inputs.remove(sock)
        if sock in self.outputs:
            self.outputs.remove(sock)
        if sock in self.message_queue:
            try:
                del self.message_queue[sock]
            except KeyError:
                debug.wtf(self, 'Go home Python ! You\'re drunk')
Example #3
0
    def write(self, sock):
        '''Operation d'ecriture sur le socket.
        Le mecanisme d'ecriture est gere par une Queue d'operation.

        Parametre:
            sock -- instance du socket a ecrire
        '''

        try:
            msg = self.message_queue[sock].get_nowait()
        except Queue.Empty:
            self.outputs.remove(sock)

            if sock in self.message_queue:
                try:
                    del self.message_queue[sock]
                except KeyError:
                    debug.wtf(self, 'GTFO Python !!!')
        except KeyError:
            if sock in self.outputs:
                self.outputs.remove(sock)
        else:
            try:
                sock.send(msg)
            except socket.error:
                debug.error(self, 'Error for sending to ' + str(sock))
                self.disconnect(sock)
            else:
                debug.log(self, 'Sending to ' + str(sock), 3)
                debug.log(self, 'To ' + str(sock) + '`' + msg + '`', 5)
Example #4
0
        def maj_time(self, sock):
            '''Met a jour le temps d'activite d'un socket ainsi que tous ses sockets lies.'''

            sock.maj_time()
            if sock in self.link:
                try:
                    s = self.link[sock]
                except KeyError:
                    debug.wtf(self, 'WTF Python !!')
                else:
                    self.maj_time(s)
Example #5
0
    def add_message(self, sock, msg):
        '''Ajoute un message a la queue d'envoie associe au socket en parametre.

        Parametre:
            sock -- socket ou envoyer le message
            msg  -- message a rajouter a la queue d'envoie
        '''

        try:
            if not sock in self.message_queue:
                self.message_queue[sock] = Queue.Queue()

            try:
                self.message_queue[sock].put_nowait(msg)
            except KeyError:
                debug.wtf(self, 'Python fait encore de la merde')

            if not sock in self.outputs:
                self.outputs += [sock]
        except Queue.Full:
            pass