summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/main.cpp1
-rw-r--r--client/mainwindow.cpp284
-rw-r--r--client/mainwindow.h47
-rw-r--r--client/mainwindow.ui73
-rw-r--r--client/resources.qrc6
-rw-r--r--client/restarter.pro8
6 files changed, 169 insertions, 250 deletions
diff --git a/client/main.cpp b/client/main.cpp
index 8692105..3ce6fdb 100644
--- a/client/main.cpp
+++ b/client/main.cpp
@@ -22,7 +22,6 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
- w.show();
return a.exec();
}
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
-}
diff --git a/client/mainwindow.h b/client/mainwindow.h
index 22f3222..98725a6 100644
--- a/client/mainwindow.h
+++ b/client/mainwindow.h
@@ -23,17 +23,33 @@
#include <QSslSocket>
#include <QFile>
#include <QSslKey>
+#include <QGroupBox>
+#include <QPushButton>
+#include <api_protocol.h>
-#ifdef MINIMAL
-const QString host = "192.168.0.1";
-#else
-const QString host = "gluzskaya.ru";
-#endif
+
+using namespace proto;
+
+const QString host = "127.0.0.1";
namespace Ui {
class MainWindow;
}
+struct service_ui
+{
+ QGroupBox *group;
+ std::list<QPushButton*> commands;
+ ~service_ui()
+ {
+ delete group;
+ for(std::list<QPushButton*>::iterator i = commands.begin(), end = commands.end(); i != end; ++i)
+ delete *i;
+ }
+
+
+};
+
class MainWindow : public QMainWindow
{
Q_OBJECT
@@ -46,27 +62,20 @@ protected slots:
void ssl_verify_error_handler(const QSslError);
private slots:
- void on_restart_vbox_btn_clicked();
- void restart_vbox_sock_connected();
- void reboot();
- void halt();
- void restart_cups();
- void restart_ppp();
void disconnected();
+ void connected();
+ void handle_data();
+ void handle_button_click();
- void on_reboot_clicked();
-
- void on_halt_clicked();
-
- void on_restart_cups_clicked();
-
- void on_restart_ppp_clicked();
private:
void enable_buttons(bool);
- QSslSocket *get_socket();
+ void get_socket();
+ void _connect();
Ui::MainWindow *ui;
QSslSocket *sock;
+ std::list<service_s> *services;
+ std::list<service_ui*> svc_ui_list;
};
#endif // MAINWINDOW_H
diff --git a/client/mainwindow.ui b/client/mainwindow.ui
index 5894198..b91bc22 100644
--- a/client/mainwindow.ui
+++ b/client/mainwindow.ui
@@ -11,81 +11,16 @@
</rect>
</property>
<property name="windowTitle">
- <string>Окно</string>
+ <string>Window</string>
</property>
<widget class="QWidget" name="centralWidget">
- <widget class="QPushButton" name="restart_vbox_btn">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>10</y>
- <width>181</width>
- <height>25</height>
- </rect>
- </property>
- <property name="text">
- <string>Перезапустить vm (1c)</string>
- </property>
- </widget>
- <widget class="QPushButton" name="reboot">
- <property name="geometry">
- <rect>
- <x>520</x>
- <y>320</y>
- <width>161</width>
- <height>25</height>
- </rect>
- </property>
- <property name="text">
- <string>Перезагрузить сервер</string>
- </property>
- </widget>
- <widget class="QPushButton" name="halt">
- <property name="geometry">
- <rect>
- <x>520</x>
- <y>350</y>
- <width>161</width>
- <height>25</height>
- </rect>
- </property>
- <property name="text">
- <string>Выключить серверер</string>
- </property>
- </widget>
- <widget class="QPushButton" name="restart_cups">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>40</y>
- <width>231</width>
- <height>25</height>
- </rect>
- </property>
- <property name="text">
- <string>Перезапустить сервер печати</string>
- </property>
- </widget>
- <widget class="QPushButton" name="restart_ppp">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>70</y>
- <width>191</width>
- <height>25</height>
- </rect>
- </property>
- <property name="text">
- <string>Перезапустить интернет</string>
- </property>
- </widget>
<widget class="QLabel" name="state_lbl">
<property name="geometry">
<rect>
<x>10</x>
- <y>370</y>
- <width>491</width>
- <height>16</height>
+ <y>315</y>
+ <width>661</width>
+ <height>71</height>
</rect>
</property>
<property name="text">
diff --git a/client/resources.qrc b/client/resources.qrc
index 34e2d9e..3c76c1e 100644
--- a/client/resources.qrc
+++ b/client/resources.qrc
@@ -1,7 +1,7 @@
<RCC>
<qresource prefix="/">
- <file>res/ca.crt</file>
- <file>res/client.key</file>
- <file>res/client.crt</file>
+ <file>ca.crt</file>
+ <file>cli.crt</file>
+ <file>cli.key</file>
</qresource>
</RCC>
diff --git a/client/restarter.pro b/client/restarter.pro
index 31052b5..90adbf7 100644
--- a/client/restarter.pro
+++ b/client/restarter.pro
@@ -19,10 +19,16 @@ FORMS += mainwindow.ui
#DEFINES += MINIMAL
+INCLUDEPATH += ../proto_lib
+
+debug:DEFINES += DEBUG
+
QMAKE_CXXFLAGS += -Os -fomit-frame-pointer -std=gnu++0x
QMAKE_CFLAGS += -Os -fomit-frame-pointer -std=gnu99
-LIBS += -Wl,-O1
+
+debug:LIBS += -L../proto_lib/Debug
+LIBS += -lproto -Wl,-O1
RESOURCES += \
resources.qrc