summaryrefslogtreecommitdiffstats
path: root/workspace/LogReceiver/logreceiver.cpp
blob: 5c695e5cb396994ef127e0ddfea6fe450e986211 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 #include <QtGui>
 #include <QMap>
 #include <QtNetwork>
 #include <QProcess>


 #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_default")) {
		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";

	QLocalSocket * client = server ->nextPendingConnection();
	clients.insert(client, client);
	connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
	connect(client, SIGNAL(readyRead()), this, SLOT(handleNewInput()));
}

void LogReceiver::handleNewInput() {

	QObject* sender = const_cast<QObject*> (QObject::sender());
	QLocalSocket* socket = static_cast<QLocalSocket*> (sender);

	QLocalSocket * client = clients.value(socket);

	QByteArray data = client->readAll();
	QString logMsg(data);
	QString interface = logMsg.section(";",0,0);
	QString s_state = logMsg.section(";", 1, 1);
	QString s_subState = logMsg.section(";", 2, 2);
	QString msg = logMsg.section(";", 3, 3);

	qDebug() << logMsg;

	qDebug() << msg;

	int st = s_state.toInt();
	int sst = s_subState.toInt();

	switch (st) {
	case STAT_OK:
		qDebug() << "received stat_ok";
		switch (sst) {
		case DHCP_DISCOVER:
			break;
		case DHCP_OFFER:

			break;
		case DHCP_REQUEST:

			break;
		case DHCP_DECLINE:

			break;
		case DHCP_ACK:

			break;
		case DHCP_NAK:

			break;
		case DHCP_RELEASE:

			break;
		case DHCP_INFORM:

			break;
		default:
			break;
		}
		break;
	case STAT_ERROR:
		qDebug() << "received stat_error";
		break;
	default:
		qDebug() << "undefined status";
	}

	statusLabel->setText(logMsg);
}

QList<QNetworkInterface> LogReceiver::getListOfNetworkInterfaces() {
	QList<QNetworkInterface> nIList = QNetworkInterface::allInterfaces();
	QList<QNetworkInterface> result;
	foreach(QNetworkInterface nI, nIList) {
		if (((!(nI.flags() & QNetworkInterface::CanBroadcast)||
				nI.flags() & QNetworkInterface::IsLoopBack) ||
				nI.flags() & QNetworkInterface::IsPointToPoint))
		{
			continue;
		}
	   	qDebug() << nI.humanReadableName();
	   	result.append(nI);
	}
	return result;
}

void LogReceiver::runDHCPCD(QList<QNetworkInterface> &interfaces) {
	foreach(QNetworkInterface ni, interfaces) {
		QProcess * p = new QProcess(this);
        clientProcesses.insert(p->pid(),p);
		p->start(pathToDhcpcdExe,dhcpcdArguments);
		connect(p, SIGNAL(started()), this, SLOT(handleProcessStarted()));
		connect(p, SIGNAL(finished(int, QProcess::ExitStatus)),
				this, SLOT(handleProcessFinished(int, QProcess::ExitStatus)));
	}
}

void LogReceiver::handleProcessFinished(int exitCode,
		QProcess::ExitStatus exitStatus) {

	QObject* sender = const_cast<QObject*> (QObject::sender());
	QProcess* process = static_cast<QProcess*> (sender);

	QProcess * client = clientProcesses.value(process->pid());
}

void LogReceiver::handleProcessStarted() {

	QObject* sender = const_cast<QObject*> (QObject::sender());
	QProcess* process = static_cast<QProcess*> (sender);

	QProcess * client = clientProcesses.value(process->pid());
}