Debian Codegraph @

src/arq/flmsg-arq.cxx

raw
1 // ----------------------------------------------------------------------------
2 //
3 // flmsg is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // flmsg is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
15 // ----------------------------------------------------------------------------
16
17 #include <config.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include <cstring>
24 #include <ctime>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <errno.h>
31
32 #include <FL/Fl.H>
33 #include <FL/Fl_Widget.H>
34 #include <FL/Enumerations.H>
35 #include <FL/Fl_Window.H>
36 #include <FL/Fl_Button.H>
37 #include <FL/Fl_Group.H>
38 #include <FL/Fl_Sys_Menu_Bar.H>
39 #include <FL/x.H>
40 #include <FL/Fl_Help_Dialog.H>
41 #include <FL/Fl_Menu_Item.H>
42
43 // this tests depends on a modified FL/filename.H in the Fltk-1.3.0
44 // change
45 //# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__)
46 // to
47 //# if defined(WIN32) && !defined(__CYGWIN__) && !defined(__WATCOMC__) && !defined(__WOE32__)
48
49 #include <FL/filename.H>
50
51 #ifdef __MINGW32__
52 # include "compat.h"
53 #endif
54
55 #include <dirent.h>
56
57 #include "fileselect.h"
58
59 #include "socket.h"
60
61 #include <dirent.h>
62
63 #include "flmsg.h"
64 #include "flmsg_dialog.h"
65 #include "flmsg_arq.h"
66 #include "xml_io.h"
67
68 #include "threads.h"
69 #include "flmsg_arq.h"
70 #include "flmisc.h"
71 #include "stacktrace.h"
72 #include "icons.h"
73 #include "arq.h"
74
75 #include "base64.h"
76 #include "gettext.h"
77 #include "status.h"
78 #include "wrap.h"
79 #include "time_table.h"
80
81 bool SHOWDEBUG = true;
82
83 Fl_Double_Window *dlgconfig = (Fl_Double_Window *)0;
84
85 arq flmsg_arq;
86
87 bool traffic = false;
88 int arqstate = 0;
89
90 std::string RX;
91 std::string TX;
92
93 std::string MyCall;
94 std::string UrCall;
95
96 #if !defined(__APPLE__) && !defined(__WOE32__) && USE_X
97 Pixmap flarq_icon_pixmap;
98 #endif
99
100 std::string rxbuffer;
101
102 size_t bufsize = 0;
103
104 int blocksSent = 0;
105
106 float vers = 0;
107 float version;
108
109 extern std::string arqstates[];
110
111 const char *ASCII[32] = {
112 "<NUL>", "<SOH>", "<STX>", "<ETX>", // 0x00 - 0x03
113 "<EOT>", "<ENQ>", "<ACK>", "<BEL>", // 0x04 - 0x07
114 "<BX>", "<TAB>", "<LF>", "<VT>", // 0x08 - 0x0B
115 "<FF>", "<CR>", "<SO>", "<SI>", // 0x0C - 0x0F
116 "<DLE>", "<DC1>", "<DC2>", "<DC3>", // 0x10 - 0x13
117 "<DC4>", "<NAK>", "<SYN>", "<ETB>", // 0x14 - 0x17
118 "<CAN>", "<EM>", "<SUB>", "<ESC>", // 0x18 - 0x1B
119 "<FS>", "<GS>", "<RS>", "<US>" // 0x1C - 0x1F
120 };
121
122 static pthread_mutex_t nocr_mutex = PTHREAD_MUTEX_INITIALIZER;
123 std::string noCR(std::string s)
124 {
125 guard_lock lock(&nocr_mutex);
126 std::string text = s;
127 size_t p;
128 while ((p = text.find('\r')) != std::string::npos)
129 text.erase(p,1);
130 return text;
131 }
132
133 static pthread_mutex_t noctl_mutex = PTHREAD_MUTEX_INITIALIZER;
134 std::string noCTL(std::string s)
135 {
136 guard_lock lock(&noctl_mutex);
137 std::string text;
138 int c;
139 for (size_t n = 0; n < s.length(); n++) {
140 c = s[n];
141 if ((c >= 0) && (c < 0x20))
142 text.append(ASCII[c]);
143 else
144 text += c;
145 }
146 return text;
147 }
148
149 std::string incomingText = "";
150 std::string txtarqload = "";
151 std::string rxfname = "";
152 std::string arqstart = "ARQ::STX\n";
153 std::string arqend = "ARQ::ETX\n";
154 std::string arqfile = "ARQ:FILE::";
155 std::string arqbase64 = "ARQ:ENCODING::BASE64\n";
156 std::string arqsizespec = "ARQ:SIZE::";
157 size_t startpos = std::string::npos;
158 size_t endpos = std::string::npos;
159 size_t fnamepos = std::string::npos;
160 size_t indx = std::string::npos;
161 size_t sizepos = std::string::npos;
162 size_t lfpos = std::string::npos;
163 size_t arqPayloadSize = 0;
164 bool rxARQfile = false;
165 bool rxARQhavesize = false;
166 bool rxTextReady = false;
167
168 time_t StartTime_t;
169 time_t EndTime_t;
170 time_t TransferTime_t;
171 double TransferTime;
172
173 char statemsg[80];
174
175 void dispState()
176 {
177 switch (flmsg_arq.state()) {
178 case arq::ARQ_CONNECTING :
179 txtSTATE->value("CONNECTING");
180 txtSTATE->redraw();
181 btnSEND->label("Wait");
182 btnSEND->redraw_label();
183 break;
184 case arq::ARQ_CONNECTED :
185 txtSTATE->value("CONNECTED");
186 txtSTATE->redraw();
187 txtSENDTO->value( flmsg_arq.urCall().c_str() );
188 btnSEND->label("End");
189 btnSEND->redraw_label();
190 break;
191 case arq::ARQ_DISCONNECT:
192 txtSTATE->value("ARQ_DISCONNECT");
193 txtSTATE->redraw();
194 btnSEND->label("Wait");
195 btnSEND->redraw_label();
196 break;
197 case arq::ARQ_DISCONNECTING:
198 txtSTATE->value("ARQ_DISCONNECTING");
199 txtSTATE->redraw();
200 btnSEND->label("Wait");
201 btnSEND->redraw_label();
202 break;
203 case arq::ARQ_DOWN :
204 default :
205 txtSTATE->value("NOT CONNECTED");
206 txtSTATE->redraw();
207 btnSEND->label("Send");
208 btnSEND->redraw_label();
209 }
210 }
211
212 void client_transmit(const std::string& s )
213 {
214 MilliSleep(50);
215 try {
216 if (!s.empty()) {
217 xml_send_tx_chars(s);
218 }
219 }
220 catch (const SocketException& e) {
221 std::cerr << e.what() << '\n';
222 }
223 }
224
225 size_t rxbuffer_len = 0;
226 size_t rxbuffer_ptr = 0;
227
228 bool client_receive(char &c)
229 {
230 if (rxbuffer_len && (rxbuffer_ptr < rxbuffer_len)) {
231 c = rxbuffer[rxbuffer_ptr];
232 rxbuffer_ptr++;
233 return true;
234 }
235 rxbuffer.clear();
236 rxbuffer_len = rxbuffer_ptr = 0;
237 std::string rxstr = xml_get_rx_chars();
238 if (!rxstr.empty()) {
239 rxbuffer = rxstr;
240 rxbuffer_len = rxbuffer.length();
241 }
242
243 return false;
244 }
245
246 void restrt(void *)
247 {
248 prgSTATE->value(0.0);
249 prgSTATE->label("");
250 prgSTATE->redraw_label();
251 prgSTATE->redraw();
252 rxARQfile = false;
253 incomingText.clear();
254 }
255
256 void restart()
257 {
258 Fl::awake(restrt);
259 }
260
261 void arqSEND()
262 {
263 int state = Fl::event_state();
264 if ((state & FL_CTRL) == FL_CTRL) {
265 flmsg_arq.reset_arq();
266 txtSENDTO->value("");
267 Fl::awake(restrt);
268 return;
269 }
270 if (progStatus.my_call.empty()) return;
271
272 if (strlen(txtSENDTO->value()) == 0) return;
273
274 int arqstate = flmsg_arq.state();
275
276 if (arqstate == arq::ARQ_DOWN) {
277 flmsg_arq.myCall(progStatus.my_call.c_str());
278 cb_wrap_autosend();
279 } else {
280 flmsg_arq.disconnect();
281 txtSENDTO->value("");
282 Fl::awake(restrt);
283 }
284 }
285
286 pthread_mutex_t payload_mutex = PTHREAD_MUTEX_INITIALIZER;
287
288 void save_this_file_as(std::string fname, std::string txt)
289 {
290 if (txt.find("<transfer>") != std::string::npos) {
291 cb_transfer_wrap_import(fname, txt);
292 select_form(TRANSFER);
293 return;
294 }
295
296 std::string fnam = fname;
297 std::string outfname;
298 std::string tst_name;
299 std::string tst_ext = "";
300
301 // write to file with name(s) such as
302 // test.b2s, test-01.txt, test-02.txt etc. up to 999
303 size_t pos = fnam.find(".");
304 if (pos != std::string::npos) {
305 tst_name = fnam.substr(0, pos);
306 tst_ext = fnam.substr(pos);
307 } else
308 tst_name = fnam;
309
310 char fnum[8]="";
311 int num = 0;
312 outfname.assign(ICS_msg_dir).append(tst_name).append(tst_ext);
313
314 std::ifstream tstfile;
315 tstfile.open(outfname.c_str());
316 while (tstfile) {
317 tstfile.close();
318 snprintf(fnum, sizeof(fnum), "-%03d", (++num % 1000));
319 outfname.assign(ICS_msg_dir).append(tst_name).append(fnum).append(tst_ext);
320 tstfile.open(outfname.c_str());
321 }
322
323 // decompress_maybe returns 0 on failure
324 decompress_maybe(txt);
325
326 std::ofstream ofile(outfname.c_str(), std::ios::binary);
327 if (ofile) ofile << txt;
328 ofile.close();
329
330 add_rcvd_msg(tst_name.append(fnum).append(tst_ext));
331
332 return;
333
334 }
335
336 void display_payloadtext(std::string fname, std::string txt)
337 {
338 save_this_file_as(fname, txt);
339 }
340
341 // process an AutoSend data stream
342
343 void direct_payload(std::string txt)
344 {
345 LOG_INFO("%s", txt.substr(0, 50).c_str());
346
347 size_t pos = txt.find("WRAP:fn ");
348 if (pos == std::string::npos) return;
349 txt.erase(0, pos + 8);
350 pos = txt.find("]");
351 if (pos == std::string::npos) return;
352
353 std::string fname = txt.substr(0,pos);
354 txt.erase(0,pos + 1);
355 pos = txt.find("[WRAP:chk");
356 if (pos == std::string::npos) return;
357 txt.erase(pos);
358
359 save_this_file_as(fname, txt);
360 }
361
362 // process an ARQ data stream
363
364 void payloadtext(void *)
365 {
366 guard_lock payload(&payload_mutex);
367
368 if ((startpos = incomingText.find(arqstart)) != std::string::npos) {
369 startpos += arqstart.length();
370 time(&StartTime_t);
371 } else
372 return;
373
374 if ((endpos = incomingText.find(arqend)) != std::string::npos) {
375 fnamepos = incomingText.find(arqfile);
376 fnamepos += arqfile.length();
377 indx = incomingText.find('\n', fnamepos);
378 rxfname = incomingText.substr(fnamepos, indx - fnamepos);
379 txtarqload = incomingText.substr(startpos, endpos - startpos);
380 if (incomingText.find(arqbase64) != std::string::npos) {
381 base64 b64;
382 txtarqload = b64.decode(txtarqload);
383 }
384 startpos = std::string::npos;
385 endpos = std::string::npos;
386 fnamepos = std::string::npos;
387 indx = std::string::npos;
388 sizepos = std::string::npos;
389 lfpos = std::string::npos;
390 arqPayloadSize = 0;
391 rxARQfile = false;
392 rxARQhavesize = false;
393 rxTextReady = true;
394 if (incomingText.find("FLMSG_XFR") != std::string::npos) {
395 std::string outfname;
396 std::string arqstr = "[ARQ:fn ";
397 size_t p = txtarqload.find(arqstr);
398 if (p != std::string::npos) {
399 txtarqload.erase(0, p + arqstr.length());
400 p = txtarqload.find("]");
401 if (p != std::string::npos) {
402
403 outfname = txtarqload.substr(0, p);
404 txtarqload.erase(0, p + 1);
405 display_payloadtext(outfname, txtarqload);
406
407 }
408 }
409 }
410 incomingText = "";
411 }
412 }
413
414 void payloadText(std::string s)
415 {
416 guard_lock payload(&payload_mutex);
417 incomingText.assign (s);
418 Fl::awake(payloadtext);
419 }
420
421 void abted(void *)
422 {
423 notify_dialog* alert_window = new notify_dialog;
424 alert_window->set_non_modal();
425 alert_window->notify(_("Transfer aborted"), 0, true);
426 restart();
427 }
428
429 void abortedTransfer()
430 {
431 Fl::awake(abted);
432 }
433
434 static std::string arqascii = "ARQ:ENCODING::ASCII\n";
435
436 void send_xml_text(std::string txt)
437 {
438 size_t txtsize;
439 char sizemsg[40];
440
441 if (!txt.empty()) {
442 TX.erase();
443 TX.append(arqfile);
444 TX.append("FLMSG_XFR");
445 TX.append("\n");
446 TX.append(arqascii);
447 txtsize = txt.length();
448 arqPayloadSize = txtsize;
449 blocksSent = 0;
450 snprintf(sizemsg, sizeof(sizemsg), "ARQ:SIZE::%d\n",
451 static_cast<int>(txtsize));
452 TX.append(sizemsg);
453 TX.append(arqstart);
454 TX.append(txt);
455 TX.append(arqend);
456 }
457 traffic = true;
458 }
459
460 static float update_val;
461 static char szPercent[8];
462 void updateQuality(void *)
463 {
464 prgSTATE->value( update_val );
465 snprintf(szPercent, sizeof(szPercent), "%0.f %%", update_val*100.0 + 0.5);
466 prgSTATE->label(szPercent);
467 }
468
469 void showQuality(float val)
470 {
471 update_val = val;
472 Fl::awake(updateQuality);
473 }
474
475 void mainloop(void *)
476 {
477
478 if (traffic) {
479 flmsg_arq.sendtext(txtSENDTO->value(), TX);
480 traffic = false;
481 time(&StartTime_t);
482 }
483 dispState();
484 Fl::repeat_timeout(0.1, mainloop);
485 }
486
487 static std::string str_status;
488 //static double dtime;
489 pthread_mutex_t protect_status = PTHREAD_MUTEX_INITIALIZER;
490
491 void print_status(void *data)
492 {
493 if (!btext) return;
494
495 guard_lock protect(&protect_status);
496 if (str_status.empty()) return;
497
498 size_t pos;
499 pos = str_status.find("\n");
500 while (pos != std::string::npos) {
501 btext->add(str_status.substr(0, pos).c_str());
502 str_status.erase(0, pos + 1);
503 pos = str_status.find("\n");
504 }
505 btext->redraw();
506 btext->bottomline(btext->size());
507 str_status.clear();
508 }
509
510 static std::string notify_string;
511
512 void notify_(void *)
513 {
514 notify_dialog* alert_window = new notify_dialog;
515 alert_window->notify(notify_string.c_str(), progStatus.arq_notify_timeout, true);
516 }
517
518 std::string sXfrd = _("Transferred ");
519 std::string sRcvd = _("Received ");
520 std::string sRtry = _("Retries failed: ");
521 std::string sTout = _("Timed out:");
522 std::string sTo = _(" to ");
523 std::string sFm = _(" from ");
524 std::string sColon = _(" : ");
525
526 void STATUSprint(std::string s)
527 {
528 size_t p = 0;
529
530 if ( s.find(sXfrd) != std::string::npos) { // transferred
531 notify_string = s;
532 p = notify_string.find(sXfrd);
533 if (p != std::string::npos) notify_string[p + sXfrd.length() - 1] = '\n';
534 p = notify_string.find(sTo);
535 if (p != std::string::npos) notify_string[p] = '\n';
536 Fl::awake(notify_);
537 }
538 else if (s.find(sRcvd) != std::string::npos && progStatus.notify_receipt) { // received
539 notify_string = s;
540 p = notify_string.find(sRcvd);
541 if (p != std::string::npos) notify_string[p + sRcvd.length() - 1] = '\n';
542 p = notify_string.find(sFm);
543 if (p != std::string::npos) notify_string[p] = '\n';
544 Fl::awake(notify_);
545 }
546 else if (s.find(sRtry) != std::string::npos) { // retries
547 notify_string = s;
548 p = notify_string.find(">");
549 if (p != std::string::npos)
550 notify_string.insert(p+1, "\n");
551 p = notify_string.find(sColon);
552 while (p != std::string::npos) {
553 notify_string.replace(p, sColon.length(), "\n");
554 p = notify_string.find(sColon);
555 }
556 Fl::awake(notify_);
557 }
558 else if (s.find(sTout) != std::string::npos) { // timed
559 notify_string = s;
560 p = notify_string.find(sTout);
561 notify_string[p + sTout.length() -1] = '\n';
562 p = notify_string.find(sColon);
563 while (p != std::string::npos) {
564 notify_string.replace(p, sColon.length(), "\n");
565 p = notify_string.find(sColon);
566 }
567 Fl::awake(notify_);
568 }
569 }
570
571 void arqlog(std::string nom, std::string s)
572 {
573 char szGMT[80];
574 tm *now;
575 time_t tm;
576
577 if (nom.empty() && s.empty()) return;
578
579 time(&tm);
580 now = localtime( &tm );
581 memset(szGMT, 0, 80);
582 strftime(szGMT, sizeof(szGMT), "[%X] ", now);
583
584 std::string txtout;
585 txtout.assign(szGMT).append(nom).append(" ").append(noCTL(s));
586
587 STATUSprint(txtout);
588
589 std::string logname = FLMSG_log_dir;
590 logname.append("flmsg.log");
591 std::ofstream logfile(logname.c_str(), std::ios::app);
592 if (logfile){
593 logfile << txtout;
594 if (s.length() == 0 || s[s.length()-1] != '\n') logfile << std::endl;
595 }
596 logfile.close();
597
598 if (txtout.length() > 60) txtout.erase(60);
599
600 if (txtout.length() == 0) return;
601
602 Fl_Color color = FL_BLACK;
603 if (s.find(sTout) != std::string::npos) // timed
604 color = FL_RED;
605 else if (s.find(sXfrd) != std::string::npos) // transferred
606 color = FL_BLUE;
607 else if (s.find(sRtry) != std::string::npos) // retries
608 color = FL_RED;
609 else if (nom == "<TX>")
610 color = FL_DARK_RED;
611 else if (nom == "<RX>")
612 color = FL_DARK_GREEN;
613
614 char clr[12];
615 snprintf( clr, sizeof(clr),"@C%d@.", color);
616
617 txtout.insert(0, clr);
618 txtout.append("\n");
619
620 guard_lock protect(&protect_status);
621 str_status.append(txtout);
622 Fl::awake(print_status);
623
624 }
625
626 static std::string txecho_s;
627 static void txecho(void *)
628 {
629 std::string style;
630 blocksSent += txecho_s.length();
631 std::string text = noCR(txecho_s);
632 txecho_s.clear();
633 }
634
635 void TXecho(std::string s)
636 {
637 txecho_s = s;
638 Fl::awake(txecho);
639 }
640
641 static std::string rxurcall_s;
642 static void rxurcall(void *)
643 {
644 txtSENDTO->value(rxurcall_s.c_str());
645 rxurcall_s.clear();
646 }
647
648 void rxUrCall(std::string s)
649 {
650 rxurcall_s = s;
651 Fl::awake(rxurcall);
652 }
653
654 std::string flmsg_trx()
655 {
656 return xml_fldigi_trx();
657 }
658
659 float flmsg_xmt_time(std::string s)
660 {
661 float xfr_time = 0, overhead;
662 float rsid_time = 3.0;
663
664 if (s.empty()) return rsid_time;
665
666 xfr_time = seconds_from_c_string(cbo_modes->value(), s.c_str(), s.size(), &overhead);
667 xfr_time += overhead;
668 xfr_time += rsid_time;
669 return xfr_time;
670 }
671
672 void init_flmsg_arq()
673 {
674 // the following sequence of assigning callback functions is mandatory
675 // for the arq class to function
676
677 flmsg_arq.setRetries(progStatus.retries);
678 flmsg_arq.setExponent(progStatus.exponent);
679
680 flmsg_arq.setSendFunc (client_transmit);
681 flmsg_arq.setGetCFunc (client_receive);
682 flmsg_arq.setDisconnected(restart);
683 flmsg_arq.setrxUrCall (rxUrCall);
684 flmsg_arq.setOK_to_transmit (fldigi_OK_to_transmit);
685
686 flmsg_arq.setPrintRX (payloadText);
687 flmsg_arq.setPrintSTATUS (STATUSprint);
688 flmsg_arq.setPrintLOG (arqlog);
689
690 flmsg_arq.setQualityValue(showQuality);
691
692 flmsg_arq.setTRX (flmsg_trx);
693
694 flmsg_arq.setTransmitTime (flmsg_xmt_time);
695
696 flmsg_arq.myCall(progStatus.my_call);
697
698 flmsg_arq.start_arq();
699
700 Fl::add_timeout(0.1, mainloop);
701 }
702