def __init__(self, task_port, result_port, master): self.master = master self.thread = threading.Thread(target=self.main) self.task_socket = objsocket.objsocket() self.result_socket = objsocket.objsocket() self.task_socket.bind_and_listen(task_port) self.result_socket.bind_and_listen(result_port)
def start(self): while True: sock = objsocket.objsocket() successful = sock.connect(self.master_ip, self.master_port) if successful: print('Connected to master {}@{}'.format( self.master_ip, self.master_port)) else: time.sleep(1) continue while True: try: task = sock.recv_data() except objsocket.EndOfInputException: print('Disconnected from master {}@{}'.format( self.master_ip, self.master_port)) sock.close() break task = pickle.loads(task) if task != None: print('Got task {}'.format(task)) sock.send_obj(self.function(task)) print('Sent result') else: sock.send_obj(None)
def __init__(self, manager_port, allowed_node_ips, master): self.master = master self.allowed_node_ips = allowed_node_ips self.thread = threading.Thread(target=self.main) self.nodes = {} # {ip: node, } self.sock = objsocket.objsocket() self.sock.bind_and_listen(manager_port, len(allowed_node_ips))
def onSocketConnected(self, sslSock): peerName = SnUtil.getSslSocketPeerName(sslSock) # need peer name if peerName is None: sslSock.close() logging.debug("SnPeerManager.onSocketConnected: Fail, no peer name") return # only peer in self-net is allowed if peerName not in self.peerInfoDict: sslSock.close() logging.debug("SnPeerManager.onSocketConnected: Fail, foreign peer, %s" % (peerName)) return # only one connection between a pair of hosts if self.peerInfoDict[peerName].fsmState != _PeerInfoInternal.STATE_NONE: sslSock.close() logging.debug("SnPeerManager.onSocketConnected: Fail, duplicate connection") return # send keep-alive packet for every second assert sslSock.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE) == 0 sslSock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1) sslSock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 1) sslSock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 100) sslSock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # record sock oldFsmState = self.peerInfoDict[peerName].fsmState self.peerInfoDict[peerName].fsmState = _PeerInfoInternal.STATE_INIT self.peerInfoDict[peerName].powerStateWhenInactive = self.POWER_STATE_UNKNOWN self.peerInfoDict[peerName].infoObj = None self.peerInfoDict[peerName].sock = objsocket(objsocket.SOCKTYPE_SSL_SOCKET, sslSock, self.onSocketRecv, self.onSocketError, self._gcComplete) logging.info("SnPeerManager.onSocketConnected: %s", _dbgmsg_peer_state_change(peerName, oldFsmState, self.peerInfoDict[peerName].fsmState)) # timer operation self._startOrStopPeerProbeTimer() # send localInfo self._sendObject(peerName, self.param.configManager.getVersion()) self._sendObject(peerName, self.param.configManager.getCfgSerializationObject()) self._sendObject(peerName, self.param.localManager.getLocalInfo())
def _init__(self, ip, task_port, result_port): self.task_socket = objsocket.objsocket() self.result_socket = objsocket.objsocket() self.task_socket.connect(ip, task_port) self.result_socket.connect(ip, result_port)
def __init__(self, function, port): self.sock = objsocket.objsocket() self.sock.bind_and_listen(port) self.function = function
def __init__(self, ip, port, master): self.ip = ip self.port = port self.master = master self.sock = objsocket.objsocket() self.thread = threading.Thread(target=self.main)
def _moiChangeState(self, moi, newState, failMessage=""): assert _moi_state_is_valid(newState, failMessage) assert moi.calling is None # change state oldState = moi.state logging.info("SnLocalManager.moiChangeState: %s, %s -> %s", _moi_key_to_str(moi), _moi_state_to_str(oldState), _moi_state_to_str(newState)) moi.state = newState # change workState, clear peerPacketQueue if newState in [_MoiObj.STATE_INACTIVE, _MoiObj.STATE_REJECT, _MoiObj.STATE_PEER_REJECT, _MoiObj.STATE_EXCEPT, _MoiObj.STATE_PEER_EXCEPT]: moi.workState = SnModuleInstance.WORK_STATE_IDLE moi.peerPacketQueue.clear() # change failMessage if newState in [_MoiObj.STATE_REJECT, _MoiObj.STATE_PEER_REJECT, _MoiObj.STATE_EXCEPT]: moi.failMessage = failMessage # do post change operation if True: if newState == _MoiObj.STATE_ACTIVE: assert moi.failMessage == "" assert moi.workState == SnModuleInstance.WORK_STATE_IDLE if not moi.propDict["standalone"]: exec("import %s" % (moi.moduleName.replace("-", "_"))) moi.mo = eval("%s.ModuleInstanceObject(self, moi.peerName, moi.userName, moi.moduleName, moi.tmpDir)" % (moi.moduleName.replace("-", "_"))) else: moi.proc = self._startSubProc(moi.peerName, moi.userName, moi.moduleName, moi.tmpDir, moi.logFile) fcntl.fcntl(moi.proc.stdout, fcntl.F_SETFL, os.O_NONBLOCK) moi.procPipe = objsocket(objsocket.SOCKTYPE_PIPE_PAIR, (moi.proc.stdout, moi.proc.stdin), self.onProcPipeRecv, self.onProcPipeError, self._procPipeGcComplete) self._moiCallFunc(moi, "onActive") return if newState == _MoiObj.STATE_FULL: assert moi.failMessage == "" assert moi.workState == SnModuleInstance.WORK_STATE_IDLE if len(moi.peerPacketQueue) > 0: self._moiProcessPacket(moi) return if newState == _MoiObj.STATE_INACTIVE: assert moi.failMessage == "" moi.workState = SnModuleInstance.WORK_STATE_IDLE self._moiCallFunc(moi, "onInactive") return if newState == _MoiObj.STATE_REJECT: moi.failMessage = failMessage moi.workState = SnModuleInstance.WORK_STATE_IDLE self._sendReject(moi.peerName, moi.userName, moi.moduleName, moi.failMessage) self._moiCallFunc(moi, "onInactive") return if newState == _MoiObj.STATE_PEER_REJECT: moi.failMessage = failMessage moi.workState = SnModuleInstance.WORK_STATE_IDLE self._moiCallFunc(moi, "onInactive") return if newState == _MoiObj.STATE_EXCEPT: moi.failMessage = failMessage moi.workState = SnModuleInstance.WORK_STATE_IDLE if oldState in [_MoiObj.STATE_ACTIVE, _MoiObj.STATE_FULL]: self._sendExcept(moi.peerName, moi.userName, moi.moduleName) if moi.propDict["standalone"]: if moi.proc is not None: moi.proc.terminate() return if newState == _MoiObj.STATE_PEER_EXCEPT: moi.failMessage = "" moi.workState = SnModuleInstance.WORK_STATE_IDLE self._moiCallFunc(moi, "onInactive") return assert False return