1111#include " poll2_socket.h"
1212#include " CTerminal.h"
1313
14- #define SCAN_VERSION " 1.2.00"
15- #define SCAN_DATE " Oct. 1st, 2015"
14+ #define SCAN_VERSION " 1.2.01"
15+ #define SCAN_DATE " Oct. 2nd, 2015"
16+
17+ // Maximum size of the shared memory buffer
18+ static const unsigned int maxShmSizeL = 4052 ; // in pixie words (4050 + 2 header words)
19+ static const unsigned int maxShmSize = maxShmSizeL * 4 ; // in bytes
1620
1721std::string prefix, extension;
1822
@@ -27,7 +31,6 @@ bool force_overwrite;
2731bool shm_mode;
2832
2933bool kill_all = false ;
30- bool scan_running = false ;
3134bool run_ctrl_exit = false ;
3235
3336Server poll_server;
@@ -49,6 +52,22 @@ Terminal *term_;
4952
5053std::string sys_message_head = std::string(PROG_NAME) + " : " ;
5154
55+ unsigned int split_str (std::string str_, std::vector<std::string> &args, char delimiter_=' ' ){
56+ args.clear ();
57+ std::string temp = " " ;
58+ unsigned int count = 0 ;
59+ for (unsigned int i = 0 ; i < str_.size (); i++){
60+ if (str_[i] == delimiter_ || i == str_.size ()-1 ){
61+ if (i == str_.size ()-1 ){ temp += str_[i]; }
62+ args.push_back (temp);
63+ temp = " " ;
64+ count++;
65+ }
66+ else { temp += str_[i]; }
67+ }
68+ return count;
69+ }
70+
5271void start_run_control (Unpacker *core_){
5372 if (debug_mode){
5473 pldHead.SetDebugMode ();
@@ -62,46 +81,56 @@ void start_run_control(Unpacker *core_){
6281 // Now we're ready to read the first data buffer
6382 if (shm_mode){
6483 std::cout << std::endl;
65- unsigned int data[250000 ];
66- unsigned int shm_data[10002 ]; // Array to store the temporary shm data (~40 kB)
84+ unsigned int data[250000 ]; // Array for storing spill data. Larger than any RevF spill should be.
85+ unsigned int shm_data[maxShmSizeL ]; // Array to store the temporary shm data (~16 kB)
6786 int dummy;
6887 int previous_chunk;
6988 int current_chunk;
7089 int total_chunks;
71- int nBytes ;
72- unsigned int nTotalBytes ;
90+ int nWords ;
91+ unsigned int nTotalWords ;
7392
93+ bool full_spill = false ;
94+
7495 while (true ){
96+ if (kill_all == true ){
97+ break ;
98+ }
99+
100+ int select_dummy;
75101 previous_chunk = 0 ;
76102 current_chunk = 0 ;
77103 total_chunks = -1 ;
78- nTotalBytes = 0 ;
104+ nTotalWords = 0 ;
105+ full_spill = true ;
106+
107+ std::stringstream status;
108+ if (!poll_server.Select (dummy)){
109+ status << " \033 [0;33m" << " [IDLE]" << " \033 [0m" << " Waiting for a spill..." ;
110+ term_->SetStatus (status.str ());
111+ continue ;
112+ }
79113
114+ if (!poll_server.Select (select_dummy)){ continue ; } // Server timeout
115+
116+ // Get the spill
80117 while (current_chunk != total_chunks){
81- if (kill_all == true ){
82- run_ctrl_exit = true ;
83- return ;
84- }
85-
86- std::stringstream status;
87- if (!poll_server.Select (dummy)){
88- status << " \033 [0;33m" << " [IDLE]" << " \033 [0m" << " Waiting for a spill..." ;
89- term_->SetStatus (status.str ());
90- continue ;
91- }
118+ if (!poll_server.Select (select_dummy)){ // Server timeout
119+ std::cout << sys_message_head << " Network timeout before recv full spill!\n " ;
120+ full_spill = false ;
121+ break ;
122+ }
92123
93- nBytes = poll_server.RecvMessage ((char *)shm_data, 40008 ) ; // Read from the socket
124+ nWords = poll_server.RecvMessage ((char *)shm_data, maxShmSize) / 4 ; // Read from the socket
94125 if (strcmp ((char *)shm_data, " $CLOSE_FILE" ) == 0 || strcmp ((char *)shm_data, " $OPEN_FILE" ) == 0 || strcmp ((char *)shm_data, " $KILL_SOCKET" ) == 0 ){ continue ; } // Poll2 network flags
95126 // Did not read enough bytes
96- else if (nBytes < 8 ){
127+ else if (nWords < 2 ){
97128 continue ;
98129 }
99- status << " \033 [0;32m" << " [RECV] " << " \033 [0m" << nBytes;
100- term_->SetStatus (status.str ());
101130
102- if (debug_mode){ std::cout << " debug: Received " << nBytes << " bytes from the network\n " ; }
131+ if (debug_mode){ std::cout << " debug: Received " << nWords << " words from the network\n " ; }
103132 memcpy ((char *)¤t_chunk, &shm_data[0 ], 4 );
104- memcpy ((char *)&total_chunks, &shm_data[4 ], 4 );
133+ memcpy ((char *)&total_chunks, &shm_data[1 ], 4 );
105134
106135 if (previous_chunk == -1 && current_chunk != 1 ){ // Started reading in the middle of a spill, ignore the rest of it
107136 if (debug_mode){ std::cout << " debug: Skipping chunk " << current_chunk << " of " << total_chunks << std::endl; }
@@ -115,24 +144,29 @@ void start_run_control(Unpacker *core_){
115144 previous_chunk = current_chunk;
116145
117146 // Copy the shm spill chunk into the data array
118- if (nTotalBytes + 2 + nBytes <= 1000000 ){ // This spill chunk will fit into the data buffer
119- memcpy (&data[nTotalBytes ], &shm_data[8 ], nBytes - 8 );
120- nTotalBytes += (nBytes - 8 );
147+ if (nTotalWords + 2 + nWords <= 250000 ){ // This spill chunk will fit into the data buffer
148+ memcpy (&data[nTotalWords ], &shm_data[2 ], (nWords - 2 )* 4 );
149+ nTotalWords += (nWords - 2 );
121150 }
122151 else {
123- if (debug_mode){ std::cout << " debug: Abnormally full spill buffer with " << nTotalBytes + 2 + nBytes << " bytes !\n " ; }
152+ if (debug_mode){ std::cout << " debug: Abnormally full spill buffer with " << nTotalWords + 2 + nWords << " words !\n " ; }
124153 break ;
125154 }
126155 }
156+
157+ status << " \033 [0;32m" << " [RECV] " << " \033 [0m" << nTotalWords << " words" ;
158+ term_->SetStatus (status.str ());
127159
128- if (debug_mode){ std::cout << " debug: Retrieved spill of " << nTotalBytes << " bytes (" << nTotalBytes/ 4 << " words )\n " ; }
129- if (!dry_run_mode){
160+ if (debug_mode){ std::cout << " debug: Retrieved spill of " << nTotalWords << " words (" << nTotalWords* 4 << " bytes )\n " ; }
161+ if (!dry_run_mode && full_spill ){
130162 int word1 = 2 , word2 = 9999 ;
131- memcpy (&data[nTotalBytes ], (char *)&word1, 4 );
132- memcpy (&data[nTotalBytes+ 4 ], (char *)&word2, 4 );
133- core_->ReadSpill (data, nTotalBytes/ 4 + 2 , is_verbose);
163+ memcpy (&data[nTotalWords ], (char *)&word1, 4 );
164+ memcpy (&data[nTotalWords+ 1 ], (char *)&word2, 4 );
165+ core_->ReadSpill (data, nTotalWords + 2 , is_verbose);
134166 }
135- num_spills_recvd++;
167+
168+ if (!full_spill){ std::cout << sys_message_head << " Not processing spill fragment!\n " ; }
169+ else { num_spills_recvd++; }
136170 }
137171 }
138172 else if (file_format == 0 ){
@@ -208,27 +242,86 @@ void start_run_control(Unpacker *core_){
208242 run_ctrl_exit = true ;
209243}
210244
211- void start_cmd_control (){
245+ void start_cmd_control (Unpacker *pack_){
246+ if (!pack_){ return ; }
247+
212248 std::string cmd = " " , arg;
213249
214- bool cmd_ready = true ;
215-
216250 while (true ){
217251 cmd = term_->GetCommand ();
218- if (cmd == " CTRL_D" ){ cmd = " quit" ; }
219- else if (cmd == " CTRL_C" ){ continue ; }
252+ if (cmd == " _SIGSEGV_" ){
253+ std::cout << " \033 [0;31m[SEGMENTATION FAULT]\033 [0m\n " ;
254+ exit (EXIT_FAILURE);
255+ }
256+ else if (cmd == " CTRL_D" ){
257+ std::cout << sys_message_head << " Received EOF (ctrl-d) signal. Exiting...\n " ;
258+ cmd = " quit" ;
259+ }
260+ else if (cmd == " CTRL_C" ){
261+ std::cout << sys_message_head << " Warning! Received SIGINT (ctrl-c) signal.\n " ;
262+ continue ;
263+ }
264+ else if (cmd == " CTRL_Z" ){
265+ std::cout << sys_message_head << " Warning! Received SIGTSTP (ctrl-z) signal.\n " ;
266+ continue ;
267+ }
220268 term_->flush ();
221269
222- if (cmd_ready){
223- if (cmd == " quit" || cmd == " exit" ){
224- if (scan_running){ std::cout << sys_message_head << " Warning! Cannot quit while scan is running\n " ; }
225- else {
226- kill_all = true ;
227- while (!run_ctrl_exit){ sleep (1 ); }
228- break ;
229- }
270+ if (cmd == " " ){ continue ; }
271+
272+ size_t index = cmd.find (" " );
273+ if (index != std::string::npos){
274+ arg = cmd.substr (index+1 , cmd.size ()-index); // Get the argument from the full input string
275+ cmd = cmd.substr (0 , index); // Get the command from the full input string
276+ }
277+ else { arg = " " ; }
278+
279+ std::vector<std::string> arguments;
280+ split_str (arg, arguments);
281+
282+ if (cmd == " quit" || cmd == " exit" ){
283+ kill_all = true ;
284+ while (!run_ctrl_exit){ sleep (1 ); }
285+ break ;
286+ }
287+ else if (cmd == " version" || cmd == " v" ){
288+ std::cout << " " << PROG_NAME << " v" << SCAN_VERSION << " (" << SCAN_DATE << " )\n " ;
289+ std::cout << " Poll2 Socket v" << POLL2_SOCKET_VERSION << " (" << POLL2_SOCKET_DATE << " )\n " ;
290+ std::cout << " HRIBF Buffers v" << HRIBF_BUFFERS_VERSION << " (" << HRIBF_BUFFERS_DATE << " )\n " ;
291+ std::cout << " CTerminal v" << CTERMINAL_VERSION << " (" << CTERMINAL_DATE << " )\n " ;
292+ }
293+ else if (cmd == " help" || cmd == " h" ){
294+ std::cout << " Help:\n " ;
295+ std::cout << " debug - Toggle debug mode flag (default=false)\n " ;
296+ std::cout << " quiet - Toggle quiet mode flag (default=false)\n " ;
297+ std::cout << " quit - Close the program\n " ;
298+ std::cout << " help (h) - Display this dialogue\n " ;
299+ std::cout << " version (v) - Display Poll2 version information\n " ;
300+ pack_->CmdHelp (" " );
301+ }
302+ else if (cmd == " debug" ){ // Toggle debug mode
303+ if (debug_mode){
304+ std::cout << sys_message_head << " Toggling debug mode OFF\n " ;
305+ debug_mode = false ;
230306 }
231- else { std::cout << sys_message_head << " Unknown command '" << cmd << " '\n " ; }
307+ else {
308+ std::cout << sys_message_head << " Toggling debug mode ON\n " ;
309+ debug_mode = true ;
310+ }
311+ pack_->SetDebugMode (debug_mode);
312+ }
313+ else if (cmd == " quiet" ){ // Toggle quiet mode
314+ if (!is_verbose){
315+ std::cout << sys_message_head << " Toggling quiet mode OFF\n " ;
316+ is_verbose = true ;
317+ }
318+ else {
319+ std::cout << sys_message_head << " Toggling quiet mode ON\n " ;
320+ is_verbose = false ;
321+ }
322+ }
323+ else if (!pack_->CommandControl (cmd, arguments)){ // Unrecognized command. Send it to Unpacker.
324+ std::cout << sys_message_head << " Unknown command '" << cmd << " '\n " ;
232325 }
233326 }
234327}
@@ -266,7 +359,7 @@ void help(char *name_, Unpacker *core_){
266359 std::cout << " --quiet - Toggle off verbosity flag\n " ;
267360 std::cout << " --dry-run - Extract spills from file, but do no processing\n " ;
268361 std::cout << " --fast-fwd [word] - Skip ahead to a specified word in the file (start of file at zero)\n " ;
269- core_->Help (" " );
362+ core_->ArgHelp (" " );
270363}
271364
272365int main (int argc, char *argv[]){
@@ -277,10 +370,10 @@ int main(int argc, char *argv[]){
277370 return 0 ;
278371 }
279372 else if (argc >= 2 && (strcmp (argv[1 ], " --version" ) == 0 || strcmp (argv[1 ], " -v" ) == 0 )){ // Display version information
280- std::cout << " " << PROG_NAME << " ------- v" << SCAN_VERSION << " (" << SCAN_DATE << " )\n " ;
281- std::cout << " |hribf_buffers- v" << HRIBF_BUFFERS_VERSION << " (" << HRIBF_BUFFERS_DATE << " )\n " ;
282- std::cout << " |CTerminal----- v" << CTERMINAL_VERSION << " (" << CTERMINAL_DATE << " )\n " ;
283- std::cout << " |poll2_socket-- v" << POLL2_SOCKET_VERSION << " (" << POLL2_SOCKET_DATE << " )\n " ;
373+ std::cout << " " << PROG_NAME << " v" << SCAN_VERSION << " (" << SCAN_DATE << " )\n " ;
374+ std::cout << " Poll2 Socket v" << POLL2_SOCKET_VERSION << " (" << POLL2_SOCKET_DATE << " )\n " ;
375+ std::cout << " HRIBF Buffers v" << HRIBF_BUFFERS_VERSION << " (" << HRIBF_BUFFERS_DATE << " )\n " ;
376+ std::cout << " CTerminal v" << CTERMINAL_VERSION << " (" << CTERMINAL_DATE << " )\n " ;
284377 return 0 ;
285378 }
286379
@@ -302,6 +395,8 @@ int main(int argc, char *argv[]){
302395
303396 Unpacker *core = GetCore (); // Get a pointer to the main Unpacker object.
304397
398+ core->SetMsgPrefix (sys_message_head);
399+
305400 // Loop through the arg list and extract ScanMain arguments.
306401 std::string current_arg;
307402 while (!scan_args.empty ()){
@@ -480,7 +575,7 @@ int main(int argc, char *argv[]){
480575 // Start the command control thread. This needs to be the last thing we do to
481576 // initialize, so the user cannot enter commands before setup is complete
482577 std::cout << " Starting command thread\n\n " ;
483- std::thread comctrl (start_cmd_control);
578+ std::thread comctrl (start_cmd_control, core );
484579
485580 // Synchronize the threads and wait for completion
486581 comctrl.join ();
0 commit comments