openc3-cosmos-cfdp 2.0.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +18 -0
  3. data/README.md +103 -55
  4. data/Rakefile +12 -0
  5. data/lib/cfdp.py +564 -0
  6. data/lib/cfdp.rb +68 -14
  7. data/lib/cfdp_api.py +302 -0
  8. data/lib/cfdp_api.rb +55 -9
  9. data/microservices/CFDP/Gemfile +18 -4
  10. data/microservices/CFDP/Gemfile.dev +22 -8
  11. data/microservices/CFDP/Gemfile.prod +18 -4
  12. data/microservices/CFDP/Rakefile +13 -2
  13. data/microservices/CFDP/app/controllers/application_controller.rb +2 -2
  14. data/microservices/CFDP/app/controllers/cfdp_controller.rb +62 -31
  15. data/microservices/CFDP/app/models/cfdp_checksum.rb +12 -4
  16. data/microservices/CFDP/app/models/cfdp_crc_checksum.rb +10 -2
  17. data/microservices/CFDP/app/models/cfdp_mib.rb +173 -32
  18. data/microservices/CFDP/app/models/cfdp_null_checksum.rb +10 -2
  19. data/microservices/CFDP/app/models/cfdp_pdu.rb +96 -20
  20. data/microservices/CFDP/app/models/cfdp_receive_transaction.rb +139 -11
  21. data/microservices/CFDP/app/models/cfdp_source_transaction.rb +433 -93
  22. data/microservices/CFDP/app/models/cfdp_topic.rb +2 -2
  23. data/microservices/CFDP/app/models/cfdp_transaction.rb +174 -4
  24. data/microservices/CFDP/app/models/cfdp_user.rb +216 -73
  25. data/microservices/CFDP/config/routes.rb +1 -0
  26. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_ack.rb +7 -6
  27. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_eof.rb +1 -2
  28. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_file_data.rb +2 -25
  29. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_finished.rb +8 -7
  30. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_keep_alive.rb +7 -6
  31. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_metadata.rb +1 -2
  32. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_nak.rb +7 -6
  33. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_prompt.rb +1 -2
  34. data/microservices/CFDP/spec/controllers/cfdp_controller_spec.rb +226 -1
  35. data/microservices/CFDP/spec/models/cfdp_checksum_spec.rb +239 -0
  36. data/microservices/CFDP/spec/models/cfdp_crc_checksum_spec.rb +16 -13
  37. data/microservices/CFDP/spec/models/cfdp_mib_spec.rb +880 -138
  38. data/microservices/CFDP/spec/models/cfdp_null_checksum_spec.rb +111 -0
  39. data/microservices/CFDP/spec/models/cfdp_pdu_file_data_spec.rb +48 -0
  40. data/microservices/CFDP/spec/models/cfdp_pdu_nak_spec.rb +2 -2
  41. data/microservices/CFDP/spec/models/cfdp_pdu_spec.rb +129 -0
  42. data/microservices/CFDP/spec/models/cfdp_receive_transaction_spec.rb +109 -10
  43. data/microservices/CFDP/spec/models/cfdp_source_transaction_spec.rb +357 -0
  44. data/microservices/CFDP/spec/models/cfdp_transaction_spec.rb +282 -5
  45. data/microservices/CFDP/spec/models/cfdp_user_spec.rb +462 -20
  46. data/microservices/CFDP/spec/requests/cfdp_spec.rb +92 -43
  47. data/microservices/CFDP/spec/spec_helper.rb +34 -3
  48. data/plugin.txt +55 -4
  49. data/public/store_img.png +0 -0
  50. data/pyproject.toml +70 -0
  51. data/targets/CFDPTEST/procedures/cfdp_test_suite.py +179 -0
  52. data/targets/CFDPTEST/procedures/cfdp_test_suite.rb +2 -2
  53. data/targets/CFDPTEST/procedures/interrupt_test.rb +31 -0
  54. metadata +25 -12
  55. data/LICENSE.txt +0 -18
@@ -36,6 +36,87 @@ class CfdpPdu < OpenC3::Packet
36
36
  DIRECTIVE_CODE_BYTE_SIZE = 1
37
37
  CRC_BYTE_SIZE = 2
38
38
 
39
+ # Crc16.new computes a 256 entry lookup table, which costs roughly 80x more than calculating
40
+ # a CRC over a whole PDU. The table only depends on the polynomial, and calc does not mutate
41
+ # the instance, so one shared instance serves every thread.
42
+ CRC16 = OpenC3::Crc16.new
43
+
44
+ # Defining the items of a packet costs roughly 40x more than reading or writing them, and a
45
+ # PDU is built or decommutated for every single file segment. Build one prototype per variant
46
+ # and clone it instead. Structure#clone copies the buffer but shares the item definitions,
47
+ # which is safe as long as a clone is only ever read from and written to, never redefined.
48
+ @@prototypes = {}
49
+ @@variable_header_prototypes = {}
50
+ @@file_data_prototypes = {}
51
+ @@prototype_mutex = Mutex.new
52
+
53
+ # Build the prototype for key once and return a clone of it. The key must capture every input
54
+ # that changes the layout, since everything sharing a key shares the item definitions.
55
+ def self.prototype(cache, key)
56
+ prototype = cache[key]
57
+ unless prototype
58
+ @@prototype_mutex.synchronize do
59
+ prototype = cache[key] ||= yield
60
+ end
61
+ end
62
+ return prototype
63
+ end
64
+
65
+ # A PDU with the standard items already defined. Equivalent to new(crcs_required:) but does
66
+ # not rebuild the item definitions.
67
+ def self.build(crcs_required:)
68
+ crcs_required = !!crcs_required
69
+ return prototype(@@prototypes, crcs_required) { new(crcs_required: crcs_required) }.clone
70
+ end
71
+
72
+ # The variable header layout depends only on the entity id and sequence number lengths and
73
+ # whether the PDU is a file directive, so it can be cached on those.
74
+ def self.build_variable_header_packet(id_length, seq_num_length, type)
75
+ key = [id_length, seq_num_length, type]
76
+ return prototype(@@variable_header_prototypes, key) do
77
+ define_variable_header_packet(id_length, seq_num_length, type)
78
+ end.clone
79
+ end
80
+
81
+ # The file data contents layout depends only on the protocol version, whether segment metadata
82
+ # is present, and the large file flag. This is on the per file segment path, so it matters most.
83
+ def self.build_file_data_packets(version, segment_metadata_flag, large_file_flag)
84
+ key = [version, segment_metadata_flag, large_file_flag]
85
+ metadata, contents = prototype(@@file_data_prototypes, key) do
86
+ define_file_data_packets(version, segment_metadata_flag, large_file_flag)
87
+ end
88
+ return [metadata&.clone, contents.clone]
89
+ end
90
+
91
+ def self.define_file_data_packets(version, segment_metadata_flag, large_file_flag)
92
+ s = nil
93
+ if version != 0 and segment_metadata_flag == "PRESENT"
94
+ s = OpenC3::Packet.new(nil, nil, :BIG_ENDIAN)
95
+ item = s.append_item("RECORD_CONTINUATION_STATE", 2, :UINT)
96
+ item.states = RECORD_CONTINUATION_STATES
97
+ s.append_item("SEGMENT_METADATA_LENGTH", 6, :UINT)
98
+ s.append_item("SEGMENT_METADATA", 0, :BLOCK)
99
+ end
100
+
101
+ s2 = OpenC3::Structure.new(:BIG_ENDIAN)
102
+ s2.append_item("OFFSET", large_file_flag == "SMALL_FILE" ? 32 : 64, :UINT)
103
+ s2.append_item("FILE_DATA", 0, :BLOCK)
104
+
105
+ return [s, s2]
106
+ end
107
+
108
+ def self.define_variable_header_packet(id_length, seq_num_length, type)
109
+ s = OpenC3::Packet.new(nil, nil, :BIG_ENDIAN)
110
+ s.append_item("SOURCE_ENTITY_ID", id_length * 8, :UINT)
111
+ s.append_item("SEQUENCE_NUMBER", seq_num_length * 8, :UINT, nil, :BIG_ENDIAN, :TRUNCATE)
112
+ s.append_item("DESTINATION_ENTITY_ID", id_length * 8, :UINT)
113
+ if type == "FILE_DIRECTIVE"
114
+ item = s.append_item("DIRECTIVE_CODE", 8, :UINT)
115
+ item.states = DIRECTIVE_CODES
116
+ end
117
+ return s
118
+ end
119
+
39
120
  def initialize(crcs_required:)
40
121
  super()
41
122
  append_item("VERSION", 3, :UINT)
@@ -68,7 +149,7 @@ class CfdpPdu < OpenC3::Packet
68
149
  pdu_hash = {}
69
150
  source_entity = CfdpMib.source_entity
70
151
  crcs_required = source_entity['crcs_required']
71
- pdu = new(crcs_required: crcs_required)
152
+ pdu = build(crcs_required: crcs_required)
72
153
  pdu.buffer = pdu_data
73
154
 
74
155
  # Handle CRC
@@ -76,12 +157,11 @@ class CfdpPdu < OpenC3::Packet
76
157
  if pdu_hash["CRC_FLAG"] == "CRC_PRESENT"
77
158
  unless crcs_required
78
159
  # Recreate with CRC
79
- pdu = new(crcs_required: true)
160
+ pdu = build(crcs_required: true)
80
161
  pdu.buffer = pdu_data
81
162
  end
82
163
  pdu_hash["CRC"] = pdu.read("CRC")
83
- crc16 = OpenC3::Crc16.new
84
- calculated = crc16.calc(pdu.buffer(false)[0..-3])
164
+ calculated = CRC16.calc(pdu.buffer(false)[0..-3])
85
165
  if pdu_hash["CRC"] != calculated
86
166
  raise "PDU with invalid CRC received: Received: #{sprintf("0x%04X", pdu_hash["CRC"])}, Calculated: #{sprintf("0x%04X", calculated)}"
87
167
  end
@@ -159,9 +239,13 @@ class CfdpPdu < OpenC3::Packet
159
239
  return pdu_hash
160
240
  end
161
241
 
162
- def self.build_initial_pdu(type:, destination_entity:, file_size:, segmentation_control: "NOT_PRESERVED", transmission_mode: nil)
163
- version = destination_entity['protocol_version_number']
164
- pdu = self.new(crcs_required: destination_entity['crcs_required'])
242
+ # remote_entity is the peer the PDU is being sent to. It defaults to destination_entity, but differs
243
+ # when the receiver replies to the file sender, where destination_entity names the receiver in the
244
+ # header. All PDUs are encoded with the peer's protocol version, CRC, and length settings.
245
+ def self.build_initial_pdu(type:, destination_entity:, file_size:, segmentation_control: "NOT_PRESERVED", transmission_mode: nil, remote_entity: nil)
246
+ remote_entity ||= destination_entity
247
+ version = remote_entity['protocol_version_number']
248
+ pdu = self.build(crcs_required: remote_entity['crcs_required'])
165
249
  pdu.write("VERSION", version)
166
250
  pdu.write("TYPE", type)
167
251
  pdu.write("DIRECTION", "TOWARD_FILE_RECEIVER")
@@ -169,9 +253,9 @@ class CfdpPdu < OpenC3::Packet
169
253
  transmission_mode = transmission_mode.upcase
170
254
  pdu.write("TRANSMISSION_MODE", transmission_mode)
171
255
  else
172
- pdu.write("TRANSMISSION_MODE", destination_entity['default_transmission_mode'].upcase)
256
+ pdu.write("TRANSMISSION_MODE", remote_entity['default_transmission_mode'].upcase)
173
257
  end
174
- if destination_entity['crcs_required']
258
+ if remote_entity['crcs_required']
175
259
  pdu.write("CRC_FLAG", "CRC_PRESENT")
176
260
  else
177
261
  pdu.write("CRC_FLAG", "CRC_NOT_PRESENT")
@@ -189,9 +273,9 @@ class CfdpPdu < OpenC3::Packet
189
273
  else
190
274
  pdu.write("SEGMENTATION_CONTROL", segmentation_control)
191
275
  end
192
- pdu.write("ENTITY_ID_LENGTH", destination_entity['entity_id_length'])
276
+ pdu.write("ENTITY_ID_LENGTH", remote_entity['entity_id_length'])
193
277
  pdu.write("SEGMENT_METADATA_FLAG", "NOT_PRESENT") # Not implemented - always 0
194
- pdu.write("SEQUENCE_NUMBER_LENGTH", destination_entity['sequence_number_length'])
278
+ pdu.write("SEQUENCE_NUMBER_LENGTH", remote_entity['sequence_number_length'])
195
279
  return pdu
196
280
  end
197
281
 
@@ -199,15 +283,7 @@ class CfdpPdu < OpenC3::Packet
199
283
  id_length = read("ENTITY_ID_LENGTH") + 1
200
284
  seq_num_length = read("SEQUENCE_NUMBER_LENGTH") + 1
201
285
  type = read("TYPE")
202
- s = OpenC3::Packet.new(nil, nil, :BIG_ENDIAN)
203
- s.append_item("SOURCE_ENTITY_ID", id_length * 8, :UINT)
204
- s.append_item("SEQUENCE_NUMBER", seq_num_length * 8, :UINT, nil, :BIG_ENDIAN, :TRUNCATE)
205
- s.append_item("DESTINATION_ENTITY_ID", id_length * 8, :UINT)
206
- if type == "FILE_DIRECTIVE"
207
- item = s.append_item("DIRECTIVE_CODE", 8, :UINT)
208
- item.states = DIRECTIVE_CODES
209
- end
210
- return s
286
+ return self.class.build_variable_header_packet(id_length, seq_num_length, type)
211
287
  end
212
288
 
213
289
  def build_variable_header(source_entity_id:, transaction_seq_num:, destination_entity_id:, directive_code: nil)
@@ -18,9 +18,10 @@
18
18
  # See https://github.com/OpenC3/openc3-cosmos-cfdp/pull/12 for details
19
19
 
20
20
  require_relative 'cfdp_transaction'
21
+ require 'openc3/io/json_rpc'
21
22
 
22
23
  class CfdpReceiveTransaction < CfdpTransaction
23
- def initialize(pdu_hash)
24
+ def initialize(pdu_hash, no_persist: false)
24
25
  super()
25
26
  @id = CfdpTransaction.build_transaction_id(pdu_hash["SOURCE_ENTITY_ID"], pdu_hash["SEQUENCE_NUMBER"])
26
27
  @transaction_seq_num = pdu_hash["SEQUENCE_NUMBER"]
@@ -44,12 +45,14 @@ class CfdpReceiveTransaction < CfdpTransaction
44
45
  @nak_start_of_scope = 0
45
46
  @keep_alive_count = 0
46
47
  @finished_count = 0
47
- @source_entity_id = nil
48
+ # Every PDU header carries the source entity so this is known even if the METADATA PDU
49
+ # has not been received yet
50
+ @source_entity_id = pdu_hash["SOURCE_ENTITY_ID"]
48
51
  @inactivity_timeout = nil
49
52
  @inactivity_count = 0
50
53
  @keep_alive_timeout = nil
51
54
  CfdpMib.transactions[@id] = self
52
- handle_pdu(pdu_hash)
55
+ handle_pdu(pdu_hash, no_persist: no_persist)
53
56
  @inactivity_timeout = Time.now + CfdpMib.entity(@source_entity_id)['keep_alive_interval']
54
57
  @keep_alive_timeout = Time.now + CfdpMib.entity(@source_entity_id)['keep_alive_interval'] if @transmission_mode == 'ACKNOWLEDGED' and CfdpMib.entity(@source_entity_id)['enable_keep_alive']
55
58
  end
@@ -166,6 +169,7 @@ class CfdpReceiveTransaction < CfdpTransaction
166
169
  raise "cmd_info not defined for source entity: #{@metadata_pdu_hash['SOURCE_ENTITY_ID']}" unless target_name and packet_name and item_name
167
170
  @finished_pdu = CfdpPdu.build_finished_pdu(
168
171
  source_entity: source_entity,
172
+ remote_entity: source_entity,
169
173
  transaction_seq_num: @transaction_seq_num,
170
174
  destination_entity: destination_entity,
171
175
  condition_code: @condition_code,
@@ -188,6 +192,7 @@ class CfdpReceiveTransaction < CfdpTransaction
188
192
  @state = "FINISHED" unless @state == "CANCELED" or @state == "ABANDONED"
189
193
  @transaction_status = "TERMINATED"
190
194
  @complete_time = Time.now.utc
195
+ save_state
191
196
  OpenC3::Logger.info("CFDP Finished Receive Transaction #{@id}, #{@condition_code}", scope: ENV['OPENC3_SCOPE'])
192
197
 
193
198
  if CfdpMib.source_entity['transaction_finished_indication']
@@ -201,6 +206,7 @@ class CfdpReceiveTransaction < CfdpTransaction
201
206
 
202
207
  def complete_file_received?
203
208
  return false unless @file_size
209
+ return true if @file_size == 0
204
210
  offset = 0
205
211
  while offset
206
212
  next_offset = @segments[offset]
@@ -217,6 +223,10 @@ class CfdpReceiveTransaction < CfdpTransaction
217
223
  end
218
224
  end
219
225
  end
226
+ # Every segment must advance the offset. A segment that does not (which a zero length or
227
+ # corrupt File Data PDU would produce) makes this walk loop forever at 100% CPU, hanging
228
+ # the receive thread for the life of the process since @segments is persisted.
229
+ break if next_offset and next_offset <= offset
220
230
  offset = next_offset
221
231
  end
222
232
  return false
@@ -318,12 +328,13 @@ class CfdpReceiveTransaction < CfdpTransaction
318
328
  end
319
329
 
320
330
  def send_keep_alive
321
- source_entity = CfdpMib.entity(@metadata_pdu_hash['SOURCE_ENTITY_ID'])
331
+ source_entity = CfdpMib.entity(@source_entity_id)
322
332
  destination_entity = CfdpMib.source_entity
323
333
  target_name, packet_name, item_name = source_entity["cmd_info"]
324
334
 
325
335
  keep_alive_pdu = CfdpPdu.build_keep_alive_pdu(
326
336
  source_entity: source_entity,
337
+ remote_entity: source_entity,
327
338
  transaction_seq_num: @transaction_seq_num,
328
339
  destination_entity: destination_entity,
329
340
  file_size: @file_size,
@@ -413,6 +424,7 @@ class CfdpReceiveTransaction < CfdpTransaction
413
424
  if start_of_scope != end_of_scope
414
425
  nak_pdu = CfdpPdu.build_nak_pdu(
415
426
  source_entity: source_entity,
427
+ remote_entity: source_entity,
416
428
  transaction_seq_num: @transaction_seq_num,
417
429
  destination_entity: destination_entity,
418
430
  file_size: @file_size,
@@ -431,14 +443,21 @@ class CfdpReceiveTransaction < CfdpTransaction
431
443
  end
432
444
  end
433
445
 
434
- def handle_pdu(pdu_hash)
446
+ def handle_pdu(pdu_hash, no_persist: false)
447
+ # The no_persist option is used when a transaction is being revived from a saved state
448
+ # (see: CfdpMib.load_saved_transactions). It prevents overwriting the saved state in the
449
+ # backing store with default values when the constructor calls handle_pdu.
450
+
435
451
  source_entity = CfdpMib.entity(@source_entity_id)
436
452
  @inactivity_timeout = Time.now + source_entity['keep_alive_interval'] if source_entity
437
453
 
438
454
  case pdu_hash["DIRECTIVE_CODE"]
439
455
  when "METADATA"
440
456
  @metadata_pdu_count += 1
441
- return if @metadata_pdu_hash # Discard repeats
457
+ if @metadata_pdu_hash # Discard repeats
458
+ save_state if @id and not no_persist
459
+ return
460
+ end
442
461
  @metadata_pdu_hash = pdu_hash
443
462
  @source_entity_id = @metadata_pdu_hash['SOURCE_ENTITY_ID']
444
463
  kw_args = {}
@@ -511,12 +530,13 @@ class CfdpReceiveTransaction < CfdpTransaction
511
530
  CfdpTopic.write_indication("EOF-Recv", transaction_id: @id) if CfdpMib.source_entity['eof_recv_indication']
512
531
 
513
532
  destination_entity = CfdpMib.source_entity
514
- source_entity = CfdpMib.entity(@metadata_pdu_hash['SOURCE_ENTITY_ID'])
533
+ source_entity = CfdpMib.entity(@source_entity_id)
515
534
  if @transmission_mode == "ACKNOWLEDGED" and source_entity['enable_acks']
516
535
  target_name, packet_name, item_name = source_entity["cmd_info"]
517
536
  # Ack EOF PDU
518
537
  ack_pdu = CfdpPdu.build_ack_pdu(
519
538
  source_entity: source_entity,
539
+ remote_entity: source_entity,
520
540
  transaction_seq_num: @transaction_seq_num,
521
541
  destination_entity: destination_entity,
522
542
  segmentation_control: "NOT_PRESERVED",
@@ -558,11 +578,16 @@ class CfdpReceiveTransaction < CfdpTransaction
558
578
  end
559
579
 
560
580
  else # File Data
561
- @source_entity_id = @metadata_pdu_hash['SOURCE_ENTITY_ID']
562
-
563
- @tmp_file ||= Tempfile.new('cfdp', binmode: true)
564
581
  offset = pdu_hash['OFFSET']
565
582
  file_data = pdu_hash['FILE_DATA']
583
+ # A zero length segment carries no data and would be recorded in @segments as a segment
584
+ # that does not advance the offset, which makes complete_file_received? loop forever
585
+ if file_data.nil? or file_data.length == 0
586
+ OpenC3::Logger.warn("CFDP Receive Transaction #{@id} ignoring zero length file data PDU at offset #{offset}", scope: ENV['OPENC3_SCOPE'])
587
+ return
588
+ end
589
+
590
+ @tmp_file ||= Tempfile.new('cfdp', binmode: true)
566
591
  progress = offset + file_data.length
567
592
 
568
593
  need_send_naks = false
@@ -575,7 +600,9 @@ class CfdpReceiveTransaction < CfdpTransaction
575
600
 
576
601
  # Ignore repeated segments
577
602
  if !@segments[offset] or @segments[offset] != progress
578
- if @file_size and progress > @file_size
603
+ # The file size is only known once the METADATA or EOF PDU has been received. Until then
604
+ # @file_size is still 0 and must not be used to declare a file size error.
605
+ if (@metadata_pdu_hash or @eof_pdu_hash) and @file_size and progress > @file_size
579
606
  @condition_code = "FILE_SIZE_ERROR"
580
607
  handle_fault()
581
608
  else
@@ -592,5 +619,106 @@ class CfdpReceiveTransaction < CfdpTransaction
592
619
 
593
620
  send_naks() if need_send_naks
594
621
  end
622
+
623
+ save_state if @id and not no_persist
624
+ end
625
+
626
+ def save_state
627
+ state_data = {
628
+ 'id' => @id,
629
+ 'frozen' => @frozen,
630
+ 'state' => @state,
631
+ 'transaction_status' => @transaction_status,
632
+ 'progress' => @progress,
633
+ 'transaction_seq_num' => @transaction_seq_num,
634
+ 'condition_code' => @condition_code,
635
+ 'delivery_code' => @delivery_code,
636
+ 'file_status' => @file_status,
637
+ 'metadata_pdu_hash' => @metadata_pdu_hash,
638
+ 'metadata_pdu_count' => @metadata_pdu_count,
639
+ 'create_time' => @create_time&.iso8601(6),
640
+ 'proxy_response_info' => @proxy_response_info,
641
+ 'proxy_response_needed' => @proxy_response_needed,
642
+ 'canceling_entity_id' => @canceling_entity_id,
643
+ 'fault_handler_overrides' => @fault_handler_overrides,
644
+ 'source_file_name' => @source_file_name,
645
+ 'destination_file_name' => @destination_file_name,
646
+ 'transmission_mode' => @transmission_mode,
647
+ 'messages_to_user' => @messages_to_user,
648
+ 'filestore_requests' => @filestore_requests,
649
+ 'tmp_file_path' => @tmp_file&.path,
650
+ 'segments' => @segments,
651
+ 'eof_pdu_hash' => @eof_pdu_hash,
652
+ 'checksum' => @checksum&.as_json,
653
+ 'full_checksum_needed' => @full_checksum_needed,
654
+ 'file_size' => @file_size,
655
+ 'filestore_responses' => @filestore_responses,
656
+ 'nak_timeout' => @nak_timeout&.iso8601(6),
657
+ 'nak_timeout_count' => @nak_timeout_count,
658
+ 'check_timeout' => @check_timeout&.iso8601(6),
659
+ 'check_timeout_count' => @check_timeout_count,
660
+ 'nak_start_of_scope' => @nak_start_of_scope,
661
+ 'keep_alive_count' => @keep_alive_count,
662
+ 'finished_count' => @finished_count,
663
+ 'source_entity_id' => @source_entity_id,
664
+ 'inactivity_timeout' => @inactivity_timeout&.iso8601(6),
665
+ 'inactivity_count' => @inactivity_count,
666
+ 'keep_alive_timeout' => @keep_alive_timeout&.iso8601(6),
667
+ 'finished_ack_timeout' => @finished_ack_timeout&.iso8601(6),
668
+ 'finished_pdu' => @finished_pdu,
669
+ 'finished_ack_pdu_hash' => @finished_ack_pdu_hash,
670
+ 'prompt_pdu_hash' => @prompt_pdu_hash
671
+ }
672
+ state_data.compact!
673
+
674
+ # Store as JSON to handle all data types safely
675
+ serialized_data = JSON.generate(state_data.as_json, allow_nan: true)
676
+ OpenC3::Store.set("#{self.class.redis_key_prefix}cfdp_transaction_state:#{@id}", serialized_data)
677
+ OpenC3::Store.sadd("#{self.class.redis_key_prefix}cfdp_saved_transaction_ids", @id)
678
+ end
679
+
680
+ def load_state(transaction_id)
681
+ state_data = super(transaction_id, no_log: true)
682
+ return false unless state_data
683
+
684
+ # Load receive-specific state
685
+ @transmission_mode = state_data['transmission_mode']
686
+ @messages_to_user = state_data['messages_to_user'] || []
687
+ @filestore_requests = state_data['filestore_requests'] || []
688
+
689
+ if state_data['tmp_file_path']
690
+ begin
691
+ @tmp_file = File.open(File.basename(state_data['tmp_file_path']), 'r+b')
692
+ rescue
693
+ @tmp_file = nil
694
+ end
695
+ else
696
+ @tmp_file = nil
697
+ end
698
+
699
+ @segments = state_data['segments']&.transform_keys(&:to_i) || {}
700
+ @eof_pdu_hash = state_data['eof_pdu_hash']
701
+ @checksum = state_data['checksum']
702
+ @full_checksum_needed = state_data['full_checksum_needed']
703
+ @file_size = state_data['file_size'] || 0
704
+ @filestore_responses = state_data['filestore_responses'] || []
705
+ @nak_timeout = state_data['nak_timeout'] ? Time.parse(state_data['nak_timeout']) : nil
706
+ @nak_timeout_count = state_data['nak_timeout_count'] || 0
707
+ @check_timeout = state_data['check_timeout'] ? Time.parse(state_data['check_timeout']) : nil
708
+ @check_timeout_count = state_data['check_timeout_count'] || 0
709
+ @nak_start_of_scope = state_data['nak_start_of_scope'] || 0
710
+ @keep_alive_count = state_data['keep_alive_count'] || 0
711
+ @finished_count = state_data['finished_count'] || 0
712
+ @source_entity_id = state_data['source_entity_id']
713
+ @inactivity_timeout = state_data['inactivity_timeout'] ? Time.parse(state_data['inactivity_timeout']) : nil
714
+ @inactivity_count = state_data['inactivity_count'] || 0
715
+ @keep_alive_timeout = state_data['keep_alive_timeout'] ? Time.parse(state_data['keep_alive_timeout']) : nil
716
+ @finished_ack_timeout = state_data['finished_ack_timeout'] ? Time.parse(state_data['finished_ack_timeout']) : nil
717
+ @finished_pdu = state_data['finished_pdu']
718
+ @finished_ack_pdu_hash = state_data['finished_ack_pdu_hash']
719
+ @prompt_pdu_hash = state_data['prompt_pdu_hash']
720
+
721
+ OpenC3::Logger.info("CFDP Transaction #{@id} state loaded", scope: ENV['OPENC3_SCOPE'])
722
+ return state_data
595
723
  end
596
724
  end