玉兔远程控制 0.1.0-bate6
载入中...
搜索中...
未找到
Backend.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include <QTimer>
4#include <QLoggingCategory>
5
6#include "Backend.h"
7
8static Q_LOGGING_CATEGORY(log, "Operate")
9
10CBackend::CBackend(COperate *pOperate, bool bStopSignal)
11 : QObject() // Because it's in a different thread with pOperate
12 , m_bStopSignal(bStopSignal)
13{
14 qDebug(log) << Q_FUNC_INFO;
15 SetConnect(pOperate);
16}
17
18CBackend::~CBackend()
19{
20 qDebug(log) << Q_FUNC_INFO;
21}
22
23int CBackend::SetConnect(COperate *pOperate)
24{
25 qDebug(log) << Q_FUNC_INFO;
26 if(!pOperate) return -1;
27
28 bool check = false;
29 check = connect(this, SIGNAL(sigRunning()),
30 pOperate, SIGNAL(sigRunning()));
31 Q_ASSERT(check);
32 check = connect(this, SIGNAL(sigFinished()),
33 pOperate, SIGNAL(sigFinished()));
34 Q_ASSERT(check);
35 check = connect(this, SIGNAL(sigStop()),
36 pOperate, SIGNAL(sigStop()));
37 Q_ASSERT(check);
38 check = connect(this, SIGNAL(sigError(const int, const QString&)),
39 pOperate, SIGNAL(sigError(const int, const QString&)));
40 Q_ASSERT(check);
41 check = connect(this, SIGNAL(sigInformation(const QString&)),
42 pOperate, SIGNAL(sigInformation(const QString&)));
43 Q_ASSERT(check);
44 check = connect(
45 this, SIGNAL(sigShowMessageBox(const QString&, const QString&,
46 const QMessageBox::Icon&)),
47 pOperate, SIGNAL(sigShowMessageBox(const QString&, const QString&,
48 const QMessageBox::Icon&)));
49 Q_ASSERT(check);
50 check = connect(this, SIGNAL(sigBlockShowMessageBox(
51 const QString&, const QString&,
52 QMessageBox::StandardButtons,
53 QMessageBox::StandardButton&,
54 bool&, QString)),
55 pOperate, SLOT(slotBlockShowMessageBox(
56 const QString&, const QString&,
57 QMessageBox::StandardButtons,
58 QMessageBox::StandardButton&,
59 bool&, QString)),
60 Qt::BlockingQueuedConnection);
61 Q_ASSERT(check);
62 check = connect(this, SIGNAL(sigBlockInputDialog(const QString&,
63 const QString&,
64 const QString&,
65 QString&)),
66 pOperate, SLOT(slotBlockInputDialog(const QString&,
67 const QString&,
68 const QString&,
69 QString&)),
70 Qt::BlockingQueuedConnection);
71 Q_ASSERT(check);
72 check = connect(
73 this,
74 SIGNAL(sigBlockShowWidget(const QString&, int&, void*)),
75 pOperate, SLOT(slotBlockShowWidget(const QString&, int&, void*)),
76 Qt::BlockingQueuedConnection);
77 Q_ASSERT(check);
78 return 0;
79}
80
82{
83 qDebug(log) << Q_FUNC_INFO;
84 int nRet = 0;
85 nRet = static_cast<int>(OnInit());
86 if(nRet < 0) return nRet;
87
88 if(0 == nRet)
89 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
90 return 0;
91}
92
94{
95 qDebug(log) << Q_FUNC_INFO;
96 int nRet = 0;
97 nRet = OnClean();
98 return nRet;
99}
100
102{
103 return 0;
104}
105
107{
108 //qDebug(log) << "CConnect::slotTimeOut()";
109 try {
110 // >= 0: continue. Call interval
111 // = -1: stop
112 // < -1: error
113 int nTime = OnProcess();
114 if(nTime >= 0)
115 {
116 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
117 return;
118 }
119 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
120 if(nTime < -1) {
121 qCritical(log) << "Process fail:" << nTime;
122 emit sigError(nTime, "Process fail or stop");
123 }
124 } catch(std::exception e) {
125 qCritical(log) << "Process fail:" << e.what();
126 emit sigError(-2, e.what());
127 } catch (...) {
128 qCritical(log) << "Process fail";
129 emit sigError(-3, "Process fail");
130 }
131
132 if(m_bStopSignal) {
133 // Error or stop, must notify user disconnect it
134 emit sigStop();
135 }
136}
137
139{
140 qWarning(log) << "Need to implement CConnect::OnProcess()";
141 return 0;
142}
后端接口。它由协议插件实现。 它默认启动一个定时器来开启一个非 Qt 事件循环(就是普通的循环处理)。 详见: Start()、 slotTimeOut()、 OnProcess() 。 当然,它仍然支...
Definition Backend.h:42
void sigInformation(const QString &szInfo)
从后台线程中触发在主线程中显示信息,不阻塞后台线程
virtual int Stop()
停止
Definition Backend.cpp:93
virtual int WakeUp()
Wake up.
Definition Backend.cpp:101
virtual int OnProcess()
具体操作处理
Definition Backend.cpp:138
void sigStop()
需要通知用户停止时触发。仅由插件触发。 当从插件中需要停止时触发。例如:对端断开连接、重置连接或者连接出错。
virtual OnInitReturnValue OnInit()=0
初始化
virtual int OnClean()=0
清理
void sigShowMessageBox(const QString &szTitle, const QString &szMessage, const QMessageBox::Icon &icon=QMessageBox::Information)
从后台线程中触发在主线程中显示消息对话框(QMessageBox),不阻塞后台线程
virtual int Start()
开始。根据 OnInit() 返回值来决定是否开始定时器来支持非 qt 事件
Definition Backend.cpp:81
void sigError(const int nError, const QString &szError=QString())
当有错误产生时触发
void sigBlockShowMessageBox(const QString &szTitle, const QString &szMessage, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton &nRet, bool &checkBox, QString checkBoxContext=QString())
阻塞后台线程,并在前台线程中显示消息对话框(QMessageBox)
void sigRunning()
当插件开始成功后触发。仅由插件触发
void sigFinished()
停止成功信号。仅由插件触发
virtual void slotTimeOut()
一个非 Qt 事件处理,它调用 OnProcess(),并根据其返回值开始新的定时器。 如果是不是一个非 Qt 事件循环(就是普通的循环处理), 可以重载它,或者 OnInit() 返回值大于 0
Definition Backend.cpp:106
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
阻塞后台线程,并在前台线程中显示窗口。
bool m_bStopSignal
When an error occurs, emit a COperate::sigStop() signal
Definition Backend.h:277
void sigBlockInputDialog(const QString &szTitle, const QString &szLable, const QString &szMessage, QString &szText)
阻塞后台线程,并在前台线程中显示输入对话框 (QInputDialog)
操作接口。
Definition Operate.h:51