Removed the event_received() signal/slot mechanism in DataReceiver.
[aesalon.git] / gui / src / data / NetworkReceiver.cpp
blobd4242a23da57a59aa3d12dae70040d92ab4ebec7
2 #include "NetworkReceiver.h"
3 #include "NetworkReceiver.moc"
4 #include "storage/AllocEvent.h"
5 #include "storage/FreeEvent.h"
7 NetworkReceiver::NetworkReceiver(DataThread *data_thread, QString host, quint16 port) : DataReceiver(data_thread), host(host), port(port) {
8 tcp_socket = new QTcpSocket(this);
9 tcp_socket->connectToHost(host, port);
10 connect(tcp_socket, SIGNAL(readyRead()), this, SLOT(data_received()));
11 connect(tcp_socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
12 connect(tcp_socket, SIGNAL(connected()), SLOT(connected()));
15 NetworkReceiver::~NetworkReceiver() {
18 quint64 NetworkReceiver::pop_quint64() {
19 quint64 ret = 0;
20 ret |= unprocessed.at(0);
21 ret |= quint64(quint8(unprocessed.at(1))) << 8;
22 ret |= quint64(quint8(unprocessed.at(2))) << 16;
23 ret |= quint64(quint8(unprocessed.at(3))) << 24;
24 ret |= quint64(quint8(unprocessed.at(4))) << 32;
25 ret |= quint64(quint8(unprocessed.at(5))) << 40;
26 ret |= quint64(quint8(unprocessed.at(6))) << 48;
27 ret |= quint64(quint8(unprocessed.at(7))) << 56;
28 unprocessed.remove(0, 8);
29 return ret;
32 void NetworkReceiver::data_received() {
33 QByteArray received = tcp_socket->readAll();
34 unprocessed += received;
35 while(unprocessed.size()) {
36 quint8 type_byte = unprocessed.at(0);
37 unprocessed.remove(0, 1);
38 /* If the first bit is set, then it's a block event . . . */
39 if(type_byte & 0x01) {
40 quint8 block_type = (type_byte & 0x06) >> 1;
41 quint64 address = pop_quint64();
42 if(block_type == 0) {
43 emit event_received(new AllocEvent(address, pop_quint64()));
45 else if(block_type == 1) {
46 emit event_received(new FreeEvent(address));
47 emit event_received(new AllocEvent(pop_quint64(), pop_quint64()));
49 else if(block_type == 2) {
50 emit event_received(new FreeEvent(address));
56 void NetworkReceiver::connected() {
57 emit begun();
60 void NetworkReceiver::disconnected() {
61 emit finished();