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
00035
const ACE_Time_Value
TIME_PERIOD(60);
00036
00037 class ControllerThread :
public ACS::Thread {
00038
private:
00039 ACSDaemonContext *
context;
00040 ACE_Thread_Mutex *
m_mutex;
00041 ACE_Condition<ACE_Thread_Mutex> *
m_wait;
00042 volatile bool running;
00043
public:
00044
ControllerThread(
const ACE_CString &name,
00045
const ACS::TimeInterval& responseTime = ThreadBase::defaultResponseTime,
00046
const ACS::TimeInterval& sleepTime = ThreadBase::defaultSleepTime);
00047
~ControllerThread();
00048 void setContext(
ACSDaemonContext *icontext) {
context = icontext; }
00049
void onStart();
00050
void stop();
00051
void exit();
00052
void runLoop() ACE_THROW_SPEC ((CORBA::SystemException, ::ACSErrTypeCommon::BadParameterEx));
00053 };
00054
00055 class
ControlledServiceRequest;
00056
00057 class
ServiceController {
00058
protected:
00059 ACSDaemonContext *context;
00060 bool autorestart;
00061 ACE_Thread_Mutex *m_mutex;
00062 acsdaemon::ServiceState state;
00063 bool active;
00064 Request *startreq, *stopreq;
00065
00066
friend class ControlledServiceRequest;
00067
00068
void stopping();
00069
00070
void requestComplete(
Request *request);
00071
virtual bool setState(acsdaemon::ServiceState istate);
00072
protected:
00073
virtual ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL) = 0;
00074
virtual acsdaemon::ServiceState getActualState() = 0;
00075
virtual void fireAlarm(acsdaemon::ServiceState state) = 0;
00076
public:
00077 ServiceController(
ACSDaemonContext *icontext,
bool iautorestart);
00078
virtual ~ServiceController();
00079 ACSDaemonContext *getContext() {
return context; }
00080
void restart();
00081
bool start(acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceAlreadyRunningEx));
00082
void stop(acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceNotRunningEx));
00083 acsdaemon::ServiceState getLastState() {
return state; }
00084 };
00085
00086 class ControlledServiceRequest :
public Request {
00087
private:
00088 ServiceController *controller;
00089 Request *request;
00090 bool delreq;
00091 bool isstopping;
00092
protected:
00093
void abort();
00094
bool execute();
00095
public:
00096 ControlledServiceRequest(ServiceController *icontroller,
Request *irequest,
bool iisstopping);
00097 ~ControlledServiceRequest();
00098 };
00099
00100
class ImpController;
00101
00102 class ImpRequest :
public Request {
00103
private:
00104 ImpController *controller;
00105 ACE_CString command;
00106
protected:
00107 void abort() {}
00108
bool execute();
00109
public:
00110
ImpRequest(
ImpController *icontroller, ACSServiceRequestType itype, ACSServiceType iservice);
00111 };
00112
00113 class ImpController :
public ServiceController {
00114
private:
00115 ACSServiceType service;
00116 ACE_CString corbaloc;
00117
protected:
00118 ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL);
00119 acsdaemon::ServiceState getActualState();
00120 void fireAlarm(acsdaemon::ServiceState state) {}
00121
public:
00122
ImpController(
ACSDaemonContext *icontext, ACSServiceType iservice,
bool iautostart =
true);
00123 ACSServiceType getACSService() {
return service; }
00124
void setManagerReference(
const char * ref);
00125 };
00126
00127 class ACSServiceController :
public ServiceController {
00128
private:
00129 ACSServiceRequestDescription *desc;
00130 ACE_CString corbaloc;
00131 bool alarmSystemInitialized;
00132
protected:
00133 ControlledServiceRequest *createControlledServiceRequest(ACSServiceRequestType itype, acsdaemon::DaemonCallback_ptr callback = NULL);
00134 acsdaemon::ServiceState getActualState();
00135
virtual bool setState(acsdaemon::ServiceState istate);
00136
void fireAlarm(acsdaemon::ServiceState state);
00137
public:
00138
ACSServiceController(
ACSDaemonContext *icontext,
ACSServiceRequestDescription *idesc,
bool iautostart);
00139 ~
ACSServiceController();
00140 };
00141
00142 class ACSDaemonContext {
00143
private:
00144 CORBA::ORB_ptr orb;
00145 ACS::ThreadManager tm;
00146 RequestProcessorThread *reqproc;
00147 ControllerThread *ctrl;
00148 ACE_Thread_Mutex *m_mutex;
00149 ServiceController **impcontrollers;
00150 ServiceController **acsservicecontrollers;
00151 std::map<const char *, ServiceController **> acsservicecontrollersmap;
00152 ServiceController *getImpController(ACSServiceType service);
00153 ServiceController *getACSServiceController(
ACSServiceRequestDescription *desc);
00154 ACE_CString managerReference;
00155
void setImpControllersManagerReference(
const char * ref);
00156
public:
00157
ACSDaemonContext(std::string name);
00158 ~
ACSDaemonContext();
00159
void initialize(CORBA::ORB_ptr iorb);
00160
void dispose(CORBA::ORB_ptr iorb);
00161
void processRequest(ACSServiceRequestTarget target, ACSServiceRequestType type,
ACSServiceRequestDescription *desc, acsdaemon::DaemonCallback_ptr callback = NULL) ACE_THROW_SPEC ((acsdaemonErrType::ServiceAlreadyRunningEx, acsdaemonErrType::ServiceNotRunningEx));
00162 RequestProcessorThread *getRequestProcessor() {
return reqproc; }
00163 CORBA::ORB_ptr getORB() {
return orb; }
00164
void checkControllers();
00165 acsdaemon::ServiceState getACSServiceState(
int instance_number,
const char *name = NULL);
00166 void setManagerReference(
const char * ref) { managerReference = ref; setImpControllersManagerReference(ref); };
00167 const char * getManagerReference()
const {
return managerReference.is_empty() ?
NULL : managerReference.c_str(); }
00168 };
00169
00170
00171
#endif