![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #ifndef WNS_SIMULATOR_SIGNALHANDLER_HPP 00029 #define WNS_SIMULATOR_SIGNALHANDLER_HPP 00030 00031 #include <WNS/container/Registry.hpp> 00032 #include <WNS/Singleton.hpp> 00033 #include <WNS/NonCopyable.hpp> 00034 00035 #include <boost/signal.hpp> 00036 00037 #include <memory> 00038 #include <string> 00039 #include <csignal> 00040 00041 namespace wns { namespace simulator { 00042 00047 class SignalHandler : 00048 private NonCopyable 00049 { 00050 friend class wns::DefaultCreation<wns::simulator::SignalHandler>; 00051 typedef boost::signal0<void> Handler; 00052 typedef wns::container::Registry<int, Handler*, wns::container::registry::DeleteOnErase> Map; 00053 public: 00060 template <typename HANDLER> 00061 void 00062 addSignalHandler(int signum, HANDLER handler) 00063 { 00064 // block all signals until the handler is added 00065 sigset_t allSignals; 00066 sigfillset(&allSignals); 00067 sigprocmask(SIG_BLOCK, &allSignals, NULL); 00068 00069 // will throw if there is already a handler registered with this 00070 // number 00071 // use std::auto_ptr to avoid leak if registry throws 00072 std::auto_ptr<Handler> localHandler(new Handler()); 00073 map_.insert(signum, localHandler.get()); 00074 // if everything went fine (no throw) release the pointer since the 00075 // registry now cares about the pointer 00076 localHandler.release(); 00077 00078 map_.find(signum)->disconnect_all_slots(); 00079 map_.find(signum)->connect(handler); 00080 struct sigaction action; 00081 sigfillset (&action.sa_mask); 00082 action.sa_flags = 0; 00083 action.sa_handler = SignalHandler::catchSignal; 00084 sigaction(signum, &action, NULL); 00085 // Unblock all signals 00086 sigprocmask(SIG_UNBLOCK, &allSignals, NULL); 00087 } 00088 00093 void 00094 removeSignalHandler(int signum); 00095 00099 void 00100 removeAllSignalHandlers(); 00101 00102 private: 00103 // Callable by SingletonHolder only 00107 SignalHandler(); 00108 00112 virtual 00113 ~SignalHandler(); 00114 00119 static void 00120 catchSignal(int signum); 00121 00125 Map map_; 00126 }; 00127 00132 typedef wns::SingletonHolder<wns::simulator::SignalHandler, 00133 wns::DefaultCreation, 00134 wns::AtApplicationExit> GlobalSignalHandler; 00135 00136 } // namespace simulator 00137 } // namespace wns 00138 00139 #endif // NOT defined WNS_SIMULATOR_SIGNALHANDLER_HPP 00140
1.5.5