summaryrefslogtreecommitdiff
path: root/qt/consoleserver/server.cpp
blob: 9fd532d12d82e261226d54ca7e17a98f900d98d6 (plain)
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
/*
 * Copyright Daniel Jones 2016-2018
 *
 * This file is part of RGBController.
 *
 * RGBController is free software: you can redistribute it and/or modifiy
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * RGBController is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with RGBController.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "server.h"

server::server(QObject *parent) : QObject(parent)
{
	srv = new QTcpServer();
	connect(srv, SIGNAL(newConnection()), this, SLOT(new_connection()));
	qDebug() << "server loaded but not started";
}

bool server::server_start()
{
	qDebug() << "server started";
	if (srv->listen(QHostAddress::Any, 3001))
	{
		qDebug() << "listening on 3001"; 
		return true;
	}
	else
	{
		return false;
	}
}

bool server::server_stop()
{
	srv->close();
	if (srv->isListening())
		return false;
	return true;
}

void server::new_connection()
{
	QTcpSocket *socket;
	socket = srv->nextPendingConnection();
	socket->waitForReadyRead(3000);
	handle(QString(socket->readAll()).remove(QRegExp("[\\n\\t\\r]")));
	socket->close();
	delete socket;
}

void server::handle(QString msg)
{
	qDebug() << "received" << msg;
	if (msg.contains(";"))
	{
		QStringList tmp = msg.split(";");
		for (int x = 0; x < tmp.size(); x++)
		{
			emit sendcmd(tmp.at(x));
		}
	}
	else
	{
		emit sendcmd(msg);
	}
}