xorp

service.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 
00023 #ifndef __LIBXORP_SERVICE_HH__
00024 #define __LIBXORP_SERVICE_HH__
00025 
00026 #include "bug_catcher.hh"
00027 
00031 enum ServiceStatus {
00032     SERVICE_READY   = 0x001,    // Ready for startup
00033     SERVICE_STARTING    = 0x002,    // Starting up
00034     SERVICE_RUNNING = 0x004,    // Running, service operational
00035     SERVICE_PAUSING = 0x008,    // Transitioning to paused state
00036     SERVICE_PAUSED  = 0x010,    // Paused, non-operational
00037     SERVICE_RESUMING    = 0x020,    // Resuming from pause
00038     SERVICE_SHUTTING_DOWN = 0x040,  // Transitioning to shutdown
00039     SERVICE_SHUTDOWN    = 0x080,    // Shutdown, non-operational
00040     SERVICE_FAILED  = 0x100,    // Failed, non-operational
00041     SERVICE_ALL     = SERVICE_READY |
00042               SERVICE_STARTING |
00043               SERVICE_RUNNING |
00044                   SERVICE_PAUSING |
00045               SERVICE_PAUSED |
00046               SERVICE_RESUMING |
00047               SERVICE_SHUTTING_DOWN |
00048               SERVICE_SHUTDOWN |
00049               SERVICE_FAILED
00050 };
00051 
00057 const char* service_status_name(ServiceStatus s);
00058 
00059 class ServiceChangeObserverBase;
00060 
00082 class ServiceBase : public BugCatcher {
00083 public:
00084     ServiceBase(const string& name = "Unknown");
00085 
00086     virtual ~ServiceBase() = 0;
00087 
00095     virtual int startup() = 0;
00096 
00104     virtual int shutdown() = 0;
00105 
00115     virtual int reset();
00116 
00126     virtual int pause();
00127 
00137     virtual int resume();
00138 
00144     const string& service_name() const      { return _name; }
00145 
00149     ServiceStatus status() const        { return _status; }
00150 
00155     const string& status_note() const       { return _note; }
00156 
00160     const char* status_name() const;
00161 
00169     int set_observer(ServiceChangeObserverBase* so);
00170 
00177     int unset_observer(ServiceChangeObserverBase* so);
00178 
00179 protected:
00186     void set_status(ServiceStatus status, const string& note);
00187 
00193     void set_status(ServiceStatus status);
00194 
00195 protected:
00196     string          _name;
00197     ServiceStatus       _status;
00198     string          _note;
00199     ServiceChangeObserverBase*  _observer;
00200 };
00201 
00205 class ServiceChangeObserverBase {
00206 public:
00207     virtual ~ServiceChangeObserverBase() = 0;
00208 
00209     virtual void status_change(ServiceBase*  service,
00210                    ServiceStatus old_status,
00211                    ServiceStatus new_status) = 0;
00212 };
00213 
00219 class ServiceFilteredChangeObserver
00220     : public ServiceChangeObserverBase {
00221 public:
00232     ServiceFilteredChangeObserver(ServiceChangeObserverBase*    child,
00233                   ServiceStatus         from_mask,
00234                   ServiceStatus         to_mask);
00235 
00236 protected:
00237     void status_change(ServiceBase* service,
00238                ServiceStatus    old_status,
00239                ServiceStatus    new_status);
00240 
00241 protected:
00242     ServiceChangeObserverBase*  _child;
00243     ServiceStatus       _from_mask;
00244     ServiceStatus       _to_mask;
00245 };
00246 
00247 #endif // __LIBXORP_SERVICE_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations