def connect_socket(self, address): socket = socketutil.client_socket() socket.connect(address) socket_id = "%s:%d" % socket.getsockname() log.debug(self,"Connected socket is %s %s" % (socket, socket.getsockname()[1])) StageSocket(socket, socket_id, self).start() return socketutil.SocketReference(self.__overlay.here, socket_id)
def close(self): print "Closing socket %s" % self.id self.exit = True self.messages.add(None) self.sock.close() self.store.remove_socket(self.id) log.debug(self, "Closed socket %s" % self.id)
def open_socket(self, port): if self.sockets.has_key("0.0.0.0:%d"%port):#bit naughty... return socketutil.SocketReference(self.__overlay.here, "0.0.0.0:%d"%port) opened_socket = socketutil.server_socket(port) socket_id = "%s:%d" % opened_socket.getsockname() log.debug(self, "Opened socket %d" % port) StageSocket(opened_socket, socket_id, self, self.__overlay.here).start() print "Returning socket reference for %s" % socket_id return socketutil.SocketReference(self.__overlay.here, socket_id)
def __init__(self, sock, socket_id, store, manager_loc): threading.Thread.__init__(self) log.debug(self, "New socket at %s" % socket_id) self.sock = sock self.wfile = sock.makefile('w') self.rfile = sock.makefile('r') self.id = socket_id self.store = store self.store.add_socket(socket_id, self) self.exit = False self.manager_loc = manager_loc self.messages = MessageQueue() self.results = ResultMap() self.m_id = 0
def run(self): while not self.exit: try: request = self.messages.get_next() if self.exit: break mid, target, method, args, kwds = request log.debug(self,"Call %s on %s with %s and %s" % (method, target, args, kwds)) meth = getattr(target, method) result = meth(*args, **kwds) # print "Got result %s" % result #special cases if method == 'makefile': log.debug(self, "Called makefile %s, %s, %s" % (self.manager_loc, self.id, args[0][0])) result = socketutil.SocketFileReference(self.manager_loc, self.id, args[0][0]) elif method == 'accept': socket_id = "%s:%d" % result[1] StageSocket(result[0], socket_id, self.store, self.manager_loc).start() result = socketutil.SocketReference(self.manager_loc, socket_id) elif method == 'close': self.exit = True self.store.remove_socket(self.id) log.debug(self, "Socket %s closing" % self.id) self.results.add(SocketResult(mid,result)) except Exception as e: log.debug(self, "Socket exception: %s" % e) # t, v, tr = sys.exc_info() log.debug(self, "%s" % traceback.format_exc()) if self.exit: break else: self.results.add(SocketResult(mid,None)) log.debug(self,"Socket %s run out" % self.id)
def add_socket(self, socket_id, handler): with self.__socket_lock: self.sockets[socket_id] = handler log.debug(self, "Socket store is %s" % self.sockets.keys())