source: trunk/MagicSoft/Cosy/cosy.cc@ 8864

Last change on this file since 8864 was 8864, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 7.3 KB
Line 
1#include <iostream>
2#include <iomanip>
3#include <fstream>
4
5#include <TSystem.h>
6#include <TApplication.h>
7#include <TObjectTable.h>
8
9#include "ethernet.h"
10#include "vmodican.h"
11
12#include "MEnv.h"
13#include "MArgs.h"
14#include "MCosy.h"
15#include "MTime.h"
16
17#include "MLogManip.h"
18
19#include "MStarguider.h"
20
21using namespace std;
22
23#define EXPERT
24
25static void StartUpMessage()
26{
27 gLog << all << endl;
28
29 // 1 2 3 4 5 6
30 // 123456789012345678901234567890123456789012345678901234567890
31 gLog << "========================================================" << endl;
32 gLog << " COSY " << endl;
33 gLog << " Magic Drive Control System Software " << endl;
34 gLog << " Compiled with ROOT v" << ROOT_RELEASE << " on <" << __DATE__ << ">" << endl;
35 gLog << "========================================================" << endl;
36 gLog << endl;
37}
38
39static void Usage()
40{
41 // 1 2 3 4 5 6 7 8
42 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
43 gLog << all << endl;
44 gLog << "Sorry the usage is:" << endl;
45 gLog << " cosy [options]" << endl << endl;
46 gLog << " Arguments:" << endl;
47 gLog << " n/a" << endl;
48 gLog << endl;
49 gLog << " Root Options:" << endl;
50 gLog << " -b Batch mode (no graphical output to screen)" << endl<<endl;
51 gLog << " Options:" << endl;
52 gLog.Usage();
53 gLog << " --debug-env=0 Disable debugging setting resources <default>" << endl;
54 gLog << " --debug-env[=1] Display untouched resources after program execution" << endl;
55 gLog << " --debug-env=2 Display untouched resources after eventloop setup" << endl;
56 gLog << " --debug-env=3 Debug setting resources from resource file and command line" << endl;
57 gLog << " --debug-mem Debug memory usage" << endl << endl;
58 gLog << " --debug-threads Debug threads" << endl << endl;
59 gLog << " --rc=Name:option Set or overwrite a resource of the resource file." << endl << endl;
60 gLog << " --version, -V Show startup message with version number" << endl;
61 gLog << " -?, -h, --help This help" << endl << endl;
62}
63
64/* ---------------------------------------------------------------------- */
65int main(int argc, char **argv)
66{
67 if (!MARS::CheckRootVer())
68 return 0xff;
69
70 MLog::RedirectErrorHandler(MLog::kColor);
71
72 //
73 // Evaluate arguments
74 //
75 MArgs arg(argc, argv, kTRUE);
76 gLog.Setup(arg);
77
78 StartUpMessage();
79
80 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
81 return 0;
82
83 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
84 {
85 Usage();
86 return 2;
87 }
88
89 const Int_t channel = arg.GetIntAndRemove("--channel=", 0);
90 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
91 const Bool_t kDebugThreads = arg.HasOnlyAndRemove("--debug-threads");
92 const TString sps = arg.GetStringAndRemove("--sps=", "sps");
93 const TString ceco = arg.GetStringAndRemove("--cc=", "ceco"); // ceco
94 const TString pointing = arg.GetStringAndRemove("--pointing-model=", "bending2.txt"); // ceco
95 const TString kConfig = arg.GetStringAndRemove("--config=", ".cosyrc"); // ceco
96 Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
97 kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
98
99 //
100 // check for the right usage of the program (number of arguments)
101 //
102 if (arg.GetNumArguments()>0)
103 {
104 gLog << warn << "WARNING - Wrong number of arguments..." << endl;
105 Usage();
106 return 2;
107 }
108
109 //
110 // Now we access/read the resource file. This will remove all
111 // --rc= from the list of arguments.
112 //
113 MEnv env(kConfig);
114 if (!env.IsValid())
115 {
116 gLog << err << "ERROR - Reading resource file " << kConfig << "." << endl;
117 return 0xfe;
118 }
119
120 // And move the resource options from the command line to the MEnv
121 if (!env.TakeEnv(arg, kDebugEnv>2))
122 return 0xfd;
123
124 //
125 // check for the right usage of the program (number of options)
126 //
127 if (arg.GetNumOptions()>0)
128 {
129 gLog << warn << "WARNING - Unknown commandline options..." << endl;
130 arg.Print("options");
131 gLog << endl;
132 return 2;
133 }
134
135 if (!gLog.IsOutputDeviceEnabled(MLog::eFile))
136 {
137 const TString name = MCosy::GetFileName("log", "cosy", "log");
138 gLog << inf << "Open automatic logfile: " << name << endl;
139 gLog.SetOutputFile(name);
140 gLog.EnableOutputDevice(MLog::eFile);
141 }
142
143 gLog << all << "Starting Cosy at " << MTime(-1) << " in thread " << TThread::SelfId() << "..." << endl;
144
145 //
146 // start the main window
147 //
148 gLog << all << "- Initialising Root environment." << endl;
149
150 //
151 // Initialize root
152 //
153 //MArray::Class()->IgnoreTObjectStreamer();
154 //MParContainer::Class()->IgnoreTObjectStreamer();
155
156 TApplication app("cosy", &argc, argv);
157 if (gROOT->IsBatch())
158 {
159 gLog << err << "ERROR - Cannot run in Batch mode!" << endl;
160 return 0;
161 }
162 if (!gClient)
163 {
164 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
165 return 1;
166 }
167
168 if (kDebugMem)
169 TObject::SetObjectStat(kTRUE);
170
171 //
172 // Create the Network. Device: /dev/dpm_00, Rate: 500kbps
173 //
174 gLog << all <<"- Constructing MCosy." << endl;
175/*
176 //
177 // check for the right usage of the program
178 //
179 int mode = 0;
180 if (argc==2 && (argv[1][0]=='-' || argv[1][1]=='m'))
181 switch (argv[1][2])
182 {
183 case '0': // standard
184 mode = 0;
185 break;
186 case '1': // SE mode
187 mode = 1;
188 break;
189 case '2': // GUI demo mode
190 mode = 2;
191 break;
192 }
193*/
194 MCosy *cosy = new MCosy(env, ceco, pointing);
195
196 Interface *interface = new Ethernet(sps, 5357, 5358, cosy);
197 // Interface *interface = new VmodIcan(cosy, "/dev/dpm_00", 125);
198
199 gLog << all << "- Starting MCosy." << endl;
200
201 cosy->Start(env);
202
203 // FIXME: Is this the right position?
204 if (kDebugEnv>0)
205 env.PrintUntouched();
206
207
208 MStarguider *client=0;
209 if (channel>=0)
210 {
211 gLog << all << "- Starting Camera." << endl;
212 client=new MStarguider(MObservatory::kMagic1, channel);
213 cosy->SetStarguider(client);
214 client->SetCosy(cosy);
215 }
216
217 gLog << all << "- Starting mainloop." << endl;
218
219 app.Run(kTRUE);
220
221 if (kDebugThreads)
222 TThread::Ps();
223
224 if (channel>=0)
225 {
226 client->SetCosy(NULL);
227 cosy->SetStarguider(NULL);
228 gLog << all << "- Stopping starg." << endl;
229 delete client;
230 }
231
232 gLog << all << "- Stopping cosy." << endl;
233 cosy->Stop();
234
235 gLog << all << MTime(-1) << ": MCosy stopped." << endl;
236
237 delete interface;
238
239 gLog << all << "Deleting cosy at " << MTime(-1) << endl;
240
241 delete cosy;
242
243 if (kDebugThreads)
244 TThread::Ps();
245
246 //delete app;
247
248 if (TObject::GetObjectStat())
249 {
250 TObject::SetObjectStat(kFALSE);
251 gObjectTable->Print();
252 }
253
254 gLog << all << "The End." << endl;
255}
Note: See TracBrowser for help on using the repository browser.