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 typedef void Sigfunc(int); // Plauger, 1992
00035 
00063 class SignalHandler
00064 {
00065 private:
00066     // Ensure we're a Singleton.
00067     SignalHandler()
00068     {}
00069 
00070     // Singleton pointer.
00071     static SignalHandler *d_instance;
00072 
00073     // Table of pointers to instances of EventHandlers. Since EventHandler is
00074     // abstract, the pointers will actually reference instances that are
00075     // children of EventHandler. NSIG is defined in signal.h but this may be
00076     // a portability issue.
00077     static EventHandler *d_signal_handlers[NSIG];
00078 
00079     // This array holds the old signal handlers. Once the handler in
00080     // d_signal_handler[signum] is run, look here to see what the original
00081     // action was. This is important since libdap++ is often embedded in code
00082     // that already has a non-default signal handler for things like SIGINT.
00083     static Sigfunc *d_old_handlers[NSIG];
00084 
00085     // Entry point adapter installed into sigaction(). This must be static
00086     // method (or a regular C-function) to conform to sigaction's interface.
00087     // this is the part of SignalHandler that uses the Adapter pattern.
00088     static void dispatcher(int signum);
00089 
00090     // Delete the global instance. Call this with atexit().
00091     static void delete_instance();
00092 
00093     // Call this using pthread_once() to ensure there's only one instance
00094     // when running in a MT world.
00095     static void initialize_instance();
00096 
00097     friend class SignalHandlerTest;
00098     friend class HTTPCacheTest;
00099 
00100 public:
00101     static SignalHandler *instance();
00102 
00104     virtual ~SignalHandler()
00105     {}
00106 
00107     EventHandler *register_handler(int signum, EventHandler *eh,
00108                                    bool override = false);
00109 
00110     EventHandler *remove_handler(int signum);
00111 };
00112 
00113 #endif // signal_handler_h

Generated on Wed Jun 27 12:56:39 2007 for libdap++ by  doxygen 1.4.7