]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/MUON/src/PubSub/TriggerDDLFilePublisher.cxx
This commit was generated by cvs2svn to compensate for changes in r11742,
[u/mrichter/AliRoot.git] / HLT / MUON / src / PubSub / TriggerDDLFilePublisher.cxx
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Author: Artur Szostak
4 // Email:  artur@alice.phy.uct.ac.za | artursz@iafrica.com
5 //
6 ////////////////////////////////////////////////////////////////////////////////
7
8 #include <iostream>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <signal.h>
12 #include <asm/errno.h>
13
14 using std::endl;
15 using std::cout;
16 using std::cerr;
17
18 #include "System/SystemError.hpp"
19 #include "System/Socket.hpp"
20 #include "BCMP/Receiver.hpp"
21 #include "DDL/FileList.hpp"
22 #include "System/SignalHandler.hpp"
23 #include "Error.hpp"
24 #include "Utils.hpp"
25
26 using namespace dHLT;
27 using DDL::FileList;
28
29
30 bool terminate = false;
31
32 HandleSignals(SIGINT,
33         terminate = true;
34 );
35
36
37 class Handler : public BCMP::EventHandler
38 {
39 public:
40         
41         virtual void OnConnect(const System::Address& address)
42         {
43                 cout << "Connected to: " << address << endl;
44         };
45         
46         virtual void OnDisconnect(const System::Address& address)
47         {
48                 cout << "Disonnected to: " << address << endl;
49         };
50         
51         virtual void OnConnectionLost(const System::Address& address)
52         {
53                 cout << "Connection to: " << address << " lost." << endl;
54         };
55         
56         virtual void OnMessage(
57                         const char* message, const UInt length,
58                         const System::Address& from
59                 )
60         {
61                 cout << "Got message of " << length << " bytes from: " << from << endl;
62                 char* str = new char[length+1];
63                 memcpy(str, message, length);
64                 str[length] = '\0';
65                 cout << "    : " << str << endl;
66                 delete [] str;
67         };
68 };
69
70 Handler handler;
71
72
73
74 void PrintUsage()
75 {
76         cerr << "Usage: > TriggerDDLFilePublisher [-r] <filelist> [<port>]" << endl;
77         cerr << "        -r : Optional flag indicating to recursively go through sub directories" << endl;
78         cerr << "             and add files to the file list for publishing." << endl;
79         cerr << "<filelist> : Required file name containing a file list (one file/directory per line)" << endl;
80         cerr << "             of files to publish." << endl;
81         cerr << "    <port> : Optional port number to listen to for L2 signals." << endl;
82 };
83
84
85 /* Parse command line and fill the port number and filelist.
86    If the port number or filelist is untouched then the default
87    values are used.
88    Returns true if the command line is parsed properly.
89  */
90 bool ParseCommandLine(const int argc, const char** argv, UShort& port, FileList& filelist)
91 {
92         // TODO: parse command line.
93         // command line format:
94         // > TriggerDDLFilePublisher [-r] <filelist> [<port>]
95         //         -r : Optional flag indicating to recursively go through sub directories
96         //              and add files to the file list for publishing.
97         // <filelist> : The file name containing a file list (one file/directory per line)
98         //              of files to publish.
99         //     <port> : The port number to listen to for L2 signals.
100         
101         PrintUsage();
102         return false;
103 };
104
105
106 int main(const int argc, const char** argv)
107 {
108         try
109         {
110                 UShort port = 4900;
111                 FileList filelist;
112
113                 bool valid_arguments = ParseCommandLine(argc, argv, port, filelist);
114                 if (not valid_arguments) return 1;
115                 
116                 BCMP::Receiver receiver(&handler, port);
117                 cout << "Listening on: " << receiver.LocalAddress() << endl;
118                 
119                 // Enter an event handling loop:
120                 while (not terminate)
121                 {
122                         try
123                         {
124                                 // Try handle events or timeout every 50 milliseconds
125                                 // to check if we were signaled to terminate or not. 
126                                 receiver.HandleEvents(10);
127                         }
128                         catch (System::Error& e)
129                         {
130                                 if (e.ErrorCode() != EINTR) throw;
131                         };
132                 };
133         }
134         catch (const Error& e)
135         {
136                 cerr << "Error [" << e.ErrorCode() << "]: " << e << endl;
137         };
138         
139         cout << "done." << endl;
140
141         return 0;
142 };