def send(self, msg): # TODO: Busy wait will do for initial startup but for dealing with server down in the middle of things # TODO: then busy wait is probably inappropriate. while True: # keep going until we break out inside the loop try: self.logger.debug('Attempting to connect to ' + self.serverName + ' server at ' + str(self.address) + ' port ' + str(self.port)) conn = hub.connect((self.address, self.port)) self.logger.debug('Connect to ' + self.serverName + ' successful.') break except SocketError as serr: if serr.errno == errno.ECONNREFUSED: self.logger.debug( 'Connect to ' + self.serverName + ' failed because connection was refused (the server is down). Trying again.' ) else: # Not a recognized error. Treat as fatal. self.logger.debug('Connect to ' + self.serverName + ' gave socket error ' + str(serr.errno)) raise serr except: self.logger.exception('Connect to ' + self.serverName + ' threw unknown exception') raise conn.sendall(msg) conn.close()
def _connect_tcp(self, peer_addr, conn_handler, time_out=None, bind_address=None): """Creates a TCP connection to given peer address. Tries to create a socket for `timeout` number of seconds. If successful, uses the socket instance to start `client_factory`. The socket is bound to `bind_address` if specified. """ LOG.debug('Connect TCP called for %s:%s' % (peer_addr[0], peer_addr[1])) if netaddr.valid_ipv4(peer_addr[0]): family = socket.AF_INET else: family = socket.AF_INET6 with Timeout(time_out, socket.error): sock = hub.connect(peer_addr, family=family, bind=bind_address) if sock: # Connection name for pro-active connection is made up # of local end address + remote end address conn_name = ('L: ' + str(sock.getsockname()) + ', R: ' + str(sock.getpeername())) self._asso_socket_map[conn_name] = sock # If connection is established, we call connection handler # in a new thread. self._spawn(conn_name, conn_handler, sock) return sock
def send(self, msg): # TODO: Busy wait will do for initial startup but for dealing with server down in the middle of things # TODO: then busy wait is probably inappropriate. while True: # keep going until we break out inside the loop try: self.logger.debug('Attempting to connect to '+self.serverName+' server at '+str(self.address)+' port '+str(self.port)) conn = hub.connect((self.address, self.port)) self.logger.debug('Connect to '+self.serverName+' successful.') break except SocketError as serr: if serr.errno == errno.ECONNREFUSED: self.logger.debug('Connect to '+self.serverName+' failed because connection was refused (the server is down). Trying again.') else: # Not a recognized error. Treat as fatal. self.logger.debug('Connect to '+self.serverName+' gave socket error '+str(serr.errno)) raise serr except: self.logger.exception('Connect to '+self.serverName+' threw unknown exception') raise conn.sendall(msg) conn.close()