RPCServer::RPCServer(int port){ QSettings settings; bool ok = false; while(!ok){ ok = listen(QHostAddress::Any, port); if(!ok && port < rpcMaxPort){ /* Try going for higher ports until we can listen to one */ qDebug("RPCServer: Failed to bind to port %d. Trying higher ports...",port); port++; }else{ qDebug("RPCServer: Listening on port %d",port); settings.setValue("RPCServer/serverPort",port); } } if(!ok){ qDebug("RPCServer: All ports exhausted. Binding failed."); return; } attachSlot(QString("reconstructionStarted()"),this,SLOT(reconstructionStarted(quint64))); attachSlot(QString("reconstructionStopped()"),this,SLOT(reconstructionStopped(quint64))); attachSlot(QString("identificationKeySent(int)"),this,SLOT(receiveIdentificationKey(quint64,int))); QObject::connect(this,SIGNAL(clientConnected(quint64)),this,SLOT(onClientConnected(quint64))); QObject::connect(this,SIGNAL(clientDisconnected(quint64)),this,SLOT(onClientDisconnected(quint64))); }
void HostServer::onDisconnected(const PeerPtr& peer) { onClientDisconnected(peer); NetworkPeers::iterator it = std::find(peers.begin(), peers.end(), peer); assert( it != peers.end() ); peers.erase(it); }
void FtpServer::onClientConnected() { sc = sv.nextPendingConnection(); emit msg(sc->peerAddress().toString() + QString(":") + QString::number(sc->peerPort()) + QString(" connected")); connect(sc, SIGNAL(disconnected()), this, SLOT(onClientDisconnected())); connect(sc, SIGNAL(disconnected()), sc, SLOT(deleteLater())); connect(sc, SIGNAL(readyRead()), this, SLOT(read())); respond(220); }
void ServerBase::update(void) { if (!mIsRunning) return; ENetEvent event; while (enet_host_service(mInternalHost, &event, 0) > 0) { switch(event.type) { case ENET_EVENT_TYPE_CONNECT: { IncomingClientBase *client = new Kiaro::Network::IncomingClientBase(event.peer, this); event.peer->data = client; mConnectedClientSet.insert(mConnectedClientSet.end(), (size_t)client); onClientConnected(client); break; } case ENET_EVENT_TYPE_DISCONNECT: { Kiaro::Network::IncomingClientBase *disconnected = (Kiaro::Network::IncomingClientBase*)event.peer->data; onClientDisconnected(disconnected); mConnectedClientSet.erase((size_t)disconnected); delete disconnected; break; } case ENET_EVENT_TYPE_RECEIVE: { if (!event.peer->data) { enet_packet_destroy(event.packet); throw std::runtime_error("ServerBase: Invalid ENet peer data on packet receive!"); } Kiaro::Network::IncomingClientBase *sender = (Kiaro::Network::IncomingClientBase*)event.peer->data; Kiaro::Support::BitStream incomingStream(event.packet->data, event.packet->dataLength, event.packet->dataLength); onReceivePacket(incomingStream, sender); enet_packet_destroy(event.packet); break; } case ENET_EVENT_TYPE_NONE: break; } } }
void SIpcServer::incomingConnection(quintptr socketDescriptor) { SIpcConnection *s = new SIpcConnection(this); s->setSocketDescriptor(socketDescriptor); connect(s, SIGNAL(messageArrived(const QString &, const QByteArray &)), SLOT(onClientMessage(const QString &, const QByteArray &))); connect(s, SIGNAL(disconnected()), SLOT(onClientDisconnected())); mPeers.append(s); #ifdef SIPCSERVER_DEBUG sDebug() << "Registered connection " << s << ", total peers: " << mPeers.count(); #endif }
void Server::incomingConnection(qintptr socketDescriptor) { Client *client = new Client(socketDescriptor); clients->push_back(client); QObject::connect(client,SIGNAL(sendMessageToAll(QString)),this,SLOT(sendMessageToAll(QString))); QObject::connect(client,SIGNAL(showDialog(QString)),this,SLOT(onShowDialog(QString))); QObject::connect(client,SIGNAL(signalDisconnected(QString)),this,SLOT(onClientDisconnected(QString))); QObject::connect(client,SIGNAL(signalConnected(QString)),this,SLOT(onClientConnected(QString))); QObject::connect(client,SIGNAL(saveHistory(QString)),this,SLOT(onSaveHistory(QString))); QObject::connect(client,SIGNAL(getHistory()),this,SLOT(onGetHistory(Client*))); onGetHistory(client); }
GameScreenWidget::GameScreenWidget(QWidget *parent, Server *server, Client *client, bool isServer) : QWidget(parent), server(server), client(client), isServer(isServer), currentPlayerId(0), currentPlayerIndex(0), waitTurns(0), rankingPosition(1),requestTurns(0), ui(new Ui::GameScreenWidget) { ui->setupUi(this); ui->Player3->setOrientation(OpponentCardsWidget::Orientation::Vertical); ui->Player4->setOrientation(OpponentCardsWidget::Orientation::Vertical); if (!isServer) { connect(client, SIGNAL(onDisconnected()), this, SLOT(onClientDisconnected())); connect(client, SIGNAL(onDataReceived(int,QString)), this, SLOT(onClientDataReceived(int,QString))); connect(client, SIGNAL(onError(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError))); ui->labelCurrentPlayer->setText(client->getPlayer()->getName()); ui->labelPlayer2->hide(); ui->labelPlayer3->hide(); ui->labelPlayer4->hide(); for (int i = 0; i < client->getOtherPlayers().size(); i++) { Player* p = client->getOtherPlayers().at(i); switch(i) { case 0: ui->labelPlayer2->show(); ui->labelPlayer2->setText(p->getName()); break; case 1: ui->labelPlayer3->show(); ui->labelPlayer3->setText(p->getName()); break; case 2: ui->labelPlayer4->show(); ui->labelPlayer4->setText(p->getName()); break; } } }
Task_MigrateCtTarget::Task_MigrateCtTarget( const QObject *parent, const SmartPtr<CDspDispConnection> &pDispConnection, CDispToDispCommandPtr pCmd, const SmartPtr<IOPackage> &p) : CDspTaskHelper(pDispConnection->getUserSession(), p), Task_DispToDispConnHelper(getLastError()), m_pParent(parent), m_pCheckDispConnection(pDispConnection), m_pCheckPackage(p), m_cDstHostInfo(CDspService::instance()->getHostInfo()->data()), m_pOpaqueLock(NULL), m_nSteps(0), m_nBundlePermissions(0), m_nConfigPermissions(0) { CVmMigrateCheckPreconditionsCommand * pCheckCmd = CDispToDispProtoSerializer::CastToDispToDispCommand<CVmMigrateCheckPreconditionsCommand>(pCmd); m_hConnHandle = pDispConnection->GetConnectionHandle(); m_nMigrationFlags = pCheckCmd->GetMigrationFlags(); m_nReservedFlags = pCheckCmd->GetReservedFlags(); m_nVersion = pCheckCmd->GetVersion(); m_sVmConfig = pCheckCmd->GetVmConfig(); m_nPrevVmState = pCheckCmd->GetVmPrevState(); m_sVmDirPath = pCheckCmd->GetTargetVmHomePath(); m_sSrcHostInfo = pCheckCmd->GetSourceHostHardwareInfo(); /* initialize all vars from pCheckCmd - after exit from constructor package buffer will invalid */ bool bConnected = QObject::connect( &CDspService::instance()->getIOServer(), SIGNAL(onClientDisconnected(IOSender::Handle)), SLOT(clientDisconnected(IOSender::Handle)), Qt::DirectConnection); PRL_ASSERT(bConnected); }
void HostClient::onDisconnected(const PeerPtr& peer) { state = HostState::Disconnected; onClientDisconnected(peer); }
PRL_RESULT Task_MigrateCtTarget::run_body() { PRL_RESULT nRetCode = PRL_ERR_SUCCESS; bool bConnected; QTimer *pTimer; CDispToDispCommandPtr pReply; SmartPtr<IOPackage> pPackage; IOSendJob::Handle hJob; if (operationIsCancelled()) { nRetCode = PRL_ERR_OPERATION_WAS_CANCELED; goto exit; } bConnected = QObject::connect(m_pParent, SIGNAL(onPackageReceived( const SmartPtr<CDspDispConnection> &, const QString &, const SmartPtr<IOPackage> &)), SLOT(handleStartPackage( const SmartPtr<CDspDispConnection> &, const QString &, const SmartPtr<IOPackage> &)), Qt::DirectConnection); PRL_ASSERT(bConnected); m_nReservedFlags |= PVM_CT_MIGRATE; pReply = CDispToDispProtoSerializer::CreateVmMigrateCheckPreconditionsReply( m_lstCheckPrecondsErrors, QStringList(), m_nReservedFlags); pPackage = DispatcherPackage::createInstance( pReply->GetCommandId(), pReply->GetCommand()->toString(), m_pCheckPackage); hJob = m_pCheckDispConnection->sendPackage(pPackage); if (!hJob.isValid()) { nRetCode = PRL_ERR_OPERATION_FAILED; goto exit; } /* set timer */ pTimer = new QTimer(); pTimer->setSingleShot(true); bConnected = QObject::connect(pTimer, SIGNAL(timeout()), SLOT(handleStartCommandTimeout()), Qt::DirectConnection); pTimer->start(VM_MIGRATE_START_CMD_WAIT_TIMEOUT); /* will wait StartMigration command */ nRetCode = exec(); pTimer->stop(); if (bConnected) QObject::disconnect(pTimer, SIGNAL(timeout()), this, SLOT(handleStartCommandTimeout())); delete pTimer; QObject::disconnect(m_pParent, SIGNAL(onPackageReceived( const SmartPtr<CDspDispConnection> &, const QString &, const SmartPtr<IOPackage> &)), this, SLOT(handleStartPackage( const SmartPtr<CDspDispConnection> &, const QString &, const SmartPtr<IOPackage> &))); if (PRL_FAILED(nRetCode)) goto exit; if (operationIsCancelled()) { nRetCode = PRL_ERR_OPERATION_WAS_CANCELED; goto exit; } if ( !(m_nReservedFlags & PVM_DONT_COPY_VM) ) { /* to create Vm bundle */ if (!CFileHelper::WriteDirectory(m_sTargetVmHomePath, &getClient()->getAuthHelper())) { nRetCode = PRL_ERR_BACKUP_CANNOT_CREATE_DIRECTORY; CVmEvent *pEvent = getLastError(); pEvent->setEventCode(nRetCode); pEvent->addEventParameter( new CVmEventParameter(PVE::String, m_sTargetVmHomePath, EVT_PARAM_MESSAGE_PARAM_0)); WRITE_TRACE(DBG_FATAL, "[%s] Cannot create \"%s\" directory", __FUNCTION__, QSTR2UTF8(m_sTargetVmHomePath)); goto exit; } /* set original permissions to Vm bundle (https://jira.sw.ru/browse/PSBM-8269) */ if (m_nBundlePermissions) { QFile vmBundle(m_sTargetVmHomePath); if (!vmBundle.setPermissions((QFile::Permissions)m_nBundlePermissions)) { WRITE_TRACE(DBG_FATAL, "[%s] Cannot set permissions for Vm bundle \"%s\", will use default", __FUNCTION__, QSTR2UTF8(m_sTargetVmHomePath)); } } } m_nSteps |= MIGRATE_STARTED; if ((m_nPrevVmState == VMS_RUNNING) || (m_nPrevVmState == VMS_PAUSED)) { nRetCode = PRL_ERR_VM_MIGRATE_UNSUITABLE_VM_STATE; } else { nRetCode = migrateStoppedVm(); /* migration completed and connection state is indifferent for us https://jira.sw.ru/browse/PSBM-8925 */ QObject::disconnect( &CDspService::instance()->getIOServer(), SIGNAL(onClientDisconnected(IOSender::Handle)), this, SLOT(clientDisconnected(IOSender::Handle))); } if (PRL_FAILED(nRetCode)) goto exit; /* rename conf file since source sent it with his origin ctid */ if (m_nOriginCtid != m_nCtid) { QString srcPath = m_sTargetVmHomePath + QString("/%1.conf").arg(m_nOriginCtid); QString dstPath = m_sTargetVmHomePath + QString("/%1.conf").arg(m_nCtid); QFile::rename(srcPath, dstPath); } nRetCode = CVzHelper::dst_complete_migrate_env(m_sVmUuid, m_nCtid, m_nOriginCtid, &m_pOpaqueLock); if (PRL_FAILED(nRetCode)) { WRITE_TRACE(DBG_FATAL, "Can not compelete destination CT migration for ctid %u uuid %s", m_nCtid, QSTR2UTF8(m_sVmUuid)); goto exit; } if (PVMT_CLONE_MODE & getRequestFlags()) m_pVmConfig->getVmIdentification()->setSourceVmUuid(m_sOriginVmUuid); if (PVMT_CLONE_MODE & getRequestFlags()) { if ( PVMT_SWITCH_TEMPLATE & getRequestFlags() ) m_pVmConfig->getVmSettings()->getVmCommonOptions()->setTemplate( !m_pVmConfig->getVmSettings()->getVmCommonOptions()->isTemplate() ); Task_CloneVm::ResetNetSettings(m_pVmConfig); } /* do not fail since here - Ct already migrated */ { QString sServerUuid = CDspService::instance()->getDispConfigGuard().getDispConfig() ->getVmServerIdentification()->getServerUuid(); QString sLastServerUuid = m_pVmConfig->getVmIdentification()->getServerUuid(); m_pVmConfig->getVmIdentification()->setServerUuid(sServerUuid); m_pVmConfig->getVmIdentification()->setLastServerUuid(sLastServerUuid); SmartPtr<CVmConfiguration> pOldConfig = CDspService::instance()->getVzHelper() ->getVzlibHelper().get_env_config(m_nCtid); if (pOldConfig == NULL) { WRITE_TRACE(DBG_FATAL, "Can't get config for CT %s [%d]", QSTR2UTF8(m_sVmUuid), m_nCtid); } else { get_op_helper().apply_env_config(m_pVmConfig, pOldConfig); } } // insert new item in user's VM Directory m_pVmConfig = CDspService::instance()->getVzHelper()->getVzlibHelper().get_env_config(m_nCtid); if (PRL_FAILED(CDspService::instance()->getVzHelper()->insertVmDirectoryItem(m_pVmConfig))) { WRITE_TRACE(DBG_FATAL, "Can't insert CT to VmDirectory by error %#x, %s", nRetCode, PRL_RESULT_TO_STRING(nRetCode) ); } exit: setLastErrorCode(nRetCode); return nRetCode; }
MainController::MainController(MainView *view, QString settingsFilePath, QObject *parent) : QObject(parent) ,m_view(view) ,m_settings(new Settings(this)) ,m_heartbeatText(HEARTBEAT_TEXT) ,m_heartbeatResponseText(HEARTBEAT_RESPONSE_TEXT) ,m_heartbeat_interval(0) ,m_hearbeatTimer(new QTimer(this)) ,m_errorTimer(new QTimer(this)) ,m_appSettings(new ApplicationSettings(this)) { connect(m_appSettings, SIGNAL(error(QString)),this, SLOT(onAppSettingsError(QString))); /* load setting from the json file */ if (m_appSettings->parseJSON(settingsFilePath)) { m_screen = new Screen(view, m_appSettings->screenSaverTimeout(), m_appSettings->screenOriginalBrigtness(), m_appSettings->screenDimBrigtness(), this); m_watchdog = new Watchdog(this, m_appSettings->enableWatchdog()); m_beep = new Beep(this); /* Define objects that can be used in qml */ m_view->rootContext()->setContextProperty("connection",this); m_view->rootContext()->setContextProperty("settings", m_settings); m_view->rootContext()->setContextProperty("screen", m_screen); m_view->rootContext()->setContextProperty("watchdog", m_watchdog); m_view->rootContext()->setContextProperty("beeper", m_beep); /* Enable or disable ack */ if (m_appSettings->enableAck()) enableLookupAck(); else disableLookupAck(); /* Enablle or disable heartbeat */ connect(m_hearbeatTimer,SIGNAL(timeout()),this, SLOT(onHeartbeatTimerTimeout())); if (m_appSettings->enableHeartbeat()) enableHeartbeat(m_appSettings->heartbeatInterval()); else disableHeartbeat(); /* Add a connection to the view statusChanged signal */ connect(m_view, SIGNAL(statusChanged(QQuickView::Status)), this, SLOT(onViewStatusChanged(QQuickView::Status))); m_clients = 0; m_enableTranslator = false; m_startUpError = ""; /* Create the TCP string servers and add connections */ int i = 0; foreach(const StringServerSetting &server, m_appSettings->stringServers()) { StringServer *stringServer = new StringServer(this, server.port(), server.parseJson(), server.translate(), server.translateId(), server.primaryConnection()); if (server.translate()) m_enableTranslator = true; connect(stringServer, SIGNAL(PrimaryConnectionAvailable()), this, SLOT(onPrimaryConnectionAvailable())); connect(stringServer, SIGNAL(MessageAvailable(QByteArray, bool, bool, QString)) , this, SLOT(onMessageAvailable(QByteArray, bool, bool, QString))); connect(stringServer, SIGNAL(ClientConnected()), this, SLOT(onClientConnected())); connect(stringServer, SIGNAL(ClientDisconnected()), this, SLOT(onClientDisconnected())); if (stringServer->Start()) { m_stringServerList.append(stringServer); i += 1; } else { delete stringServer; } } /* Create the Serial Servers and add the connections */ i = 0; foreach(const SerialServerSetting &server, m_appSettings->serialServers()) { SerialServer *serialServer = new SerialServer(server, this); connect(serialServer, SIGNAL(ClientConnected()), this, SLOT(onClientConnected())); connect(serialServer, SIGNAL(PrimaryConnectionAvailable()), this, SLOT(onPrimaryConnectionAvailable())); connect(serialServer, SIGNAL(MessageAvailable(QByteArray, bool, bool, QString)) , this, SLOT(onMessageAvailable(QByteArray, bool, bool, QString))); connect(serialServer, SIGNAL(Error(QString)), this, SLOT(showError(QString))); if (server.translate()) m_enableTranslator = true; if (serialServer->Start()) { m_serialServerList.append(serialServer); i += 1; } else { delete serialServer; } } if (m_enableTranslator){ /* Initialize the Translator object */ m_transLator = new Translator(m_appSettings->translateFile(), m_appSettings->translateMaxMapSize(), this); /* Load and parse the translate file. */ m_transLator->loadTranslations(); } /* check if we need to load a language translate file */ if (m_appSettings->languageFile().length() > 0) { loadLanguageTranslator(m_appSettings->languageFile()); } setMainViewPath(m_appSettings->mainView()); m_view->show(); }