| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a18841b commit d2b1c09
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,30 +96,59 @@ class logging_delegate | |||
| 96 | 96 | virtual void log_error(const std::string& s) const; | |
| 97 | 97 | }; | |
| 98 | 98 | ||
| 99 | + template <typename CHILD> | ||
| 99 | 100 | class event_supplier | |
| 100 | 101 | { | |
| 101 | 102 | public: | |
| 102 | - event_supplier(); | ||
| 103 | + event_supplier() | ||
| 104 | + { | ||
| 105 | + } | ||
| 103 | 106 | ||
| 104 | - virtual ~event_supplier(); | ||
| 107 | + ~event_supplier() | ||
| 108 | + { | ||
| 109 | + } | ||
| 105 | 110 | ||
| 106 | - virtual void supply_events( | ||
| 111 | + void supply_events( | ||
| 107 | 112 | fd_set* read_fdset, | |
| 108 | 113 | fd_set* write_fdset, | |
| 109 | 114 | fd_set* exc_fdset, | |
| 110 | 115 | int* max | |
| 111 | - ) const ; | ||
| 116 | + ) const | ||
| 117 | + { | ||
| 118 | + static_cast<CHILD*>(this)->supply_events( | ||
| 119 | + read_fdset, write_fdset, exc_fdset, max | ||
| 120 | + ); | ||
| 121 | + } | ||
| 112 | 122 | ||
| 113 | - virtual long get_timeout() const; | ||
| 123 | + long get_timeout() const | ||
| 124 | + { | ||
| 125 | + return static_cast<CHILD*>(this)->get_timeout(); | ||
| 126 | + } | ||
| 114 | 127 | ||
| 115 | - virtual void dispatch_events() const; | ||
| 128 | + void dispatch_events() const | ||
| 129 | + { | ||
| 130 | + static_cast<CHILD*>(this)->dispatch_events(); | ||
| 131 | + } | ||
| 116 | 132 | }; | |
| 117 | 133 | ||
| 118 | 134 | class create_webserver; | |
| 119 | 135 | ||
| 120 | 136 | typedef bool(*validator_ptr)(const std::string&); | |
| 121 | 137 | typedef void(*unescaper_ptr)(char*); | |
| 122 | 138 | ||
| 139 | + typedef void(*supply_events_ptr)( | ||
| 140 | + fd_set*, | ||
| 141 | + fd_set*, | ||
| 142 | + fd_set*, | ||
| 143 | + int* | ||
| 144 | + ); | ||
| 145 | + | ||
| 146 | + typedef long(*get_timeout_ptr)(); | ||
| 147 | + | ||
| 148 | + typedef void(*dispatch_events_ptr)(); | ||
| 149 | + | ||
| 150 | + struct event_tuple; | ||
| 151 | + | ||
| 123 | 152 | /** | |
| 124 | 153 | * Class representing the webserver. Main class of the apis. | |
| 125 | 154 | **/ | |
@@ -261,7 +290,9 @@ class webserver | |||
| 261 | 290 | this->unescaper = unescaper; | |
| 262 | 291 | } | |
| 263 | 292 | ||
| 264 | - void register_event_supplier(const std::string& id, event_supplier* ev_supplier); | ||
| 293 | + template<typename T> | ||
| 294 | + void register_event_supplier(const std::string& id, event_supplier<T>* ev_supplier); | ||
| 295 | + void remove_event_supplier(const std::string& id); | ||
| 265 | 296 | ||
| 266 | 297 | void run(); | |
| 267 | 298 | ||
@@ -336,7 +367,7 @@ class webserver | |||
| 336 | 367 | std::vector<details::daemon_item*> daemons; | |
| 337 | 368 | std::vector<pthread_t> threads; | |
| 338 | 369 | ||
| 339 | - std::map<std::string, event_supplier*> event_suppliers; | ||
| 370 | + std::map<std::string, event_tuple*> event_suppliers; | ||
| 340 | 371 | ||
| 341 | 372 | void init(http_resource* single_resource); | |
| 342 | 373 | static void* select(void* self); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -281,6 +281,21 @@ struct cache_entry | |||
| 281 | 281 | } | |
| 282 | 282 | }; | |
| 283 | 283 | ||
| 284 | + struct event_tuple | ||
| 285 | + { | ||
| 286 | + supply_events_ptr supply_events; | ||
| 287 | + get_timeout_ptr get_timeout; | ||
| 288 | + dispatch_events_ptr dispatch_events; | ||
| 289 | + | ||
| 290 | + template<typename T> | ||
| 291 | + event_tuple(event_supplier<T>* es) | ||
| 292 | + { | ||
| 293 | + supply_events(std::bind1st(std::mem_fun(&T::supply_events), es)); | ||
| 294 | + get_timeout(std::bind1st(std::mem_fun(&T::get_timeout), es)); | ||
| 295 | + dispatch_events(std::bind1st(std::mem_fun(&T::dispatch_events), es)); | ||
| 296 | + } | ||
| 297 | + }; | ||
| 298 | + | ||
| 284 | 299 | using namespace http; | |
| 285 | 300 | ||
| 286 | 301 | int policy_callback (void *, const struct sockaddr*, socklen_t); | |
@@ -327,20 +342,6 @@ void logging_delegate::log_access(const string& s) const {} | |||
| 327 | 342 | ||
| 328 | 343 | void logging_delegate::log_error(const string& s) const {} | |
| 329 | 344 | ||
| 330 | - //EVENT SUPPLIER | ||
| 331 | - event_supplier::event_supplier() {} | ||
| 332 | - | ||
| 333 | - event_supplier::~event_supplier() {} | ||
| 334 | - | ||
| 335 | - void event_supplier::supply_events(fd_set* read_fdset, fd_set* write_fdset, fd_set* exc_fdset, int* max) const {} | ||
| 336 | - | ||
| 337 | - long event_supplier::get_timeout() const | ||
| 338 | - { | ||
| 339 | - return 0L; | ||
| 340 | - } | ||
| 341 | - | ||
| 342 | - void event_supplier::dispatch_events() const {} | ||
| 343 | - | ||
| 344 | 345 | //WEBSERVER CREATOR | |
| 345 | 346 | create_webserver& create_webserver::https_mem_key(const std::string& https_mem_key) | |
| 346 | 347 | { | |
@@ -654,7 +655,7 @@ void* webserver::select(void* self) | |||
| 654 | 655 | } | |
| 655 | 656 | ||
| 656 | 657 | { | |
| 657 | - std::map<std::string, event_supplier*>::const_iterator it; | ||
| 658 | + std::map<std::string, event_tuple*>::const_iterator it; | ||
| 658 | 659 | pthread_rwlock_rdlock(&di->ws->runguard); | |
| 659 | 660 | for(it = di->ws->event_suppliers.begin(); it != di->ws->event_suppliers.end(); ++it) | |
| 660 | 661 | { | |
@@ -704,7 +705,7 @@ void* webserver::select(void* self) | |||
| 704 | 705 | ::select (max + 1, &rs, &ws, &es, &timeout_value); | |
| 705 | 706 | MHD_run (di->daemon); | |
| 706 | 707 | { | |
| 707 | - std::map<std::string, event_supplier*>::const_iterator it; | ||
| 708 | + std::map<std::string, event_tuple*>::const_iterator it; | ||
| 708 | 709 | pthread_rwlock_rdlock(&di->ws->runguard); | |
| 709 | 710 | for(it = di->ws->event_suppliers.begin(); it != di->ws->event_suppliers.end(); ++it) | |
| 710 | 711 | { | |
@@ -1866,10 +1867,27 @@ void webserver::get_response(cache_entry* ce, http_response** res) | |||
| 1866 | 1867 | *res = ce->response.ptr(); | |
| 1867 | 1868 | } | |
| 1868 | 1869 | ||
| 1869 | - void webserver::register_event_supplier(const std::string& id, event_supplier* ev) | ||
| 1870 | + template<typename T> | ||
| 1871 | + void webserver::register_event_supplier(const std::string& id, event_supplier<T>* ev) | ||
| 1872 | + { | ||
| 1873 | + pthread_rwlock_wrlock(&runguard); | ||
| 1874 | + map<string, event_tuple*>::iterator it = event_suppliers.find(id); | ||
| 1875 | + if(it != event_suppliers.end()) | ||
| 1876 | + delete it->second; | ||
| 1877 | + event_suppliers[id] = new event_tuple(&ev); | ||
| 1878 | + pthread_rwlock_unlock(&runguard); | ||
| 1879 | + } | ||
| 1880 | + | ||
| 1881 | + void webserver::remove_event_supplier(const std::string& id) | ||
| 1870 | 1882 | { | |
| 1871 | 1883 | pthread_rwlock_wrlock(&runguard); | |
| 1872 | - event_suppliers[id] = ev; | ||
| 1884 | + map<string, event_tuple*>::iterator it = event_suppliers.find(id); | ||
| 1885 | + if(it != event_suppliers.end()) | ||
| 1886 | + { | ||
| 1887 | + event_tuple* evp(it->second); | ||
| 1888 | + event_suppliers.erase(it); | ||
| 1889 | + delete evp; | ||
| 1890 | + } | ||
| 1873 | 1891 | pthread_rwlock_unlock(&runguard); | |
| 1874 | 1892 | } | |
| 1875 | 1893 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments