/* # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg # # This program is free software distributed under the GPL version 2. # See http://openslx.org/COPYING # # If you have any feedback please consult http://openslx.org/feedback and # send your suggestions, praise, or complaints to feedback@openslx.org # # General information about OpenSLX can be found at http://openslx.org/ # ----------------------------------------------------------------------------- # src/core/pvsConnection.cpp # - ... # ----------------------------------------------------------------------------- */ #include "pvsClient.h" #include "pvsServer.h" #include "pvsConnectionManager.h" #include "src/util/consoleLogger.h" #include "src/core/vncConnection.h" #include "src/gui/mainWindow.h" #define CONST_PASS "" PVSClient::PVSClient(PVSClientConnection *newClient) { newClient->setParent(this); // so the object will be deleted when this object dies _pvsClientConnection = newClient; _vncConnection = NULL; _vncRwPasswordReceived = _vncPasswordReceived = _vncAllowed = false; _vncInitMutex = false; _hostString = newClient->getAddress(); _hostName = _hostString; // for now //_gotFrame = false; _vncPort = -1; _vncProject = false; _connectionFrame = NULL; _loginName = "unseted"; } PVSClient::~PVSClient() { if (_vncConnection) { disconnect(_vncConnection, SIGNAL(finished()), this, SLOT(vncFinished())); _vncConnection->clear(); delete _vncConnection; _vncConnection = NULL; } } /* TODO: Call this method from an extra thread. * Imo there should be one thread (probably in ConnectionManager) that does this job. * Also, this function should just receive the data etc, but the actual drawing has to happen * in the Qt main thread (the thread the window belongs to) * Right now with GTK it would, but pay attention when switching to Qt * */ void PVSClient::tick() { if (_vncInitMutex) return; if (_vncPasswordReceived && _vncAllowed && _vncRequested && _vncConnection == NULL) { vncProbe(); } } QString PVSClient::getIp() { return _hostString; } QString PVSClient::getDesktopName() { if (_vncConnection) { return _vncConnection->getDesktopName(); } return _hostName; } QString PVSClient::getUserName() { return _userName; } QString PVSClient::getLoginName() { return _loginName; } //Returns Port int PVSClient::getPort(){ return _vncPort; } bool PVSClient::getVNCAllowed() { return _vncAllowed; } void PVSClient::requestVNCConnect() { _vncRequested = true; _pvsClientConnection->push_back_send(PVSMsg(PVSCOMMAND, "VNCREQUEST", "YES")); } void PVSClient::setVncPassword(QString password) { _vncPassword = password; _vncPasswordReceived = true; } void PVSClient::setVncRwPassword(QString password) { _vncRwPassword = password; _vncRwPasswordReceived = true; } QString PVSClient::getPassword() { return _vncPassword; } QString PVSClient::getRWPassword() { return _vncRwPassword; } void PVSClient::setProject(bool value) { _vncProject = value; } void PVSClient::setVncPort(int newPort) { if (newPort < 1) { ConsoleLog writeNetwork("Client sent invalid vnc-port."); return; } _vncPort = newPort; ConsoleLog writeNetwork(QString("Received new vnc-port: ").append(int2String(newPort))); } void PVSClient::setAllowed(bool allow) { _vncAllowed = allow; if (allow) { ConsoleLog writeLine(QString("VNCConnection was granted by the Client.")); _vncRequested = true; } else { ConsoleLog writeLine(QString("VNCConnection was denied by the Client.")); _vncPort = 0; _vncPasswordReceived = false; } if(_vncProject){ if(allow){ MainWindow::getConnectionWindow()->projectStations(_hostString); } else{ _vncProject = false; } } } void PVSClient::shutDownVNC() { if (_vncConnection) { disconnect(_vncConnection, SIGNAL(finished()), this, SLOT(vncFinished())); _vncConnection->clear(); delete _vncConnection; _vncConnection = NULL; } } void PVSClient::shutDownClient() { if (_pvsClientConnection) { _pvsClientConnection->closeConnection(); } } void PVSClient::onClientDisconnected() { _pvsClientConnection = NULL; } bool PVSClient::getLocked() { // TODO: Implement! // Previously "_pvsClientConnection->isLocked()" war returned, // but that method always returned "false" and was removed now. // It really didn't make any sense to put that there anyways, it has // to be taken care of in this class. return false; } bool PVSClient::sendMessage(PVSMsgType type, QString ident, QString message) { if (_pvsClientConnection) { _pvsClientConnection->push_back_send(PVSMsg(type, (char*)ident.toUtf8().data(), message)); return true; } return false; } bool PVSClient::sendMessage(PVSMsg message) { if (_pvsClientConnection) { _pvsClientConnection->push_back_send(message); return true; } return false; } void PVSClient::vncProbe() { _vncInitMutex = true; if (_vncPasswordReceived && _vncAllowed && _vncRequested) { #ifndef local_test QString portString; if (_vncPort > 0) { portString = int2String(_vncPort); } else { portString = int2String(5900); //std port ConsoleLog writeError("WARNING: Using default vnc-port 5900"); } #else QString portString; portString = int2String(5900); #endif if (_hostString.length() > 0) { char ** args = new char*[1]; QString fullstring(_hostString); fullstring.append(":"); fullstring.append(portString); args[0] = new char[fullstring.length()+1]; std::cout << "[pvsC]connecting to: " << fullstring.toStdString() << std::endl; ConsoleLog writeNetwork(QString("connecting to: ").append(fullstring)); //strcpy(args[0], host); strcpy(args[0], fullstring.toUtf8().data()); VNCConnectInfo tmpInfo(1, args, _vncPassword); VNCConnection* newConnection = new VNCConnection(tmpInfo.getPassword()); if (newConnection) { if (newConnection->initClient(&tmpInfo)) { _vncProject = false; if (_vncConnection) _vncConnection->setDead(); _vncConnection = newConnection; connect(_vncConnection, SIGNAL(finished()), this, SLOT(vncFinished())); //TODO: comment beachten !!!!! //vncAllowed = false; // to make sure we recheck if the allowance wasnt revoked in the meantime to prevent // another connection ConsoleLog writeLine(QString("VNC connection attempt succeeded.")); MainWindow::getWindow()->getConnectionWindow()->onVNCAdd(this); } else { delete newConnection; // TODO: post on the log console ConsoleLog writeError(QString("VNC connection attempt failed.")); _vncRequested = false; // otherwise loop of death } } } } _vncInitMutex = false; } void PVSClient::vncFinished() { disconnect(_vncConnection, SIGNAL(finished()), this, SLOT(vncFinished())); delete _vncConnection; _vncConnection = NULL; _vncPort = 0; _vncPasswordReceived = false; } QVector PVSClient::getProcessesVector() { return processesVector; } void PVSClient::clearProcessesVector() { processesVector.clear(); } void PVSClient::processesVectorAdd(QString msg) { if (msg.indexOf("<#>") > 0) processesVector.append(msg); else if (msg.indexOf("vector ready") == 0) { emit processVectorReady(true); } }