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 bf6e64e commit d7b569c
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 39 deletions.
2 changes: 1 addition & 1 deletion lib_fiber/samples-gui/QtFiber/fiber_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void fiber_client::run() {
qDebug() << "Fiber-" << acl::fiber::self() << " recv: " << buf;
}

parent_->setProgress((100 * i) / max_);
parent_->setProgress((100 * (int) i) / (int) max_);
if (i % 10 == 0 && delay_ > 0) {
acl::fiber::delay(delay_);
}
Expand Down
18 changes: 9 additions & 9 deletions lib_fiber/samples-gui/QtFiber/inputdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

InputDialog::InputDialog(QWidget *parent) : QDialog(parent)
{
setWindowTitle("input: ");
setGeometry(200, 200, 300, 200);
setWindowTitle("Http Options: ");
setGeometry(200, 200, 600, 400);

QVBoxLayout *layout = new QVBoxLayout(this);

lineEdit = new QLineEdit(this);
lineEdit->setPlaceholderText("输入内容...");
layout->addWidget(lineEdit);
options = new QTextEdit(this);
options->setPlaceholderText("输入内容...");
layout->addWidget(options);

button = new QPushButton("确定", this);
layout->addWidget(button);
confirm = new QPushButton("确定", this);
layout->addWidget(confirm);

connect(button, &QPushButton::clicked, this, &InputDialog::onAccept);
connect(confirm, &QPushButton::clicked, this, &InputDialog::onAccept);
}

void InputDialog::onAccept()
{
emit dialogAccepted(lineEdit->text());
emit dialogAccepted(options->toPlainText());
accept();
}
6 changes: 3 additions & 3 deletions lib_fiber/samples-gui/QtFiber/inputdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define INPUTDIALOG_H

#include <QDialog>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>

Expand All @@ -20,8 +20,8 @@ private slots:
void onAccept();

private:
QLineEdit *lineEdit;
QPushButton *button;
QTextEdit *options;
QPushButton *confirm;
};

#endif // INPUTDIALOG_H
57 changes: 47 additions & 10 deletions lib_fiber/samples-gui/QtFiber/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui_->stopServer, &QPushButton::clicked, this, &MainWindow::onStopServer);
connect(ui_->startClient, &QPushButton::clicked, this, &MainWindow::onStartClient);
connect(ui_->urlGet, &QPushButton::clicked, this, &MainWindow::onUrlGet);
connect(ui_->httpOptions, &QPushButton::clicked, this, &MainWindow::onHttpOptions);

ui_->startSchedule->setEnabled(false);
ui_->stopServer->setEnabled(false);
ui_->startClient->setEnabled(false);
ui_->url->setText(url_.c_str());
}

MainWindow::~MainWindow()
Expand Down Expand Up @@ -63,6 +65,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
void MainWindow::onClear()
{
ui_->display->clear();
ui_->reqHdr->clear();
}

void MainWindow::setProgress(int n)
Expand Down Expand Up @@ -138,24 +141,40 @@ void MainWindow::onStartClient()

void MainWindow::onUrlGet()
{
const auto url = ui_->url->text();
if (url.isEmpty()) {
QMessageBox::information(this, "UrlGet", "url is empty!");
return;
}

if (!acl::fiber::scheduled()) {
qDebug() << "gui fiber not scheudled yet!";
return;
}

url_ = url.toStdString();

go[this] {
const char *addr = "www.baidu.com:80";
const char *host = "www.baidu.com";
acl::http_request req(addr);
req.request_header()
.set_url("/")
.set_host(host);
acl::http_request req(url_.c_str());
acl::http_header& reqHdr = req.request_header();
if (!host_.empty()) {
reqHdr.set_host(host_.c_str());
}

for (const auto& it : headers_) {
reqHdr.add_entry(it.first.c_str(), it.second.c_str());
}

acl::string buf;
reqHdr.build_request(buf);
ui_->reqHdr->setText(buf.c_str());

if (!req.request(nullptr, 0)) {
qDebug() << "Send http request to " << addr << " error: " << acl::last_serror();
qDebug() << "Send http request to " << url_.c_str() << " error: " << acl::last_serror();
return;
}

acl::string buf;
buf.clear();
if (req.get_body(buf)) {
this->onDownloadFinish(true, req);
} else {
Expand Down Expand Up @@ -187,6 +206,7 @@ void MainWindow::onStartSchedule()

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

qDebug() << "Begin schedule_gui!";
acl::fiber::schedule_gui();
Expand All @@ -205,11 +225,15 @@ void MainWindow::onStopSchedule()
ui_->stopSchedule->setEnabled(false);
ui_->startSchedule->setEnabled(true);

ui_->startClient->setEnabled(false);
ui_->startServer->setEnabled(false);
ui_->stopServer->setEnabled(false);

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

void MainWindow::onInputClicked()
void MainWindow::onHttpOptions()
{
InputDialog dialog(this);
QRect mainWindowGeometry = this->frameGeometry();
Expand All @@ -223,7 +247,20 @@ void MainWindow::onInputClicked()

void MainWindow::onDialogAccepted(const QString &text)
{
//input_display_->setText("输入内容: " + text);
ui_->reqHdr->setText("输入内容:\r\n" + text);
headers_.clear();
acl::string buf(text.toStdString().c_str());
std::vector<acl::string>& lines = buf.split2("\r\n");
for (const auto it : lines) {
acl::string line = it.c_str();
auto& kv = line.split_nameval(':');
if (kv.first.empty() || kv.second.empty()) {
continue;
}
kv.first.trim_space();
kv.second.trim_left_space().trim_right_space();
headers_[kv.first.c_str()] = kv.second.c_str();
}
}

void MainWindow::keyPressEvent(QKeyEvent *event)
Expand Down
6 changes: 5 additions & 1 deletion lib_fiber/samples-gui/QtFiber/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MainWindow : public QMainWindow
void onStartClient();
void onStartSchedule();
void onStopSchedule();
void onInputClicked();
void onHttpOptions();
void onUrlGet();

void onClear();
Expand All @@ -59,5 +59,9 @@ class MainWindow : public QMainWindow
fiber_server *server_ = nullptr;
QProcess *process_;
struct timeval *stamp_;

std::string url_ = "http://www.baidu.com/";
std::string host_;
std::map<std::string, std::string> headers_;
};
#endif // MAINWINDOW_H
76 changes: 61 additions & 15 deletions lib_fiber/samples-gui/QtFiber/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>1600</width>
<width>2000</width>
<height>800</height>
</rect>
</property>
Expand All @@ -18,17 +18,17 @@
<property name="geometry">
<rect>
<x>390</x>
<y>20</y>
<y>79</y>
<width>800</width>
<height>600</height>
<height>541</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="startSchedule">
<property name="geometry">
<rect>
<x>70</x>
<y>20</y>
<y>80</y>
<width>291</width>
<height>51</height>
</rect>
Expand All @@ -41,7 +41,7 @@
<property name="geometry">
<rect>
<x>70</x>
<y>80</y>
<y>130</y>
<width>291</width>
<height>51</height>
</rect>
Expand All @@ -54,7 +54,7 @@
<property name="geometry">
<rect>
<x>70</x>
<y>140</y>
<y>200</y>
<width>291</width>
<height>51</height>
</rect>
Expand All @@ -67,7 +67,7 @@
<property name="geometry">
<rect>
<x>70</x>
<y>200</y>
<y>250</y>
<width>291</width>
<height>51</height>
</rect>
Expand All @@ -80,7 +80,7 @@
<property name="geometry">
<rect>
<x>70</x>
<y>260</y>
<y>300</y>
<width>291</width>
<height>51</height>
</rect>
Expand All @@ -92,9 +92,9 @@
<widget class="QPushButton" name="urlGet">
<property name="geometry">
<rect>
<x>70</x>
<y>320</y>
<width>291</width>
<x>1270</x>
<y>130</y>
<width>301</width>
<height>51</height>
</rect>
</property>
Expand All @@ -105,9 +105,9 @@
<widget class="QPushButton" name="clear">
<property name="geometry">
<rect>
<x>1210</x>
<y>20</y>
<width>241</width>
<x>70</x>
<y>380</y>
<width>291</width>
<height>51</height>
</rect>
</property>
Expand All @@ -128,13 +128,59 @@
<number>0</number>
</property>
</widget>
<widget class="QPushButton" name="httpOptions">
<property name="geometry">
<rect>
<x>1570</x>
<y>130</y>
<width>291</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>Http options</string>
</property>
</widget>
<widget class="QLineEdit" name="url">
<property name="geometry">
<rect>
<x>1270</x>
<y>80</y>
<width>591</width>
<height>51</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>1220</x>
<y>90</y>
<width>41</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>URL:</string>
</property>
</widget>
<widget class="QTextEdit" name="reqHdr">
<property name="geometry">
<rect>
<x>1270</x>
<y>180</y>
<width>591</width>
<height>441</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1600</width>
<width>2000</width>
<height>21</height>
</rect>
</property>
Expand Down

0 comments on commit d7b569c

Please sign in to comment.