Go to the documentation of this file.00001 #ifndef _ACS_SERVICE_CONTROLLER_H_
00002 #define _ACS_SERVICE_CONTROLLER_H_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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;
00073
00074 friend class ControlledServiceRequest;
00075
00076 void stopping();
00077
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 virtual ACE_CString getServiceName() = 0;
00086 ServiceController(ACSDaemonContext *icontext, bool iautorestart);
00087 virtual ~ServiceController();
00088 ACSDaemonContext *getContext() { return context; }
00089 void restart();
00090 bool start(acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceAlreadyRunningEx));
00091 void stop(acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceNotRunningEx));
00092 acsdaemon::ServiceState getLastState() { return state; }
00093 };
00094
00095 class ControlledServiceRequest : public Request {
00096 private:
00097 ServiceController *controller;
00098 Request *request;
00099 bool delreq;
00100 bool isstopping;
00101 protected:
00102 void abort();
00103 bool execute();
00104 public:
00105 ControlledServiceRequest(ServiceController *icontroller, Request *irequest, bool iisstopping);
00106 ~ControlledServiceRequest();
00107 };
00108
00109 class ImpController;
00110
00111 class ImpRequest : public Request {
00112 private:
00113 ImpController *controller;
00114 ACE_CString command;
00115 protected:
00116 void abort() {}
00117 bool execute();
00118 public:
00119 ImpRequest(ImpController *icontroller, ACSServiceRequestType itype, ACSServiceType iservice);
00120 };
00121
00122 class ImpController : public ServiceController {
00123 private:
00124 ACSServiceType service;
00125 ACE_CString corbaloc;
00126 protected:
00127 ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL);
00128 acsdaemon::ServiceState getActualState();
00129 void fireAlarm(acsdaemon::ServiceState state) {}
00130 public:
00131 ImpController(ACSDaemonContext *icontext, ACSServiceType iservice, bool iautostart = true);
00132 ACSServiceType getACSService() { return service; }
00133 virtual ACE_CString getServiceName();
00134 void setManagerReference(const short instance_number, const char * ref);
00135 };
00136
00137 class ACSServiceController : public ServiceController {
00138 private:
00139 ACSServiceRequestDescription *desc;
00140 ACE_CString corbaloc;
00141 bool alarmSystemInitialized;
00142 ::alarmsystem::AlarmService_var alarmService;
00143 protected:
00144 ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL);
00145 acsdaemon::ServiceState getActualState();
00146 virtual bool setState(acsdaemon::ServiceState istate);
00147 void fireAlarm(acsdaemon::ServiceState state);
00148 public:
00149 ACSServiceController(ACSDaemonContext *icontext, ACSServiceRequestDescription *idesc, bool iautostart);
00150 virtual ACE_CString getServiceName();
00151 ~ACSServiceController();
00152 };
00153
00154 class ACSDaemonContext {
00155 private:
00156 CORBA::ORB_ptr orb;
00157 ACS::ThreadManager tm;
00158 RequestProcessorThread *reqproc;
00159 ControllerThread *ctrl;
00160 ACE_Thread_Mutex *m_mutex;
00161 ServiceController **impcontrollers;
00162 ServiceController **acsservicecontrollers;
00163 std::map<std::string, ServiceController **> acsservicecontrollersmap;
00164 ServiceController *getImpController(ACSServiceType service);
00165 ServiceController *getACSServiceController(ACSServiceRequestDescription *desc);
00166 std::map<short, std::string> managerReferences;
00167 DetailedServiceStateProvider *detailedServiceStateProvider;
00168 void setImpControllersManagerReference(const short instance_number, const char * ref);
00169 public:
00170 ACSDaemonContext(std::string name, DetailedServiceStateProvider *dssp = NULL);
00171 ~ACSDaemonContext();
00172 void initialize(CORBA::ORB_ptr iorb);
00173 void dispose(CORBA::ORB_ptr iorb);
00174 void processRequest(ACSServiceRequestTarget target, ACSServiceRequestType type, ACSServiceRequestDescription *desc, acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceAlreadyRunningEx, acsdaemonErrType::ServiceNotRunningEx));
00175 RequestProcessorThread *getRequestProcessor() { return reqproc; }
00176 CORBA::ORB_ptr getORB() { return orb; }
00177 void checkControllers();
00178 acsdaemon::ServiceState getACSServiceState(int instance_number, const char *name = NULL);
00179 acsdaemon::ServiceState getDetailedServiceState(ACSServiceRequestDescription *desc, CORBA::Object_ptr obj) const {
00180 return detailedServiceStateProvider ? detailedServiceStateProvider->getDetailedServiceState(desc, obj) : acsdaemon::RUNNING; };
00181 void setManagerReference(const short instance_number, const char * ref) { managerReferences[instance_number] = ref; setImpControllersManagerReference(instance_number, ref); };
00182 const char * getManagerReference(const short instance_number) { if (managerReferences.find(instance_number) != managerReferences.end()) return managerReferences[instance_number].c_str(); else return NULL; }
00183 };
00184
00185
00186 #endif