def start_server(self, interpreter=None): '''Listen on a @port for incoming application connections. When connected to store the client's information and attempt to read its transmitted data. ARGS: @port -- The port number to start the server socket on RETURN: None -- Theoretically Endless ''' # Bind this socket to port and listen for connections self.sock.bind((self.address, self.port)) self.sock.listen(5) # While the application is running attempt listen for connections and read the # connecting devices message. while True: # Get the connected socket and make a Batman Socket out of it client, address = self.sock.accept() tcp_client = TCPSocket(address, client) # Start threads to add devices to the list of clients and read # their messages read_thread = Thread(target=self.read_client, args=[tcp_client, interpreter]) read_thread.start()
def respond_to_client(self, client, msg): from TCPSocket import TCPSocket port = find_tcp_port(client.address) responder = TCPSocket(self.address) responder.connect(client.address, port) responder.write(msg) responder.close()
def connection(mode): if mode == 'tcp': from TCPSocket import TCPSocket return TCPSocket() if mode == 'udp': from UDPSocket import UDPSocket return UDPSocket() import journal journal.error('pyre.ipc').log("unknown connection mode '%s'" % mode) return None
def add_connection(self, ip, logId="Overview"): ''' A method that connects with the given host on the given port. It creates a new Connection, performs the handshake, then adds the Connection and creates a new GUI tab. :param str ip: The ipv4 address or username of the computer to connect to. :param int port: The port to try connecting to. :param str logId: The default tab to log status of connection add to. ''' self.log("Attempting to add connection with " + ip + "...", logId) #Prevent creation of duplicate connections if ip in self.connections: self.log("Connection already exists.", logId) return None if "." not in ip: #Without a dot assume ip is a username if ip in self.userList.byName: name = ip ip = self.userList.byName[name][0] self.log("%s found at %s." % (name, ip), logId) else: self.log( "%s is not a known user or correctly formatted IP address, please try again." % (ip), logId) return None else: name = ip self.log("Adding new connection with %s:%i..." % (ip, self.port), logId) try: newConnection = Connection(name, TCPSocket(ip, self.port), self) name = self.do_handshake(newConnection) except: self.log("Connection open failed.", logId) return self.connections[name] = newConnection newConnection.start_listening() self.guiQueue.put((self.master.add_tab, name)) self.log('%s:%i (%s) connected.' % (ip, self.port, name), logId)
def listen_for_connections(self): ''' A method containing an infinite loop that listens for incoming socket connections. When one is detected, it creates a new Connection, performs the handshake, and then adds the Connection and creates a new GUI tab for the chat. ''' self.log('Listening for connections...') while True: try: #Accept a new incoming connection (clientsocket, address) = self.serverSocket.accept() except OSError: #Program is being shut down, end loop return None self.log('New incoming connection from %s:%i' % (address[0], address[1])) #Create a new TCPSocket and Connection object with the returned socket newConnection = Connection(address, TCPSocket(clientsocket), self) #Get the nickname of the other person. try: nickname = self.do_handshake(newConnection) except: self.log("Incoming connection add failed.") continue self.connections[nickname] = newConnection #Start listening through the connection newConnection.start_listening() #Add a tab to the GUI self.guiQueue.put((self.master.add_tab, nickname)) self.log('%s:%i (%s) connected.' % (address[0], address[1], nickname))
def __init__(self): PortMonitor.__init__(self) TCPSocket.__init__(self) return