]> Kevux Git Server - kevux-tools/commitdiff
Progress: Further work in TacocaT.
authorKevin Day <thekevinday@gmail.com>
Mon, 11 Sep 2023 02:51:55 +0000 (21:51 -0500)
committerKevin Day <thekevinday@gmail.com>
Mon, 11 Sep 2023 02:51:55 +0000 (21:51 -0500)
Minor fixes, removing some todos and fixmes.
Add some message printing.

sources/c/tacocat/main/common.c
sources/c/tacocat/main/common/define.h
sources/c/tacocat/main/common/type.h
sources/c/tacocat/main/print/error.c
sources/c/tacocat/main/print/error.h
sources/c/tacocat/main/print/message.c
sources/c/tacocat/main/print/message.h
sources/c/tacocat/main/receive.c

index 1d75a321e795696f2afc3a2227fdd8f2795f6824..4b0a7969c33316237212b9a683f109f7f33b89bb 100644 (file)
@@ -428,7 +428,6 @@ extern "C" {
                 macro_setting_load_handle_send_receive_error_continue_2(f_string_dynamic_append);
               }
               else {
-                // @todo move this block into its own function.
                 main->setting.state.status = f_network_from_ip_name(address, &host);
 
                 // @todo provide network-specific error messages.
@@ -558,7 +557,7 @@ extern "C" {
             sets[i]->array[j].name.used = main->program.parameters.arguments.array[index].used;
             sets[i]->array[j].name.size = 0;
 
-            // @fixme only open the file when reading/writing and then close it at the end. This open is fine if it is used as a check on startup, but in this case immediately close it.
+            // Make sure the file exists and can be read and can be written to, then close when done.
             main->setting.state.status = f_file_open(sets[i]->array[j].name, F_file_mode_all_rw_d, &sets[i]->array[j].file);
 
             if (F_status_is_error(main->setting.state.status)) {
@@ -570,6 +569,11 @@ extern "C" {
 
               sets[i]->array[j].status = main->setting.state.status;
             }
+            else {
+              main->setting.state.status = f_file_close(&sets[i]->array[j].file);
+
+              macro_setting_load_handle_send_receive_error_file_continue_1(f_file_open, sets[i]->array[j].name, f_file_operation_close_s, fll_error_file_type_file_e);
+            }
           }
           else {
             main->setting.state.status = F_status_set_error(F_parameter);
@@ -590,7 +594,6 @@ extern "C" {
       }
       else if (main->program.parameters.array[parameters[i]].result & f_console_result_found_e) {
         main->setting.flag -= main->setting.flag & flags[i];
-
         main->setting.state.status = F_status_set_error(F_parameter);
 
         macro_setting_load_print_first();
index eee7e09bf542fd2f6ef24e5007425fa3e605e672..37db5095de86b4faa10e9617c57330f219201c23 100644 (file)
@@ -158,8 +158,7 @@ extern "C" {
         f_file_close_id(id_data); \
       } \
       \
-      flag -= flag & kt_tacocat_socket_flag_block_control_e; \
-      flag -= flag & kt_tacocat_socket_flag_block_payload_e; \
+      flag = 0; \
       \
       return; \
     }
@@ -168,7 +167,7 @@ extern "C" {
     if (F_status_is_error(status)) { \
       kt_tacocat_print_error_on(&main->program.error, macro_kt_tacocat_f(method), kt_tacocat_receive_s, name, status); \
       \
-      flag -= kt_tacocat_socket_flag_block_control_e; \
+      flag = 0; \
       \
       return; \
     }
index 8f26e08fb09ae978956782b0d44681c9e963fb16..198560fe13d23a73cd9c491b5dfef5145c2b1b14 100644 (file)
@@ -21,17 +21,17 @@ extern "C" {
  *
  * size_block: The size in bytes to used to represent a block when sending or receiving packets.
  *
- * flag:   An array of flags for each socket.
- * retry:  An array of the current number of retries performed for the given network packet.
- * file:   An array of files for each socket.
- * socket: An array of the network sockets.
- * status: An array of statuses for each socket.
+ * flag:   A set of flags.
+ * retry:  The current number of retries performed.
+ * file:   The file structure.
+ * socket: Socket structure.
+ * status: The status of the socket operations.
  *
- * buffer:  An array of buffers for sending or receiving data between clients for each socket.
- * client:  An array representing a single client address for some network connection.
- * name:    An array of file names associated with each file in the files array.
- * network: An array of networks for each socket.
- * packet:  An array of simple packet ranges representing the parts of the packet for each socket for use during processing.
+ * buffer:  A buffer for sending or receiving data between clients.
+ * client:  A single client address for some network connection.
+ * name:    A file name for reading from or writing to.
+ * network: A network name for the socket.
+ * packet:  The simple packet range representing the parts of the packet for use during processing.
  */
 #ifndef _di_kt_tacocat_socket_set_t_
   typedef struct {
index 02b04fa2487f604a6230edf3a4a5b618d51b9c03..82901cc824bf672b5deb990445688d7a532eb702 100644 (file)
@@ -70,7 +70,7 @@ extern "C" {
 
     f_file_stream_lock(print->to);
 
-    fl_print_format("%[%QNetwork packets too large for%] ", print->to, print->set->error, print->prefix, print->set->error);
+    fl_print_format("%[%QNetwork packet is too large for%] ", print->to, print->set->error, print->prefix, print->set->error);
     fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, on, print->set->notable);
     fl_print_format(" %[buffer for '%]", print->to, print->set->error, print->set->error, f_string_eol_s);
     fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, network, print->set->notable);
@@ -107,7 +107,7 @@ extern "C" {
 #endif // _di_kt_tacocat_print_error_on_busy_
 
 #ifndef _di_kt_tacocat_print_error_on_file_
-  f_status_t kt_tacocat_print_error_on_file(fl_print_t * const print, const f_string_t function, f_string_static_t on, const f_string_static_t network, const f_status_t status, const f_string_static_t name) {
+  f_status_t kt_tacocat_print_error_on_file(fl_print_t * const print, const f_string_t function, f_string_static_t on, const f_string_static_t network, const f_status_t status, const f_string_static_t name, const f_string_static_t operation) {
 
     if (!print || !print->custom) return F_status_set_error(F_output_not);
     if (print->verbosity < f_console_verbosity_error_e) return F_output_not;
@@ -124,7 +124,7 @@ extern "C" {
 
     f_file_stream_unlock(print->to);
 
-    fll_error_file_print(print, F_status_set_fine(((kt_tacocat_main_t *) print->custom)->setting.state.status), function, F_true, name, f_file_operation_write_s, fll_error_file_type_file_e);
+    fll_error_file_print(print, F_status_set_fine(((kt_tacocat_main_t *) print->custom)->setting.state.status), function, F_true, name, operation, fll_error_file_type_file_e);
   }
 #endif // _di_kt_tacocat_print_error_on_file_
 
index 69eee5c9781fa251bfa3925f411f6f8f3e640280..18566218470771c4a6520fde551f9cdf6f64d425 100644 (file)
@@ -193,6 +193,8 @@ extern "C" {
  *   The status code representing the error.
  * @param name
  *   The name of the file or directory.
+ * @param operation
+ *   The file operation that fails, such as 'open' or 'write'.
  *
  * @return
  *   F_okay on success.
@@ -203,7 +205,7 @@ extern "C" {
  * @see fll_error_file_print()
  */
 #ifndef _di_kt_tacocat_print_error_on_file_
-  extern f_status_t kt_tacocat_print_error_on_file(fl_print_t * const print, const f_string_t function, f_string_static_t on, const f_string_static_t network, const f_status_t status, const f_string_static_t name);
+  extern f_status_t kt_tacocat_print_error_on_file(fl_print_t * const print, const f_string_t function, f_string_static_t on, const f_string_static_t network, const f_status_t status, const f_string_static_t name, const f_string_static_t operation);
 #endif // _di_kt_tacocat_print_error_on_file_
 
 /**
index a5d2936f1063bb9e2da3e685161b4fad0bc9cc19..a4a39990a586579632a8e378456cfcae0a0b706d 100644 (file)
@@ -59,6 +59,61 @@ extern "C" {
   }
 #endif // _di_kt_tacocat_print_message_help_
 
+#ifndef _di_kt_tacocat_print_message_receive_operation_complete_
+  f_status_t kt_tacocat_print_message_receive_operation_complete(fl_print_t * const print, const kt_tacocat_socket_set_t set) {
+
+    if (!print) return F_status_set_error(F_output_not);
+    if (print->verbosity < f_console_verbosity_normal_e) return F_output_not;
+
+    f_file_stream_lock(print->to);
+
+    fl_print_format("Packet from ", print->to);
+    fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, set.network, print->set->notable);
+    fl_print_format(" is complete.%r", print->to, f_string_eol_s);
+
+    f_file_stream_unlock(print->to);
+  }
+#endif // _di_kt_tacocat_print_message_receive_operation_complete_
+
+#ifndef _di_kt_tacocat_print_message_receive_operation_control_size_
+  f_status_t kt_tacocat_print_message_receive_operation_control_size(fl_print_t * const print, const kt_tacocat_socket_set_t set) {
+
+    if (!print) return F_status_set_error(F_output_not);
+    if (print->verbosity < f_console_verbosity_normal_e) return F_output_not;
+
+    f_file_stream_lock(print->to);
+
+    fl_print_format("Packet from ", print->to);
+    fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, set.network, print->set->notable);
+    fl_print_format(" is %[%ul%] Bytes.%r", print->to, print->set->notable, set.packet.size, print->set->notable, f_string_eol_s);
+
+    f_file_stream_unlock(print->to);
+  }
+#endif // _di_kt_tacocat_print_message_receive_operation_control_size_
+
+#ifndef _di_kt_tacocat_print_message_receive_operation_received_
+  f_status_t kt_tacocat_print_message_receive_operation_received(fl_print_t * const print, const kt_tacocat_socket_set_t set) {
+
+    if (!print) return F_status_set_error(F_output_not);
+    if (print->verbosity < f_console_verbosity_normal_e) return F_output_not;
+
+    f_file_stream_lock(print->to);
+
+    fl_print_format("%rReceived packet from ", print->to, f_string_eol_s);
+    fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, set.network, print->set->notable);
+
+    if (print->verbosity > f_console_verbosity_normal_e) {
+      fl_print_format(" (appending to file '", print->to);
+      fl_print_format(f_string_format_Q_single_s.string, print->to, print->set->notable, set.name, print->set->notable);
+      fl_print_format("')", print->to);
+    }
+
+    fl_print_format(".%r", print->to, f_string_eol_s);
+
+    f_file_stream_unlock(print->to);
+  }
+#endif // _di_kt_tacocat_print_message_receive_operation_received_
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
index 2dd2f4999b8fef6979232e9bfe39619c932ea5a9..e0a5de55f735fae12151f4b8d1edefc95c611ef3 100644 (file)
@@ -22,6 +22,8 @@ extern "C" {
  * @param print
  *   The output structure to print to.
  *
+ *   This requires print.custom to be fake_main_t.
+ *
  *   This locks, uses, and unlocks the file stream.
  *
  *   This does not alter print.custom.setting.state.status.
@@ -33,20 +35,77 @@ extern "C" {
  *   F_output_not on success, but no printing is performed.
  *
  *   F_output_not (with error bit) if setting is NULL.
- *
- * @see f_file_stream_flush()
- * @see f_file_stream_lock()
- * @see f_file_stream_unlock()
- * @see f_print_dynamic_raw()
- * @see fl_print_format()
- * @see fll_program_print_help_header()
- * @see fll_program_print_help_option()
- * @see fll_program_print_help_option_long()
  */
 #ifndef _di_kt_tacocat_print_message_help_
   extern f_status_t kt_tacocat_print_message_help(fl_print_t * const print, const f_color_context_t context);
 #endif // _di_kt_tacocat_print_message_help_
 
+/**
+ * Print message about receive operation packet being complete.
+ *
+ * @param print
+ *   The output structure to print to.
+ *
+ *   This locks, uses, and unlocks the file stream.
+ *
+ *   This does not alter print.custom.setting.state.status.
+ * @param set
+ *   The socket set relating to the message.
+ *
+ * @return
+ *   F_okay on success.
+ *   F_output_not on success, but no printing is performed.
+ *
+ *   F_output_not (with error bit) if setting is NULL.
+ */
+#ifndef _di_kt_tacocat_print_message_receive_operation_complete_
+  extern f_status_t kt_tacocat_print_message_receive_operation_complete(fl_print_t * const print, const kt_tacocat_socket_set_t set);
+#endif // _di_kt_tacocat_print_message_receive_operation_complete_
+
+/**
+ * Print message about receive operation packet control size.
+ *
+ * @param print
+ *   The output structure to print to.
+ *
+ *   This locks, uses, and unlocks the file stream.
+ *
+ *   This does not alter print.custom.setting.state.status.
+ * @param set
+ *   The socket set relating to the message.
+ *
+ * @return
+ *   F_okay on success.
+ *   F_output_not on success, but no printing is performed.
+ *
+ *   F_output_not (with error bit) if setting is NULL.
+ */
+#ifndef _di_kt_tacocat_print_message_receive_operation_control_size_
+  extern f_status_t kt_tacocat_print_message_receive_operation_control_size(fl_print_t * const print, const kt_tacocat_socket_set_t set);
+#endif // _di_kt_tacocat_print_message_receive_operation_control_size_
+
+/**
+ * Print message about receive operation packet received.
+ *
+ * @param print
+ *   The output structure to print to.
+ *
+ *   This locks, uses, and unlocks the file stream.
+ *
+ *   This does not alter print.custom.setting.state.status.
+ * @param set
+ *   The socket set relating to the message.
+ *
+ * @return
+ *   F_okay on success.
+ *   F_output_not on success, but no printing is performed.
+ *
+ *   F_output_not (with error bit) if setting is NULL.
+ */
+#ifndef _di_kt_tacocat_print_message_receive_operation_received_
+  extern f_status_t kt_tacocat_print_message_receive_operation_received(fl_print_t * const print, const kt_tacocat_socket_set_t set);
+#endif // _di_kt_tacocat_print_message_receive_operation_received_
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
index 75e8e33b9141f406192e20c596022af62f243ec0..1c933f0a303c3f769fc0a43340ca0736aa2b4e6b 100644 (file)
@@ -65,18 +65,32 @@ extern "C" {
     kt_tacocat_socket_set_t * const set = &main->setting.receive.array[index];
 
     // This is a new packet (kt_tacocat_socket_flag_none_e).
-    if (!(main->setting.receive.array[index].flag)) {
-      main->setting.receive.array[index].flag = kt_tacocat_socket_flag_block_control_e;
-      main->setting.receive.array[index].retry = 0;
+    if (!(set->flag)) {
+      kt_tacocat_print_message_receive_operation_received(&main->program.message, *set);
+
+      set->flag = kt_tacocat_socket_flag_block_control_e;
+      set->retry = 0;
       set->buffer.used = 0;
       set->socket.size_read = kt_tacocat_packet_read_d;
+
+      set->status = f_file_open(set->name, F_file_mode_all_rw_d, &set->file);
+
+      if (F_status_is_error(set->status)) {
+        kt_tacocat_print_error_on_file(&main->program.error, macro_kt_tacocat_f(f_file_open), kt_tacocat_receive_s, set->network, set->status, set->name, f_file_operation_open_s);
+      }
     }
 
     // Load the header of the new packet.
-    if (main->setting.receive.array[index].flag & kt_tacocat_socket_flag_block_control_e) {
+    if (set->flag & kt_tacocat_socket_flag_block_control_e) {
       kt_tacocat_receive_process_control(main, index);
 
-      if (F_status_is_error(set->status) || set->buffer.used < kt_tacocat_packet_peek_d) {
+      if (set->buffer.used < kt_tacocat_packet_peek_d) {
+        f_file_close_id(&set->socket.id_data);
+
+        return;
+      }
+
+      if (set->buffer.used < kt_tacocat_packet_peek_d) {
         f_file_close_id(&set->socket.id_data);
 
         return;
@@ -88,24 +102,23 @@ extern "C" {
 
       // Make sure the buffer is large enough for payload processing block reads.
       set->status = f_memory_array_increase_by(set->socket.size_read, sizeof(f_char_t), (void **) &set->buffer.string, &set->buffer.used, &set->buffer.size);
-      macro_kt_receive_process_handle_error_exit_1(main, f_memory_array_increase_by, set->network, set->status, main->setting.receive.array[index].flag, &set->socket.id_data);
+      macro_kt_receive_process_handle_error_exit_1(main, f_memory_array_increase_by, set->network, set->status, set->flag, &set->socket.id_data);
     }
 
-    if (main->setting.receive.array[index].flag & kt_tacocat_socket_flag_block_payload_e) {
+    if (set->flag & kt_tacocat_socket_flag_block_payload_e) {
       size_t length_read = 0;
 
       set->status = f_socket_read_stream(&set->socket, 0, (void *) set->buffer.string, &length_read);
-      macro_kt_receive_process_handle_error_exit_1(main, f_socket_read_stream, set->network, set->status, main->setting.receive.array[index].flag, &set->socket.id_data);
+      macro_kt_receive_process_handle_error_exit_1(main, f_socket_read_stream, set->network, set->status, set->flag, &set->socket.id_data);
 
       if (length_read) {
         set->buffer.used = length_read;
 
-        set->status = f_file_write(main->setting.receive.array[index].file, set->buffer, 0);
+        set->status = f_file_write(set->file, set->buffer, 0);
 
         // Keep going on error, but in the future more advanced error handling/recovery is needed to make this more robust.
         if (F_status_is_error(set->status)) {
-          // @fixme The file name is not being saved, need to add the file name. For now just adding "set->socket.name" as a placeholder.
-          kt_tacocat_print_error_on_file(&main->program.error, macro_kt_tacocat_f(f_file_write), kt_tacocat_receive_s, set->network, set->status, set->socket.name);
+          kt_tacocat_print_error_on_file(&main->program.error, macro_kt_tacocat_f(f_file_write), kt_tacocat_receive_s, set->network, set->status, set->name, f_file_operation_write_s);
         }
 
         // Reset buffer used and increment counter.
@@ -114,15 +127,16 @@ extern "C" {
 
         f_file_close_id(&set->socket.id_data);
 
-        if (set->packet.payload.stop < set->packet.size) return;
+        if (set->packet.payload.stop + 1 < set->packet.size) return;
       }
-
-      main->setting.receive.array[index].flag -= kt_tacocat_socket_flag_block_payload_e;
     }
 
     // Done processing the Packet.
+    kt_tacocat_print_message_receive_operation_complete(&main->program.message, *set);
+
     f_file_close_id(&set->socket.id_data);
-    main->setting.receive.array[index].flag = 0;
+    f_file_close(&set->file);
+    set->flag = 0;
 
     if (set->buffer.size > kt_tacocat_max_maintain_d) {
       set->buffer.used = 0;
@@ -239,6 +253,8 @@ extern "C" {
     // The payload range "stop" is used to represent the total amount of bytes processed so far (uncluding the header).
     set->packet.payload.start = 0;
     set->packet.payload.stop = set->buffer.used - 1;
+
+    kt_tacocat_print_message_receive_operation_control_size(&main->program.message, *set);
   }
 #endif // _di_kt_tacocat_receive_process_control_