diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-15 20:53:16 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-15 20:53:16 +0200 |
commit | 4a2dd17aa0601fffd42f2ec1c92c6a1b79f648d3 (patch) | |
tree | 810c02c1cdd244aebdfebdba888a2a70158ab138 /client/mainwindow.cpp | |
parent | eda5eaf4a1be590b9e4905f322b3e0923f65d312 (diff) |
work on client started (services retrieving implemented)
server services packet handling implemented
Diffstat (limited to 'client/mainwindow.cpp')
-rw-r--r-- | client/mainwindow.cpp | 284 |
1 files changed, 127 insertions, 157 deletions
diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index 428d88b..943e04d 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -18,126 +18,167 @@ #include "mainwindow.h" #include "ui_mainwindow.h" + + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + sock(NULL) { ui->setupUi(this); -#ifdef MINIMAL - ui->halt->hide(); - ui->reboot->hide(); - ui->reboot->hide(); - ui->restart_cups->hide(); - ui->restart_ppp->hide(); - ui->state_lbl->hide(); - this->setFixedSize(ui->restart_vbox_btn->geometry().width() +17, ui->restart_vbox_btn->geometry().height() +17); -#else + this->setWindowTitle(tr("Client")); this->setFixedSize(this->size()); -#endif + show(); + ui->state_lbl->setVisible(true); ui->state_lbl->setText(" "); + ui->state_lbl->setText(tr("Preparing..")); + get_socket(); + _connect(); + ui->state_lbl->setText(QString(" ")); } -QSslSocket *MainWindow::get_socket() +void MainWindow::get_socket() { - QSslSocket *s = new QSslSocket; - QFile in(":/res/ca.crt"); - in.open(QIODevice::ReadOnly); - QByteArray buf = in.readAll(); - in.close(); - QList<QSslCertificate> ca_list; - ca_list.push_back(QSslCertificate(buf)); - s->setCaCertificates(ca_list); - in.setFileName(":/res/client.crt"); - in.open(QIODevice::ReadOnly); - buf = in.readAll(); - in.close(); - QSslCertificate c(buf); - s->setLocalCertificate(c); - in.setFileName(":/res/client.key"); - in.open(QIODevice::ReadOnly); - buf = in.readAll(); - QSslKey key(buf, QSsl::Rsa); - s->setPrivateKey(key); - s->setPeerVerifyMode(QSslSocket::VerifyPeer); - s->setProtocol(QSsl::SslV3); - connect(s, SIGNAL(peerVerifyError(QSslError)), this, SLOT(ssl_verify_error_handler(QSslError))); - - return s; + if(!sock) + { + sock = new QSslSocket; + QFile in(":/ca.crt"); + in.open(QIODevice::ReadOnly); + QByteArray buf = in.readAll(); + in.close(); + QList<QSslCertificate> ca_list; + ca_list.push_back(QSslCertificate(buf)); + sock->setCaCertificates(ca_list); + in.setFileName(":/cli.crt"); + in.open(QIODevice::ReadOnly); + buf = in.readAll(); + in.close(); + QSslCertificate c(buf); + sock->setLocalCertificate(c); + in.setFileName(":/cli.key"); + in.open(QIODevice::ReadOnly); + buf = in.readAll(); + QSslKey key(buf, QSsl::Rsa); + sock->setPrivateKey(key); + sock->setPeerVerifyMode(QSslSocket::VerifyPeer); + sock->setProtocol(QSsl::SslV3); + connect(sock, SIGNAL(peerVerifyError(QSslError)), this, SLOT(ssl_verify_error_handler(QSslError))); + } } -void MainWindow::ssl_verify_error_handler(const QSslError error) +void MainWindow::_connect() { - switch(error.error()) + if(!sock) { - case QSslError::InvalidCaCertificate: case QSslError::NoPeerCertificate: case QSslError::UnspecifiedError: - case QSslError::AuthorityIssuerSerialNumberMismatch: - ui->state_lbl->setText(QString::fromUtf8("Ошибка соединения !")); - sock->abort(); + ui->state_lbl->setText(tr("Socket error..")); return; - default: - break; } - sock->ignoreSslErrors(); + ui->state_lbl->setText(tr("Connecting..")); + sock->connectToHostEncrypted(host, 1313); + connect(sock, SIGNAL(encrypted()), SLOT(connected())); + connect(sock, SIGNAL(readyRead()), SLOT(handle_data())); + connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); } - -MainWindow::~MainWindow() +void MainWindow::connected() { - delete ui; + packet *p = packet::cli_make_init_packet(); + unsigned char *b = p->buf(); + sock->write((char*)b, p->raw().size()); + delete [] b; + delete p; } -void MainWindow::enable_buttons(bool enable) +void MainWindow::handle_data() { - ui->restart_vbox_btn->setEnabled(enable); - ui->reboot->setEnabled(enable); - ui->halt->setEnabled(enable); - ui->restart_cups->setEnabled(enable); - ui->restart_ppp->setEnabled(enable); - ui->restart_vbox_btn->setEnabled(enable); -} + QByteArray a = sock->readAll(); + std::vector<unsigned char> v; + v.assign(a.data(), a.data() + a.size()); + packet p(v); + if(!p.is_good()) + { +#ifdef DEBUG + QMessageBox msg; + msg.setText("bad packet"); + msg.exec(); +#endif + return; + } + if(!p.is_server_packet()) + { +#ifdef DEBUG + QMessageBox msg; + msg.setText("not server packet"); + msg.exec(); +#endif + return; + } + switch(p.get_type()) + { + case TYPE_SERVICES: + { //TODO: controls positioning + services = packet::cli_extract_services(p); + for(std::list<service_s>::iterator i = services->begin(), end = services->end(); i != end; ++i) + { + service_ui *u = new service_ui; + u->group = new QGroupBox(i->service.c_str(), this); + u->group->show(); + for(std::list<service_s::cmd>::iterator ii = i->cmds.begin(), eend = i->cmds.end(); ii != eend; ++ii) + { + u->commands.push_back(new QPushButton(QString::fromUtf8(ii->command.c_str()), u->group)); + connect(u->commands.back(), SIGNAL(clicked()), SLOT(handle_button_click())); + u->commands.back()->setToolTip(QString::fromUtf8(ii->description.c_str())); + u->commands.back()->show(); + } + svc_ui_list.push_back(u); + } + } + break; + case TYPE_AUTH: + { + if(p.get_status() == STATUS_SUCCESS) + { + packet *p = packet::cli_make_request_services_packet(); + unsigned char *b = p->buf(); + sock->write((char*)b, p->raw().size()); + delete [] b; + delete p; + } + } + break; + default: break; -void MainWindow::on_restart_vbox_btn_clicked() -{ - ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); - sock = get_socket(); - ui->state_lbl->setText(QString::fromUtf8("Соединение..")); - sock->connectToHostEncrypted(host, 1313); - connect(sock, SIGNAL(encrypted()), SLOT(restart_vbox_sock_connected())); - connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); + }; } -void MainWindow::restart_vbox_sock_connected() -{ - ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); - enable_buttons(false); - sock->write("restart vbox\0"); -} -void MainWindow::reboot() +void MainWindow::handle_button_click() { - ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); - enable_buttons(false); - sock->write("reboot now\0"); + //TODO + } -void MainWindow::halt() +void MainWindow::ssl_verify_error_handler(const QSslError error) { - ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); - enable_buttons(false); - sock->write("halt now\0"); + switch(error.error()) + { + case QSslError::InvalidCaCertificate: case QSslError::NoPeerCertificate: case QSslError::UnspecifiedError: + case QSslError::AuthorityIssuerSerialNumberMismatch: + ui->state_lbl->setText(tr("Connection error")); + sock->abort(); + return; + default: + break; + } + sock->ignoreSslErrors(); } -void MainWindow::restart_cups() +MainWindow::~MainWindow() { - ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); - enable_buttons(false); - sock->write("restart cups\0"); + delete ui; } -void MainWindow::restart_ppp() +void MainWindow::enable_buttons(bool enable) { - ui->state_lbl->setText(QString::fromUtf8("Выполнение..")); - enable_buttons(false); - sock->write("restart ppp\0"); } @@ -148,74 +189,3 @@ void MainWindow::disconnected() sock->deleteLater(); } -void MainWindow::on_reboot_clicked() -{ -#ifndef MINIMAL - QMessageBox msg; - msg.setText(QString::fromUtf8("Вы видели куда ткнули ?")); - msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); - if(msg.exec() == QMessageBox::Yes) - { - QMessageBox msg; - msg.setText(QString::fromUtf8("Уверены что хотите перезагрузить сервер ?")); - msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); - if(msg.exec() == QMessageBox::Yes) - { - ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); - sock = get_socket(); - ui->state_lbl->setText(QString::fromUtf8("Соединение..")); - sock->connectToHostEncrypted(host, 1313); - connect(sock, SIGNAL(encrypted()), SLOT(reboot())); - connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); - } - } -#endif -} - -void MainWindow::on_halt_clicked() -{ -#ifndef MINIMAL - QMessageBox msg; - msg.setText(QString::fromUtf8("Вы видели куда ткнули ?")); - msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); - if(msg.exec() == QMessageBox::Yes) - { - QMessageBox msg; - msg.setText(QString::fromUtf8("Уверены что хотите выключить сервер ?")); - msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No); - if(msg.exec() == QMessageBox::Yes) - { - ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); - sock = get_socket(); - ui->state_lbl->setText(QString::fromUtf8("Соединение..")); - sock->connectToHostEncrypted(host, 1313); - connect(sock, SIGNAL(encrypted()), SLOT(halt())); - connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); - } - } -#endif -} - -void MainWindow::on_restart_cups_clicked() -{ -#ifndef MINIMAL - ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); - sock = get_socket(); - ui->state_lbl->setText(QString::fromUtf8("Соединение..")); - sock->connectToHostEncrypted(host, 1313); - connect(sock, SIGNAL(encrypted()), SLOT(restart_cups())); - connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); -#endif -} - -void MainWindow::on_restart_ppp_clicked() -{ -#ifndef MINIMAL - ui->state_lbl->setText(QString::fromUtf8("Подготовка..")); - sock = get_socket(); - ui->state_lbl->setText(QString::fromUtf8("Соединение..")); - sock->connectToHostEncrypted(host, 1313); - connect(sock, SIGNAL(encrypted()), SLOT(restart_ppp())); - connect(sock, SIGNAL(disconnected()), SLOT(disconnected())); -#endif -} |