Skip to content

Commit 33bd448

Browse files
committed
完成用户界面和前后台通信
1 parent 25422f2 commit 33bd448

15 files changed

+820
-0
lines changed

DrCOM_JLU_Qt.pro

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2019-03-03T13:30:28
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui network widgets
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = DrCOM_JLU_Qt
12+
TEMPLATE = app
13+
14+
# The following define makes your compiler emit warnings if you use
15+
# any feature of Qt which has been marked as deprecated (the exact warnings
16+
# depend on your compiler). Please consult the documentation of the
17+
# deprecated API in order to know how to port your code away from it.
18+
DEFINES += QT_DEPRECATED_WARNINGS
19+
20+
# You can also make your code fail to compile if you use deprecated APIs.
21+
# In order to do so, uncomment the following line.
22+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
23+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24+
25+
CONFIG += c++11
26+
27+
SOURCES += \
28+
main.cpp \
29+
mainwindow.cpp \
30+
dogcomcontroller.cpp \
31+
interruptiblesleeper.cpp \
32+
dogcom.cpp
33+
34+
HEADERS += \
35+
mainwindow.h \
36+
dogcomcontroller.h \
37+
constants.h \
38+
interruptiblesleeper.h \
39+
dogcom.h
40+
41+
FORMS += \
42+
mainwindow.ui
43+
44+
# Default rules for deployment.
45+
qnx: target.path = /tmp/$${TARGET}/bin
46+
else: unix:!android: target.path = /opt/$${TARGET}/bin
47+
!isEmpty(target.path): INSTALLS += target
48+
49+
RESOURCES += \
50+
DrCOM_JLU_Qt.qrc

DrCOM_JLU_Qt.qrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<RCC>
2+
<qresource prefix="/">
3+
<file>images/offline.png</file>
4+
<file>images/online.png</file>
5+
</qresource>
6+
</RCC>

constants.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CONSTANTS_H
2+
#define CONSTANTS_H
3+
4+
enum {
5+
// 离线原因
6+
OFF_UNKNOWN,
7+
OFF_WRONG_PASS,
8+
OFF_WRONG_MAC,
9+
OFF_USER_LOGOUT,
10+
11+
// 当前状态
12+
STATE_OFFLINE,
13+
STATE_LOGGING,
14+
STATE_ONLINE
15+
16+
};
17+
18+
#endif // CONSTANTS_H

dogcom.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "dogcom.h"
2+
3+
#include <QDebug>
4+
#include "constants.h"
5+
6+
DogCom::DogCom(InterruptibleSleeper *s)
7+
{
8+
sleeper=s;
9+
}
10+
11+
void DogCom::Stop()
12+
{
13+
sleeper->Interrupt();
14+
}
15+
16+
void DogCom::FillConfig(QString a, QString p, QString m)
17+
{
18+
account=a;password=p;mac_addr=m;
19+
}
20+
21+
void DogCom::run()
22+
{
23+
qDebug()<<"Start dogcoming...";
24+
// 后台登录维持连接的线程
25+
// 一旦离线就return
26+
qDebug()<<"Sending handshake...";
27+
// if(!account.compare("1")&&!password.compare("1")&&!mac_addr.compare("F4:8E:38:E8:46:72")){
28+
// if(!account.compare("1")&&!password.compare("1")&&!mac_addr.compare("1A:2B:3C:4D:5E:6F")){
29+
// if(!account.compare("1")&&!password.compare("1")&&!mac_addr.compare("1A:2B:3C:4D:5E:61")){
30+
if(!account.compare("1")&&!password.compare("1")&&!mac_addr.compare("AC:2B:6E:A2:1B:38")){
31+
// 登录成功
32+
qDebug()<<"Logged in.";
33+
emit ReportOnline();
34+
}else{
35+
// 登录失败 因为没有Interrupt,但是后边也要重用这个睡眠定时器,所以此处手动interrupt一下,使登录操作保持一致
36+
sleeper->Interrupt();
37+
qDebug()<<"Login failed";
38+
emit ReportOffline(OFF_WRONG_PASS);
39+
return;
40+
}
41+
42+
qDebug()<<"sending packets cyclically";
43+
int i=0;
44+
while(true){
45+
qDebug()<<QString("in MyThread: %1").arg(i++);
46+
if(sleeper->Sleep(2000)){
47+
//睡眠成功
48+
}else{
49+
qDebug()<<"Interruptted by user";
50+
emit ReportOffline(OFF_USER_LOGOUT);
51+
return;
52+
}
53+
}
54+
}

dogcom.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef DOGCOM_H
2+
#define DOGCOM_H
3+
4+
#include <QThread>
5+
#include "interruptiblesleeper.h"
6+
7+
class DogCom : public QThread
8+
{
9+
Q_OBJECT
10+
public:
11+
DogCom(InterruptibleSleeper *);
12+
void Stop();
13+
void FillConfig(QString a,QString p,QString m);
14+
protected:
15+
void run();
16+
private:
17+
InterruptibleSleeper *sleeper;
18+
QString account;
19+
QString password;
20+
QString mac_addr;
21+
signals:
22+
void ReportOffline(int reason);
23+
void ReportOnline();
24+
};
25+
26+
#endif // DOGCOM_H

dogcomcontroller.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "dogcomcontroller.h"
2+
#include <QDebug>
3+
4+
DogcomController::DogcomController()
5+
{
6+
sleeper=new InterruptibleSleeper();
7+
dogcom=new DogCom(sleeper);
8+
9+
connect(dogcom,&DogCom::ReportOnline,this,&DogcomController::HandleDogcomOnline);
10+
connect(dogcom,&DogCom::ReportOffline,this,&DogcomController::HandleDogcomOffline);
11+
}
12+
13+
void DogcomController::Login(const QString &account,const QString &password,const QString &mac_addr){
14+
qDebug()<<"Resetting sleeper...";
15+
sleeper->Reset();
16+
qDebug()<<"Reset sleeper done.";
17+
qDebug()<<"Filling config...";
18+
dogcom->FillConfig(account,password,mac_addr);
19+
qDebug()<<"Fill config done.";
20+
dogcom->start();
21+
}
22+
23+
void DogcomController::LogOut()
24+
{
25+
dogcom->Stop();
26+
}
27+
28+
void DogcomController::HandleDogcomOffline(int reason)
29+
{
30+
emit HaveBeenOffline(reason);
31+
}
32+
33+
void DogcomController::HandleDogcomOnline()
34+
{
35+
emit HaveLoggedIn();
36+
}

dogcomcontroller.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef DOGCOMCONTROLLER_H
2+
#define DOGCOMCONTROLLER_H
3+
4+
#include <QApplication>
5+
#include <QObject>
6+
#include "constants.h"
7+
#include "dogcom.h"
8+
#include "interruptiblesleeper.h"
9+
10+
class DogcomController : public QObject
11+
{
12+
Q_OBJECT
13+
public:
14+
DogcomController();
15+
void Login(const QString &account,const QString &password,const QString &mac);
16+
void LogOut();
17+
public slots:
18+
void HandleDogcomOffline(int reason);
19+
void HandleDogcomOnline();
20+
signals:
21+
void HaveBeenOffline(int reason);
22+
void HaveLoggedIn();
23+
private:
24+
InterruptibleSleeper *sleeper;
25+
DogCom *dogcom;
26+
};
27+
28+
#endif // DOGCOMCONTROLLER_H

images/offline.png

24.8 KB
Loading

images/online.png

7.09 KB
Loading

interruptiblesleeper.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "interruptiblesleeper.h"
2+
3+
InterruptibleSleeper::InterruptibleSleeper(QObject *parent) : QObject(parent)
4+
{
5+
}
6+
7+
bool InterruptibleSleeper::Sleep(int timeout)
8+
{
9+
if(m.tryLock(timeout)){
10+
//获取到锁 睡眠失败 被中断了
11+
// Interrupt之后此处尝试获取互斥锁成功了,因此还需要再解一下锁
12+
m.unlock();
13+
return false;
14+
}else{
15+
//未获取到锁 睡眠成功 没有被中断
16+
return true;
17+
}
18+
}
19+
20+
void InterruptibleSleeper::Reset(){
21+
m.lock();
22+
}
23+
24+
void InterruptibleSleeper::Interrupt(){
25+
m.unlock();
26+
}

0 commit comments

Comments
 (0)