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 
217  // try to read the sample cryo parameter XML file
218  autoRun->ReadSampleCryoXML();
219 
220  // create auto run sequnce parser and object
221  autoRunCmdVector = new PAutoRunCmdVector();
222  odbTagVector = new POdbTagVector();
223  autoRunParser = new PAutoRunParser(autoRun->GetUserAutoRunSeqFln(),
224  *autoRun->GetXMLSchemaFln(),
225  *autoRun->GetXMLAutoRunFln(),
226  autoRunCmdVector,
227  odbTagVector);
228 
229  // connect autoRunParser with errorHandler of PLemAutoRun
230  QObject::connect(autoRunParser, SIGNAL(ParseError(int,int,int,int,QString)),
231  autoRun, SLOT(HandleParseError(int,int,int,int,QString)));
232 
233  // connect autoRunParser with errorHandler of PErrorHandler
234  QObject::connect(autoRunParser, SIGNAL(ParseError(int,int,int,int,QString)),
235  errorHandler, SLOT(HandleErrors(int,int,int,int,QString)));
236 
237  if (autoRun->IsIdle()) {
238  // write autoRun into the LiveData record
239  autoRun->FeedLiveDataAutoRun();
240 
241  // convert user auto run sequence to the XML auto run sequence
242  autoRunParser->ConvertToXML();
243 
244  // parse the XML auto run sequence
245  autoRunParser->Parse();
246 
247  // get auto run sequence from parser
248  autoRun->SetAutoRunSeq(autoRunCmdVector);
249 
250  // get ODB tag vector from parser
251  autoRun->SetOdbTagVec(odbTagVector);
252 
253  // start auto run sequence
254  autoRun->ExecAutoRun();
255  } else {
256  cm_msg(MERROR, "lemAutoRun", "lemAutoRun is NOT idle?! Something is wrong, please check.");
257  }
258 
259  // disconnect form MIDAS
260  autoRun->DisconnectFromExp();
261 
262  // clean up objects
263  if (autoRunParser)
264  delete autoRunParser;
265  if (autoRunCmdVector)
266  delete autoRunCmdVector;
267  if (odbTagVector)
268  delete odbTagVector;
269  if (errorHandler)
270  delete errorHandler;
271  if (autoRun)
272  delete autoRun;
273 
274  return 0;
275 }
276 
277 //---------------------------------------------------------------------------------------
278 // end
279 //---------------------------------------------------------------------------------------
QString * GetXMLAutoRunFln()
Definition: PLemAutoRun.h:239
QString GetUserAutoRunSeqFln()
PLemAutoRun::GetUserAutoRunSeqFln.
void lar_syntax()
Definition: lemAutoRun.cpp:160
void ReadSampleCryoXML()
void FeedLiveDataAutoRun()
PLemAutoRun::FeedLiveDataAutoRun.
void SetAutoRunSeq(PAutoRunCmdVector *arcv)
Definition: PLemAutoRun.h:243
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:242
int main(int argc, char *argv[])
QVector< OdbTag > POdbTagVector
Definition: lemAutoRun.h:249
QString * GetXMLSchemaFln()
Definition: PLemAutoRun.h:238
void ReadStartupXML()
void ExecAutoRun()
void DisconnectFromExp()