How should I design my app to meet this criteria ?
-
NOTE: This is a NON-GUI app
I've wrote an HTTP upload/download class using QNetworkAccessManager and i use it like below:-
int main(..){ QCoreApplication app(...); HTTP http(&app); QNetworkRequest DownloadRequest; QNetworkRequest UploadRequest; UploadRequest.setUrl(QUrl("path/to/server")); http.UploadFile(UploadRequest, (QFileInfo)"path/to/file1"); http.UploadFile(UploadRequest, (QFileInfo)"path/to/file2"); http.UploadFile(UploadRequest, (QFileInfo)"path/to/file3"); DownloadRequest.setUrl(QUrl("link/to/fileX")); http.DownloadFile(DownloadRequest); app.exec(); }
This works fine and now I want to embed this module in another project which is a client app constantly communicating with my server app. So I created a std::thread (not a Qthread) and moved QCoreApplication & app.exec() to avoid being paused in the event loop like below
int main(..){ std::thread UploadDownloadModule(..); // this contains the upload/download and app.exec // do some stuff here }
This works fine but I want UploadDownloadModule(..) to get finished once the download/upload process completes. I would also like to invoke UploadDownloadModule(..) on request.
How should I design my app to meet the above criteria?
-
NOTE: This is a NON-GUI app
I've wrote an HTTP upload/download class using QNetworkAccessManager and i use it like below:-
int main(..){ QCoreApplication app(...); HTTP http(&app); QNetworkRequest DownloadRequest; QNetworkRequest UploadRequest; UploadRequest.setUrl(QUrl("path/to/server")); http.UploadFile(UploadRequest, (QFileInfo)"path/to/file1"); http.UploadFile(UploadRequest, (QFileInfo)"path/to/file2"); http.UploadFile(UploadRequest, (QFileInfo)"path/to/file3"); DownloadRequest.setUrl(QUrl("link/to/fileX")); http.DownloadFile(DownloadRequest); app.exec(); }
This works fine and now I want to embed this module in another project which is a client app constantly communicating with my server app. So I created a std::thread (not a Qthread) and moved QCoreApplication & app.exec() to avoid being paused in the event loop like below
int main(..){ std::thread UploadDownloadModule(..); // this contains the upload/download and app.exec // do some stuff here }
This works fine but I want UploadDownloadModule(..) to get finished once the download/upload process completes. I would also like to invoke UploadDownloadModule(..) on request.
How should I design my app to meet the above criteria?
@pingal said in How should I design my app to meet this criteria ?:
I want to embed this module in another project which is a client app
Is this app a Qt app?
-
@pingal said in How should I design my app to meet this criteria ?:
I want to embed this module in another project which is a client app
Is this app a Qt app?
-
@pingal You could pass UploadDownloadModule a callback function, so it can call it when its finished and pass the result to this callback. To invoke UploadDownloadModule you can simply create a new UploadDownloadModule instance passing needed parameters to it.
-
@pingal You could pass UploadDownloadModule a callback function, so it can call it when its finished and pass the result to this callback. To invoke UploadDownloadModule you can simply create a new UploadDownloadModule instance passing needed parameters to it.
-
@pingal said in How should I design my app to meet this criteria ?:
What about the app.exec(..) in the callback ?
Why in call back?
app.exec() will be started in the thread.
As soon as the thread finishes it call the call back. -
@pingal said in How should I design my app to meet this criteria ?:
What about the app.exec(..) in the callback ?
Why in call back?
app.exec() will be started in the thread.
As soon as the thread finishes it call the call back.@jsulm Would you be kind enough to provide me an example (i think i'm not getting you) keeping in view the above picture I gave (?)
Here is the simplified form of my thread function
void UploadDownload() { QCoreApplication app(a, nullptr); HTTP http(&app); QNetworkRequest DownloadRequest; DownloadRequest.setUrl(QUrl("link/to/fileX")); http.DownloadFile(DownloadRequest); app.exec(); }
-
@jsulm Would you be kind enough to provide me an example (i think i'm not getting you) keeping in view the above picture I gave (?)
Here is the simplified form of my thread function
void UploadDownload() { QCoreApplication app(a, nullptr); HTTP http(&app); QNetworkRequest DownloadRequest; DownloadRequest.setUrl(QUrl("link/to/fileX")); http.DownloadFile(DownloadRequest); app.exec(); }
@pingal Untested
void callBack() { std::cout << "Done" << std::endl; } typedef void(*CallBack)(); void UploadDownload(CallBack callBack) { // Do the work ... app.exec(); // When done call call back callBack(); } std::thread thread(UploadDownload, callBack);