summaryrefslogtreecommitdiffstats
path: root/src/core/pvsClient.cpp
blob: 0448f97568b85f0463a52acbbe3ae6f3320b2d81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
# 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<QString> 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);
	}
}