]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/src/PipeIO/triggerDDL.cxx
Also dropping references to AliMUONTriggerCircuit which are depricated. This is a...
[u/mrichter/AliRoot.git] / HLT / MUON / src / PipeIO / triggerDDL.cxx
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Artur Szostak
4 // Email:  artur@alice.phy.uct.ac.za | artursz@iafrica.com
5 //
6 // Author: Gareth de Vaux
7 // Email:  devaux@lhc.phy.uct.ac.za | dhlt@lordcow.org
8 //
9 ////////////////////////////////////////////////////////////////////////////////
10
11 #include <iostream>
12 #include <stdio.h>
13 #include <signal.h>
14 #include <asm/errno.h>
15
16 using std::endl;
17 using std::cout;
18 using std::cerr;
19
20 #include "System/SystemError.hpp"
21 #include "System/Socket.hpp"
22 #include "DDL/L2SignalReceiver.hpp"
23 #include "DDL/FileList.hpp"
24 #include "System/SignalHandler.hpp"
25 #include "Debug/print.hpp"
26 #include "Error.hpp"
27 #include "Utils.hpp"
28
29 using namespace dHLT;
30 using DDL::FileList;
31
32
33 class L2SignalReceiver : public DDL::L2SignalReceiver
34 {
35 public:
36
37         L2SignalReceiver(FileList& list, const UShort port = 4900)
38                 : DDL::L2SignalReceiver(port)
39         {
40                 filelist = &list;
41         };
42
43         virtual void GotL2(const EventID eventid)
44         {
45                 cout << "Got signal: " << eventid << endl;
46         };
47         
48 private:
49
50         FileList* filelist;
51 };
52
53
54 L2SignalReceiver* signalreceiver = NULL;
55
56 // If we get a SIGINT ( keyboard Ctrl+C ) signal or SIGHUP then terminate the program.
57 HandleSignals(SIGINT,
58         if (signalreceiver != NULL) signalreceiver->Terminate();
59 );
60 HandleSignals(SIGHUP,
61         if (signalreceiver != NULL) signalreceiver->Terminate();
62 );
63
64
65 void PrintUsage()
66 {
67         cerr << "Usage: > triggerDDL [-r] <filelist> [<port>]" << endl;
68         cerr << "        -r : Optional flag indicating to recursively go through sub directories" << endl;
69         cerr << "             and add files to the file list for publishing." << endl;
70         cerr << "<filelist> : Required file name containing a file list (one file/directory per line)" << endl;
71         cerr << "             of files to publish." << endl;
72         cerr << "    <port> : Optional port number to listen to for L2 signals." << endl;
73 };
74
75
76 bool CheckIfEmpty(const FileList& filelist)
77 {
78         if (filelist.Empty())
79         {
80                 cerr << "Error: No files added to file list. Is the path empty?" << endl;
81                 return false;
82         }
83         else
84                 return true;
85 };
86
87
88 /* Parse command line and fill the port number and filelist.
89    If the port number or filelist is untouched then the default
90    values are used.
91    Returns true if the command line is parsed properly.
92  */
93 bool ParseCommandLine(const int argc, const char** argv, UShort& port, FileList& filelist)
94 {
95         for (int i = 1; i < argc; i++)
96         {
97                 if (    strcmp(argv[i], "-h") == 0 or strcmp(argv[i], "-help") == 0
98                         or strcmp(argv[i], "--help") == 0)
99                 {
100                         PrintUsage();
101                         return false;
102                 };
103         };
104
105         if (argc == 2)
106         {
107                 filelist = argv[1];
108                 return CheckIfEmpty(filelist);
109         }
110
111         if (argc == 3)
112         {
113                 if (strcmp(argv[1],"-r") == 0)
114                 {
115                         filelist.Add(argv[2], true);
116                         return CheckIfEmpty(filelist);
117                 }
118                 else
119                 {
120                         int x = atoi(argv[2]);
121                         if ( not (0 < x and x < 65536) )
122                         {
123                                 cerr << "Error: Port is out of range, valid range is [1..65535]." << endl;
124                                 return false;
125                         }
126                         else
127                         {
128                                 port = x;
129                                 filelist.Add(argv[1], false);
130                                 return CheckIfEmpty(filelist);
131                         }
132                 }
133         }
134
135         if (argc == 4)
136         {
137                 if (strcmp(argv[1],"-r") != 0)
138                 {
139                         cerr << "Error: Unknown flag '" << argv[1] << "', expected '-r'." << endl;
140                         return false;
141                 };
142                 
143                 int x = atoi(argv[3]);
144                 if ( not (0 < x and x < 65536) )
145                 {
146                         cerr << "Error: Port is out of range, valid range is [1..65535]." << endl;
147                         return false;
148                 }
149                 else
150                 {
151                         port = x;
152                         filelist.Add(argv[2], true);
153                         return CheckIfEmpty(filelist);
154                 }
155         }
156
157         PrintUsage();
158         return false;
159 };
160
161
162 int main(const int argc, const char** argv)
163 {
164         int returncode = 0;
165         try
166         {
167                 UShort port = 4900;
168                 FileList filelist;
169
170                 bool valid_arguments = ParseCommandLine(argc, argv, port, filelist);
171                 if (valid_arguments)
172                 {
173                         L2SignalReceiver receiver(filelist, port);
174                         signalreceiver = &receiver;
175                         receiver.Run();
176                 }
177                 else
178                         returncode = 1;
179         }
180         catch (const System::Error& e)
181         {
182                 cerr << "Error [" << e.ErrorCode();
183                 if (e.ErrorCode() == EACCES)
184                         cerr << "]: Could not allocate socket. ";
185                 else
186                         cerr << "]: ";
187                 cerr << e << endl;
188                 returncode = 2;
189         }
190         catch (const Error& e)
191         {
192                 cerr << "Error [" << e.ErrorCode() << "]: " << e << endl;
193                 returncode = 2;
194         }
195         catch (...)
196         {
197                 cerr << "Error: Unknown exception!" << endl;
198                 returncode = 2;
199         };
200
201         return returncode;
202 };
203