1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 #ifndef http_cache_interrupt_handler_h
27 #define http_cache_interrupt_handler_h
28 
29 #include <signal.h>
30 
31 #include <cassert>
32 #include <iostream>
33 #include <algorithm>
34 
35 #include "HTTPCache.h"
36 #include "EventHandler.h"
37 #include "debug.h"
38 
39 namespace libdap
40 {
41 
42 static void
unlink_file(const string & f)43 unlink_file(const string &f)
44 {
45     unlink(f.c_str());
46 }
47 
48 /** Handle SIGINT for HTTPCache. When the cache is in use and the process is
49     sent SIGINT, we must make sure that the cache is left in a consistent
50     state. This includes removing the lock file, updating the index file and
51     making sure no partially written data or meta data files exist.
52 
53     @see EventHandler
54     @see SignalHandler
55     @see HTTPCache
56     @author James Gallagher <jgallagher@opendap.org> */
57 class HTTPCacheInterruptHandler : public EventHandler
58 {
59 private:
60 
61 public:
62     ///
HTTPCacheInterruptHandler()63     HTTPCacheInterruptHandler()
64     {}
65 
66     ///
~HTTPCacheInterruptHandler()67     virtual ~HTTPCacheInterruptHandler()
68     {}
69 
70     /** Handle SIGINT. This handler first deletes any files opened but not
71     added to the cache index files and then calls
72     HTTPCache::delete_instance().
73 
74     @param signum We know it is SIGINT; included here as a check and only
75     when NDEBUG is not defined.
76     @return Never returns. */
handle_signal(int signum)77     virtual void handle_signal(int signum)
78     {
79         assert(signum == SIGINT);
80         DBG(cerr << "Inside the HTTPCacheInterruptHandler." << endl);
81 
82         vector<string> *of = &HTTPCache::_instance->d_open_files;
83 
84         DBG(copy(of->begin(), of->end(),
85                  ostream_iterator<string>(cerr, "\n")));
86 
87         for_each(of->begin(), of->end(), unlink_file);
88 
89         HTTPCache::delete_instance();
90     }
91 };
92 
93 } // namespace libdap
94 
95 #endif // http_cache_interrupt_handler_h
96