def deleteSelectedItems(self): selection = self.gpsTable.selectionModel() indexes = selection.selectedRows() #uuids: {clientId: [uuid,]} uuids = {} for index in indexes: uuid_ = self.gpsModel.gpsItems[index.row()].id() clientId = application.lookUpClientIdByResourceId(uuid_) if clientId in uuids: uuids[clientId].append(uuid_) else: uuids[clientId] = [uuid_] if len(uuids) == 0: return for clientId in uuids: message = Message(cmd=Message.CMD_DELETE_GPS) message['ids'] = uuids[clientId] EventManager.trigger(Event('Client.replyReady.' + clientId, message)) self.deleteGpsItems(uuids[clientId]) for uuid_ in uuids[clientId]: toolBarId = application.lookUpToolBarIdByResourceId(uuid_) if toolBarId: EventManager.trigger(Event('ToolBar.changeState.' + toolBarId, True)) else: Log.e(u'未找到对应的ToolBar')
def handleSaveClicked(self): readable = self.readableEdit.text() username = self.nameEdit.text() password = self.passwordEdit.text() if len(username) > 0 and len(password) > 0: resourceId = uuid.uuid4().hex account = { 'id': resourceId, 'desc': readable, 'username': username, 'password': password } self.wantSaveAccount.emit(account) clientId = application.getRandomClientId() message = Message(cmd=Message.CMD_ADD_ACCOUNT) message['account'] = account if clientId: EventManager.trigger( Event('Client.replyReady.' + clientId, message)) application.addResource(resourceId, clientId) toolBarId = application.lookUpToolBarIdByResourceId(resourceId) if toolBarId: EventManager.trigger( Event('ToolBar.changeState.' + toolBarId, True)) else: Log.e(u'未找到对应的服务器,增加失败') else: Log.w(u'用户名和密码均不为空时才能保存')
def startVM(self, request): clientId = request.getParam('clientId') if not clientId: Log.e('ExternalDispatcher.startVM: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.startVM: 该客户端在服务器中不存在') return vmId = request['vmId'] success, vmInfo = self.vm.startMachine(vmId) if not success: Log.e("启动虚拟机失败") EventManager.trigger( Event('Socket.addReply.' + clientId, Message(cmd=Message.CMD_VM_START_FAIL, vmId=vmId))) return else: Log.i('成功启动虚拟机: ' + str(vmInfo['name'])) if clientId in self.externalToVm: self.externalToVm[clientId].append((vmId, vmInfo)) else: self.externalToVm[clientId] = [(vmId, vmInfo)] EventManager.trigger( Event( 'Socket.addReply.' + clientId, Message(cmd=Message.CMD_VM_START_OK, vmId=vmId, port=vmInfo['port'], password=vmInfo['password']))) message = Message(cmd=Message.CMD_VM_UPDATED, vmId=vmId, status=VirtualMachine.Status_Running) EventManager.trigger(Event('Message.broadcast', message, ()))
def loadResources(self, request): """加载GPS坐标,虚拟机信息,账户,用户id""" clientId = request['clientId'] vmMessage = Message(cmd=Message.CMD_QUERY_VMS) EventManager.trigger(Event('Client.replyReady.' + clientId, vmMessage)) gpsMessage = Message(cmd=Message.CMD_QUERY_GPS) EventManager.trigger(Event('Client.replyReady.' + clientId, gpsMessage)) accountMessage = Message(cmd=Message.CMD_QUERY_ACCOUNT) EventManager.trigger( Event('Client.replyReady.' + clientId, accountMessage))
def _addGpsData(self, params, clientId): message = Message(cmd=Message.CMD_ADD_GPS_OK, id=params['id']) EventManager.trigger(Event('Socket.addReply.' + clientId, message)) message = Message(cmd=Message.CMD_GPS_ADDED) gps = [{ 'id': params['id'], 'desc': params['desc'], 'lng': params['lng'], 'lat': params['lat'] }] message['gps'] = gps EventManager.trigger(Event('Message.broadcast', message, (clientId, )))
def _addAccountData(self, params, clientId): message = Message(cmd=Message.CMD_ADD_ACCOUNT_OK, id=params['id']) EventManager.trigger(Event('Socket.addReply.' + clientId, message)) message = Message(cmd=Message.CMD_ACCOUNT_ADDED) accounts = [{ 'id': params['id'], 'desc': params['desc'], 'username': params['username'], 'password': params['password'] }] message['accounts'] = accounts EventManager.trigger(Event('Message.broadcast', message, (clientId, )))
def setCurrent(self, index): if index == -1: self.current = None EventManager.trigger(Event('Webchat.currentChanged', self.current)) return self.webchats[index].setStyleSheet( "WebChat{background-color: #b2d235;}") self.current = self.webchats[index] for i, webchat in enumerate(self.webchats): if i != index: self.webchats[i].setStyleSheet( "WebChat{background-color: #FF0;}") EventManager.trigger(Event('Webchat.currentChanged', self.current))
def dispatchRequest(self, client): Log.i('Default dispatcher: ' + str(client)) Log.w('You need to setDispatcher for the Server') request = client.getRequest() cmd = request.getCMD() if cmd == Message.CMD_CLIENT_VALIDATED: client.setClientId(request['clientId']) message = Message(cmd=Message.CMD_QUERY_GPS, id=1) EventManager.trigger(Event('Client.replyReady.' + client.clientId, message)) message = Message(cmd=Message.CMD_QUERY_GPS, id=2) EventManager.trigger(Event('Client.replyReady.' + client.clientId, message)) message = Message(cmd=Message.CMD_QUERY_GPS, id=3) EventManager.trigger(Event('Client.replyReady.' + client.clientId, message)) message = Message(cmd=Message.CMD_QUERY_GPS, id=4) EventManager.trigger(Event('Client.replyReady.' + client.clientId, message))
def updateVm(self, request): clientId = request.getParam('clientId') if not clientId: Log.e('ExternalDispatcher.updateVm: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.updateVm: 该客户端在服务器中不存在') vmId = request['vmId'] self.vm.setGuestPropertyValue(vmId, 'desc', request['desc']) message = Message(cmd=Message.CMD_UPDATE_VM_OK, vmId=vmId) EventManager.trigger(Event('Socket.addReply.' + clientId, message)) message = Message(cmd=Message.CMD_VM_UPDATED, vmId=vmId, desc=request['desc']) EventManager.trigger(Event('Message.broadcast', message, (clientId, )))
def queryVms(self, request): clientId = request.getParam('clientId') if not clientId: Log.e('ExternalDispatcher.queryVms: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.queryVms: 该客户端在服务器中不存在') return vms = self.vm.getAllMachines() vms_ = {} for index, vmId in enumerate(vms): vm = vms[vmId] vms_[vm['name']] = { 'status': vm['status'], 'vmId': vmId, 'vmType': vm['type'], 'desc': vm['desc'] } message = Message(cmd=Message.CMD_QUERY_VMS_OK) message['vms'] = { 'name': '服务器', 'serverId': clientId, 'desc': '', 'vms': vms_ } EventManager.trigger(Event('Socket.addReply.' + clientId, message))
def handleAddGpsOk(self, request): toolBarId = application.lookUpToolBarIdByResourceId(request['id']) if toolBarId: EventManager.trigger( Event('ToolBar.changeState.' + toolBarId, False)) else: Log.e(u'未找到需要改变状态的ToolBar')
def closeTab(self, index): widget = self.tabWidget.widget(index) msgBox = QMessageBox() msgBox.setText(u'是否真的要关闭: ' + self.tabWidget.tabText(index)) msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No) msgBox.setDefaultButton(QMessageBox.No) msgBox.setWindowTitle(u'关闭tab页') info = widget.information() if info: try: accounts = info['accounts'] text = u'该tab页已登陆帐号:\n' for account in accounts: text += account + '\n' text.rstrip('\n') msgBox.setInformativeText(unicode(text)) except KeyError: pass result = msgBox.exec_() if result == QMessageBox.Yes: for webChat in widget.webchats: vmId = webChat.vmId() clientId = application.lookUpClientIdByResourceId(vmId) message = Message(cmd=Message.CMD_CLOSE_VM) message['vmId'] = vmId EventManager.trigger( Event('Client.replyReady.' + clientId, message)) self.tabWidget.removeTab(index)
def closeVM(self, request): clientId = request.getParam('clientId') if not clientId: Log.e('ExternalDispatcher.closeVM: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.closeVM: 该客户端在服务器中不存在') return vmId = request.getParam('vmId') if not vmId: Log.e('ExternalDispatcher.closeVM: 缺少虚拟机Id参数') return for vm in self.externalToVm[clientId]: if vm[0] == vmId: success, vmInfo = self.vm.closeMachine(vmId) if not success: break self.externalToVm[clientId].remove(vm) message = Message(cmd=Message.CMD_VM_UPDATED, vmId=vmId, status=VirtualMachine.Status_Idle) EventManager.trigger(Event('Message.broadcast', message, ())) return True Log.e("关闭虚拟机失败")
def registerCurrentLocation(self, *args): if len(args) == 1 and isinstance(args[0], dict): current = args[0] elif len(args) == 3: current = {'desc': args[2], 'lng': args[0], 'lat': args[1]} else: raise ValueError(u'参数个数不合法') EventManager.trigger(Event('Map.locationChanged', current))
def loadVms(self, request): vms = request['vms'] clientId = request['clientId'] pureVms = vms['vms'] for name in pureVms: application.addResource(pureVms[name]['vmId'], clientId) application.addItemToToolBar(pureVms[name]['vmId'], 0, 0) EventManager.trigger(Event('Message.addVms', vms))
def queryAccount(self, request): clientId = request.getParam('clientId') if not clientId: Log.e('ExternalDispatcher.queryAccount: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.queryAccount: 该客户端在服务器中不存在') return EventManager.trigger(Event('DataCenter.loadAllAccount', clientId))
def startNewVm(self, info): webchat = WebChat(info['vmId'], info['vmType'], info['desc']) index, tab = self.tabContainer.lookUpVmSpace() tab.addWebchat(webchat) self.tabContainer.adjustTabName(tab) self.tabContainer.setCurrentWidget(tab, index) message = Message(cmd=Message.CMD_START_VM, vmId=info['vmId']) EventManager.trigger( Event('Client.replyReady.' + info['serverId'], message))
def setupDatabase(self): from collections import OrderedDict self.db = Database('chat-advance.db') if not self.db.createConnection(): EventManager.trigger(Event('Database.databaseConnectionFail')) return if not self.db.isTableExist(DataCenter.TABLE_FRIENDS): self.db.createTable(DataCenter.TABLE_SERVERS, OrderedDict([('name', Database.TYPE_TEXT), ('url', Database.TYPE_TEXT), ('port', Database.TYPE_TEXT)]))
def handleDeleteGpsOk(self, request): ids = request['ids'] for id_ in ids: toolBarId = application.lookUpToolBarIdByResourceId(id_) if toolBarId: EventManager.trigger( Event('ToolBar.changeState.' + toolBarId, False)) else: Log.e(u'未找到需要改变状态的ToolBar') application.delItemFromToolBar(id_)
def editLocation(self, index, unused, roles): """同步修改到服务器,对于GPS坐标来说,客户端只能更改坐标备注""" if Qt.EditRole not in roles: self.gpsTable.update() return item = self.gpsModel.itemForIndex(index) resourceId = item.id() clientId = application.lookUpClientIdByResourceId(resourceId) if clientId: message = Message(cmd=Message.CMD_UPDATE_GPS) message['id'] = resourceId message['desc'] = item.description() EventManager.trigger(Event('Client.replyReady.' + clientId, message)) toolBarId = application.lookUpToolBarIdByResourceId(resourceId) if toolBarId: print 'ToolBar.changeState.' + toolBarId EventManager.trigger(Event('ToolBar.changeState.' + toolBarId, True)) else: Log.e(u'未找到对应的服务器,修改失败')
def editVm(self, index, unused, roles): """同步修改到服务器,只能修改虚拟机备注""" if Qt.EditRole not in roles: self.treeView.update() return item = self.model.itemForIndex(index) resourceId = item.id() clientId = application.lookUpClientIdByResourceId(resourceId) if clientId: message = Message(cmd=Message.CMD_UPDATE_VM) message['vmId'] = resourceId message['desc'] = item.description() EventManager.trigger( Event('Client.replyReady.' + clientId, message)) toolBarId = application.lookUpToolBarIdByResourceId(resourceId) if toolBarId: print 'ToolBar.changeState.' + toolBarId EventManager.trigger( Event('ToolBar.changeState.' + toolBarId, True))
def deleteGps(self, request): clientId = request.getParam('clientId') if not clientId: Log.e('ExternalDispatcher.deleteGps: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.deleteGps: 该客户端在服务器中不存在') return EventManager.trigger( Event('DataCenter.deleteGps', request['ids'], clientId))
def addAccount(self, request): clientId = request.getParam('clientId') account = request['account'] if not clientId: Log.e('ExternalDispatcher.addAccount: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.addAccount: 该客户端在服务器中不存在') return EventManager.trigger( Event('DataCenter.addAccount', account['id'], account['desc'], account['username'], account['password'], clientId))
def _sendAccountData(self, data, clientId): accounts = [] for account in data: accounts.append({ 'id': account[1], 'desc': account[2], 'username': account[3], 'password': account[4] }) message = Message(cmd=Message.CMD_QUERY_ACCOUNT_OK) message['accounts'] = accounts EventManager.trigger(Event('Socket.addReply.' + clientId, message))
def addGps(self, request): clientId = request.getParam('clientId') gps = request['gps'] if not clientId: Log.e('ExternalDispatcher.updateGps: 权限不足') return if clientId not in self.server.clients: Log.e('ExternalDispatcher.updateGps: 该客户端在服务器中不存在') return EventManager.trigger( Event('DataCenter.addGps', gps['id'], gps['desc'], gps['lng'], gps['lat'], clientId))
def _sendGpsData(self, data, clientId): gps_ = [] for gps in data: gps_.append({ 'id': gps[1], 'desc': gps[2], 'lng': gps[3], 'lat': gps[4] }) message = Message(cmd=Message.CMD_QUERY_GPS_OK) message['gps'] = gps_ EventManager.trigger(Event('Socket.addReply.' + clientId, message))
def clearVmForClient(self, clientId): if clientId in self.externalToVm: for vm in self.externalToVm[clientId]: success, vmInfo = self.vm.closeMachine(vm[0]) if not success: Log.e('关闭虚拟机失败:' + vm[1]['name']) continue message = Message(cmd=Message.CMD_VM_UPDATED, vmId=vm[0], status=VirtualMachine.Status_Idle) EventManager.trigger(Event('Message.broadcast', message, ())) del self.externalToVm[clientId]
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 handleSaveClicked(self): readable = self.readableEdit.text() lng = self.lngEdit.text() lat = self.latEdit.text() if len(lng) > 0 and len(lat) > 0: resourceId = uuid.uuid4().hex gps = {'id': resourceId, 'desc': readable, 'lng': lng, 'lat': lat} self.wantSaveLocation.emit(gps) clientId = application.getRandomClientId() message = Message(cmd=Message.CMD_ADD_GPS) message['gps'] = gps if clientId: EventManager.trigger( Event('Client.replyReady.' + clientId, message)) application.addResource(resourceId, clientId) toolBarId = application.lookUpToolBarIdByResourceId(resourceId) if toolBarId: EventManager.trigger( Event('ToolBar.changeState.' + toolBarId, True)) else: Log.e(u'未找到对应的服务器,增加失败') else: Log.w(u'经度和纬度均不为空时才能保存')
def editAccount(self, index, unused, roles): """同步修改到服务器""" if Qt.EditRole not in roles: self.table.update() return item = self.model.itemForIndex(index) resourceId = item.id() clientId = application.lookUpClientIdByResourceId(resourceId) if clientId: message = Message(cmd=Message.CMD_UPDATE_ACCOUNT) message['id'] = resourceId message['desc'] = item.description() message['username'] = item.username() message['password'] = item.password() EventManager.trigger( Event('Client.replyReady.' + clientId, message)) toolBarId = application.lookUpToolBarIdByResourceId(resourceId) if toolBarId: print 'ToolBar.changeState.' + toolBarId EventManager.trigger( Event('ToolBar.changeState.' + toolBarId, True)) else: Log.e(u'未找到对应的服务器,修改失败')