summaryrefslogblamecommitdiffstats
path: root/workspace/LogReceiver/logreceiver.cpp
blob: 4aeebfcddd42bc31b2fd0d5bbfe228c335491d98 (plain) (tree)
1
2
3
4
5
6
7
8
9
                 
                
                     

                    
 

                    
                    
                    



                          
                    



                                           
                         





                                                 
                                                              



















                                                                                                
                                
 
                                                                     


                                                                                 

                                       
                        


                                   






                             

                                             
 












                                                                             












                                                    
                             



                                                       
 

                                                        
                             
 
                          
 

                                     

                     


                                                

                                   
                                                

                                
                                                

                                  
                                                




                                  
                                                












                                  


                                
                      
                     


                                                  
                                   
         
 
                                       

 


                                                                             
                  


                                                                              

                                                                                  




                                                   
                                            
                                                                      
                    





                                                                   
                                                               





                                                                                              
                                             


         










                                                                                              




                                                                   
                                                                   

                                                                  

                                                                                    



                                          





                                                                       
 























































                                                                                      

 










                                                         
 #include <QtGui>
 #include <QMap>
 #include <QtNetwork>
 #include <QProcess>


 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <syslog.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);

	QList<QNetworkInterface> list = getListOfNetworkInterfaces();
	buildGui();
	addInterfacesToGroupBox(interfacesMap);
	pathToDhcpcdExe = "/home/niklas/fbgui/workspace/customdhcpcd/src/dhcpcd";
	//dhcpcdArguments.append("-d");
	QString ifName("eth1");
	runDHCPCD(list);


	setWindowTitle(tr("NetD"));
}

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);

	QString data(client->readAll());

	data = data.trimmed();

	QStringList lines = data.split("\n");

	for (int i=0; i < lines.length(); i++) {
		handleNewInputLine(lines.at(i));
	}
}

void LogReceiver::handleNewInputLine(QString data) {

	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);

	int pBar = indexToIfaceNameMap.value(interface);

	//qDebug() << logMsg;

	//qDebug() << msg;

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

	switch (st) {
	case LOG_INFO:
		qDebug() << "received LOG_INFO";
		qDebug() << sst;
		switch (sst) {
		case DHCP_DISCOVER:
			handleProgress(pBar,10);
			break;
		case DHCP_OFFER:
			handleProgress(pBar,20);
			break;
		case DHCP_REQUEST:
			handleProgress(pBar,30);
			break;
		case DHCP_DECLINE:

			break;
		case DHCP_ACK:
			handleProgress(pBar,40);
			break;
		case DHCP_NAK:

			break;
		case DHCP_RELEASE:

			break;
		case DHCP_INFORM:

			break;
		default:
			break;
		}

		qDebug() << msg;

		break;
	case LOG_ERR:
		qDebug() << "received stat_error";
		break;
	default:
		qDebug() << logMsg;
	}

	//statusLabel->setText(logMsg);
}

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

void LogReceiver::runDHCPCD(QList<QNetworkInterface> &interfaces) {
	foreach(QNetworkInterface ni, interfaces) {
		dhcpcdArguments.append(ni.humanReadableName());
		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)));
		dhcpcdArguments.removeLast();
	}
}

void LogReceiver::runDHCPCD(QString interface) {
		dhcpcdArguments.append(interface);
		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)));
		dhcpcdArguments.removeLast();
}

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

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

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

	qDebug() << "process finished: " << client->pid() << exitCode << exitStatus;
}

void LogReceiver::handleProcessStarted() {

/*
	//QObject* sender = const_cast<QObject*> (QObject::sender());
	QProcess* process = static_cast<QProcess*> (QObject::sender());
*/
	//QProcess* p = qobject_cast<QProcess * >(QObject::sender());
	//QProcess * client = clientProcesses.value(process->pid());


	qDebug() << "process started: ";
}

void LogReceiver::buildGui() {

	ndStatusLabel = new QLabel(tr("test"));
	ndStatusLabel->setSizePolicy(QSizePolicy::Expanding,
	                                    QSizePolicy::Expanding);
	ndStatusLabel->setAlignment(Qt::AlignCenter);
	ndStatusLabel->setMinimumSize(100, 20);

	// create interface group box
	createInterfaceGroupBox();

    mainLayout = new QVBoxLayout;
    mainLayout->addWidget(ndStatusLabel);
    mainLayout->addWidget(interfaceGroupBox);

    setLayout(mainLayout);
}

void LogReceiver::createInterfaceGroupBox(){
    interfaceGroupBox = new QGroupBox(tr("Interfaces"));

    interfaceGroupBoxLayout = new QVBoxLayout;
    /* add interfaces via addInterfacesToGroupBox()*/

    interfaceGroupBox->setLayout(interfaceGroupBoxLayout);
}

void LogReceiver::addInterfacesToGroupBox(QMap<int , QNetworkInterface> &interfaces) {
	for(int i = 0; i < interfaces.size(); i++) {
		QHBoxLayout *hBoxLayout = new QHBoxLayout;
	    QLabel *label = new QLabel(interfaces.value(i).humanReadableName());
	    QProgressBar *pBar = new QProgressBar(this);
	    pBar->setRange(1,100);
	    pBar->setMaximumSize(200,20);

	    progressBars.insert(i,pBar);

	    hBoxLayout->addWidget(label, Qt::AlignLeft);
	    hBoxLayout->addWidget(pBar, Qt::AlignRight);

	    interfaceGroupBoxLayout->addLayout(hBoxLayout,2);
	}
}

void LogReceiver::handleProgress(int iFaceIndex, int newValue) {
		QProgressBar * pBar = progressBars.value(iFaceIndex);
		if(newValue >= pBar->value()) {
	        pBar->setValue(newValue);
		}
		else {
		    qDebug() << "Error: new value is smaller than the old value!";
		}
}

bool LogReceiver::checkBlackList(QString i) {
	if (i.startsWith("v", Qt::CaseInsensitive)) {
	    return true;
	}
	else if(i.startsWith("d", Qt::CaseInsensitive)) {
		return true;
	}
	else {
		return false;
	}
}