• Classes
  • Modules
  • Namespaces
  • Files
  • Related Pages
  • File List
  • File Members

acsServiceController.h

Go to the documentation of this file.
00001 #ifndef _ACS_SERVICE_CONTROLLER_H_
00002 #define _ACS_SERVICE_CONTROLLER_H_
00003 
00004 /*******************************************************************************
00005 *    ALMA - Atacama Large Millimiter Array
00006 *    (c) European Southern Observatory, 2002
00007 *    Copyright by ESO (in the framework of the ALMA collaboration)
00008 *    and Cosylab 2002, All rights reserved
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 * "@(#) $Id: acsServiceController.h,v 1.9 2009/10/06 13:46:25 msekoran Exp $"
00025 *
00026 * who       when      what
00027 * --------  --------  ----------------------------------------------
00028 * azagar   2008-10-21 created
00029 */
00030 
00031 #include "acsRequest.h"
00032 #include <acsThreadManager.h>
00033 #include <map>
00034 #include <string>
00035 #include <AcsAlarmSystemC.h>
00036 
00037 const ACE_Time_Value TIME_PERIOD(15);
00038 
00039 class DetailedServiceStateProvider {
00040   public:
00041     virtual ~DetailedServiceStateProvider() {};
00042     virtual acsdaemon::ServiceState getDetailedServiceState(ACSServiceRequestDescription *desc, CORBA::Object_ptr obj) = 0;
00043 };
00044 
00045 class ControllerThread : public ACS::Thread {
00046   private:
00047     ACSDaemonContext *context;
00048     ACE_Thread_Mutex *m_mutex;
00049     ACE_Condition<ACE_Thread_Mutex> *m_wait;
00050     volatile bool running;
00051   public:
00052     ControllerThread(const ACE_CString &name,
00053            const ACS::TimeInterval& responseTime = ThreadBase::defaultResponseTime, 
00054            const ACS::TimeInterval& sleepTime = ThreadBase::defaultSleepTime);
00055     ~ControllerThread();
00056     void setContext(ACSDaemonContext *icontext) { context = icontext; }
00057     void onStart();
00058     void stop();
00059     void exit();
00060     void runLoop() ACE_THROW_SPEC ((CORBA::SystemException, ::ACSErrTypeCommon::BadParameterEx));
00061 };
00062 
00063 class ControlledServiceRequest;
00064 
00065 class ServiceController {
00066   protected:
00067     ACSDaemonContext *context;
00068     bool autorestart;
00069     ACE_Thread_Mutex *m_mutex;
00070     volatile acsdaemon::ServiceState state;
00071     volatile bool active;
00072     Request *startreq, *stopreq; // last request in queue (either start or stop, not both)
00073 
00074     friend class ControlledServiceRequest;
00075     // called once the request returned by createControlledServiceRequest begins stopping the service
00076     void stopping();
00077     // called once the request returned by createControlledServiceRequest is complete
00078     void requestComplete(Request *request);
00079     virtual bool setState(acsdaemon::ServiceState istate);
00080   protected:
00081     virtual ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL) = 0;
00082     virtual acsdaemon::ServiceState getActualState() = 0;
00083     virtual void fireAlarm(acsdaemon::ServiceState state) = 0;
00084   public:
00085     ServiceController(ACSDaemonContext *icontext, bool iautorestart);
00086     virtual ~ServiceController();
00087     ACSDaemonContext *getContext() { return context; }
00088     void restart();
00089     bool start(acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceAlreadyRunningEx));
00090     void stop(acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceNotRunningEx));
00091     acsdaemon::ServiceState getLastState() { return state; }
00092 };
00093 
00094 class ControlledServiceRequest : public Request {
00095   private:
00096     ServiceController *controller;
00097     Request *request;
00098     bool delreq;
00099     bool isstopping;
00100   protected:
00101     void abort();
00102     bool execute();
00103   public:
00104     ControlledServiceRequest(ServiceController *icontroller, Request *irequest, bool iisstopping);
00105     ~ControlledServiceRequest();
00106 };
00107 
00108 class ImpController;
00109 
00110 class ImpRequest : public Request {
00111   private:
00112     ImpController *controller;
00113     ACE_CString command;
00114   protected:
00115     void abort() {}
00116     bool execute();
00117   public:
00118     ImpRequest(ImpController *icontroller, ACSServiceRequestType itype, ACSServiceType iservice);
00119 };
00120 
00121 class ImpController : public ServiceController {
00122   private:
00123     ACSServiceType service;
00124     ACE_CString corbaloc;
00125   protected:
00126     ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL);
00127     acsdaemon::ServiceState getActualState();
00128     void fireAlarm(acsdaemon::ServiceState state) {}
00129   public:
00130     ImpController(ACSDaemonContext *icontext, ACSServiceType iservice, bool iautostart = true);
00131     ACSServiceType getACSService() { return service; }
00132     void setManagerReference(const short instance_number, const char * ref);
00133 };
00134 
00135 class ACSServiceController : public ServiceController {
00136   private:
00137     ACSServiceRequestDescription *desc;
00138     ACE_CString corbaloc;
00139     bool alarmSystemInitialized;
00140     ::alarmsystem::AlarmService_var alarmService;
00141  protected:
00142     ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL);
00143     acsdaemon::ServiceState getActualState();
00144     virtual bool setState(acsdaemon::ServiceState istate);
00145     void fireAlarm(acsdaemon::ServiceState state);
00146   public:
00147     ACSServiceController(ACSDaemonContext *icontext, ACSServiceRequestDescription *idesc, bool iautostart);
00148     ~ACSServiceController();
00149 };
00150 
00151 class ACSDaemonContext {
00152   private:
00153     CORBA::ORB_ptr orb;
00154     ACS::ThreadManager tm;
00155     RequestProcessorThread *reqproc;
00156     ControllerThread *ctrl;
00157     ACE_Thread_Mutex *m_mutex;
00158     ServiceController **impcontrollers;
00159     ServiceController **acsservicecontrollers;
00160     std::map<std::string, ServiceController **> acsservicecontrollersmap;
00161     ServiceController *getImpController(ACSServiceType service);
00162     ServiceController *getACSServiceController(ACSServiceRequestDescription *desc);
00163     std::map<short, std::string> managerReferences;
00164     DetailedServiceStateProvider *detailedServiceStateProvider;
00165     void setImpControllersManagerReference(const short instance_number, const char * ref);
00166   public:
00167     ACSDaemonContext(std::string name, DetailedServiceStateProvider *dssp = NULL);
00168     ~ACSDaemonContext();
00169     void initialize(CORBA::ORB_ptr iorb);
00170     void dispose(CORBA::ORB_ptr iorb);
00171     void processRequest(ACSServiceRequestTarget target, ACSServiceRequestType type, ACSServiceRequestDescription *desc, acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceAlreadyRunningEx, acsdaemonErrType::ServiceNotRunningEx));
00172     RequestProcessorThread *getRequestProcessor() { return reqproc; }
00173     CORBA::ORB_ptr getORB() { return orb; }
00174     void checkControllers();
00175     acsdaemon::ServiceState getACSServiceState(int instance_number, const char *name = NULL);
00176     acsdaemon::ServiceState getDetailedServiceState(ACSServiceRequestDescription *desc, CORBA::Object_ptr obj) const {
00177         return detailedServiceStateProvider ? detailedServiceStateProvider->getDetailedServiceState(desc, obj) : acsdaemon::RUNNING; };
00178     void setManagerReference(const short instance_number, const char * ref) { managerReferences[instance_number] = ref; setImpControllersManagerReference(instance_number, ref); };
00179     const char * getManagerReference(const short instance_number) { if (managerReferences.find(instance_number) != managerReferences.end()) return managerReferences[instance_number].c_str(); else return NULL; }
00180 };
00181 
00182 
00183 #endif

Generated on Thu Jul 8 2010 19:47:46 for ACS-9.0 C++ API by  doxygen 1.7.0