Blame AlarmHandler.h

Packit a4aae4
Packit a4aae4
// -*- mode: c++; c-basic-offset:4 -*-
Packit a4aae4
Packit a4aae4
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
Packit a4aae4
// Access Protocol.
Packit a4aae4
Packit a4aae4
// Copyright (c) 2002,2003 OPeNDAP, Inc.
Packit a4aae4
// Author: James Gallagher <jgallagher@opendap.org>
Packit a4aae4
//
Packit a4aae4
// This library is free software; you can redistribute it and/or
Packit a4aae4
// modify it under the terms of the GNU Lesser General Public
Packit a4aae4
// License as published by the Free Software Foundation; either
Packit a4aae4
// version 2.1 of the License, or (at your option) any later version.
Packit a4aae4
//
Packit a4aae4
// This library is distributed in the hope that it will be useful,
Packit a4aae4
// but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a4aae4
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a4aae4
// Lesser General Public License for more details.
Packit a4aae4
//
Packit a4aae4
// You should have received a copy of the GNU Lesser General Public
Packit a4aae4
// License along with this library; if not, write to the Free Software
Packit a4aae4
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit a4aae4
//
Packit a4aae4
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
Packit a4aae4
Packit a4aae4
#ifndef alarm_handler_h
Packit a4aae4
#define alarm_handler_h
Packit a4aae4
Packit a4aae4
#include <cstdio>
Packit a4aae4
Packit a4aae4
#include <string>
Packit a4aae4
Packit a4aae4
#include "EventHandler.h"
Packit a4aae4
Packit a4aae4
namespace libdap
Packit a4aae4
{
Packit a4aae4
Packit a4aae4
/** Handle the time out alarm. When an OPeNDAP server runs until the time out
Packit a4aae4
    alarm is triggered, this class provides the concrete implementation of
Packit a4aae4
    EventHandler::handle_signal().
Packit a4aae4
Packit a4aae4
    @see EventHandler
Packit a4aae4
    @see SignalHandler
Packit a4aae4
    @author James Gallagher <jgallagher@opendap.org> */
Packit a4aae4
class AlarmHandler : public EventHandler
Packit a4aae4
{
Packit a4aae4
private:
Packit a4aae4
    FILE *d_file;  // Sink for the Error object.
Packit a4aae4
    string d_version;
Packit a4aae4
Packit a4aae4
public:
Packit a4aae4
    // Ensure that d_stream gets initialized...
Packit a4aae4
    AlarmHandler() : d_file( 0 )// , d_stream( cout )
Packit a4aae4
    {}
Packit a4aae4
Packit a4aae4
    AlarmHandler(FILE *s) : d_file(s)//, d_stream( cout )
Packit a4aae4
    {}
Packit a4aae4
Packit a4aae4
    /** Store information to be used by the handler.
Packit a4aae4
        @param out Write to this stream.
Packit a4aae4
        @deprecated The stream param is ignored. Use the default constructor instead. */
Packit a4aae4
    AlarmHandler(ostream &) : d_file(0)//, d_stream( out )
Packit a4aae4
    {}
Packit a4aae4
Packit a4aae4
    virtual ~AlarmHandler()
Packit a4aae4
    {
Packit a4aae4
        if( d_file )
Packit a4aae4
            fclose( d_file ) ;
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
    /** Handle an alarm signal. When one of our servers gets an alarm, that
Packit a4aae4
    means it has hit its time out. We need to dump two CRLF pairs down
Packit a4aae4
    the stream and then send an Error object explaining that a timeout
Packit a4aae4
    has been reached.
Packit a4aae4
Packit a4aae4
    Because this is a signal handler, it should call only reentrant
Packit a4aae4
    system services, functions, et cetera. This handler never returns
Packit a4aae4
    to the code that was running when the alarm signal was raised.
Packit a4aae4
Packit a4aae4
    @param signum We know it is SIGALRM; here as a check
Packit a4aae4
    @return Never returns; calls exit after sending the Error object. */
Packit a4aae4
    virtual void handle_signal(int signum)
Packit a4aae4
    {
Packit a4aae4
        if (signum != SIGALRM)
Packit a4aae4
            throw Error("SIGALRM handler caught another signal!");
Packit a4aae4
Packit a4aae4
        throw Error("Timeout");
Packit a4aae4
    }
Packit a4aae4
Packit a4aae4
};
Packit a4aae4
Packit a4aae4
} // namespace libdap
Packit a4aae4
Packit a4aae4
#endif