lemAutoRun  1.0
lemAutoRun.cpp
Go to the documentation of this file.
1 /********************************************************************************************
2 
3  lemAutoRun.cpp
4 
5 *********************************************************************************************
6 
7  begin : Andreas Suter, 2006/03/14
8  modfied: :
9  copyright : (C) 2006 by
10  email : andreas.suter@psi.ch
11 
12 ********************************************************************************************/
13 
14 /***************************************************************************
15  * *
16  * This program is free software; you can redistribute it and/or modify *
17  * it under the terms of the GNU General Public License as published by *
18  * the Free Software Foundation; either version 2 of the License, or *
19  * (at your option) any later version. *
20  * *
21  ***************************************************************************/
22 
23 #include <iostream>
24 using namespace std;
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <QCoreApplication>
30 #include <QObject>
31 #include <QString>
32 
33 #include <QFile>
34 #include <QTimer>
35 
36 #include "PAutoRunParser.h"
37 #include "PErrorHandler.h"
38 #include "PLemAutoRun.h"
39 
40 #include "msystem.h"
41 
42 //---------------------------------------------------------------------------------------
52 int lar_wrong_syntax(int argc, char *argv[], QString &host, QString &exp, bool &daemon)
53 {
54  int wrong = 0;
55 
56  host = "";
57  exp = "";
58  daemon = false;
59 
60  if ((argc < 1) || (argc > 6)) // number of arguments out of range
61  return 1;
62 
63  switch (argc) {
64  case 1: // only calling argument
65  break;
66  case 2: // only '-D' possible
67  if (strstr(argv[1], "-D")) {
68  daemon = true;
69  wrong = 0;
70  } else {
71  wrong = 1;
72  }
73  break;
74  case 3: // -h Hostname or -e Experiment possible
75  if (strstr(argv[1], "-h")) {
76  host = argv[2];
77  wrong = 0;
78  } else if (strstr(argv[1], "-e")) {
79  exp = argv[2];
80  wrong = 0;
81  } else {
82  wrong = 1;
83  }
84  break;
85  case 4: // (-h Hostname or -e Experiment possible) and '-D'
86  if (strstr(argv[3], "-D")) {
87  daemon = true;
88  if (strstr(argv[1], "-h")) {
89  host = argv[2];
90  wrong = 0;
91  } else if (strstr(argv[1], "-e")) {
92  exp = argv[2];
93  wrong = 0;
94  } else {
95  wrong = 1;
96  }
97  } else {
98  wrong = 1;
99  }
100  break;
101  case 5: // -h Hostname -e Experiment or -e Experiment -h Hostname
102  if (strstr(argv[1], "-h")) {
103  host = argv[2];
104  if (strstr(argv[3], "-e")) {
105  wrong = 0;
106  exp = argv[4];
107  } else {
108  wrong = 1;
109  }
110  } else if (strstr(argv[1], "-e")) {
111  exp = argv[2];
112  if (strstr(argv[3], "-h")) {
113  wrong = 0;
114  host = argv[4];
115  } else {
116  wrong = 1;
117  }
118  } else {
119  wrong = 1;
120  }
121  break;
122  case 6: // (-h Hostname -e Experiment or -e Experiment -h Hostname) and '-D'
123  if (strstr(argv[5], "-D")) {
124  daemon = true;
125  if (strstr(argv[1], "-h")) {
126  host = argv[2];
127  if (strstr(argv[3], "-e")) {
128  wrong = 0;
129  exp = argv[4];
130  } else {
131  wrong = 1;
132  }
133  } else if (strstr(argv[1], "-e")) {
134  exp = argv[2];
135  if (strstr(argv[3], "-h")) {
136  wrong = 0;
137  host = argv[4];
138  } else {
139  wrong = 1;
140  }
141  } else {
142  wrong = 1;
143  }
144  } else {
145  wrong = 1;
146  }
147  break;
148  default:
149  wrong = 1;
150  break;
151  }
152 
153  return wrong;
154 }
155 
156 //---------------------------------------------------------------------------------------
161 {
162  cout << endl << "usage: lemAutoRun [-h Hostname] [-e Experiment] [-D start as daemon]";
163  cout << endl << endl;
164 }
165 
166 //---------------------------------------------------------------------------------------
173 int main(int argc, char *argv[])
174 {
175  // create main Qt application entry point, the last entry forces it to be a console application
176  QCoreApplication app(argc, argv, false);
177 
178  QString host = "";
179  QString exp = "";
180  bool daemon;
181 
182  PLemAutoRun *autoRun;
183  PAutoRunParser *autoRunParser;
184  PErrorHandler *errorHandler;
185  PAutoRunCmdVector *autoRunCmdVector;
186  POdbTagVector *odbTagVector;
187 
188  // check arguments
189  if (lar_wrong_syntax(argc, argv, host, exp, daemon)) {
190  lar_syntax();
191  return 0;
192  }
193 
194  // should start as a daemon
195  if (daemon){
196  printf("Becoming a daemon...\n");
197  ss_daemon_init(FALSE);
198  }
199 
200  // create auto run object and error handler
201  autoRun = new PLemAutoRun(host, exp);
202  errorHandler = new PErrorHandler();
203 
204  // connect autoRun with errorHandler
205  QObject::connect(autoRun, SIGNAL(ErrorMsg(int,int,int,int,QString)),
206  errorHandler, SLOT(HandleErrors(int,int,int,int,QString)));
207  // connect errorHandler with autoRun
208  QObject::connect(errorHandler, SIGNAL(Terminate()),
209  autoRun, SLOT(DisconnectAndQuit()));
210 
211  // try to read startup XML file
212  autoRun->ReadStartupXML();
213 
214  // try to connect to MIDAS
215  autoRun->ConnectToExp();
216  if (!autoRun->IsConnected()) {
217  delete errorHandler;
218  delete autoRun;
219  return 1;
220  }
221 
222  // try to read the sample cryo parameter XML file
223  autoRun->ReadSampleCryoXML();
224 
225  // create auto run sequnce parser and object
226  autoRunCmdVector = new PAutoRunCmdVector();
227  odbTagVector = new POdbTagVector();
228  autoRunParser = new PAutoRunParser(autoRun->GetUserAutoRunSeqFln(),
229  *autoRun->GetXMLSchemaFln(),
230  *autoRun->GetXMLAutoRunFln(),
231  autoRunCmdVector,
232  odbTagVector);
233 
234  // connect autoRunParser with errorHandler of PLemAutoRun
235  QObject::connect(autoRunParser, SIGNAL(ParseError(int,int,int,int,QString)),
236  autoRun, SLOT(HandleParseError(int,int,int,int,QString)));
237 
238  // connect autoRunParser with errorHandler of PErrorHandler
239  QObject::connect(autoRunParser, SIGNAL(ParseError(int,int,int,int,QString)),
240  errorHandler, SLOT(HandleErrors(int,int,int,int,QString)));
241 
242  if (autoRun->IsIdle()) {
243  // write autoRun into the LiveData record
244  autoRun->FeedLiveDataAutoRun();
245 
246  // convert user auto run sequence to the XML auto run sequence
247  autoRunParser->ConvertToXML();
248 
249  // parse the XML auto run sequence
250  autoRunParser->Parse();
251 
252  // get auto run sequence from parser
253  autoRun->SetAutoRunSeq(autoRunCmdVector);
254 
255  // get ODB tag vector from parser
256  autoRun->SetOdbTagVec(odbTagVector);
257 
258  // start auto run sequence
259  autoRun->ExecAutoRun();
260  } else {
261  cm_msg(MERROR, "lemAutoRun", "lemAutoRun is NOT idle?! Something is wrong, please check.");
262  }
263 
264  // disconnect form MIDAS
265  autoRun->DisconnectFromExp();
266 
267  // clean up objects
268  if (autoRunParser)
269  delete autoRunParser;
270  if (autoRunCmdVector)
271  delete autoRunCmdVector;
272  if (odbTagVector)
273  delete odbTagVector;
274  if (errorHandler)
275  delete errorHandler;
276  if (autoRun)
277  delete autoRun;
278 
279  return 0;
280 }
281 
282 //---------------------------------------------------------------------------------------
283 // end
284 //---------------------------------------------------------------------------------------
QString * GetXMLAutoRunFln()
Definition: PLemAutoRun.h:253
QString GetUserAutoRunSeqFln()
PLemAutoRun::GetUserAutoRunSeqFln.
void lar_syntax()
Definition: lemAutoRun.cpp:160
void ReadSampleCryoXML()
int main(int argc, char *argv[])
Definition: lemAutoRun.cpp:173
void FeedLiveDataAutoRun()
PLemAutoRun::FeedLiveDataAutoRun.
void SetAutoRunSeq(PAutoRunCmdVector *arcv)
Definition: PLemAutoRun.h:257
QVector< AutoRunCmd > PAutoRunCmdVector
Definition: lemAutoRun.h:233
void ConnectToExp()
INT ss_daemon_init(BOOL keep_stdout)
int lar_wrong_syntax(int argc, char *argv[], QString &host, QString &exp, bool &daemon)
Definition: lemAutoRun.cpp:52
void SetOdbTagVec(POdbTagVector *otv)
Definition: PLemAutoRun.h:256
QVector< OdbTag > POdbTagVector
Definition: lemAutoRun.h:249
QString * GetXMLSchemaFln()
Definition: PLemAutoRun.h:252
void ReadStartupXML()
bool IsConnected()
Definition: PLemAutoRun.h:244
void ExecAutoRun()
void DisconnectFromExp()