Browse Source

add serverctrl.

yuchuli 1 year ago
parent
commit
bc9084af52

+ 73 - 0
src/tool/serverctrl/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 11 - 0
src/tool/serverctrl/main.cpp

@@ -0,0 +1,11 @@
+#include "mainwindow.h"
+
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    MainWindow w;
+    w.show();
+    return a.exec();
+}

+ 120 - 0
src/tool/serverctrl/mainwindow.cpp

@@ -0,0 +1,120 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+using namespace QSsh;
+
+#include <iostream>
+
+MainWindow::MainWindow(QWidget *parent)
+    : QMainWindow(parent)
+    , ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+
+    SshConnectionParameters param;
+    param.setHost("223.84.137.240");
+    param.setUserName("ykjs");
+    param.setPassword("admin@123");
+    param.setPort(4096);
+    param.authenticationType = SshConnectionParameters::AuthenticationTypeTryAllPasswordBasedMethods;
+    param.timeout = 30;
+
+    mpRP = new RemoteProcess(param);
+
+    connect(mpRP,SIGNAL(ServiceState(int )),this,SLOT(onUpdateState(int)));
+
+
+    ResetPush();
+
+    mpTimer = new QTimer(this);
+    mpTimer->setInterval(2000);
+    connect(mpTimer,SIGNAL(timeout()),this,SLOT(onTimer()));
+//    mpRP->run();
+}
+
+MainWindow::~MainWindow()
+{
+    delete mpRP;
+    delete ui;
+}
+
+
+void MainWindow::on_pushButton_ConnectServer_clicked()
+{
+    mpRP->run();
+}
+
+void MainWindow::onUpdateState(int nState)
+{
+    ui->pushButton_StopRM->setEnabled(false);
+    ui->pushButton_StartRM->setEnabled(false);
+    ui->pushButton_StartGroup->setEnabled(false);
+    ui->pushButton_StopGroup->setEnabled(false);
+
+    switch (nState) {
+    case 0:
+        ui->pushButton_StartRM->setEnabled(true);
+        ui->pushButton_StartGroup->setEnabled(true);
+        break;
+    case 1:
+        ui->pushButton_StopRM->setEnabled(true);
+        ui->pushButton_StartGroup->setEnabled(true);
+        break;
+    case 2:
+        ui->pushButton_StartRM->setEnabled(true);
+        ui->pushButton_StopGroup->setEnabled(true);
+        break;
+    case 3:
+        ui->pushButton_StopRM->setEnabled(true);
+        ui->pushButton_StopGroup->setEnabled(true);
+        break;
+    default:
+        break;
+
+    }
+
+    ui->pushButton_ConnectServer->setEnabled(false);
+}
+
+void MainWindow::on_pushButton_StartRM_clicked()
+{
+    ResetPush();
+    mpRP->runCmd("./startrm.sh");
+    mpTimer->start();
+}
+
+void MainWindow::on_pushButton_StartGroup_clicked()
+{
+    ResetPush();
+    mpRP->runCmd("./startgroup.sh");
+    mpTimer->start();
+}
+
+void MainWindow::on_pushButton_StopRM_clicked()
+{
+    ResetPush();
+    mpRP->runCmd("./stoprm.sh");
+    mpTimer->start();
+}
+
+void MainWindow::on_pushButton_StopGroup_clicked()
+{
+    ResetPush();
+    mpRP->runCmd("./stopgroup.sh");
+    mpTimer->start();
+}
+
+void MainWindow::onTimer()
+{
+    std::cout<<"timer."<<std::endl;
+    mpRP->run();
+    mpTimer->stop();
+}
+
+void MainWindow::ResetPush()
+{
+    ui->pushButton_StopRM->setEnabled(false);
+    ui->pushButton_StartRM->setEnabled(false);
+    ui->pushButton_StartGroup->setEnabled(false);
+    ui->pushButton_StopGroup->setEnabled(false);
+
+}

+ 46 - 0
src/tool/serverctrl/mainwindow.h

@@ -0,0 +1,46 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+#include <QTimer>
+
+#include "remoteprocess.h"
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class MainWindow; }
+QT_END_NAMESPACE
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    MainWindow(QWidget *parent = nullptr);
+    ~MainWindow();
+
+private slots:
+    void on_pushButton_ConnectServer_clicked();
+
+    void onUpdateState(int nState);
+
+    void on_pushButton_StartRM_clicked();
+
+    void on_pushButton_StartGroup_clicked();
+
+    void on_pushButton_StopRM_clicked();
+
+    void on_pushButton_StopGroup_clicked();
+
+    void onTimer();
+
+private:
+    Ui::MainWindow *ui;
+
+    RemoteProcess* mpRP;
+
+    QTimer * mpTimer;
+
+    void ResetPush();
+};
+#endif // MAINWINDOW_H

+ 107 - 0
src/tool/serverctrl/mainwindow.ui

@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>768</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralwidget">
+   <widget class="QPushButton" name="pushButton_ConnectServer">
+    <property name="geometry">
+     <rect>
+      <x>50</x>
+      <y>30</y>
+      <width>151</width>
+      <height>51</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>连接服务器</string>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="pushButton_StartRM">
+    <property name="geometry">
+     <rect>
+      <x>99</x>
+      <y>110</y>
+      <width>231</width>
+      <height>81</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>开启远程驾驶服务</string>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="pushButton_StopRM">
+    <property name="geometry">
+     <rect>
+      <x>443</x>
+      <y>110</y>
+      <width>231</width>
+      <height>81</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>关闭远程驾驶服务</string>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="pushButton_StartGroup">
+    <property name="geometry">
+     <rect>
+      <x>99</x>
+      <y>230</y>
+      <width>231</width>
+      <height>81</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>开启编队服务</string>
+    </property>
+   </widget>
+   <widget class="QPushButton" name="pushButton_StopGroup">
+    <property name="geometry">
+     <rect>
+      <x>444</x>
+      <y>230</y>
+      <width>231</width>
+      <height>81</height>
+     </rect>
+    </property>
+    <property name="text">
+     <string>关闭编队服务</string>
+    </property>
+   </widget>
+   <widget class="QPlainTextEdit" name="plainTextEdit">
+    <property name="geometry">
+     <rect>
+      <x>50</x>
+      <y>340</y>
+      <width>671</width>
+      <height>171</height>
+     </rect>
+    </property>
+   </widget>
+  </widget>
+  <widget class="QMenuBar" name="menubar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>768</width>
+     <height>28</height>
+    </rect>
+   </property>
+  </widget>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 140 - 0
src/tool/serverctrl/remoteprocess.cpp

@@ -0,0 +1,140 @@
+#include "remoteprocess.h"
+
+
+#include <iostream>
+#include <thread>
+
+using namespace QSsh;
+
+const QByteArray StderrOutput("ChannelTest");
+
+RemoteProcess::RemoteProcess(const QSsh::SshConnectionParameters &params)
+    : m_sshParams(params),
+      m_timeoutTimer(new QTimer(this)),
+      m_sshConnection(0),
+      m_remoteRunner(new SshRemoteProcessRunner(this)),
+      m_state(Inactive)
+{
+
+    connect(m_remoteRunner, SIGNAL(connectionError()),
+        SLOT(handleConnectionError()));
+    connect(m_remoteRunner, SIGNAL(processStarted()),
+        SLOT(handleProcessStarted()));
+    connect(m_remoteRunner, SIGNAL(readyReadStandardOutput()), SLOT(handleProcessStdout()));
+    connect(m_remoteRunner, SIGNAL(readyReadStandardError()), SLOT(handleProcessStderr()));
+    connect(m_remoteRunner, SIGNAL(processClosed(int)),
+        SLOT(handleProcessClosed(int)));
+    m_started = false;
+
+ //   m_remoteRunner->run("ls -a /tmp", m_sshParams);
+}
+
+RemoteProcess::~RemoteProcess()
+{
+    if(m_started)
+    {
+        delete m_remoteRunner;
+
+    }
+}
+
+void RemoteProcess::run()
+{
+
+
+//    std::cout << "Testing successful remote process... " << std::flush;
+//    m_state = TestingSuccess;
+
+    mbQueryState = true;
+//    m_timeoutTimer->start();
+    m_remoteRunner->run("ps -ef | grep driver_", m_sshParams);
+}
+
+void RemoteProcess::runCmd(QString strcmd)
+{
+    QByteArray ba(strcmd.toLatin1().data());
+    m_remoteRunner->run(ba, m_sshParams);
+}
+
+void RemoteProcess::handleConnectionError()
+{
+    const QString error = m_state == TestingIoDevice || m_state == TestingProcessChannels
+        ? m_sshConnection->errorString() : m_remoteRunner->lastConnectionErrorString();
+
+    std::cerr << "Error: Connection failure (" << qPrintable(error) << ")." << std::endl;
+}
+
+void RemoteProcess::handleProcessStarted()
+{
+    if (m_started) {
+ //       std::cerr << "Error: Received started() signal again." << std::endl;
+    } else {
+        m_started = true;
+        std::cout<<" connected."<<std::endl;
+    }
+}
+
+void RemoteProcess::handleProcessStdout()
+{
+    if (!m_started) {
+        std::cerr << "Error: Remote output from non-started process."
+            << std::endl;
+ //       QCoreApplication::exit(EXIT_FAILURE);
+    }
+    else {
+        m_remoteStdout = m_remoteRunner->readAllStandardOutput();
+        std::cout<<" out: "<<m_remoteStdout.data()<<std::endl;
+        if(mbQueryState)
+        {
+            mbQueryState = false;
+            QString str = m_remoteStdout;
+            bool brm = false;
+            bool bgr = false;
+            int nState = 0;
+            if(str.indexOf("/driver_cloud_grpc_server_h264")>=0)
+            {
+                brm = true;
+
+            }
+            if(str.indexOf("/driver_group_grpc_server")>=0)
+            {
+                bgr = true;
+            }
+            if(brm&&bgr)
+            {
+                nState = 3;
+            }
+            else
+            {
+                if(brm)
+                {
+                    nState = 1;
+                }
+                else
+                {
+                    if(bgr)
+                        nState = 2;
+                }
+            }
+
+            emit ServiceState(nState);
+        }
+    }
+}
+
+void RemoteProcess::handleProcessStderr()
+{
+    if (!m_started) {
+        std::cerr << "Error: Remote error output from non-started process."
+            << std::endl;
+
+    } else {
+        m_remoteStderr = m_remoteRunner->readAllStandardError();
+        std::cout<<"error: "<<m_remoteStderr.data()<<std::endl;
+    }
+}
+
+void RemoteProcess::handleProcessClosed(int exitStatus)
+{
+    std::cout<<" process close. "<<std::endl;
+}

+ 66 - 0
src/tool/serverctrl/remoteprocess.h

@@ -0,0 +1,66 @@
+#ifndef REMOTEPROCESS_H
+#define REMOTEPROCESS_H
+
+#include <QObject>
+
+#include <qssh/sshremoteprocessrunner.h>
+
+#include <QObject>
+#include <QScopedPointer>
+#include <QTimer>
+
+QT_FORWARD_DECLARE_CLASS(QTextStream)
+QT_FORWARD_DECLARE_CLASS(QTimer)
+
+class RemoteProcess : public QObject
+{
+    Q_OBJECT
+public:
+    RemoteProcess(const QSsh::SshConnectionParameters &params);
+    ~RemoteProcess();
+
+    void run();
+
+    void runCmd(QString strcmd);
+
+signals:
+    void ServiceState(int nState);
+
+
+private slots:
+    void handleConnectionError();
+    void handleProcessStarted();
+    void handleProcessStdout();
+    void handleProcessStderr();
+    void handleProcessClosed(int exitStatus);
+//    void handleTimeout();
+//    void handleReadyRead();
+//    void handleReadyReadStdout();
+//    void handleReadyReadStderr();
+//    void handleConnected();
+
+
+private:
+    enum State {
+        Inactive, TestingSuccess, TestingFailure, TestingCrash, TestingTerminal, TestingIoDevice,
+        TestingProcessChannels
+    };
+
+    const QSsh::SshConnectionParameters m_sshParams;
+    QTimer * const m_timeoutTimer;
+    QScopedPointer<QTextStream> m_textStream;
+    QSsh::SshConnection *m_sshConnection;
+    QSsh::SshRemoteProcessRunner * const m_remoteRunner;
+    QSsh::SshRemoteProcess::Ptr m_catProcess;
+    QSsh::SshRemoteProcess::Ptr m_echoProcess;
+    QByteArray m_remoteStdout;
+    QByteArray m_remoteStderr;
+    QByteArray m_remoteData;
+    State m_state;
+    bool m_started;
+
+    bool mbQueryState = false;
+
+};
+
+#endif // REMOTEPROCESS_H

+ 36 - 0
src/tool/serverctrl/serverctrl.pro

@@ -0,0 +1,36 @@
+QT       += core gui network
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+CONFIG += c++11
+
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has been marked deprecated (the exact warnings
+# depend on your compiler). Please consult the documentation of the
+# deprecated API in order to know how to port your code away from it.
+DEFINES += QT_DEPRECATED_WARNINGS
+
+# You can also make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+# You can also select to disable deprecated APIs only up to a certain version of Qt.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+    main.cpp \
+    mainwindow.cpp \
+    remoteprocess.cpp
+
+HEADERS += \
+    mainwindow.h \
+    remoteprocess.h
+
+FORMS += \
+    mainwindow.ui
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target
+
+
+LIBS +=  -L$$PWD -lQSsh