AmendHub

Download:

jcs

/

subtext

/

amendments

/

417

binkp: Move polling out of here, tweak some log messages


jcs made amendment 417 about 1 year ago
--- binkp.c Tue Mar 14 08:56:55 2023 +++ binkp.c Wed Mar 15 13:56:28 2023 @@ -29,6 +29,9 @@ /* #define BINKP_DEBUG */ +bool binkp_ready = false; +time_t binkp_next_poll = 0; + static struct fidopkt_address binkp_our_address = { 0 }; static struct binkp_connection *binkpc = NULL; static Str255 binkp_inbox_dir, binkp_done_dir, binkp_outbox_dir; @@ -37,7 +40,6 @@ static bool binkp_temp_fail = false; struct uthread *binkp_thread = NULL; void binkp_mkdir(Str255 *dir_str, char *suffix); -void binkp_poll(void); bool binkp_connect(void); void binkp_free(void); size_t binkp_send_frame(short command, char *data, size_t data_size); @@ -48,46 +50,30 @@ bool binkp_zip_decider(char *filename, size_t size); void binkp_fidopkt_processor(char *filename, unsigned char *data, size_t size); size_t binkp_buffer_file(Str255 path, char **data); -void binkp_deliver_inbox(void); +void binkp_toss_inbox(void); void binkp_deliver_outbox(void); void -binkp_process(struct uthread *uthread, void *arg) +binkp_init(void) { short newdirid, error; + + if (db->config.binkp_hostname[0] == '\0') + return; if (!fidopkt_parse_address(db->config.ftn_node_addr, &binkp_our_address)) { - logger_printf("[binkp] failed parsing local FTN node address " + logger_printf("[binkp] Failed parsing local FTN node address " "\"%s\", fix in settings", db->config.ftn_node_addr); return; } - binkp_thread = uthread; - - uthread_msleep(1000UL * 2); - binkp_mkdir(&binkp_inbox_dir, "inbox"); if (!db->config.binkp_delete_done) binkp_mkdir(&binkp_done_dir, "processed"); binkp_mkdir(&binkp_outbox_dir, "outbox"); - for (;;) { - if (db->config.binkp_hostname[0] != '\0' && - db->config.binkp_port != 0) - binkp_poll(); - - binkp_deliver_inbox(); - - if (db->config.binkp_hostname[0] != '\0' && - db->config.binkp_port != 0 && - db->config.binkp_interval_seconds) - uthread_msleep(1000UL * db->config.binkp_interval_seconds); - else - break; - } - - binkp_thread = NULL; + binkp_ready = true; } void @@ -125,8 +111,10 @@ binkp_poll(void) unsigned long started = Time, elapsed; binkp_connect(); - if (!binkpc) - return; + if (!binkpc) { + binkp_next_poll = Time + 60; + goto done; + } if (!binkp_login()) goto done; @@ -144,12 +132,16 @@ binkp_poll(void) false); elapsed = Time - started; - logger_printf("[binkp] done polling after %ld sec%s", elapsed, + logger_printf("[binkp] Finished polling in %ld sec%s", elapsed, elapsed == 1 ? "" : "s"); + + binkp_next_poll = Time + db->config.binkp_interval_seconds; done: if (binkpc != NULL) binkp_free(); + + binkp_toss_inbox(); } bool @@ -169,21 +161,21 @@ binkp_connect(void) binkpc = xmalloczero(sizeof(struct binkp_connection)); if (binkpc == NULL) { - logger_printf("[binkp] failed allocating connection of size %ld", + logger_printf("[binkp] Failed allocating connection of size %ld", sizeof(struct binkp_connection)); goto error; } binkpc->buf_size = 1024; binkpc->buf = xmalloc(binkpc->buf_size); if (binkpc->buf == NULL) { - logger_printf("[binkp] failed allocating connection buffer of " + logger_printf("[binkp] Failed allocating connection buffer of " "size %ld", binkpc->buf_size); goto error; } binkpc->tcp_buf_size = (4 * 1500) + binkpc->buf_size; binkpc->tcp_buf = xmalloc(binkpc->tcp_buf_size); if (binkpc->tcp_buf == NULL) { - logger_printf("[binkp] failed allocating TCP buffer of " + logger_printf("[binkp] Failed allocating TCP buffer of " "size %ld", binkpc->tcp_buf_size); goto error; } @@ -192,7 +184,7 @@ binkp_connect(void) error = DNSResolveName(&hostname, &host_ip, uthread_yield); if (error) { - logger_printf("[binkp] failed resolving binkp host %s: %d", + logger_printf("[binkp] Failed resolving binkp host %s: %d", db->config.binkp_hostname, error); goto error; } @@ -200,13 +192,13 @@ binkp_connect(void) error = _TCPCreate(&binkpc->tcp_send_iopb, &binkpc->tcp_stream, (Ptr)binkpc->tcp_buf, binkpc->tcp_buf_size, nil, nil, nil, false); if (error) { - logger_printf("[binkp] failed creating TCP stream: %d", error); + logger_printf("[binkp] Failed creating TCP stream: %d", error); goto error; } long2ip(host_ip, (char *)&ip_s); - logger_printf("[binkp] connecting to %s (%s) port %d", + logger_printf("[binkp] Connecting to %s (%s) port %d", db->config.binkp_hostname, ip_s, db->config.binkp_port); error = _TCPActiveOpen(&binkpc->tcp_send_iopb, binkpc->tcp_stream, @@ -215,7 +207,7 @@ binkp_connect(void) while (!error && binkpc->tcp_send_iopb.ioResult > 0) uthread_yield(); if (error || binkpc->tcp_send_iopb.ioResult != 0) { - logger_printf("[binkp] failed connecting to %s (%s) port %d: %d", + logger_printf("[binkp] Failed connecting to %s (%s) port %d: %d", db->config.binkp_hostname, ip_s, db->config.binkp_port, binkpc->tcp_send_iopb.ioResult); goto error; @@ -259,8 +251,7 @@ binkp_send_frame(short command, char *data, size_t dat if (command != BINKP_DATA) flen++; if (flen > binkpc->buf_size) { - logger_printf("[binkp] binkp_send_frame: frame too large (%u)", - flen); + logger_printf("[binkp] Frame too large (%u), can't send", flen); return 0; } @@ -290,24 +281,24 @@ binkp_send_frame(short command, char *data, size_t dat #ifdef BINKP_DEBUG switch (command) { case BINKP_COMMAND_M_PWD: - logger_printf("[binkp] sending password command of size %ld", + logger_printf("[binkp] Sending password command of size %ld", data_size); break; case BINKP_COMMAND_M_FILE: - logger_printf("[binkp] sending new file: %s", binkpc->buf + 3); + logger_printf("[binkp] Sending new file: %s", binkpc->buf + 3); break; case BINKP_COMMAND_M_EOB: - logger_printf("[binkp] sending EOB"); + logger_printf("[binkp] Sending EOB"); break; case BINKP_COMMAND_M_GOT: - logger_printf("[binkp] sending GOT for %s", + logger_printf("[binkp] Sending GOT for %s", binkpc->cur_incoming_file.filename); break; case BINKP_DATA: - logger_printf("[binkp] sending data of size %ld", data_size); + logger_printf("[binkp] Sending data of size %ld", data_size); break; default: - logger_printf("[binkp] sending command %d of size %ld: %s", + logger_printf("[binkp] Sending command %d of size %ld: %s", command, data_size, binkpc->buf + 3); } #endif @@ -352,8 +343,7 @@ binkp_read_frame(void) return false; if (binkpc->cur_frame.data_size == 0) { - logger_printf("[binkp] binkp_read_frame: bogus frame, " - "no data_size"); + logger_printf("[binkp] Received bogus frame, no data_size"); return false; } @@ -407,7 +397,7 @@ binkp_read_frame(void) if (binkpc->cur_frame.type == BINKP_TYPE_DATA) { binkpc->cur_incoming_file.data_read += rlen; #ifdef BINKP_DEBUG - logger_printf("[binkp] read %d TCP chunk (%ld / %d)", + logger_printf("[binkp] Read %d TCP chunk (%ld / %d)", rlen, frame_data_read, binkpc->cur_frame.data_size); #endif @@ -417,7 +407,7 @@ binkp_read_frame(void) error = FSWrite(binkpc->cur_incoming_file.frefnum, &len, binkpc->buf); if (error) { - logger_printf("[binkp] error writing %u to %s: %d", + logger_printf("[binkp] Error writing %u to %s: %d", rlen, PtoCstr(binkpc->cur_incoming_file.pfilename), error); CtoPstr(binkpc->cur_incoming_file.pfilename); @@ -438,7 +428,7 @@ binkp_read_frame(void) binkpc->buf[binkpc->buf_size - 1] = '\0'; #ifdef BINKP_DEBUG - logger_printf("[binkp] read command 0x%x frame [%ld]: %s", + logger_printf("[binkp] Read command 0x%x frame [%ld]: %s", binkpc->cur_frame.command_id, frame_data_read, binkpc->buf + (frame_data_read > 0 ? 1 : 0)); #endif @@ -446,7 +436,7 @@ binkp_read_frame(void) switch (binkpc->cur_frame.command_id) { case BINKP_COMMAND_M_NUL: if (strncmp(binkpc->buf + 1, "SYS ", 4) == 0) - logger_printf("[binkp] connected to %s", + logger_printf("[binkp] Connected to %s", binkpc->buf + 1 + 4); else if (strncmp(binkpc->buf + 1, "TIME ", 5) == 0) { /* @@ -458,7 +448,7 @@ binkp_read_frame(void) break; case BINKP_COMMAND_M_FILE: if (binkpc->cur_incoming_file.filename[0]) { - logger_printf("[binkp] received M_FILE but not done " + logger_printf("[binkp] Received M_FILE but not done " "with file %s!", binkpc->cur_incoming_file.filename); binkpc->cur_incoming_file.filename[0] = '\0'; if (binkpc->cur_incoming_file.frefnum > 0) { @@ -471,11 +461,11 @@ binkp_read_frame(void) &binkpc->cur_incoming_file.filename, &binkpc->cur_incoming_file.size, &binkpc->cur_incoming_file.mtime, &off) == 4) { - logger_printf("[binkp] receiving file \"%s\" size %ld", + logger_printf("[binkp] Receiving file \"%s\" size %ld", binkpc->cur_incoming_file.filename, binkpc->cur_incoming_file.size); if (off != 0) - logger_printf("[binkp] non-zero file fetch not " + logger_printf("[binkp] Non-zero file fetch offset not " "supported"); binkpc->cur_incoming_file.data_read = 0; @@ -519,7 +509,7 @@ binkp_read_frame(void) CtoPstr(binkpc->cur_incoming_file.pfilename); } } else { - logger_printf("[binkp] failed parsing M_FILE: %s", + logger_printf("[binkp] Failed parsing M_FILE: %s", binkpc->buf + 1); } break; @@ -527,18 +517,18 @@ binkp_read_frame(void) binkpc->done_receiving = true; break; case BINKP_COMMAND_M_ERR: - logger_printf("[binkp] error from remote: %s", + logger_printf("[binkp] Error from remote: %s", binkpc->buf + 1); binkpc->done_receiving = binkpc->done_sending = true; break; case BINKP_COMMAND_M_BSY: - logger_printf("[binkp] remote is busy: %s", binkpc->buf + 1); + logger_printf("[binkp] Remote is busy: %s", binkpc->buf + 1); binkpc->done_receiving = binkpc->done_sending = true; break; } } else { #ifdef BINKP_DEBUG - logger_printf("[binkp] read %d data frame of file (%ld / %ld)", + logger_printf("[binkp] Read %d data frame of file (%ld / %ld)", binkpc->cur_frame.data_size, binkpc->cur_incoming_file.data_read, binkpc->cur_incoming_file.size); @@ -546,7 +536,7 @@ binkp_read_frame(void) if (binkpc->cur_incoming_file.data_read == binkpc->cur_incoming_file.size) { #ifdef BINKP_DEBUG - logger_printf("[binkp] done reading file %s (%ld)", + logger_printf("[binkp] Done reading file %s (%ld)", binkpc->cur_incoming_file.filename, binkpc->cur_incoming_file.size); #endif @@ -558,7 +548,7 @@ binkp_read_frame(void) binkpc->cur_incoming_file.size, binkpc->cur_incoming_file.mtime); if (!binkp_send_frame(BINKP_COMMAND_M_GOT, tmp, len)) - logger_printf("[binkp] failed sending M_GOT %s", + logger_printf("[binkp] Failed sending M_GOT %s", binkpc->cur_incoming_file.filename); binkpc->cur_incoming_file.filename[0] = '\0'; @@ -620,14 +610,14 @@ binkp_login(void) continue; if (binkpc->cur_frame.command_id == BINKP_COMMAND_M_ERR) { - logger_printf("[binkp] login as %s failed, disconnecting", + logger_printf("[binkp] Login as %s failed, disconnecting", db->config.ftn_node_addr); binkp_free(); return false; } if (binkpc->cur_frame.command_id == BINKP_COMMAND_M_OK) { - logger_printf("[binkp] logged in successfully as %s", + logger_printf("[binkp] Logged in successfully as %s", db->config.ftn_node_addr); return true; } @@ -637,7 +627,7 @@ binkp_login(void) } void -binkp_deliver_inbox(void) +binkp_toss_inbox(void) { CInfoPBRec cipbr = { 0 }; HFileInfo *fpb = (HFileInfo *)&cipbr; @@ -681,7 +671,7 @@ binkp_deliver_inbox(void) PtoCstr(binkp_inbox_dir); snprintf((char *)path, sizeof(path), "%s:%s", binkp_inbox_dir, file_name_c); - logger_printf("[binkp] processing file %s", (char *)path); + logger_printf("[binkp] Tossing file %s", (char *)path); CtoPstr(path); CtoPstr(binkp_inbox_dir); @@ -704,12 +694,12 @@ binkp_deliver_inbox(void) } if (binkp_temp_fail) { - logger_printf("[binkp] temporary failure during processing, " + logger_printf("[binkp] Temporary failure during tossing, " "aborting"); return; } - logger_printf("[binkp] %s: imported %d, skipped %d", + logger_printf("[binkp] %s: tossed %d, skipped %d", (char *)file_name_c, binkp_fidopkt_imported, binkp_fidopkt_skipped); @@ -721,7 +711,7 @@ binkp_deliver_inbox(void) if (db->config.binkp_delete_done) { if ((error = FSDelete(done_path, 0)) != 0) { - logger_printf("[binkp] failed deleting %s: %d", + logger_printf("[binkp] Failed deleting %s: %d", PtoCstr(done_path), error); break; } @@ -735,13 +725,13 @@ try_rename: error = PBCatMove(&cmpbr, false); if (error == dupFNErr) { if ((error = FSDelete(done_path, 0)) != 0) { - logger_printf("[binkp] failed deleting %s: %d", + logger_printf("[binkp] Failed deleting %s: %d", PtoCstr(done_path), error); break; } goto try_rename; } else if (error) { - logger_printf("[binkp] failed moving %s to %s: %d", + logger_printf("[binkp] Failed moving %s to %s: %d", PtoCstr(path), PtoCstr(binkp_done_dir), error); CtoPstr(binkp_done_dir); break; @@ -753,7 +743,7 @@ try_rename: return; elapsed = Time - started; - logger_printf("[binkp] done importing in %ld sec%s", elapsed, + logger_printf("[binkp] Done tossing in %ld sec%s", elapsed, elapsed == 1 ? "" : "s"); } @@ -798,7 +788,7 @@ binkp_fidopkt_processor(char *filename, unsigned char if (!board) { #ifdef BINKP_DEBUG - logger_printf("[fidopkt] no local board for %s, skipping", + logger_printf("[fidopkt] No local board for %s, skipping", msg->area); #endif binkp_fidopkt_skipped++; @@ -909,7 +899,7 @@ binkp_deliver_outbox(void) if (data == NULL) { data = xmalloc(binkpc->buf_size); if (data == NULL) { - logger_printf("[binkp] failed allocating outbound buffer " + logger_printf("[binkp] Failed allocating outbound buffer " "%lu", binkpc->buf_size); return; } @@ -925,7 +915,7 @@ binkp_deliver_outbox(void) FStat(path, &sb); - logger_printf("[binkp] sending file %s size %lu", file_name_c, + logger_printf("[binkp] Sending file %s size %lu", file_name_c, sb.st_size); /* just send 1 as unix mtime */ @@ -967,29 +957,29 @@ binkp_deliver_outbox(void) } if (!binkpc) { - logger_printf("[binkp] connection lost waiting for M_GOT"); + logger_printf("[binkp] Connection lost waiting for M_GOT"); goto done; } if (binkpc->cur_frame.type != BINKP_TYPE_COMMAND || binkpc->cur_frame.command_id != BINKP_COMMAND_M_GOT) { - logger_printf("[binkp] got unexpected response to sending " - "file"); + logger_printf("[binkp] Received unexpected response to " + "sending file"); goto done; } - logger_printf("[binkp] successfully sent file %s", file_name_c); + logger_printf("[binkp] Successfully sent file %s", file_name_c); error = FSDelete(path, 0); if (error) { - logger_printf("[binkp] failed deleting file %s: %d", + logger_printf("[binkp] Failed deleting file %s: %d", PtoCstr(path), error); goto done; } } if (!binkp_send_frame(BINKP_COMMAND_M_EOB, "", 0)) - logger_printf("[binkp] failed sending M_EOB"); + logger_printf("[binkp] Failed sending M_EOB"); if (binkpc) binkpc->done_sending = true; @@ -1002,6 +992,37 @@ done: } bool +binkp_packets_in_outbox(void) +{ + CInfoPBRec cipbr = { 0 }; + HFileInfo *fpb = (HFileInfo *)&cipbr; + DirInfo *dpb = (DirInfo *)&cipbr; + Str255 file_name_c, path; + short dir_id, error; + + fpb->ioVRefNum = 0; + fpb->ioNamePtr = (StringPtr)&binkp_outbox_dir; + error = PBGetCatInfo(&cipbr, false); + if (error) { + logger_printf("[binkp] PBGetCatInfo on binkp outbox failed: %d", + error); + return; + } + dir_id = dpb->ioDrDirID; + + fpb->ioNamePtr = (StringPtr)&file_name_c; + file_name_c[0] = 0; + fpb->ioDirID = dir_id; + fpb->ioFDirIndex = 1; + + error = PBGetCatInfo(&cipbr, false); + if (error) + return false; + + return true; +} + +bool binkp_scan_message(struct fidopkt_message *msg) { Str255 path; @@ -1052,7 +1073,7 @@ binkp_scan_message(struct fidopkt_message *msg) error = FSWrite(frefnum, &size, buf); if (error) { - logger_printf("[binkp] error writing %lu to %s: %d", + logger_printf("[binkp] Error writing %lu to %s: %d", size, PtoCstr(path), error); goto done; } --- binkp.h Mon Mar 13 23:47:04 2023 +++ binkp.h Tue Mar 14 15:24:38 2023 @@ -68,9 +68,13 @@ struct binkp_connection { bool done_sending; }; -extern struct uthread *binkp_thread; -void binkp_process(struct uthread *uthread, void *arg); +extern bool binkp_ready; +extern time_t binkp_next_poll; + +void binkp_init(void); void binkp_atexit(void); +void binkp_poll(void); bool binkp_scan_message(struct fidopkt_message *msg); +bool binkp_packets_in_outbox(void); #endif