libdap++ Updated for version 3.8.2

SignalHandler.h

Go to the documentation of this file.
00001 
00002 // -*- mode: c++; c-basic-offset:4 -*-
00003 
00004 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
00005 // Access Protocol.
00006 
00007 // Copyright (c) 2002,2003 OPeNDAP, Inc.
00008 // Author: James Gallagher <jgallagher@opendap.org>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
00025 
00026 #ifndef signal_handler_h
00027 #define signal_handler_h
00028 
00029 #include <signal.h>
00030 
00031 #include "EventHandler.h"
00032 #include "InternalErr.h"
00033 
00034 namespace libdap
00035 {
00036 
00037 typedef void Sigfunc(int); // Plauger, 1992
00038 
00066 class SignalHandler
00067 {
00068 private:
00069     // Ensure we're a Singleton.
00070     SignalHandler()
00071     {}
00072 
00073     // Singleton pointer.
00074     static SignalHandler *d_instance;
00075 
00076     // Table of pointers to instances of EventHandlers. Since EventHandler is
00077     // abstract, the pointers will actually reference instances that are
00078     // children of EventHandler. NSIG is defined in signal.h but this may be
00079     // a portability issue.
00080     static EventHandler *d_signal_handlers[NSIG];
00081 
00082     // This array holds the old signal handlers. Once the handler in
00083     // d_signal_handler[signum] is run, look here to see what the original
00084     // action was. This is important since libdap++ is often embedded in code
00085     // that already has a non-default signal handler for things like SIGINT.
00086     static Sigfunc *d_old_handlers[NSIG];
00087 
00088     // Entry point adapter installed into sigaction(). This must be static
00089     // method (or a regular C-function) to conform to sigaction's interface.
00090     // this is the part of SignalHandler that uses the Adapter pattern.
00091     static void dispatcher(int signum);
00092 
00093     // Delete the global instance. Call this with atexit().
00094     static void delete_instance();
00095 
00096     // Call this using pthread_once() to ensure there's only one instance
00097     // when running in a MT world.
00098     static void initialize_instance();
00099 
00100     friend class SignalHandlerTest;
00101     friend class HTTPCacheTest;
00102 
00103 public:
00104     static SignalHandler *instance();
00105 
00107     virtual ~SignalHandler()
00108     {}
00109 
00110     EventHandler *register_handler(int signum, EventHandler *eh,
00111                                    bool override = false);
00112 
00113     EventHandler *remove_handler(int signum);
00114 };
00115 
00116 } // namespace libdap
00117 
00118 #endif // signal_handler_h