Skip to content

Commit

Permalink
Optimize and test QtFiber demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
郑树新 committed Sep 22, 2024
1 parent 6d8aa2c commit e446567
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 145 deletions.
31 changes: 0 additions & 31 deletions lib_fiber/samples-gui/QtFiber/childwindows.cpp

This file was deleted.

24 changes: 0 additions & 24 deletions lib_fiber/samples-gui/QtFiber/childwindows.h

This file was deleted.

12 changes: 9 additions & 3 deletions lib_fiber/samples-gui/QtFiber/fiber_client.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "stdafx.h"
#include "patch.h"
#include "mainwindow.h"
#include "fiber_client.h"

fiber_client::fiber_client(const char *ip, int port, size_t max, int delay)
: ip_(ip), port_(port), max_(max), delay_(delay)
fiber_client::fiber_client(MainWindow *parent, const char *ip, int port, size_t max, int delay)
: parent_(parent), ip_(ip), port_(port), max_(max), delay_(delay)
{
}

Expand All @@ -22,6 +23,8 @@ void fiber_client::run() {
char buf[4096];
std::string s("hello world!");

parent_->setProgress(0);

for (size_t i = 0; i < max_; i++) {
int ret = acl_fiber_send(conn, s.c_str(), (int) s.size(), 0);
if (ret <= 0) {
Expand All @@ -40,11 +43,14 @@ void fiber_client::run() {
qDebug() << "Fiber-" << acl::fiber::self() << " recv: " << buf;
}

if (delay_ > 0) {
parent_->setProgress((100 * i) / max_);
if (i % 10 == 0 && delay_ > 0) {
acl::fiber::delay(delay_);
}
}

parent_->setProgress(100);

struct timeval end;
gettimeofday(&end, nullptr);

Expand Down
5 changes: 4 additions & 1 deletion lib_fiber/samples-gui/QtFiber/fiber_client.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#ifndef FIBER_CLIENT_H
#define FIBER_CLIENT_H

class MainWindow;

class fiber_client : public acl::fiber {
public:
fiber_client(const char *ip, int port, size_t max = 100, int delay = 0);
fiber_client(MainWindow *parent, const char *ip, int port, size_t max = 100, int delay = 0);

protected:
// @override
void run() override;

private:
MainWindow *parent_;
std::string ip_;
int port_;
size_t max_;
Expand Down
2 changes: 1 addition & 1 deletion lib_fiber/samples-gui/QtFiber/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void app_run(int argc, char **argv)
QApplication app(argc, argv);

MainWindow w;
w.resize(800, 800);
//w.resize(1600, 800);
w.show();

QTimer::singleShot(0, startupCallback);
Expand Down
139 changes: 72 additions & 67 deletions lib_fiber/samples-gui/QtFiber/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <QDebug>
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "childwindows.h"
#include "inputdialog.h"
#include "fiber_server.h"
#include "fiber_client.h"
Expand All @@ -16,58 +15,74 @@ MainWindow::MainWindow(QWidget *parent)
, process_(new QProcess(this))
{
ui_->setupUi(this);

button_ = new QPushButton("Open Dialog", this);
button_->setGeometry(QRect(QPoint(100, 100), QSize(300, 50)));
connect(button_, &QPushButton::clicked, this, &MainWindow::onButtonClicked);

start_server_ = new QPushButton("Start fiber server", this);
start_server_->setGeometry(QRect(QPoint(100, 150), QSize(300, 50)));
connect(start_server_, &QPushButton::clicked, this, &MainWindow::onStartServer);

stop_server_ = new QPushButton("Stop fiber server", this);
stop_server_->setGeometry(QRect(QPoint(100, 200), QSize(300, 50)));
connect(stop_server_, &QPushButton::clicked, this, &MainWindow::onStopServer);

start_client_ = new QPushButton("Start fiber client", this);
start_client_->setGeometry(QRect(QPoint(100, 250), QSize(300, 50)));
connect(start_client_, &QPushButton::clicked, this, &MainWindow::onStartClient);

url_get_ = new QPushButton("Http download", this);
url_get_->setGeometry(QRect(QPoint(100, 300), QSize(300, 50)));
connect(url_get_, &QPushButton::clicked, this, &MainWindow::onUrlGet);

start_schedule_ = new QPushButton("Start fiber schedule", this);
start_schedule_->setGeometry(QRect(QPoint(100, 350), QSize(300, 50)));
connect(start_schedule_, &QPushButton::clicked, this, &MainWindow::onStartSchedule);

stop_schedule_ = new QPushButton("Stop fiber schedule", this);
stop_schedule_->setGeometry(QRect(QPoint(100, 400), QSize(300, 50)));
connect(stop_schedule_, &QPushButton::clicked, this, &MainWindow::onStopSchedule);

open_child_ = new QPushButton("Open Child Window", this);
open_child_->setGeometry(QRect(QPoint(100, 450), QSize(300, 50)));
connect(open_child_, &QPushButton::clicked, this, &MainWindow::onOpenChildWindow);

input_button_= new QPushButton("Open dialog", this);
input_button_->setGeometry(100, 500, 300,50);
connect(input_button_, &QPushButton::clicked, this, &MainWindow::onInputClicked);

input_display_ = new QLabel("输入内容: ", this);
input_display_->setGeometry(100, 550, 200, 50);
stamp_ = new struct timeval;
gettimeofday(stamp_, nullptr);

connect(ui_->clear, &QPushButton::clicked, this, &MainWindow::onClear);
connect(ui_->startSchedule, &QPushButton::clicked, this, &MainWindow::onStartSchedule);
connect(ui_->stopSchedule, &QPushButton::clicked, this, &MainWindow::onStopSchedule);
connect(ui_->startServer, &QPushButton::clicked, this, &MainWindow::onStartServer);
connect(ui_->stopServer, &QPushButton::clicked, this, &MainWindow::onStopServer);
connect(ui_->startClient, &QPushButton::clicked, this, &MainWindow::onStartClient);
connect(ui_->urlGet, &QPushButton::clicked, this, &MainWindow::onUrlGet);

ui_->startSchedule->setEnabled(false);
ui_->stopServer->setEnabled(false);
ui_->startClient->setEnabled(false);
}

MainWindow::~MainWindow()
{
delete ui_;
delete stamp_;
delete server_;
qDebug() << "The main windows was destroied!";
}

void MainWindow::closeEvent(QCloseEvent *event)
{
#if 1
acl::fiber::schedule_stop(); // 停止协程调度器
event->accept(); // 接受关闭事件
return;
#else
QMessageBox::StandardButton resBtn = QMessageBox::question(this, "Confirm Close",
tr("Are you sure you want to exit?\n"),
QMessageBox::No | QMessageBox::Yes,
QMessageBox::No);
if (resBtn == QMessageBox::Yes) {
acl::fiber::schedule_stop();
event->accept(); // 接受关闭事件
} else {
event->ignore(); // 忽略关闭事件
}
#endif
}

void MainWindow::onClear()
{
ui_->display->clear();
}

void MainWindow::setProgress(int n)
{
if (n == 100) {
ui_->progress->setValue(100);
} else {
struct timeval now;
gettimeofday(&now, nullptr);
double tc = acl::stamp_sub(now, *stamp_);
if (tc >= 200) {
ui_->progress->setValue(n % 100);
gettimeofday(stamp_, nullptr);
}
}
}

void MainWindow::onAboutToQuit()
{
qDebug() << "onAboutToQuit called!";
// acl::fiber::schedule_stop();
}

void MainWindow::onButtonClicked()
Expand All @@ -87,6 +102,9 @@ void MainWindow::onStartServer()
return;
}

ui_->stopServer->setEnabled(true);
ui_->startClient->setEnabled(true);

server_ = new fiber_server("127.0.0.1", 9001, this);
qDebug() << "Start fiber server";
server_->start();
Expand All @@ -99,6 +117,8 @@ void MainWindow::onStopServer()
server_->stop();
delete server_;
server_ = nullptr;
ui_->stopServer->setEnabled(false);
ui_->startClient->setEnabled(false);
}
}

Expand All @@ -109,7 +129,8 @@ void MainWindow::onStartClient()
return;
}

acl::fiber *fb = new fiber_client("127.0.0.1", 9001, 10000, 0);
ui_->progress->setValue(0);
acl::fiber *fb = new fiber_client(this, "127.0.0.1", 9001, 10000, 1);
qDebug() << "Start fiber client";
fb->start();
qDebug() << "Fiber client started!";
Expand Down Expand Up @@ -151,6 +172,7 @@ void MainWindow::onDownloadFinish(bool ok, const acl::http_request& req)
client->sprint_header(buf);
qDebug() << "Got response body ok!";
qDebug() << buf.c_str();
ui_->display->setText(buf.c_str());
} else {
qDebug() << "Got response body error!";
}
Expand All @@ -163,6 +185,9 @@ void MainWindow::onStartSchedule()
return;
}

ui_->stopSchedule->setEnabled(true);
ui_->urlGet->setEnabled(true);

qDebug() << "Begin schedule_gui!";
acl::fiber::schedule_gui();
qDebug() << "schedule_gui end!";
Expand All @@ -176,34 +201,14 @@ void MainWindow::onStopSchedule()
server_ = nullptr;
}

ui_->urlGet->setEnabled(false);
ui_->stopSchedule->setEnabled(false);
ui_->startSchedule->setEnabled(true);

acl::fiber::schedule_stop();
qDebug() << "Fiber schedule stopped!";
}

void MainWindow::onOpenChildWindow()
{
if (child_window_ == nullptr) {
child_window_ = new ChildWindows(this);
}

qDebug() << "Opening second window";

QScreen *screen = QApplication::primaryScreen();
if (screen) {
QRect screenGeometry = screen->geometry();
int x = (screenGeometry.width() - child_window_->width()) / 2;
int y = (screenGeometry.height() - child_window_->height()) / 2;
child_window_->move(x, y);
}

child_window_->show();
child_window_->raise();
child_window_->activateWindow();
qDebug() << "Second window visibility:" << child_window_->isVisible();
qDebug() << "Second window isMinimized:" << child_window_->isMinimized();
qDebug() << "Second window isActiveWindow:" << child_window_->isActiveWindow();
}

void MainWindow::onInputClicked()
{
InputDialog dialog(this);
Expand All @@ -218,7 +223,7 @@ void MainWindow::onInputClicked()

void MainWindow::onDialogAccepted(const QString &text)
{
input_display_->setText("输入内容: " + text);
//input_display_->setText("输入内容: " + text);
}

void MainWindow::keyPressEvent(QKeyEvent *event)
Expand Down
21 changes: 7 additions & 14 deletions lib_fiber/samples-gui/QtFiber/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace acl {
}

class fiber_server;
struct timeval;

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
Expand All @@ -27,6 +28,8 @@ class MainWindow : public QMainWindow
MainWindow(QWidget *parent = nullptr);
~MainWindow();

void setProgress(int n);

protected:
void keyPressEvent(QKeyEvent *event);

Expand All @@ -36,11 +39,13 @@ class MainWindow : public QMainWindow
void onStartClient();
void onStartSchedule();
void onStopSchedule();
void onOpenChildWindow();
void onInputClicked();
void onUrlGet();

void onClear();

void onDialogAccepted(const QString &text);
void closeEvent(QCloseEvent *event) override;

public:
void onAboutToQuit();
Expand All @@ -50,21 +55,9 @@ class MainWindow : public QMainWindow

private:
Ui::MainWindow *ui_;
QPushButton *button_;
QPushButton *start_server_;
QPushButton *stop_server_;
QPushButton *start_client_;
QPushButton *start_schedule_;
QPushButton *stop_schedule_;
QPushButton *open_child_;
QPushButton *url_get_;
std::string url_;
QPushButton *input_button_;
QLabel *input_display_;

fiber_server *server_ = nullptr;
QProcess *process_;

ChildWindows *child_window_ = nullptr;
struct timeval *stamp_;
};
#endif // MAINWINDOW_H
Loading

0 comments on commit e446567

Please sign in to comment.