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.
- checksums.yaml +4 -4
- data/LICENSE.md +18 -0
- data/README.md +103 -55
- data/Rakefile +12 -0
- data/lib/cfdp.py +564 -0
- data/lib/cfdp.rb +68 -14
- data/lib/cfdp_api.py +302 -0
- data/lib/cfdp_api.rb +55 -9
- data/microservices/CFDP/Gemfile +18 -4
- data/microservices/CFDP/Gemfile.dev +22 -8
- data/microservices/CFDP/Gemfile.prod +18 -4
- data/microservices/CFDP/Rakefile +13 -2
- data/microservices/CFDP/app/controllers/application_controller.rb +2 -2
- data/microservices/CFDP/app/controllers/cfdp_controller.rb +62 -31
- data/microservices/CFDP/app/models/cfdp_checksum.rb +12 -4
- data/microservices/CFDP/app/models/cfdp_crc_checksum.rb +10 -2
- data/microservices/CFDP/app/models/cfdp_mib.rb +173 -32
- data/microservices/CFDP/app/models/cfdp_null_checksum.rb +10 -2
- data/microservices/CFDP/app/models/cfdp_pdu.rb +96 -20
- data/microservices/CFDP/app/models/cfdp_receive_transaction.rb +139 -11
- data/microservices/CFDP/app/models/cfdp_source_transaction.rb +433 -93
- data/microservices/CFDP/app/models/cfdp_topic.rb +2 -2
- data/microservices/CFDP/app/models/cfdp_transaction.rb +174 -4
- data/microservices/CFDP/app/models/cfdp_user.rb +216 -73
- data/microservices/CFDP/config/routes.rb +1 -0
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_ack.rb +7 -6
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_eof.rb +1 -2
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_file_data.rb +2 -25
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_finished.rb +8 -7
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_keep_alive.rb +7 -6
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_metadata.rb +1 -2
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_nak.rb +7 -6
- data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_prompt.rb +1 -2
- data/microservices/CFDP/spec/controllers/cfdp_controller_spec.rb +226 -1
- data/microservices/CFDP/spec/models/cfdp_checksum_spec.rb +239 -0
- data/microservices/CFDP/spec/models/cfdp_crc_checksum_spec.rb +16 -13
- data/microservices/CFDP/spec/models/cfdp_mib_spec.rb +880 -138
- data/microservices/CFDP/spec/models/cfdp_null_checksum_spec.rb +111 -0
- data/microservices/CFDP/spec/models/cfdp_pdu_file_data_spec.rb +48 -0
- data/microservices/CFDP/spec/models/cfdp_pdu_nak_spec.rb +2 -2
- data/microservices/CFDP/spec/models/cfdp_pdu_spec.rb +129 -0
- data/microservices/CFDP/spec/models/cfdp_receive_transaction_spec.rb +109 -10
- data/microservices/CFDP/spec/models/cfdp_source_transaction_spec.rb +357 -0
- data/microservices/CFDP/spec/models/cfdp_transaction_spec.rb +282 -5
- data/microservices/CFDP/spec/models/cfdp_user_spec.rb +462 -20
- data/microservices/CFDP/spec/requests/cfdp_spec.rb +92 -43
- data/microservices/CFDP/spec/spec_helper.rb +34 -3
- data/plugin.txt +55 -4
- data/public/store_img.png +0 -0
- data/pyproject.toml +70 -0
- data/targets/CFDPTEST/procedures/cfdp_test_suite.py +179 -0
- data/targets/CFDPTEST/procedures/cfdp_test_suite.rb +2 -2
- data/targets/CFDPTEST/procedures/interrupt_test.rb +31 -0
- metadata +25 -12
- data/LICENSE.txt +0 -18
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# encoding: ascii-8bit
|
|
2
2
|
|
|
3
|
-
# Copyright
|
|
3
|
+
# Copyright 2025 OpenC3, Inc.
|
|
4
4
|
# All Rights Reserved.
|
|
5
5
|
#
|
|
6
6
|
# Licensed for Evaluation and Educational Use
|
|
@@ -23,6 +23,13 @@ require_relative 'cfdp_checksum'
|
|
|
23
23
|
require_relative 'cfdp_null_checksum'
|
|
24
24
|
require_relative 'cfdp_crc_checksum'
|
|
25
25
|
require 'tempfile'
|
|
26
|
+
require 'json'
|
|
27
|
+
require 'openc3/io/json_rpc'
|
|
28
|
+
begin
|
|
29
|
+
require 'json/add/string'
|
|
30
|
+
rescue LoadError
|
|
31
|
+
# Earlier json didn't have this file
|
|
32
|
+
end
|
|
26
33
|
|
|
27
34
|
class CfdpTransaction
|
|
28
35
|
include OpenC3::Api
|
|
@@ -42,10 +49,46 @@ class CfdpTransaction
|
|
|
42
49
|
attr_accessor :proxy_response_info
|
|
43
50
|
attr_accessor :proxy_response_needed
|
|
44
51
|
|
|
52
|
+
def self.redis_key_prefix
|
|
53
|
+
ENV['OPENC3_MICROSERVICE_NAME'] ? "#{ENV['OPENC3_MICROSERVICE_NAME']}:" : ""
|
|
54
|
+
end
|
|
55
|
+
|
|
45
56
|
def self.build_transaction_id(source_entity_id, transaction_seq_num)
|
|
46
57
|
"#{source_entity_id}__#{transaction_seq_num}"
|
|
47
58
|
end
|
|
48
59
|
|
|
60
|
+
def self.get_saved_transaction_ids
|
|
61
|
+
OpenC3::Store.smembers("#{redis_key_prefix}cfdp_saved_transaction_ids") || []
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.clear_saved_transaction_ids
|
|
65
|
+
transaction_ids = OpenC3::Store.smembers("#{redis_key_prefix}cfdp_saved_transaction_ids") || []
|
|
66
|
+
transaction_ids.each do |transaction_id|
|
|
67
|
+
OpenC3::Store.del("#{redis_key_prefix}cfdp_transaction_state:#{transaction_id}")
|
|
68
|
+
OpenC3::Store.del("#{redis_key_prefix}cfdp_transaction_state:#{transaction_id}:put_options")
|
|
69
|
+
end
|
|
70
|
+
OpenC3::Store.del("#{redis_key_prefix}cfdp_saved_transaction_ids")
|
|
71
|
+
OpenC3::Logger.info("CFDP cleared all saved transaction states", scope: ENV['OPENC3_SCOPE'])
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.has_saved_state?(transaction_id)
|
|
75
|
+
OpenC3::Store.sismember("#{redis_key_prefix}cfdp_saved_transaction_ids", transaction_id)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def delete
|
|
79
|
+
CfdpMib.transactions.delete @id
|
|
80
|
+
remove_saved_state
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def remove_saved_state
|
|
84
|
+
if @id
|
|
85
|
+
OpenC3::Store.del("#{self.class.redis_key_prefix}cfdp_transaction_state:#{@id}:put_options")
|
|
86
|
+
OpenC3::Store.del("#{self.class.redis_key_prefix}cfdp_transaction_state:#{@id}")
|
|
87
|
+
OpenC3::Store.srem("#{self.class.redis_key_prefix}cfdp_saved_transaction_ids", @id)
|
|
88
|
+
OpenC3::Logger.info("CFDP Transaction #{@id} state removed", scope: ENV['OPENC3_SCOPE'])
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
49
92
|
def initialize
|
|
50
93
|
@frozen = false
|
|
51
94
|
@state = "ACTIVE" # ACTIVE, FINISHED, CANCELED, SUSPENDED, ABANDONED
|
|
@@ -63,6 +106,47 @@ class CfdpTransaction
|
|
|
63
106
|
@destination_file_name = nil
|
|
64
107
|
@create_time = Time.now.utc
|
|
65
108
|
@complete_time = nil
|
|
109
|
+
# Set by request_shutdown to ask a transaction running in its own thread to stop at its next
|
|
110
|
+
# checkpoint. Written and read while holding @state_mutex by the waits below, but also read
|
|
111
|
+
# without the mutex by shutdown? in the middle of the copy loop.
|
|
112
|
+
@shutdown = false
|
|
113
|
+
# Broadcast whenever @state, @frozen or @shutdown changes so a suspended transfer wakes as
|
|
114
|
+
# soon as it is resumed instead of waiting out a poll interval
|
|
115
|
+
@state_mutex = Mutex.new
|
|
116
|
+
@state_changed = ConditionVariable.new
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def signal_state_change
|
|
120
|
+
@state_mutex.synchronize { @state_changed.broadcast }
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Ask the thread running this transaction to stop at its next checkpoint. The thread then
|
|
124
|
+
# unwinds normally, closing its source file and saving its state, so the transfer can be
|
|
125
|
+
# resumed when the microservice comes back. This replaces killing the thread, which could
|
|
126
|
+
# leave a half written state in Redis, a leaked file handle, or a PDU partially sent.
|
|
127
|
+
def request_shutdown
|
|
128
|
+
@state_mutex.synchronize do
|
|
129
|
+
@shutdown = true
|
|
130
|
+
@state_changed.broadcast
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def shutdown?
|
|
135
|
+
@shutdown
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Allow a transaction that was shut down to run again, used when a transfer is resumed in the
|
|
139
|
+
# same process that stopped it.
|
|
140
|
+
def clear_shutdown
|
|
141
|
+
@state_mutex.synchronize { @shutdown = false }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Wait up to timeout seconds for a state change. Returns immediately once shutdown has been
|
|
145
|
+
# requested so nothing blocks the microservice from stopping.
|
|
146
|
+
def wait_for_state_change(timeout)
|
|
147
|
+
@state_mutex.synchronize do
|
|
148
|
+
@state_changed.wait(@state_mutex, timeout) unless @shutdown
|
|
149
|
+
end
|
|
66
150
|
end
|
|
67
151
|
|
|
68
152
|
def as_json(*args)
|
|
@@ -88,6 +172,7 @@ class CfdpTransaction
|
|
|
88
172
|
@state = "SUSPENDED"
|
|
89
173
|
CfdpTopic.write_indication("Suspended", transaction_id: @id, condition_code: @condition_code) if CfdpMib.source_entity['suspended_indication']
|
|
90
174
|
end
|
|
175
|
+
signal_state_change()
|
|
91
176
|
end
|
|
92
177
|
|
|
93
178
|
def resume
|
|
@@ -98,6 +183,7 @@ class CfdpTransaction
|
|
|
98
183
|
@inactivity_timeout = Time.now + CfdpMib.source_entity['keep_alive_interval']
|
|
99
184
|
CfdpTopic.write_indication("Resumed", transaction_id: @id, progress: @progress) if CfdpMib.source_entity['resume_indication']
|
|
100
185
|
end
|
|
186
|
+
signal_state_change()
|
|
101
187
|
end
|
|
102
188
|
|
|
103
189
|
def cancel(canceling_entity_id = nil)
|
|
@@ -112,7 +198,9 @@ class CfdpTransaction
|
|
|
112
198
|
@state = "CANCELED"
|
|
113
199
|
@transaction_status = "TERMINATED"
|
|
114
200
|
@complete_time = Time.now.utc
|
|
201
|
+
save_state
|
|
115
202
|
end
|
|
203
|
+
signal_state_change()
|
|
116
204
|
end
|
|
117
205
|
|
|
118
206
|
def abandon
|
|
@@ -122,7 +210,9 @@ class CfdpTransaction
|
|
|
122
210
|
@transaction_status = "TERMINATED"
|
|
123
211
|
CfdpTopic.write_indication("Abandoned", transaction_id: @id, condition_code: @condition_code, progress: @progress)
|
|
124
212
|
@complete_time = Time.now.utc
|
|
213
|
+
save_state
|
|
125
214
|
end
|
|
215
|
+
signal_state_change()
|
|
126
216
|
end
|
|
127
217
|
|
|
128
218
|
def report
|
|
@@ -131,12 +221,14 @@ class CfdpTransaction
|
|
|
131
221
|
|
|
132
222
|
def freeze
|
|
133
223
|
OpenC3::Logger.info("CFDP Freeze Transaction #{@id}", scope: ENV['OPENC3_SCOPE'])
|
|
134
|
-
@
|
|
224
|
+
@frozen = true
|
|
225
|
+
signal_state_change()
|
|
135
226
|
end
|
|
136
227
|
|
|
137
228
|
def unfreeze
|
|
138
229
|
OpenC3::Logger.info("CFDP Unfreeze Transaction #{@id}", scope: ENV['OPENC3_SCOPE'])
|
|
139
|
-
@
|
|
230
|
+
@frozen = false
|
|
231
|
+
signal_state_change()
|
|
140
232
|
end
|
|
141
233
|
|
|
142
234
|
def build_report
|
|
@@ -195,8 +287,86 @@ class CfdpTransaction
|
|
|
195
287
|
end
|
|
196
288
|
end
|
|
197
289
|
|
|
290
|
+
def save_state
|
|
291
|
+
state_data = {
|
|
292
|
+
'id' => @id,
|
|
293
|
+
'frozen' => @frozen,
|
|
294
|
+
'state' => @state,
|
|
295
|
+
'transaction_status' => @transaction_status,
|
|
296
|
+
'progress' => @progress,
|
|
297
|
+
'transaction_seq_num' => @transaction_seq_num,
|
|
298
|
+
'condition_code' => @condition_code,
|
|
299
|
+
'delivery_code' => @delivery_code,
|
|
300
|
+
'file_status' => @file_status,
|
|
301
|
+
'metadata_pdu_hash' => @metadata_pdu_hash,
|
|
302
|
+
'metadata_pdu_count' => @metadata_pdu_count,
|
|
303
|
+
'create_time' => @create_time&.iso8601(6),
|
|
304
|
+
'proxy_response_info' => @proxy_response_info,
|
|
305
|
+
'proxy_response_needed' => @proxy_response_needed,
|
|
306
|
+
'canceling_entity_id' => @canceling_entity_id,
|
|
307
|
+
'fault_handler_overrides' => @fault_handler_overrides,
|
|
308
|
+
'source_file_name' => @source_file_name,
|
|
309
|
+
'destination_file_name' => @destination_file_name
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
# Remove nil values to clean up data
|
|
313
|
+
state_data.compact!
|
|
314
|
+
|
|
315
|
+
# Store as JSON to handle all data types safely
|
|
316
|
+
serialized_data = JSON.generate(state_data.as_json, allow_nan: true)
|
|
317
|
+
OpenC3::Store.set("#{self.class.redis_key_prefix}cfdp_transaction_state:#{@id}", serialized_data)
|
|
318
|
+
OpenC3::Store.sadd("#{self.class.redis_key_prefix}cfdp_saved_transaction_ids", @id)
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def load_state(transaction_id, no_log: false)
|
|
322
|
+
serialized_data = OpenC3::Store.get("#{self.class.redis_key_prefix}cfdp_transaction_state:#{transaction_id}")
|
|
323
|
+
return false unless serialized_data
|
|
324
|
+
|
|
325
|
+
begin
|
|
326
|
+
state_data = JSON.parse(serialized_data, allow_nan: true, create_additions: true)
|
|
327
|
+
rescue => e
|
|
328
|
+
OpenC3::Logger.error("CFDP Transaction #{transaction_id} failed to deserialize state: #{e.message}", scope: ENV['OPENC3_SCOPE'])
|
|
329
|
+
return false
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
@id = state_data['id']
|
|
333
|
+
@frozen = state_data['frozen']
|
|
334
|
+
@state = state_data['state'] || 'ACTIVE'
|
|
335
|
+
@transaction_status = state_data['transaction_status'] || 'ACTIVE'
|
|
336
|
+
@progress = state_data['progress'] || 0
|
|
337
|
+
@transaction_seq_num = state_data['transaction_seq_num']
|
|
338
|
+
@condition_code = state_data['condition_code'] || 'NO_ERROR'
|
|
339
|
+
@delivery_code = state_data['delivery_code']
|
|
340
|
+
@file_status = state_data['file_status']
|
|
341
|
+
@metadata_pdu_hash = state_data['metadata_pdu_hash']
|
|
342
|
+
@metadata_pdu_count = state_data['metadata_pdu_count'] || 0
|
|
343
|
+
@create_time = state_data['create_time'] ? Time.parse(state_data['create_time']) : nil
|
|
344
|
+
@complete_time = nil # Completed transactions are not persisted
|
|
345
|
+
@proxy_response_info = state_data['proxy_response_info']
|
|
346
|
+
@proxy_response_needed = state_data['proxy_response_needed']
|
|
347
|
+
@canceling_entity_id = state_data['canceling_entity_id']
|
|
348
|
+
@fault_handler_overrides = state_data['fault_handler_overrides'] || {}
|
|
349
|
+
@source_file_name = state_data['source_file_name']
|
|
350
|
+
@destination_file_name = state_data['destination_file_name']
|
|
351
|
+
|
|
352
|
+
OpenC3::Logger.info("CFDP Transaction #{@id} state loaded", scope: ENV['OPENC3_SCOPE']) unless no_log
|
|
353
|
+
return state_data
|
|
354
|
+
end
|
|
355
|
+
|
|
198
356
|
def cfdp_cmd(entity, target_name, packet_name, cmd_params, scope: ENV['OPENC3_SCOPE'])
|
|
199
357
|
cmd(target_name, packet_name, cmd_params, scope: ENV['OPENC3_SCOPE'])
|
|
200
|
-
|
|
358
|
+
cmd_delay(entity['cmd_delay']) if entity['cmd_delay']
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
# The inter command delay is a rate limit, so a state change that is not a shutdown must not
|
|
362
|
+
# shorten it. Waiting for the remaining time rather than sleeping means a shutdown request
|
|
363
|
+
# does not have to wait out the delay of every remaining PDU.
|
|
364
|
+
def cmd_delay(delay)
|
|
365
|
+
deadline = Time.now + delay
|
|
366
|
+
while true
|
|
367
|
+
remaining = deadline - Time.now
|
|
368
|
+
break if remaining <= 0 or shutdown?
|
|
369
|
+
wait_for_state_change(remaining)
|
|
370
|
+
end
|
|
201
371
|
end
|
|
202
372
|
end
|
|
@@ -26,12 +26,25 @@ require_relative 'cfdp_mib'
|
|
|
26
26
|
require_relative 'cfdp_receive_transaction'
|
|
27
27
|
|
|
28
28
|
class CfdpUser
|
|
29
|
+
# How long Store.read_topics blocks waiting for a PDU. Used to pace the receive loop when
|
|
30
|
+
# there are no topics to read from.
|
|
31
|
+
READ_TOPICS_TIMEOUT_S = 1.0
|
|
32
|
+
# How long stop() waits for all source transaction threads to finish unwinding after they have
|
|
33
|
+
# been asked to shut down.
|
|
34
|
+
SHUTDOWN_TIMEOUT_S = 5.0
|
|
35
|
+
|
|
29
36
|
def initialize
|
|
30
37
|
@thread = nil
|
|
31
38
|
@cancel_thread = false
|
|
32
39
|
@item_name_lookup = {}
|
|
40
|
+
# Array of [transaction, thread] pairs for every source transaction this process started.
|
|
41
|
+
# Appended to by Rails request threads as well as the receive thread, so it is mutex protected.
|
|
33
42
|
@source_transactions = []
|
|
34
|
-
|
|
43
|
+
# Set while stop() is shutting down so that registrations racing with shutdown are not lost.
|
|
44
|
+
# Protected by @source_mutex along with @source_transactions.
|
|
45
|
+
@stopping = false
|
|
46
|
+
@source_mutex = Mutex.new
|
|
47
|
+
@last_cleanup_time = Time.now.utc
|
|
35
48
|
|
|
36
49
|
at_exit do
|
|
37
50
|
stop()
|
|
@@ -39,6 +52,7 @@ class CfdpUser
|
|
|
39
52
|
end
|
|
40
53
|
|
|
41
54
|
def start
|
|
55
|
+
@source_mutex.synchronize { @stopping = false }
|
|
42
56
|
@thread = Thread.new do
|
|
43
57
|
begin
|
|
44
58
|
source_entity = CfdpMib.source_entity
|
|
@@ -50,75 +64,93 @@ class CfdpUser
|
|
|
50
64
|
topics << topic
|
|
51
65
|
@item_name_lookup[topic] = item_name.upcase
|
|
52
66
|
end
|
|
53
|
-
|
|
67
|
+
if topics.empty?
|
|
68
|
+
# Store.read_topics returns immediately when given no topics, so without this the loop
|
|
69
|
+
# below would spin at 100% CPU forever. There is nothing to receive without tlm_info,
|
|
70
|
+
# but the transaction timers still need to run, so fall back to sleeping the same
|
|
71
|
+
# amount of time read_topics would have blocked for.
|
|
72
|
+
OpenC3::Logger.error("CFDP tlm_info is not configured. PDUs cannot be received.", scope: ENV['OPENC3_SCOPE'])
|
|
73
|
+
else
|
|
74
|
+
OpenC3::Topic.update_topic_offsets(topics)
|
|
75
|
+
end
|
|
54
76
|
while !@cancel_thread
|
|
55
77
|
# TODO: Handle freezing transactions if interface disconnects (or target goes unhealthy), and unfreezing if comes back to functional
|
|
56
78
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
79
|
+
if topics.empty?
|
|
80
|
+
sleep(READ_TOPICS_TIMEOUT_S)
|
|
81
|
+
else
|
|
82
|
+
read_pdus(topics)
|
|
83
|
+
end
|
|
84
|
+
update_transactions()
|
|
85
|
+
reap_source_transactions()
|
|
86
|
+
|
|
87
|
+
current_time = Time.now.utc
|
|
88
|
+
frequency_seconds = CfdpMib.transaction_cleanup_frequency_hours * 3600
|
|
89
|
+
if (current_time - @last_cleanup_time) >= frequency_seconds
|
|
90
|
+
CfdpMib.cleanup_old_transactions()
|
|
91
|
+
@last_cleanup_time = current_time
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
rescue => err
|
|
95
|
+
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
96
|
+
end
|
|
97
|
+
end
|
|
73
98
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
rescue => err
|
|
96
|
-
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
97
|
-
end
|
|
99
|
+
resume_incomplete_source_transactions
|
|
100
|
+
return @thread
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Read and process every PDU currently available on the given topics. Blocks for up to
|
|
104
|
+
# READ_TOPICS_TIMEOUT_S when nothing is available.
|
|
105
|
+
def read_pdus(topics)
|
|
106
|
+
OpenC3::Topic.read_topics(topics, nil, (READ_TOPICS_TIMEOUT_S * 1000).to_i) do |topic, msg_id, msg_hash, redis|
|
|
107
|
+
break if @cancel_thread
|
|
108
|
+
begin
|
|
109
|
+
pdu_hash = receive_packet(topic, msg_id, msg_hash, redis)
|
|
110
|
+
|
|
111
|
+
if pdu_hash['DIRECTION'] == "TOWARD_FILE_RECEIVER"
|
|
112
|
+
if pdu_hash['DESTINATION_ENTITY_ID'] != CfdpMib.source_entity_id
|
|
113
|
+
OpenC3::Logger.error("Receiver PDU received for wrong entity: Mine: #{CfdpMib.source_entity_id}, Destination: #{pdu_hash['DESTINATION_ENTITY_ID']}", scope: ENV['OPENC3_SCOPE'])
|
|
114
|
+
next
|
|
115
|
+
end
|
|
116
|
+
else
|
|
117
|
+
if pdu_hash['SOURCE_ENTITY_ID'] != CfdpMib.source_entity_id
|
|
118
|
+
OpenC3::Logger.error("Sender PDU received for wrong entity: Mine: #{CfdpMib.source_entity_id}, Source: #{pdu_hash['SOURCE_ENTITY_ID']}", scope: ENV['OPENC3_SCOPE'])
|
|
119
|
+
next
|
|
98
120
|
end
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
transaction_id = CfdpTransaction.build_transaction_id(pdu_hash["SOURCE_ENTITY_ID"], pdu_hash["SEQUENCE_NUMBER"])
|
|
124
|
+
transaction = CfdpMib.transactions[transaction_id]
|
|
125
|
+
|
|
126
|
+
if pdu_hash["DIRECTIVE_CODE"] == "METADATA" and transaction and transaction.complete_time
|
|
127
|
+
# The transaction ID was previously used by a transaction that has already reached a
|
|
128
|
+
# terminal state (FINISHED, CANCELED, or ABANDONED), so this METADATA starts a new
|
|
129
|
+
# transaction that is reusing the ID. A METADATA PDU for a transaction that is still
|
|
130
|
+
# running is not a conflict: it is either a retransmission or metadata that arrived
|
|
131
|
+
# after the first file data PDU, and handle_pdu below deals with both cases.
|
|
132
|
+
raise "Transaction ID conflict: #{transaction_id}" unless CfdpMib.allow_duplicate_transaction_ids
|
|
133
|
+
transaction.delete
|
|
134
|
+
transaction = nil
|
|
135
|
+
end
|
|
136
|
+
if transaction
|
|
137
|
+
transaction.handle_pdu(pdu_hash)
|
|
138
|
+
elsif pdu_hash["DIRECTIVE_CODE"] == "METADATA" or pdu_hash["DIRECTIVE_CODE"].nil?
|
|
139
|
+
transaction = CfdpReceiveTransaction.new(pdu_hash) # Also calls handle_pdu inside
|
|
140
|
+
else
|
|
141
|
+
raise "Unknown transaction: #{transaction_id}, #{pdu_hash}"
|
|
142
|
+
end
|
|
143
|
+
if pdu_hash["DIRECTIVE_CODE"] == "METADATA" and not transaction.metadata_pdu_count > 1
|
|
144
|
+
# Handle messages_to_user
|
|
145
|
+
messages_to_user = []
|
|
146
|
+
if pdu_hash["TLVS"]
|
|
147
|
+
pdu_hash["TLVS"].each do |tlv|
|
|
148
|
+
if tlv["TYPE"] == "MESSAGE_TO_USER"
|
|
149
|
+
messages_to_user << tlv
|
|
113
150
|
end
|
|
114
|
-
proxy_responses << params
|
|
115
|
-
transaction.proxy_response_needed = false
|
|
116
|
-
transaction.proxy_response_info = nil
|
|
117
151
|
end
|
|
118
152
|
end
|
|
119
|
-
|
|
120
|
-
start_source_transaction(params)
|
|
121
|
-
end
|
|
153
|
+
handle_messages_to_user(pdu_hash, messages_to_user) if messages_to_user.length > 0
|
|
122
154
|
end
|
|
123
155
|
rescue => err
|
|
124
156
|
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
@@ -126,38 +158,148 @@ class CfdpUser
|
|
|
126
158
|
end
|
|
127
159
|
end
|
|
128
160
|
|
|
161
|
+
# Run the timers for every known transaction and send any proxy responses they generated.
|
|
162
|
+
def update_transactions
|
|
163
|
+
proxy_responses = []
|
|
164
|
+
CfdpMib.transactions.dup.each do |transaction_id, transaction|
|
|
165
|
+
begin
|
|
166
|
+
transaction.update
|
|
167
|
+
if transaction.proxy_response_needed
|
|
168
|
+
# Send the proxy response
|
|
169
|
+
params = {}
|
|
170
|
+
params[:destination_entity_id] = transaction.proxy_response_info["SOURCE_ENTITY_ID"]
|
|
171
|
+
params[:messages_to_user] = []
|
|
172
|
+
destination_entity = CfdpMib.entity(Integer(params[:destination_entity_id]))
|
|
173
|
+
pdu = CfdpPdu.build_initial_pdu(type: "FILE_DIRECTIVE", destination_entity: destination_entity, file_size: 0, segmentation_control: "NOT_PRESERVED", transmission_mode: nil)
|
|
174
|
+
params[:messages_to_user] << pdu.build_proxy_put_response_message(condition_code: transaction.condition_code, delivery_code: transaction.delivery_code, file_status: transaction.file_status)
|
|
175
|
+
params[:messages_to_user] << pdu.build_originating_transaction_id_message(source_entity_id: transaction.proxy_response_info["SOURCE_ENTITY_ID"], sequence_number: transaction.proxy_response_info["SEQUENCE_NUMBER"])
|
|
176
|
+
transaction.filestore_responses.each do |filestore_response|
|
|
177
|
+
params[:messages_to_user] << pdu.build_proxy_filestore_response_message(action_code: filestore_response["ACTION_CODE"], status_code: filestore_response["STATUS_CODE"], first_file_name: filestore_response["FIRST_FILE_NAME"], second_file_name: filestore_response["SECOND_FILE_NAME"], filestore_message: filestore_response["FILESTORE_MESSAGE"])
|
|
178
|
+
end
|
|
179
|
+
proxy_responses << params
|
|
180
|
+
transaction.proxy_response_needed = false
|
|
181
|
+
transaction.proxy_response_info = nil
|
|
182
|
+
end
|
|
183
|
+
rescue => err
|
|
184
|
+
# A single bad transaction must not take down the receive thread, which would leave the
|
|
185
|
+
# microservice running but no longer processing any PDUs
|
|
186
|
+
OpenC3::Logger.error("CFDP Transaction #{transaction_id} update failed\n#{err.formatted}", scope: ENV['OPENC3_SCOPE'])
|
|
187
|
+
end
|
|
188
|
+
end
|
|
189
|
+
proxy_responses.each do |params|
|
|
190
|
+
start_source_transaction(params)
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Drop source transactions whose thread has finished. Without this they accumulate for the
|
|
195
|
+
# life of the process.
|
|
196
|
+
def reap_source_transactions
|
|
197
|
+
@source_mutex.synchronize do
|
|
198
|
+
@source_transactions.delete_if { |_transaction, thread| !thread.alive? }
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
129
202
|
def receive_packet(topic, msg_id, msg_hash, redis)
|
|
130
203
|
topic_split = topic.gsub(/{|}/, '').split("__") # Remove the redis hashtag curly braces
|
|
131
204
|
target_name = topic_split[2]
|
|
132
205
|
packet_name = topic_split[3]
|
|
133
206
|
stored = OpenC3::ConfigParser.handle_true_false(msg_hash["stored"])
|
|
134
207
|
packet = OpenC3::JsonPacket.new(:TLM, target_name, packet_name, msg_hash["time"].to_i, stored, msg_hash['json_data'])
|
|
135
|
-
|
|
208
|
+
item_name = @item_name_lookup[topic]
|
|
209
|
+
pdu_data = packet.read(item_name)
|
|
210
|
+
# Decom leaves out items that don't exist in the packet and items marked HIDDEN
|
|
211
|
+
raise "tlm_info item #{target_name} #{packet_name} #{item_name} not found. Check that the item name is correct and that the item is not HIDDEN" unless pdu_data
|
|
136
212
|
return CfdpPdu.decom(pdu_data)
|
|
137
213
|
end
|
|
138
214
|
|
|
139
215
|
def stop
|
|
140
216
|
@cancel_thread = true
|
|
141
|
-
@
|
|
142
|
-
|
|
217
|
+
# @stopping closes registration before the snapshot is taken. Anything registered after this
|
|
218
|
+
# point is shut down by register_source_transaction itself, so a source transaction started
|
|
219
|
+
# by a Rails request thread (or by the receive thread) while stop is running can neither be
|
|
220
|
+
# missed by the snapshot below nor survive the clear at the end of this method.
|
|
221
|
+
source_transactions = @source_mutex.synchronize do
|
|
222
|
+
@stopping = true
|
|
223
|
+
@source_transactions.dup
|
|
224
|
+
end
|
|
225
|
+
# Ask every source transaction to stop before joining anything. They are asked first because
|
|
226
|
+
# a transfer waiting for a Finished PDU is waiting on the receive thread, which is about to
|
|
227
|
+
# go away.
|
|
228
|
+
source_transactions.each do |transaction, _thread|
|
|
229
|
+
transaction.request_shutdown()
|
|
143
230
|
end
|
|
144
231
|
@thread.join if @thread
|
|
145
232
|
@thread = nil
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
233
|
+
deadline = Time.now + SHUTDOWN_TIMEOUT_S
|
|
234
|
+
source_transactions.each do |transaction, thread|
|
|
235
|
+
join_source_transaction_thread(transaction, thread, deadline)
|
|
149
236
|
end
|
|
150
237
|
|
|
151
238
|
@item_name_lookup = {}
|
|
152
|
-
@source_transactions = []
|
|
153
|
-
|
|
239
|
+
@source_mutex.synchronize { @source_transactions = [] }
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# Wait for one source transaction thread to unwind, then persist its state so the transfer can
|
|
243
|
+
# resume when the microservice comes back. Each thread stops at the next checkpoint in its copy
|
|
244
|
+
# loop, so this normally returns well inside the deadline. A thread that has not stopped by
|
|
245
|
+
# then is left alone rather than killed: killing it at an arbitrary point could leave a
|
|
246
|
+
# half written state in Redis or a PDU partially sent, and the process is exiting anyway.
|
|
247
|
+
def join_source_transaction_thread(transaction, thread, deadline)
|
|
248
|
+
remaining = deadline - Time.now
|
|
249
|
+
remaining = 0 if remaining < 0
|
|
250
|
+
begin
|
|
251
|
+
unless thread.join(remaining)
|
|
252
|
+
OpenC3::Logger.error("CFDP Transaction #{transaction.id} did not shut down within #{SHUTDOWN_TIMEOUT_S} seconds", scope: ENV['OPENC3_SCOPE'])
|
|
253
|
+
end
|
|
254
|
+
rescue => err
|
|
255
|
+
# join re-raises whatever killed the thread
|
|
256
|
+
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
257
|
+
end
|
|
258
|
+
begin
|
|
259
|
+
transaction.save_state()
|
|
260
|
+
rescue => err
|
|
261
|
+
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# Track a source transaction and the thread running it so it can be shut down and saved on
|
|
266
|
+
# shutdown. Called from Rails request threads as well as the receive thread. Returns false if
|
|
267
|
+
# the transaction arrived too late to be tracked because stop() is already running, in which
|
|
268
|
+
# case it is shut down here rather than being left running after shutdown.
|
|
269
|
+
def register_source_transaction(transaction, thread)
|
|
270
|
+
stopping = @source_mutex.synchronize do
|
|
271
|
+
@source_transactions << [transaction, thread] unless @stopping
|
|
272
|
+
@stopping
|
|
273
|
+
end
|
|
274
|
+
return true unless stopping
|
|
275
|
+
|
|
276
|
+
transaction.request_shutdown()
|
|
277
|
+
join_source_transaction_thread(transaction, thread, Time.now + SHUTDOWN_TIMEOUT_S)
|
|
278
|
+
return false
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def resume_incomplete_source_transactions()
|
|
282
|
+
CfdpMib.transactions.each do |transaction_id, transaction|
|
|
283
|
+
if transaction_id.split('__')[0].to_i == CfdpMib.source_entity_id && transaction.copy_state != "complete" # && transaction.copy_state != nil?
|
|
284
|
+
# A transaction that was shut down by a previous stop() in this same process would
|
|
285
|
+
# otherwise exit immediately at its first shutdown check
|
|
286
|
+
transaction.clear_shutdown()
|
|
287
|
+
thread = Thread.new do
|
|
288
|
+
begin
|
|
289
|
+
transaction.copy_file()
|
|
290
|
+
rescue => err
|
|
291
|
+
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
register_source_transaction(transaction, thread)
|
|
295
|
+
end
|
|
296
|
+
end
|
|
154
297
|
end
|
|
155
298
|
|
|
156
299
|
def start_source_transaction(params, proxy_response_info: nil)
|
|
157
300
|
transaction = CfdpSourceTransaction.new
|
|
158
301
|
transaction.proxy_response_info = proxy_response_info
|
|
159
|
-
|
|
160
|
-
@source_threads << Thread.new do
|
|
302
|
+
thread = Thread.new do
|
|
161
303
|
begin
|
|
162
304
|
if params[:remote_entity_id] and Integer(params[:remote_entity_id]) != CfdpMib.source_entity_id
|
|
163
305
|
# Proxy Put
|
|
@@ -222,6 +364,7 @@ class CfdpUser
|
|
|
222
364
|
OpenC3::Logger.error(err.formatted, scope: ENV['OPENC3_SCOPE'])
|
|
223
365
|
end
|
|
224
366
|
end
|
|
367
|
+
register_source_transaction(transaction, thread)
|
|
225
368
|
return transaction
|
|
226
369
|
end
|
|
227
370
|
|
|
@@ -359,7 +502,7 @@ class CfdpUser
|
|
|
359
502
|
params[:messages_to_user] << message_to_user["MESSAGE_TO_USER"]
|
|
360
503
|
|
|
361
504
|
when "PROXY_FILESTORE_REQUEST"
|
|
362
|
-
params[:filestore_requests] << message_to_user["ACTION_CODE", message_to_user["FIRST_FILE_NAME"]]
|
|
505
|
+
params[:filestore_requests] << [message_to_user["ACTION_CODE"], message_to_user["FIRST_FILE_NAME"]]
|
|
363
506
|
params[:filestore_requests][-1] << message_to_user["SECOND_FILE_NAME"] if message_to_user["SECOND_FILE_NAME"]
|
|
364
507
|
|
|
365
508
|
when "PROXY_FAULT_HANDLER_OVERRIDE"
|
|
@@ -32,11 +32,13 @@ class CfdpPdu < OpenC3::Packet
|
|
|
32
32
|
transmission_mode: nil,
|
|
33
33
|
condition_code:,
|
|
34
34
|
ack_directive_code:,
|
|
35
|
-
transaction_status
|
|
35
|
+
transaction_status:,
|
|
36
|
+
remote_entity: nil)
|
|
36
37
|
|
|
37
|
-
|
|
38
|
+
remote_entity ||= destination_entity
|
|
39
|
+
pdu = build_initial_pdu(type: "FILE_DIRECTIVE", destination_entity: destination_entity, transmission_mode: transmission_mode, file_size: 0, segmentation_control: segmentation_control, remote_entity: remote_entity)
|
|
38
40
|
pdu_header_part_1_length = pdu.length # Measured here before writing variable data - Includes CRC if present
|
|
39
|
-
pdu_header_part_1_length -= CRC_BYTE_SIZE if
|
|
41
|
+
pdu_header_part_1_length -= CRC_BYTE_SIZE if remote_entity['crcs_required'] # PDU_DATA_LENGTH field should contain CRC length
|
|
40
42
|
pdu_header = pdu.build_variable_header(source_entity_id: source_entity['id'], transaction_seq_num: transaction_seq_num, destination_entity_id: destination_entity['id'], directive_code: "ACK")
|
|
41
43
|
pdu_header_part_2_length = pdu_header.length - DIRECTIVE_CODE_BYTE_SIZE # Minus 1 = Directive code is part of data per 5.2.1.1
|
|
42
44
|
if ack_directive_code == "FINISHED" or ack_directive_code == 5
|
|
@@ -47,9 +49,8 @@ class CfdpPdu < OpenC3::Packet
|
|
|
47
49
|
pdu_contents = pdu.build_ack_pdu_contents(ack_directive_code: ack_directive_code, condition_code: condition_code, transaction_status: transaction_status)
|
|
48
50
|
pdu.write("VARIABLE_DATA", pdu_header + pdu_contents)
|
|
49
51
|
pdu.write("PDU_DATA_LENGTH", pdu.length - pdu_header_part_1_length - pdu_header_part_2_length)
|
|
50
|
-
if
|
|
51
|
-
|
|
52
|
-
pdu.write("CRC", crc16.calc(pdu.buffer(false)[0..-3]))
|
|
52
|
+
if remote_entity['crcs_required']
|
|
53
|
+
pdu.write("CRC", CRC16.calc(pdu.buffer(false)[0..-3]))
|
|
53
54
|
end
|
|
54
55
|
return pdu.buffer(false)
|
|
55
56
|
end
|