Example #1
0
 def send_message(self, broadcast, sock, message):
     # Check to see if this is a broadcast message
     if broadcast == True:
         for client_socket in self._socket_list:
             if client_socket != self._server_socket:
                 MessageProtocol.send_msg(client_socket, message)
     # Send the message to the specified socket (sock)
     else:
         MessageProtocol.send_msg(sock, message)
Example #2
0
    def run(self):
        while True:
            fd_list = [self._client_socket]
            
            # Get the list of sockets that are readable
            ready_to_read, ready_to_write, input_error = select.select(fd_list, [], [], self.__select_timeout)
            
            for sock in ready_to_read:
                # Received message from server
                if sock == self._client_socket:
                    # This should ensure all the data from the socket is received
                    message_list = []
                    
                    while 1:
                        message, bytes_read = MessageProtocol.recv_msg(sock)
                        
                        if bytes_read > 0:
                            message_list.append(message)
                        else:
                            break

                    # Check to see if data is available
                    message_list_length = len(message_list)
                    
                    if message_list_length > 0:
                        for message in message_list:
                            # Place the server message into the output queue and notify the client that data has been received
                            self._output_queue.put(message)
                        
                        self.notify_client_message()
                    # Disconnected from server
                    else:
                        self._logger.error('Disconnected from the server.')
                        sys.exit()
            
            # Check to see if data is available on the input queue
            # Note: Normally the queue would be in the select call, but I don't think 
            #       Queue is implemented as a file descriptor in Python (or Windows sucks)
            if self._input_queue.qsize() > 0:
                self._logger.debug('Retrieving message from input queue.')
                
                try:
                    message = self._input_queue.get_nowait()
                except:
                    break
                
                # Send message to the server
                self._logger.debug('Sending message to server.')
                MessageProtocol.send_msg(self._client_socket, message)