00001 // 00002 // plumbing - C++ wrapper facades for Berkeley sockets 00003 // Copyright (C) 2009 Peter Miller 00004 // 00005 // This program is free software; you can redistribute it and/or modify it 00006 // under the terms of the GNU General Public License, version 3, as published 00007 // by the Free Software Foundation. 00008 // 00009 // This program is distributed in the hope that it will be useful, but WITHOUT 00010 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00012 // more details. 00013 // 00014 // You should have received a copy of the GNU General Public License along 00015 // with this program. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 00018 #ifndef LIBPLUMBING_REACTOR_H 00019 #define LIBPLUMBING_REACTOR_H 00020 00021 #include <cstdarg> 00022 #include <list> 00023 00024 #include <libplumbing/endpoint.h> 00025 #include <libplumbing/format_printf.h> 00026 #include <libplumbing/at/service.h> 00027 00028 namespace plumbing { 00029 00030 class endpoint_functor; // forward 00031 00049 class reactor 00050 { 00051 public: 00055 virtual ~reactor(); 00056 00060 reactor(); 00061 00072 void process(bool block = false); 00073 00078 void 00079 run() 00080 { 00081 while (still_have_work_to_do()) 00082 process(true); 00083 } 00084 00094 void for_each_endpoint(endpoint_functor &func); 00095 00111 void add_endpoint(const endpoint::pointer &ep); 00112 00122 void 00123 register_at_job(const time_value &when, at_job *jp) 00124 { 00125 at.register_job(when, jp); 00126 } 00127 00137 void 00138 register_at_job_delta(const time_value &when, at_job *jp) 00139 { 00140 at.register_job_delta(when, jp); 00141 } 00142 00150 void 00151 unregister_at_job(at_job *jp) 00152 { 00153 at.unregister_job(jp); 00154 } 00155 00160 bool still_have_work_to_do() const { return !endpoints.empty(); } 00161 00162 private: 00163 typedef std::list<endpoint::pointer> endpoints_t; 00164 00169 endpoints_t endpoints; 00170 00175 at_service at; 00176 00184 void fatal_error(const char *fmt, ...) 00185 FORMAT_PRINTF(2, 3); 00196 void fatal_error_v(const char *fmt, va_list ap) 00197 FORMAT_PRINTF(2, 0); 00198 00203 void kill_suicidal_endpoints(); 00204 00208 reactor(const reactor &); 00209 00213 reactor &operator=(const reactor &); 00214 }; 00215 00216 } 00217 00218 #endif // LIBPLUMBING_REACTOR_H 00219 // vim: set ts=8 sw=4 et