#include #include #include #include #include #include #include "logreceiver.h" #include #include #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"; 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::sender()); QLocalSocket* socket = static_cast (sender); QLocalSocket * client = clients.value(socket); QByteArray data = client->readAll(); QString logMsg(data); QString s_state = logMsg.section(";", 0, 0); QString s_subState = logMsg.section(";", 1, 1); QString msg = logMsg.section(";", 2, 2); 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); }