7 #include "isisinstrumentcontrol.h"
25 class CisisicpModule :
public ATL::CAtlServiceModuleT< CisisicpModule, IDS_SERVICENAME >
28 DECLARE_LIBID(LIBID_isisicpLib)
31 HRESULT InitializeSecurity() throw()
33 // TODO : Call CoInitializeSecurity and provide the appropriate security settings for
35 // Suggested - PKT Level Authentication,
36 // Impersonation Level of RPC_C_IMP_LEVEL_IDENTIFY
37 // and an appropiate Non NULL Security Descriptor.
41 HRESULT Start(int nShowCmd) throw();
43 static int reportFunction(const DAEstatus_message& mess, void* arg);
44 static int writeEventLog(int sev, const char* message, void* arg);
48 class ISISICPApp : public Poco::Util::Application
52 ISISICPApp(int nShowCmd) : Poco::Util::Application(), m_nShowCmd(nShowCmd) { }
53 virtual int main(const std::vector<std::string>& args);
59 void initialize(Poco::Util::Application& self)
61 registerExtraLoggerChannels();
62 if (loadAppConfig("isisicp
") != 0) // does equivalent job of default loadConfiguration()
64 // LogEventEx(ICP_MSG_ERROR, "Cannot load isisicp.default.properties
", EVENTLOG_ERROR_TYPE);
65 throw std::exception("Cannot load isisicp.default.properties
");
67 Application::initialize(self);
71 CisisicpModule _AtlModule;
72 static ISISICPApp* the_app = NULL;
74 extern "C
" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/,
75 LPTSTR lpCmdLine, int nShowCmd)
78 std::vector<std::string> args;
79 args.push_back("isisicp.exe
");
80 the_app = new ISISICPApp(nShowCmd);
82 // Poco::Thread main_thread;
83 // Poco::RunnableAdapter<ISISICPApp> ra(*the_app, &ISISICPApp::runApp);
84 // main_thread.start(ra);
85 // main_thread.join();
93 int ISISICPApp::main(const std::vector<std::string>& args)
95 return _AtlModule.WinMain(m_nShowCmd);
99 HRESULT CisisicpModule::Start(int nShowCmd) throw()
101 struct tm* tm_struct;
102 static char buffer[120];
103 static report_arg_t params;
106 tm_struct = localtime(&the_time);
108 unsigned flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
109 flags &= ~_CRTDBG_LEAK_CHECK_DF; // do not check leak on exit
110 if (getenv("ISISICP_DEBUG_MEMORY
") != NULL)
112 i = atoi(getenv("ISISICP_DEBUG_MEMORY
"));
113 flags |= _CRTDBG_LEAK_CHECK_DF; // check leaks on program exit
116 flags |= _CRTDBG_DELAY_FREE_MEM_DF; // keep freed memory to check
119 flags |= _CRTDBG_CHECK_ALWAYS_DF; // check every allocation
123 flags &= ~_CRTDBG_CHECK_ALWAYS_DF;
124 flags &= 0x0000FFFF; // clear old frequency
128 sprintf(buffer, "c:\\data\\log\\ICPmemory%lu.log
", the_time);
129 HANDLE hdebug = CreateFile(buffer, GENERIC_WRITE, FILE_SHARE_READ,
130 defaultNoInheritHandles(), CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, 0);
131 _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG /* | _CRTDBG_MODE_WNDW */ );
132 _CrtSetReportFile( _CRT_WARN, hdebug );
133 _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG /* | _CRTDBG_MODE_WNDW */ );
134 _CrtSetReportFile( _CRT_ERROR, hdebug );
135 _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG /* | _CRTDBG_MODE_WNDW */ );
136 _CrtSetReportFile( _CRT_ASSERT, hdebug );
138 _CrtSetDbgFlag(flags);
139 // _CrtSetAllocHook(MyAllocHook);
140 // mkdir("c:\\data\\devel
");
142 TCHAR szPath[ MAX_PATH ];
143 GetModuleFileName( NULL, szPath, MAX_PATH ); // get the full path of isisicp.exe executable
144 std::string icp_service_dir = szPath;
145 icp_service_dir.erase(icp_service_dir.find_last_of("/\\
")); // strip off filename to get directory
148 AddEventSource(m_szServiceName, (icp_service_dir+"\\isisicp_extMC.dll
").c_str(), 0);
150 params.module = this;
151 params.logdir = the_app->config().getString("isisicp.logdir
", "c:\\data\\log
");
152 setICPChannelReporter(reportFunction, ¶ms);
154 the_app->logger().information("********************* Starting *********************
");
156 // make sure only one ICP running
157 Poco::NamedMutex proc_lock("ISISICP_PROCESS_LOCK
");
158 if ( !proc_lock.tryLock() )
160 std::string mess = Poco::format("Cannot launch ISISICP from %s (global mutex already held)
", icp_service_dir);
161 the_app->logger().fatal(mess);
162 std::cerr << mess << std::endl;
163 LogEventEx(ICP_MSG_ERROR, mess.c_str(), EVENTLOG_ERROR_TYPE);
167 // we need a console for some of the commands we spawn from the ICP e.g. end_of_run.cmd -> xcopy
169 SetConsoleTitle("ISISICP Debug Output
");
170 HWND con_win = GetConsoleWindow();
173 if (the_app->config().getBool("isisicp.hideconsole
"))
175 ShowWindow(con_win, SW_HIDE);
179 ShowWindow(con_win, SW_MINIMIZE);
182 // set process affinity mask to stop us grabbing all cpus on the system
183 DWORD_PTR proc_affinity_mask, system_affinity_mask;
184 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_affinity_mask, &system_affinity_mask) != 0)
186 proc_affinity_mask = the_app->config().getInt("isisicp.processAffinityMask
", proc_affinity_mask);
187 proc_affinity_mask &= system_affinity_mask; // it is an error to ask for more processors than are present on the system
188 if ( (proc_affinity_mask != 0) && (SetProcessAffinityMask(GetCurrentProcess(), proc_affinity_mask) != 0) )
190 the_app->logger().information("Set process CPU affinity mask to 0x
" + Poco::NumberFormatter::formatHex(proc_affinity_mask));
194 the_app->logger().error("cannot set process affinity
");
199 the_app->logger().error("cannot
get process affinity
");
202 InstrumentControlHolder::setDefaultArgs(the_app, icp_service_dir, reportFunction, writeEventLog, ¶ms);
203 CoInitializeEx(NULL, COINIT_MULTITHREADED);
204 the_app->logger().information("Starting ISISICP
");
205 LogEventEx(ICP_MSG_INFORMATIONAL, "Starting ISISICP
", EVENTLOG_INFORMATION_TYPE);
206 return CAtlServiceModuleT<CisisicpModule,IDS_SERVICENAME>::Start(nShowCmd);
209 HRESULT CisisicpModule::OnStop()
211 the_app->logger().information("Stopping ISISICP
");
212 LogEventEx(ICP_MSG_INFORMATIONAL, "Stopping ISISICP
", EVENTLOG_INFORMATION_TYPE);
216 int CisisicpModule::reportFunction(const DAEstatus_message& mess, void* arg)
218 static bool first_call = false;
219 static char icp_log_file_template[240], icp_log_file[240];
222 struct tm* tm_struct = localtime(&the_time);
223 report_arg_t* params = (report_arg_t*)arg;
224 // strftime(icp_log_file_template, sizeof(icp_log_file_template), "c:\\data\\log\\icp-%Y-%m-%d-%a-%%02d.log
", tm_struct);
225 // for(int i=0; i <= 99; i++)
227 // _snprintf(icp_log_file, sizeof(icp_log_file), icp_log_file_template, i);
228 // if (stat(icp_log_file, &stat_buffer) != 0)
230 // break; // file not found, so we can use the name
233 sprintf(icp_log_file_template, "%s\\icp-%%Y-%%m-%%d-%%a.log
", params->logdir.c_str());
234 strftime(icp_log_file, sizeof(icp_log_file), icp_log_file_template, tm_struct);
235 if (params->filename != icp_log_file)
237 if (params->h != INVALID_HANDLE_VALUE)
239 CloseHandle(params->h);
241 params->filename = icp_log_file;
242 params->h = createAppendFile(icp_log_file);
243 if (params->h != INVALID_HANDLE_VALUE)
245 params->file_error = false;
249 params->module->LogEventEx(ICP_MSG_ERROR, "Cannot create ICP LOG file
", EVENTLOG_ERROR_TYPE);
250 params->file_error = true;
255 appendToFileAsync(params->h, f.str().c_str());
257 OutputDebugString(mess.str().c_str());
258 OutputDebugString("\n
");
260 // if (mess >= SEV_ERROR)
262 // params->module->LogEventEx(ICP_MSG_ERROR, mess.str().c_str(), EVENTLOG_ERROR_TYPE);
267 int CisisicpModule::writeEventLog(int sev, const char* message, void* arg)
269 report_arg_t* params = (report_arg_t*)arg;
274 params->module->LogEventEx(ICP_MSG_INFORMATIONAL, message, EVENTLOG_INFORMATION_TYPE);
279 params->module->LogEventEx(ICP_MSG_ERROR, message, EVENTLOG_ERROR_TYPE);
283 params->module->LogEventEx(ICP_MSG_SUCCESS, message, EVENTLOG_SUCCESS);
287 params->module->LogEventEx(ICP_MSG_WARNING, message, EVENTLOG_WARNING_TYPE);
291 params->module->LogEventEx(ICP_MSG_INFORMATIONAL, message, EVENTLOG_INFORMATION_TYPE);
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_ISISICP,"{940B6DC7-2248-474D-B150-B3CC089F166A}") HRESULT InitializeSecurity()