Skip to content

Commit

Permalink
Move VNC client init to app
Browse files Browse the repository at this point in the history
Refactored init method
Added stub for VNC athentication
  • Loading branch information
Andrii Moroz authored and Andrii Moroz committed Aug 2, 2013
1 parent a9a209d commit f48eae8
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 28 deletions.
20 changes: 19 additions & 1 deletion src/Test/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,25 @@ int main(int argc, char *argv[])
routeTableWithShutdownCommand->Add<webdriver::ShutdownCommand>(shutdownCommandRoute);
wd_server->SetRouteTable(routeTableWithShutdownCommand);


// start VNC module
CommandLine cmdLine = webdriver::Server::GetInstance()->GetCommandLine();
if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer) || cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
{
QString address = "127.0.0.1";
int port = 5900;

if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer))
address = cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCServer).c_str();
if (cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
port = QString(cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCPort).c_str()).toInt();

VNCClient *client = VNCClient::getInstance();
if (!client->isReady())
client->Init(address, port);

WDEventDispatcher::getInstance()->add(new VNCEventDispatcher(client));
}

setQtSettings();
wd_server->Start();

Expand Down
94 changes: 86 additions & 8 deletions src/vnc/vncclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ VNCClient::VNCClient()
: _socket(NULL),
_versionEstablished(false),
_securityEstablished(false),
_autenticationPassed(false),
_handshakeFinished(false),
_communicationError(false),
_isReady(false),
_establishedVersion(38),
_establishedSecurity(Invalid),
_serverParameters(NULL)
_serverParameters(NULL),
_password(NULL)
{
}

VNCClient::~VNCClient()
{
delete _serverParameters;
delete _password;
}

VNCClient* VNCClient::getInstance()
Expand All @@ -94,7 +97,30 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
addr.setAddress(remoteHost);
}

// QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
QObject::connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(onError(QAbstractSocket::SocketError)));
QObject::connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(onStateChanged(QAbstractSocket::SocketState)));

_socket->connectToHost(addr, port);
_socket->waitForConnected();

return _socket->isOpen();
}

bool VNCClient::Init(QString remoteHost, quint16 port, QString* password)
{
_password = password;
_socket = new QTcpSocket();
QHostAddress addr;

if (!addr.setAddress(remoteHost))
{
remoteHost.replace(QRegExp("http*://"), "");
addr.setAddress(remoteHost);
}

// QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
QObject::connect(_socket, SIGNAL(readyRead()), this, SLOT(readSocket()));
Expand All @@ -103,6 +129,9 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
QObject::connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(onStateChanged(QAbstractSocket::SocketState)));

_socket->connectToHost(addr, port);
_socket->waitForConnected();

return _socket->isOpen();
}

Expand Down Expand Up @@ -130,6 +159,12 @@ QByteArray VNCClient::readSocket()
establishSecurity(data);
return data;
}

// Go through autentication
if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
{
// todo
}
if (!_handshakeFinished)
{
finishHandshaking(data);
Expand Down Expand Up @@ -164,6 +199,12 @@ QByteArray VNCClient::readSocket(qint64 size)
establishSecurity(data);
return data;
}

// Go through autentication
if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
{
// todo
}
if (!_handshakeFinished)
{
finishHandshaking(data);
Expand All @@ -179,8 +220,17 @@ QByteArray VNCClient::readSocket(qint64 size)

qint64 VNCClient::writeToSocket(QByteArray &data)
{
int bytesNmb = _socket->write(data);
_socket->flush();
int bytesNmb = 0;

if (QAbstractSocket::ConnectedState == _socket->state())
{
bytesNmb = _socket->write(data);
_socket->flush();
}
else
{
std::cout << "#### Socket isn't in connected state. Couldn't write to socket" << std::endl;
}

return bytesNmb;
}
Expand Down Expand Up @@ -304,15 +354,27 @@ bool VNCClient::establishSecurity(QByteArray& data)
}
case None:
{
char one = 0x01;
if (NULL == _password)
{
char one = 0x01;
QByteArray response(1, one);
writeToSocket(response);
_securityEstablished = true;
_establishedSecurity = None;
return true;
}
break;
}
case VNCAuthentication:
{
char one = 0x02;
QByteArray response(1, one);
writeToSocket(response);
_securityEstablished = true;
_establishedSecurity = None;
return true;
break;
}
case VNCAuthentication: break;
case RA2: break;
case RA2ne: break;
case Tight: break;
Expand All @@ -337,15 +399,31 @@ bool VNCClient::establishSecurity(QByteArray& data)
break;
}
case None:
{
if (NULL == _password)
{
_securityEstablished = true;
_establishedSecurity = None;
return true;
}
break;
}
case VNCAuthentication:
{
_securityEstablished = true;
_establishedSecurity = None;
_establishedSecurity = VNCAuthentication;
return true;
break;
}
case VNCAuthentication: break;
break;
}
}

return false;
}

bool VNCClient::passAutentication(QByteArray &data)
{
return false;
}

bool VNCClient::finishHandshaking(QByteArray &data)
Expand Down
5 changes: 5 additions & 0 deletions src/vnc/vncclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class VNCClient : public QObject
static VNCClient* getInstance();

bool Init(QString remoteHost, quint16 port);
bool Init(QString remoteHost, quint16 port, QString* password);
void sendKeyEvent(QKeyEvent *key);
void sendMouseEvent(QMouseEvent *mouse);
bool isReady();
Expand All @@ -35,6 +36,7 @@ private slots:

bool establishProtocolVersion(QByteArray& data);
bool establishSecurity(QByteArray& data);
bool passAutentication(QByteArray& data);
bool finishHandshaking(QByteArray& data);
bool initServerParameters(QByteArray& data);
void sendDoubleClick(QMouseEvent *event);
Expand Down Expand Up @@ -75,12 +77,15 @@ private slots:
QTcpSocket *_socket;
bool _versionEstablished;
bool _securityEstablished;
bool _autenticationPassed;
bool _handshakeFinished;
bool _communicationError;
bool _isReady;
int _establishedVersion;
Encodings _establishedSecurity;
ServerParameters* _serverParameters;

QString *_password;
};

#endif // VNCCLIENT_H
19 changes: 0 additions & 19 deletions src/webdriver/extension_qt/q_session_lifecycle_actions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,6 @@ Error* QSessionLifeCycleActions::PostInit(const base::DictionaryValue* desired_c
session_->logger().Log(kInfoLogLevel, "no proxy settings requsted.");
}

// start VNC module
CommandLine cmdLine = webdriver::Server::GetInstance()->GetCommandLine();
if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer) || cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
{
QString address = "127.0.0.1";
int port = 5900;

if (cmdLine.HasSwitch(webdriver::Switches::kVNCServer))
address = cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCServer).c_str();
if (cmdLine.HasSwitch(webdriver::Switches::kVNCPort))
port = QString(cmdLine.GetSwitchValueASCII(webdriver::Switches::kVNCPort).c_str()).toInt();

VNCClient *client = VNCClient::getInstance();
if (!client->isReady())
client->Init(address, port);

WDEventDispatcher::getInstance()->add(new VNCEventDispatcher(client));
}

return error;
}

Expand Down

0 comments on commit f48eae8

Please sign in to comment.