1 /*
2 * This file is part of GtkEveMon.
3 *
4 * GtkEveMon is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * You should have received a copy of the GNU General Public License
10 * along with GtkEveMon. If not, see <http://www.gnu.org/licenses/>.
11 */
12
13 #ifndef ASYNC_HTTP_HEADER
14 #define ASYNC_HTTP_HEADER
15
16 #include <glibmm/dispatcher.h>
17
18 #include "util/thread.h"
19 #include "util/exception.h"
20 #include "http.h"
21
22 /* This is delivered when the request ist done.
23 * The data member is NULL if there was an error.
24 * The exception member is the error description then.
25 */
26 class AsyncHttpData
27 {
28 public:
29 HttpDataPtr data;
30 Exception exception;
31 };
32
33 /* Class for asynchronous HTTP requests. Instructions:
34 * - Create class with create()
35 * - Setup the HTTP data (host, path, etc)
36 * - Connect to the done signal (disconnect if not interested anymore)
37 * - Run async_request()
38 * - Data will be delivered to all signal subscribers
39 * - No need to free, automatic deletion if all signals are processed
40 */
41 class AsyncHttp : public Thread, public Http
42 {
43 private:
44 AsyncHttpData http_result;
45 Glib::Dispatcher sig_dispatch;
46 sigc::signal<void, AsyncHttpData> sig_done;
47
48 protected:
49 AsyncHttp (void);
50
51 void* run (void);
52 void dispatch (void);
53
54 public:
55 static AsyncHttp* create (void);
56 ~AsyncHttp (void);
57
58 void async_request (void);
59 sigc::signal<void, AsyncHttpData>& signal_done (void);
60 };
61
62 /* ---------------------------------------------------------------- */
63
64 inline AsyncHttp*
create(void)65 AsyncHttp::create (void)
66 {
67 return new AsyncHttp;
68 }
69
70 inline void
async_request(void)71 AsyncHttp::async_request (void)
72 {
73 this->pt_create();
74 }
75
76 inline void
dispatch(void)77 AsyncHttp::dispatch (void)
78 {
79 this->sig_done.emit(this->http_result);
80 delete this;
81 }
82
83 inline sigc::signal<void, AsyncHttpData>&
signal_done(void)84 AsyncHttp::signal_done (void)
85 {
86 return sig_done;
87 }
88
89 #endif /* ASYNC_HTTP_HEADER */
90