summaryrefslogtreecommitdiffstats
path: root/workspace/LogReceiver/logreceiver.cpp
blob: 6988463475522eb0b30a8d7eaf4e68386004859c (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
 #include <QtGui>
 #include <QtNetwork>

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>

 #include "logreceiver.h"
 #include <qlocalserver.h>
 #include <qlocalsocket.h>
 #include "status.h"


LogReceiver::LogReceiver(QWidget *parent) :
	QDialog(parent) {
	ui.setupUi(this);

	statusLabel = new QLabel;
	quitButton = new QPushButton(tr("Quit"));
	quitButton->setAutoDefault(false);

	server = new QLocalServer(this);
	if (!server->listen("/var/tmp/qt_c_socket_test")) {
		QMessageBox::critical(this, tr("LogReceiver"), tr(
				"Unable to start the server: %1.") .arg(server->errorString()));
		close();
		return;
	}

	statusLabel->setText(tr("The server is running.\n"
		"Run the C Client example now."));

	connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
	connect(server, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));

	QHBoxLayout *buttonLayout = new QHBoxLayout;
	buttonLayout->addStretch(1);
	buttonLayout->addWidget(quitButton);
	buttonLayout->addStretch(1);

	QVBoxLayout *mainLayout = new QVBoxLayout;
	mainLayout->addWidget(statusLabel);
	mainLayout->addLayout(buttonLayout);
	setLayout(mainLayout);

	setWindowTitle(tr("Fortune Server"));
}

LogReceiver::~LogReceiver() {

}


 void LogReceiver::handleNewConnection()
 {
	 qDebug() << "New Connection arrived";

     clientSocket = server->nextPendingConnection();
     connect(clientSocket, SIGNAL(disconnected()),
             clientSocket, SLOT(deleteLater()));
     connect(clientSocket, SIGNAL(readyRead()), this, SLOT(handleNewInput()));
 }

 void LogReceiver::handleNewInput() {

	QByteArray data = clientSocket->readAll();
	char * cdata = new char[data.size() +1];
	qDebug() << data.size();
	qDebug() << strlen(cdata);
	strcpy(cdata, data.data());
	qDebug() << strlen(cdata);
	qDebug() << cdata;
	int st, sst;
	char str[40];
	sscanf(cdata, "%d;%d;%s",&st,&sst,str);

	switch (st) {
	case STAT_OK:
		qDebug() << "received stat_ok";
		break;
	case STAT_ERROR:
		qDebug() << "received stat_error";
		break;
	default:
		qDebug() << "undefined status";
	}

	qDebug() << st;
	QString logMsg(data);

	/**
	 * verarbeite den string
	 */

	qDebug() << logMsg;

	statusLabel->setText(logMsg);
}