mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-29 18:37:41 +08:00
Optimize and test QtFiber demo.
This commit is contained in:
parent
bf6e64e46e
commit
d7b569c102
@ -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_);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
#define INPUTDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QLineEdit>
|
||||
#include <QTextEdit>
|
||||
#include <QPushButton>
|
||||
#include <QVBoxLayout>
|
||||
|
||||
@ -20,8 +20,8 @@ private slots:
|
||||
void onAccept();
|
||||
|
||||
private:
|
||||
QLineEdit *lineEdit;
|
||||
QPushButton *button;
|
||||
QTextEdit *options;
|
||||
QPushButton *confirm;
|
||||
};
|
||||
|
||||
#endif // INPUTDIALOG_H
|
||||
|
@ -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()
|
||||
@ -63,6 +65,7 @@ void MainWindow::closeEvent(QCloseEvent *event)
|
||||
void MainWindow::onClear()
|
||||
{
|
||||
ui_->display->clear();
|
||||
ui_->reqHdr->clear();
|
||||
}
|
||||
|
||||
void MainWindow::setProgress(int n)
|
||||
@ -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);
|
||||
if (!req.request(nullptr, 0)) {
|
||||
qDebug() << "Send http request to " << addr << " error: " << acl::last_serror();
|
||||
return;
|
||||
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 " << url_.c_str() << " error: " << acl::last_serror();
|
||||
return;
|
||||
}
|
||||
|
||||
buf.clear();
|
||||
if (req.get_body(buf)) {
|
||||
this->onDownloadFinish(true, req);
|
||||
} else {
|
||||
@ -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();
|
||||
@ -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();
|
||||
@ -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)
|
||||
|
@ -39,7 +39,7 @@ protected:
|
||||
void onStartClient();
|
||||
void onStartSchedule();
|
||||
void onStopSchedule();
|
||||
void onInputClicked();
|
||||
void onHttpOptions();
|
||||
void onUrlGet();
|
||||
|
||||
void onClear();
|
||||
@ -59,5 +59,9 @@ private:
|
||||
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
|
||||
|
@ -6,7 +6,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1600</width>
|
||||
<width>2000</width>
|
||||
<height>800</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -18,9 +18,9 @@
|
||||
<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>
|
||||
@ -28,7 +28,7 @@
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>70</x>
|
||||
<y>20</y>
|
||||
<y>80</y>
|
||||
<width>291</width>
|
||||
<height>51</height>
|
||||
</rect>
|
||||
@ -41,7 +41,7 @@
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>70</x>
|
||||
<y>80</y>
|
||||
<y>130</y>
|
||||
<width>291</width>
|
||||
<height>51</height>
|
||||
</rect>
|
||||
@ -54,7 +54,7 @@
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>70</x>
|
||||
<y>140</y>
|
||||
<y>200</y>
|
||||
<width>291</width>
|
||||
<height>51</height>
|
||||
</rect>
|
||||
@ -67,7 +67,7 @@
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>70</x>
|
||||
<y>200</y>
|
||||
<y>250</y>
|
||||
<width>291</width>
|
||||
<height>51</height>
|
||||
</rect>
|
||||
@ -80,7 +80,7 @@
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>70</x>
|
||||
<y>260</y>
|
||||
<y>300</y>
|
||||
<width>291</width>
|
||||
<height>51</height>
|
||||
</rect>
|
||||
@ -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>
|
||||
@ -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>
|
||||
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user