Began removal of platform/. The Monitor:: namespace is completely converted.
[aesalon.git] / src / gui / ActiveSessionSocket.cpp
blob73553bbc4f0a320fea1ca932bfa5805cf086ac55
1 #include <iostream>
2 #include <QTimer>
3 #include "ActiveSessionSocket.h"
4 #include "ActiveSessionSocket.moc"
6 namespace Aesalon {
7 namespace GUI {
9 ActiveSessionSocket::ActiveSessionSocket(QString host, int port, Platform::Memory *memory) : memory(memory), host(host), port(port) {
10 socket = new QTcpSocket();
12 socket->connectToHost(host, port);
13 connect(socket, SIGNAL(readyRead()), this, SLOT(handle_data()));
14 connect(socket, SIGNAL(connected()), this, SLOT(reemit_connected()));
15 connect(socket, SIGNAL(disconnected()), this, SLOT(reemit_disconnected()));
16 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error_caught(QAbstractSocket::SocketError)));
19 ActiveSessionSocket::~ActiveSessionSocket() {
22 void ActiveSessionSocket::handle_data() {
23 QByteArray data = socket->readAll();
24 QString str = data;
25 while(data.length()) {
26 QString string = data;
27 data.remove(0, string.length()+1);
28 Platform::Event *event = Platform::Event::deserialize(string.toStdString());
29 if(event) {
30 memory->handle_event(event);
31 emit event_received(event);
32 delete event;
37 void ActiveSessionSocket::error_caught(QAbstractSocket::SocketError error) {
38 /* if it's a timeout or connection-refused, try reconnecting in 500ms . . . */
39 if(error == QAbstractSocket::SocketTimeoutError) {
40 QTimer::singleShot(500, this, SLOT(try_connecting()));
42 else if(error == QAbstractSocket::ConnectionRefusedError) {
43 QTimer::singleShot(500, this, SLOT(try_connecting()));
47 void ActiveSessionSocket::try_connecting() {
48 socket->connectToHost(host, port);
51 } // namespace GUI
52 } // namespace Aesalon