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

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