玉兔远程控制 0.1.0-alpha.2
载入中...
搜索中...
未找到
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)
11 : QObject() // Because it's in a different thread with pOperate
12{
13 qDebug(log) << Q_FUNC_INFO;
14 SetConnect(pOperate);
15}
16
17CBackend::~CBackend()
18{
19 qDebug(log) << Q_FUNC_INFO;
20}
21
22int CBackend::SetConnect(COperate *pOperate)
23{
24 qDebug(log) << Q_FUNC_INFO;
25 if(!pOperate) return -1;
26
27 bool check = false;
28 check = connect(this, SIGNAL(sigRunning()),
29 pOperate, SIGNAL(sigRunning()));
30 Q_ASSERT(check);
31 check = connect(this, SIGNAL(sigFinished()),
32 pOperate, SIGNAL(sigFinished()));
33 Q_ASSERT(check);
34 check = connect(this, SIGNAL(sigStop()),
35 pOperate, SIGNAL(sigStop()));
36 Q_ASSERT(check);
37 check = connect(this, SIGNAL(sigError(const int, const QString&)),
38 pOperate, SIGNAL(sigError(const int, const QString&)));
39 Q_ASSERT(check);
40 check = connect(this, SIGNAL(sigInformation(const QString&)),
41 pOperate, SIGNAL(sigInformation(const QString&)));
42 Q_ASSERT(check);
43 check = connect(
44 this, SIGNAL(sigShowMessageBox(const QString&, const QString&,
45 const QMessageBox::Icon&)),
46 pOperate, SIGNAL(sigShowMessageBox(const QString&, const QString&,
47 const QMessageBox::Icon&)));
48 Q_ASSERT(check);
49 check = connect(this, SIGNAL(sigBlockShowMessageBox(
50 const QString&, const QString&,
51 QMessageBox::StandardButtons,
52 QMessageBox::StandardButton&,
53 bool&, QString)),
54 pOperate, SLOT(slotBlockShowMessageBox(
55 const QString&, const QString&,
56 QMessageBox::StandardButtons,
57 QMessageBox::StandardButton&,
58 bool&, QString)),
59 Qt::BlockingQueuedConnection);
60 Q_ASSERT(check);
61 check = connect(this, SIGNAL(sigBlockInputDialog(const QString&,
62 const QString&,
63 const QString&,
64 QString&)),
65 pOperate, SLOT(slotBlockInputDialog(const QString&,
66 const QString&,
67 const QString&,
68 QString&)),
69 Qt::BlockingQueuedConnection);
70 Q_ASSERT(check);
71 check = connect(
72 this,
73 SIGNAL(sigBlockShowWidget(const QString&, int&, void*)),
74 pOperate, SLOT(slotBlockShowWidget(const QString&, int&, void*)),
75 Qt::BlockingQueuedConnection);
76 Q_ASSERT(check);
77 return 0;
78}
79
81{
82 qDebug(log) << Q_FUNC_INFO;
83 int nRet = 0;
84 nRet = static_cast<int>(OnInit());
85 if(nRet < 0) return nRet;
86
87 if(0 == nRet)
88 QTimer::singleShot(0, this, SLOT(slotTimeOut()));
89 return 0;
90}
91
93{
94 qDebug(log) << Q_FUNC_INFO;
95 int nRet = 0;
96 nRet = OnClean();
97 return nRet;
98}
99
101{
102 return 0;
103}
104
106{
107 //qDebug(log) << "CConnect::slotTimeOut()";
108 try {
109 // >= 0: continue. Call interval
110 // = -1: stop
111 // < -1: error
112 int nTime = OnProcess();
113 if(nTime >= 0)
114 {
115 QTimer::singleShot(nTime, this, SLOT(slotTimeOut()));
116 return;
117 }
118 qDebug(log) << "Process fail(< -1) or stop(= -1):" << nTime;
119 if(nTime < -1) {
120 qCritical(log) << "Process fail:" << nTime;
121 emit sigError(nTime, "Process fail or stop");
122 }
123 } catch(std::exception e) {
124 qCritical(log) << "Process fail:" << e.what();
125 emit sigError(-2, e.what());
126 } catch (...) {
127 qCritical(log) << "Process fail";
128 emit sigError(-3, "Process fail");
129 }
130
131 // Error or stop, must notify user disconnect it
132 emit sigStop();
133}
134
136{
137 qWarning(log) << "Need to implement CConnect::OnProcess()";
138 return 0;
139}
后端接口。它由协议插件实现。 它默认启动一个定时器来开启一个非 Qt 事件循环(就是普通的循环处理)。 详见: Start()、 slotTimeOut()、 OnProcess() 。 当然,它仍然支...
Definition Backend.h:42
void sigInformation(const QString &szInfo)
从后台线程中触发在主线程中显示信息,不阻塞后台线程
virtual int Stop()
停止
Definition Backend.cpp:92
virtual int WakeUp()
Wake up.
Definition Backend.cpp:100
virtual int OnProcess()
具体操作处理
Definition Backend.cpp:135
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:80
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:105
void sigBlockShowWidget(const QString &className, int &nRet, void *pContext)
阻塞后台线程,并在前台线程中显示窗口。
void sigBlockInputDialog(const QString &szTitle, const QString &szLable, const QString &szMessage, QString &szText)
阻塞后台线程,并在前台线程中显示输入对话框 (QInputDialog)
操作接口。
Definition Operate.h:51