def read(self): try: message = self.socket.recv(4096) except Exception as e: Log.e(getExceptionInfo(e)) return Client.ReadError if len(message) == 0: return Client.NoMessage self.buf += message while len(self.buf) > 4: length = int(self.buf[0:4]) if not len(self.buf) >= length + 4: break msg = self.buf[4:length + 4] self.buf = self.buf[length + 4:] message = Message() if not message.loads(msg): Log.w(u'Unknown Message') else: self.requests.put(message) return Client.NewMessage
def getParam(self, name): param = None try: param = self.request[name] except Exception as e: Log.e(getExceptionInfo(e)) return param
def __getitem__(self, item): param = None try: param = self.params[item] except Exception as e: Log.e(getExceptionInfo(e)) return param
def load(self, name, vmId, port, password): self.vmInfo = {'name': name, 'port': port, 'password': password, 'obj': self} try: self.mach = self.vbox.findMachine(name) desc = self.mach.getGuestPropertyValue('vm_desc') self.vmInfo['desc'] = str(desc) if desc else '' type_ = self.mach.getGuestPropertyValue('vm_type') if not type_ or int(type_) not in (VirtualMachine.Type_Stand, VirtualMachine.Type_Id_Extract): self.vmInfo['status'] = VirtualMachine.Status_Invalid self.vmInfo['type'] = -1 return self.vmInfo self.vmInfo['type'] = int(type_) if self.mach.state == self.mgr.constants.MachineState_Running: self.vmInfo['status'] = VirtualMachine.Status_Running return self.vmInfo self.session = self.mgr.mgr.getSessionObject(self.vbox) self.mach.lockMachine(self.session, self.mgr.constants.LockType_Write) mutable = self.session.machine mutable.setGuestPropertyValue('vm_vmId', vmId) mutable.VRDEServer.setVRDEProperty('VNCPassword', str(password)) mutable.VRDEServer.setVRDEProperty('TCP/Ports', str(port)) mutable.VRDEServer.enabled = True mutable.saveSettings() self.session.unlockMachine() except Exception, e: Log.e(getExceptionInfo(e)) return
def read(self): try: message = self.socket.recv(4096) except Exception as e: Log.e(getExceptionInfo(e)) return Client.ReadError if len(message) == 0: return Client.NoMessage self.buf += message while len(self.buf) > 4: length = int(self.buf[0:4]) if not len(self.buf) >= length + 4: break msg = self.buf[4: length + 4] self.buf = self.buf[length + 4:] message = Message() if not message.loads(msg): Log.w(u'Unknown Message') else: self.requests.put(message) return Client.NewMessage
def load(self, name, vmId, port, password): self.vmInfo = { 'name': name, 'port': port, 'password': password, 'obj': self } try: self.mach = self.vbox.findMachine(name) desc = self.mach.getGuestPropertyValue('vm_desc') self.vmInfo['desc'] = str(desc) if desc else '' type_ = self.mach.getGuestPropertyValue('vm_type') if not type_ or int(type_) not in (VirtualMachine.Type_Stand, VirtualMachine.Type_Id_Extract): self.vmInfo['status'] = VirtualMachine.Status_Invalid self.vmInfo['type'] = -1 return self.vmInfo self.vmInfo['type'] = int(type_) if self.mach.state == self.mgr.constants.MachineState_Running: self.vmInfo['status'] = VirtualMachine.Status_Running return self.vmInfo self.session = self.mgr.mgr.getSessionObject(self.vbox) self.mach.lockMachine(self.session, self.mgr.constants.LockType_Write) mutable = self.session.machine mutable.setGuestPropertyValue('vm_vmId', vmId) mutable.VRDEServer.setVRDEProperty('VNCPassword', str(password)) mutable.VRDEServer.setVRDEProperty('TCP/Ports', str(port)) mutable.VRDEServer.enabled = True mutable.saveSettings() self.session.unlockMachine() except Exception, e: Log.e(getExceptionInfo(e)) return
def reply(self): reply = self.replies.get() data = reply.dumps() if data: try: self.socket.send(bytes(str(len(data)).zfill(4)) + data) return self.WriteSuccess except Exception as e: Log.e("Can't send reply: " + getExceptionInfo(e)) return self.WriteError
def dispatchRequest(self, request): cmd = request.getCMD() if cmd in self.callbacks: self.callbacks[cmd](request) elif Message.CMD_BRIDGE_START <= cmd < Message.CMD_WEBCHAT_END: try: webchatId = request.getParam('webchatId') EventManager.trigger(Event('Socket.addReply.' + webchatId, request)) except Exception as e: Log.e(getExceptionInfo(e)) else: Log.w('未实现的命令: ' + str(cmd))
def dispatchRequest(self, request): cmd = request.getCMD() if cmd in self.callbacks: self.callbacks[cmd](request) elif Message.CMD_BRIDGE_START <= cmd < Message.CMD_WEBCHAT_END: try: webchatId = request.getParam('webchatId') EventManager.trigger( Event('Socket.addReply.' + webchatId, request)) except Exception as e: Log.e(getExceptionInfo(e)) else: Log.w('未实现的命令: ' + str(cmd))
def loads(self, message): try: message = message.decode('utf-8') except UnicodeError: Log.e('无法使用utf-8解码消息: ' + binascii.hexlify(message)) return False try: self.params = json.loads(message) Log.i('成功解析Message: ' + str(self.params)) except Exception as e: Log.e(getExceptionInfo(e)) return False return True
def setGuestPropertyValue(self, key, value=None): if not self.mach: return try: self.session = self.mgr.mgr.getSessionObject(self.vbox) self.mach.lockMachine(self.session, self.mgr.constants.LockType_Shared) mutable = self.session.machine mutable.setGuestPropertyValue(key, value) mutable.saveSettings() self.session.unlockMachine() return True except Exception, e: Log.e(getExceptionInfo(e))
def start(self): if self.mach is None: print u'虚拟机分配失败,无法启动' elif self.mach.state == self.mgr.constants.MachineState_Running: print u'虚拟机已经在运行中,不能重复启动' else: try: self.session = self.mgr.mgr.getSessionObject(self.vbox) self.progress = self.mach.launchVMProcess(self.session, "headless", "") self.progress.waitForCompletion(-1) self.console = self.session.console return True except Exception, e: Log.e(getExceptionInfo(e))
def connectToServer(self, address): """address格式为(url, port)""" if self.hasConnected(address): Log.w('已经连接到服务器') return clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clientSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try: clientSocket.connect(address) client = Client(clientSocket, -1) self.clients[client] = (address, -1) Log.i('成功连接到服务器') return True except Exception as e: Log.e(getExceptionInfo(e))
def start(self): if self.mach is None: print u'虚拟机分配失败,无法启动' elif self.mach.state == self.mgr.constants.MachineState_Running: print u'虚拟机已经在运行中,不能重复启动' else: try: self.session = self.mgr.mgr.getSessionObject(self.vbox) self.progress = self.mach.launchVMProcess( self.session, "headless", "") self.progress.waitForCompletion(-1) self.console = self.session.console return True except Exception, e: Log.e(getExceptionInfo(e))