Esempio n. 1
0
 def handleClient(self,fd):
     print "Handling Client..."
     ch = ClientHandler(self.clients[fd])
     if not ch.handle():
         self.poller.unregister(fd)
         self.clients[fd].close()
         del self.clients[fd]
Esempio n. 2
0
File: web.py Progetto: jrasm91/cs360
 def handleClient(self, fd):
     print "Handling Client..."
     ch = ClientHandler(self.clients[fd])
     if not ch.handle():
         self.poller.unregister(fd)
         self.clients[fd].close()
         del self.clients[fd]
Esempio n. 3
0
    def run(self):
        while True:
            running = self.running
            garbage = []
            # if the q is empty sleep for a second and return to the top of the loop
            if not self.q:
                time.sleep(1)
                continue
            else:
                # check if the set is full if it is start garbage collecting
                if len(running) > 0:
                    for thread in running:
                        if thread.is_alive() == False:
                            garbage.append(thread)
                            # print("Found a dead thread moving it to garbage")
                    # remove all dead threads from the garbage array
                    for trash in garbage:
                        running.remove(trash)
                        # print('Taking out all the trash threads')
                    # sleep for a second then return to the top of the loop

                    # continue
                # if there is room create and start the client thread then add it to the running set
                while len(running) < 5 and len(self.q) > 0:
                    c = self.q.popleft()
                    clientHandler = ClientHandler(c)
                    clientHandler.start()
                    running.add(clientHandler)
                    # print('Current status of the set is %s' % str(running))
                time.sleep(1)
Esempio n. 4
0
    def handle_client(self, conn, addr):
        cl = ClientHandler(conn, self.queues)
        try:
            while True:
                if cl.canRead() and not cl.handleRead():
                    raise socket.error
                if cl.canWrite() and not cl.handleWrite():
                    raise socket.error

        except socket.error, e:
            pass
Esempio n. 5
0
 def run(self):
     while True:                                          #the servers main job is to just sit and accept clients, then pass them off to a handler thread
         con, addr = self.sock.accept()
         print('Connection made at IP: ', str(addr[0]))
         print('Passing off client..')
         client = ClientHandler(con, addr[0], self, self.update)      #creating the client handler thread, needs the connection object and IP the client is from
         client.start()
         self.currentCons.append([client, addr[0]])      #adds in the connection to the current connections list
         self.displayClients()
         
     print('Closing server and connections..')
     self.sock.close()
     self.deleteAllClients()
     if self.update:
         self.updates("_________, SERVER SHUT DOWN", "______")
Esempio n. 6
0
    def new_conn(self):
        conn, addr = self.server.accept()
        conn.setblocking(0)

        self.poll.register(conn.fileno(), select.POLLIN)
        self.clients[conn.fileno()] = ClientHandler(conn, self.queues)

        serverStats['total_connections'] += 1
        serverStats['connections'] += 1
Esempio n. 7
0
    def run(self):
        while True:  #the servers main job is to just sit and accept clients, then pass them off to a handler thread
            con, addr = self.sock.accept()
            print('Connection made at IP: ', str(addr[0]))
            print('Passing off client..')
            client = ClientHandler(
                con, addr[0], self, self.update
            )  #creating the client handler thread, needs the connection object and IP the client is from
            client.start()
            self.currentCons.append([
                client, addr[0]
            ])  #adds in the connection to the current connections list
            self.displayClients()

        print('Closing server and connections..')
        self.sock.close()
        self.deleteAllClients()
        if self.update:
            self.updates("_________, SERVER SHUT DOWN", "______")
Esempio n. 8
0
    def handle_client(self, conn, addr):
        cl = ClientHandler(conn, self.queues)
        try:
            while True:
                if cl.canRead() and not cl.handleRead():
                    raise socket.error
                if cl.canWrite() and not cl.handleWrite():
                    raise socket.error

        except socket.error, e:
            pass
 def __init__(self, prefix, users, logger):
     self.prefix = prefix
     self.ackTime = str(datetime.datetime.now().time())
     self.clientHandler = ClientHandler(users)
     self.logger = logger
class Processor(object):
    def __init__(self, prefix, users, logger):
        self.prefix = prefix
        self.ackTime = str(datetime.datetime.now().time())
        self.clientHandler = ClientHandler(users)
        self.logger = logger

    def terminated(self):
        return self.isTerminated

    def getType(self, message):
        return unpack_from('B', message, 8)[0]

    ''' Main method- processing the data, given connection established with the user'''

    def process(self, data, connection):
        """

        :param data:
        :param connection:
        :return:
        """

        current_sender, msg_type, bodyLen = connection, self.getType(
            data), unpack_from('>i', data, 9)[0]

        if self.checkForAuthCode(data) is not 0:
            self.send_to_user(current_sender,
                              NotAuthorizedErrorPacket().build_packet())
            return
        ''' GOT KEEP-ALIVE PACKET'''
        if msg_type == 0x00:
            self.logger.info("Got from client Keep Alive Packet")
            self.clientHandler.handle_keep_alive(current_sender)
            ''' GOT CONNECT PACKET'''
        elif msg_type == 0x01:
            self.logger.info("Got from client CONNECT Packet")
            self.clientHandler.handle_connect(current_sender, bodyLen, data)
            ''' GOT DISCONNECT PACKET'''
        elif msg_type == 0x02:
            self.logger.info("Got from client DISCONNECT Packet")
            self.clientHandler.handle_disconnect(current_sender)
            ''' GOT PUBLIC MESSAGE PACKET'''
        elif msg_type == 0x03:
            self.logger.info("Got from client PUBLIC MESSAGE Packet")
            self.clientHandler.handle_public(current_sender, bodyLen, data)
            ''' GOT PRIVATE MESSAGE PACKET'''
        elif msg_type == 0x04:
            self.logger.info("Got from client PRIVATE MESSAGE Packet")
            self.clientHandler.handle_private(current_sender, bodyLen, data)
            ''' GOT SUDDEN DISCONNECT PACKET'''
        elif msg_type == 0x05:
            self.logger.info("Got from client SUDDEN DISCONNECT Packet")
            self.clientHandler.handle_SuddenDiscconnect(current_sender)
            ''' GOT UNIDENTIFIED PACKET'''
        else:
            self.logger.info("Got from client UNIDENTIFIED Packet")
            self.clientHandler.handle_unidentified(current_sender)

    ''' Checks weather given message is formatted according to protocol'''

    def checkForAuthCode(self, authBuffer):
        msCode = unpack_from('B' * 8, authBuffer, 0)
        return cmp(msCode, self.prefix)