Class: Roby::Interface::V1::ShellClient
- Defined in:
- lib/roby/interface/v1/shell_client.rb
Overview
An interface client using TCP that provides reconnection capabilities as well as proper formatting of the information
Instance Attribute Summary collapse
-
#client ⇒ Client?
readonly
The socket used to communicate to the server, or nil if we have not managed to connect yet.
-
#connection_method ⇒ #call
readonly
An object that can create a Client instance.
-
#remote_name ⇒ String
readonly
A string that describes the remote host.
Instance Method Summary collapse
- #__jobs ⇒ Object
- #actions(regex = nil, verbose = false) ⇒ Object
- #call(options, path, m, *args) ⇒ Object
- #cancel ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect(retry_period = 0.5) ⇒ Object
- #describe(matcher) ⇒ Object
- #drop_job(job_id) ⇒ Object
- #format_arguments(hash) ⇒ Object
- #format_exception(kind, error, *_args) ⇒ Object
- #format_job_info(id, state, task, planning_task) ⇒ Object
- #format_job_progress(kind, job_id, job_name, *_args) ⇒ Object
- #format_notification(source, level, message) ⇒ Object
- #help(subcommand = client) ⇒ Object
-
#initialize(remote_name, &connection_method) ⇒ ShellClient
constructor
A new instance of ShellClient.
- #jobs ⇒ Object
- #kill_job(job_id) ⇒ Object
- #method_missing(m, *args) ⇒ Object
-
#notification_loop(period = 0.1) {|msg| ... } ⇒ Object
Polls for messages from the remote interface and yields them.
- #path ⇒ Object
- #process ⇒ Object
-
#quit ⇒ Object
Make the remote app quit.
- #resolve_job_id(job_id) ⇒ Object
- #retry_on_com_error ⇒ Object
- #review ⇒ Object
- #safe ⇒ Object
- #safe? ⇒ Boolean
-
#silent(silent) ⇒ Object
Whether the shell should stop displaying any notification.
- #summarize_exception(kind, error, *args) ⇒ Object
- #summarize_job_progress(kind, job_id, job_name, *args) ⇒ Object
- #summarize_notification(source, level, message) ⇒ Object
-
#summarize_pending_messages(already_summarized = Set.new) {|msg| ... } ⇒ Set
Processes the exception and job_progress queues, and yields with a message that summarizes the new ones.
- #unsafe ⇒ Object
- #wtf? ⇒ Boolean
Constructor Details
#initialize(remote_name, &connection_method) ⇒ ShellClient
Returns a new instance of ShellClient.
19 20 21 22 23 24 |
# File 'lib/roby/interface/v1/shell_client.rb', line 19 def initialize(remote_name, &connection_method) @connection_method = connection_method @remote_name = remote_name @silent = false connect end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/roby/interface/v1/shell_client.rb', line 325 def method_missing(m, *args) if sub = client.find_subcommand_by_name(m.to_s) ShellSubcommand.new(self, m.to_s, sub.description, sub.commands) elsif act = client.find_action_by_name(m.to_s) Roby::Actions::Action.new(act, *args) elsif @batch && m.to_s =~ /(.*)!$/ action_name = $1 @batch.start_job(action_name, *args) review nil else begin call Hash[], [], m, *args rescue NoMethodError => e if e. =~ /undefined method .#{m}./ puts "invalid command name #{m}, call 'help' for more information" else raise end rescue ArgumentError => e if e. =~ /wrong number of arguments/ && e.backtrace.first =~ /#{m}/ puts e. else raise end end end rescue ComError Roby::Interface.warn "Lost communication with remote, will not retry the command after reconnection" connect rescue Interrupt Roby::Interface.warn "Interrupted" end |
Instance Attribute Details
#client ⇒ Client? (readonly)
Returns the socket used to communicate to the server, or nil if we have not managed to connect yet.
15 16 17 |
# File 'lib/roby/interface/v1/shell_client.rb', line 15 def client @client end |
#connection_method ⇒ #call (readonly)
Returns an object that can create a Client instance.
12 13 14 |
# File 'lib/roby/interface/v1/shell_client.rb', line 12 def connection_method @connection_method end |
#remote_name ⇒ String (readonly)
Returns a string that describes the remote host.
10 11 12 |
# File 'lib/roby/interface/v1/shell_client.rb', line 10 def remote_name @remote_name end |
Instance Method Details
#__jobs ⇒ Object
116 117 118 |
# File 'lib/roby/interface/v1/shell_client.rb', line 116 def __jobs call Hash[retry: true], [], :jobs end |
#actions(regex = nil, verbose = false) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/roby/interface/v1/shell_client.rb', line 62 def actions(regex = nil, verbose = false) actions = client.actions.sort_by(&:name) if regex regex = Regexp.new(regex) else regex = Regexp.new(".*") end actions.each do |action| if regex.match(action.name) if verbose puts "\e[1m#{action.name}!\e[0m" arguments = action.arguments.sort_by(&:name) required_arguments = [] optional_arguments = [] arguments.each do |argument| if argument.required required_arguments << argument else optional_arguments << argument end end unless required_arguments.empty? puts " required arguments" required_arguments.each do |argument| puts " #{argument.name}: #{argument.doc} [default: #{argument.default}]" end end unless optional_arguments.empty? puts " optional arguments:" optional_arguments.each do |argument| puts " #{argument.name}: #{argument.doc} [default: #{argument.default}]" end end puts " doc: #{action.doc}" unless action.doc.empty? else puts "\e[1m#{action.name}!\e[0m(#{action.arguments.map(&:name).sort.join(', ')}): #{action.doc}" end end end nil end |
#call(options, path, m, *args) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/roby/interface/v1/shell_client.rb', line 158 def call(, path, m, *args) = Kernel. , retry: false if [:retry] = .merge(retry: false) retry_on_com_error do return call , path, m, *args end else client.call(path, m, *args) end rescue Exception => e msg = Roby.format_exception(e) if msg[0] msg[0] = Roby.color(msg[0], :red) end puts msg.join("\n") puts " #{e.backtrace.join('\n ')}" nil end |
#cancel ⇒ Object
319 320 321 322 323 |
# File 'lib/roby/interface/v1/shell_client.rb', line 319 def cancel @batch = client.create_batch review nil end |
#close ⇒ Object
56 57 58 59 60 |
# File 'lib/roby/interface/v1/shell_client.rb', line 56 def close client.close @job_manager = nil @client = nil end |
#closed? ⇒ Boolean
52 53 54 |
# File 'lib/roby/interface/v1/shell_client.rb', line 52 def closed? client.closed? end |
#connect(retry_period = 0.5) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/roby/interface/v1/shell_client.rb', line 30 def connect(retry_period = 0.5) retry_warning = false begin @client = connection_method.call @batch = client.create_batch @batch_job_info = {} rescue ConnectionError, ComError => e if retry_period if e.kind_of?(ComError) Roby::Interface.warn "failed handshake with #{remote_name}, retrying ..." elsif !retry_warning Roby::Interface.warn "cannot connect to #{remote_name}, retrying every #{retry_period} seconds..." retry_warning = true end sleep retry_period retry else raise end end end |
#describe(matcher) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/roby/interface/v1/shell_client.rb', line 145 def describe(matcher) if matcher.kind_of?(Roby::Actions::Action) pp matcher.model elsif matcher.kind_of?(Roby::Actions::Models::Action) pp matcher else client.find_all_actions_matching(matcher).each do |act| pp act end end nil end |
#drop_job(job_id) ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/roby/interface/v1/shell_client.rb', line 278 def drop_job(job_id) if safe? if @batch_job_info[job_id] = resolve_job_id(job_id) @batch.drop_job job_id review end else super end nil end |
#format_arguments(hash) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/roby/interface/v1/shell_client.rb', line 105 def format_arguments(hash) hash.keys.map do |k| v = hash[k] v = if !v || v.respond_to?(:to_str) then v.inspect else v end "#{k} => #{v}" end.join(", ") end |
#format_exception(kind, error, *_args) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/roby/interface/v1/shell_client.rb', line 194 def format_exception(kind, error, *_args) color = case kind when ExecutionEngine::EXCEPTION_FATAL [:red] when ExecutionEngine::EXCEPTION_NONFATAL [:magenta] else [] end if error msg = Roby.format_exception(error.exception) if msg[0] msg[0] = Roby.color(msg[0], *color) end else msg = ["<something wrong happened in transmission of exception information>"] end msg end |
#format_job_info(id, state, task, planning_task) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/roby/interface/v1/shell_client.rb', line 128 def format_job_info(id, state, task, planning_task) if planning_task.respond_to?(:action_model) && planning_task.action_model name = "#{planning_task.action_model}(#{format_arguments(planning_task.action_arguments)})" else name = task.to_s end format("[%4d] (%s) %s", id, state.to_s, name) end |
#format_job_progress(kind, job_id, job_name, *_args) ⇒ Object
186 187 188 |
# File 'lib/roby/interface/v1/shell_client.rb', line 186 def format_job_progress(kind, job_id, job_name, *_args) ["[#{job_id}] #{job_name}: #{kind}"] end |
#format_notification(source, level, message) ⇒ Object
178 179 180 |
# File 'lib/roby/interface/v1/shell_client.rb', line 178 def format_notification(source, level, ) ["[#{level}] #{source}: #{}"] end |
#help(subcommand = client) ⇒ Object
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/roby/interface/v1/shell_client.rb', line 359 def help(subcommand = client) puts if safe? puts Roby.color("Currently in safe mode, use 'unsafe' to switch", :bold) puts "Job commands like drop_job, kill_job, ... are queued, only sent if on 'process'" puts "review display the pending job commands" puts "process apply the pending job commands" puts "cancel clear the pending job commands" else puts Roby.color("Currently in unsafe mode, use 'safe' to switch", :bold, :red) puts "Job commands like drop_job, kill_job, ... are sent directly" end puts if subcommand.respond_to?(:description) puts Roby.color(subcommand.description.join("\n"), :bold) puts end commands = subcommand.commands[""].commands unless commands.empty? puts Roby.color("Commands", :bold) puts Roby.color("--------", :bold) commands.keys.sort.each do |command_name| cmd = commands[command_name] puts "#{command_name}(#{cmd.arguments.keys.map(&:to_s).join(', ')}): #{cmd.description.first}" end end if subcommand.commands.size > 1 puts unless commands.empty? puts Roby.color("Subcommands (use help <subcommand name> for more details)", :bold) puts Roby.color("-----------", :bold) subcommand.commands.keys.sort.each do |subcommand_name| next if subcommand_name.empty? puts "#{subcommand_name}: #{subcommand.commands[subcommand_name].description.first}" end end nil end |
#jobs ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/roby/interface/v1/shell_client.rb', line 120 def jobs jobs = __jobs jobs.each do |id, job_info| puts format_job_info(id, *job_info) end nil end |
#kill_job(job_id) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/roby/interface/v1/shell_client.rb', line 266 def kill_job(job_id) if safe? if @batch_job_info[job_id] = resolve_job_id(job_id) @batch.kill_job job_id review end else super end nil end |
#notification_loop(period = 0.1) {|msg| ... } ⇒ Object
Polls for messages from the remote interface and yields them. It handles automatic reconnection, when applicable, as well
It is meant to be called in a separate thread
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/roby/interface/v1/shell_client.rb', line 437 def notification_loop(period = 0.1) already_summarized = Set.new was_connected = nil loop do has_valid_connection = begin client.poll true rescue Exception begin connect(nil) client.io.reset_thread_guard true rescue Exception end end already_summarized, = (already_summarized) yield(has_valid_connection, ) if has_valid_connection was_connected = true end if has_valid_connection && !was_connected RbReadline.puts "reconnected" elsif !has_valid_connection && was_connected RbReadline.puts "lost connection, reconnecting ..." end was_connected = has_valid_connection sleep period end end |
#path ⇒ Object
26 27 28 |
# File 'lib/roby/interface/v1/shell_client.rb', line 26 def path [] end |
#process ⇒ Object
309 310 311 312 313 314 315 316 317 |
# File 'lib/roby/interface/v1/shell_client.rb', line 309 def process if safe? @batch.__process else STDERR.puts "Not in batch context" end @batch = client.create_batch nil end |
#quit ⇒ Object
Make the remote app quit
This is defined explicitely because otherwise IRB “hooks” on quit to terminate the shell instead
481 482 483 |
# File 'lib/roby/interface/v1/shell_client.rb', line 481 def quit call({}, [], :quit) end |
#resolve_job_id(job_id) ⇒ Object
258 259 260 261 262 263 264 |
# File 'lib/roby/interface/v1/shell_client.rb', line 258 def resolve_job_id(job_id) if job_info = __jobs[job_id] job_info else STDERR.puts Roby.color("No job #{job_id}", :bold, :bright_red) end end |
#retry_on_com_error ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/roby/interface/v1/shell_client.rb', line 137 def retry_on_com_error yield rescue ComError Roby::Interface.warn "Lost communication with remote, retrying command after reconnection" connect retry end |
#review ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/roby/interface/v1/shell_client.rb', line 290 def review if safe? puts "#{@batch.__calls.size} actions queued in the current batch, "\ "use #process to send, #cancel to delete" @batch.__calls.each do |context, m, *args| if %i[drop_job kill_job].include?(m) job_id = args.first job_info = format_job_info(job_id, *@batch_job_info[job_id]) puts "#{Roby.color(m.to_s, :bold, :bright_red)} #{job_info}" elsif m == :start_job puts "#{Roby.color("#{args[0]}!", :bright_blue)}(#{args[1]})" else puts "#{Roby.color("#{m}!", :bright_blue)}(#{args.first})" end end end nil end |
#safe ⇒ Object
249 250 251 252 |
# File 'lib/roby/interface/v1/shell_client.rb', line 249 def safe @batch ||= client.create_batch nil end |
#safe? ⇒ Boolean
245 246 247 |
# File 'lib/roby/interface/v1/shell_client.rb', line 245 def safe? !!@batch end |
#silent(silent) ⇒ Object
Whether the shell should stop displaying any notification
473 474 475 |
# File 'lib/roby/interface/v1/shell_client.rb', line 473 def silent(silent) # rubocop:disable Style/TrivialAccessors @silent = silent end |
#summarize_exception(kind, error, *args) ⇒ Object
216 217 218 219 |
# File 'lib/roby/interface/v1/shell_client.rb', line 216 def summarize_exception(kind, error, *args) msg = "(#{kind}) #{format_exception(kind, error, *args).first}" [msg, false] end |
#summarize_job_progress(kind, job_id, job_name, *args) ⇒ Object
190 191 192 |
# File 'lib/roby/interface/v1/shell_client.rb', line 190 def summarize_job_progress(kind, job_id, job_name, *args) [format_job_progress(kind, job_id, job_name, *args).first, true] end |
#summarize_notification(source, level, message) ⇒ Object
182 183 184 |
# File 'lib/roby/interface/v1/shell_client.rb', line 182 def summarize_notification(source, level, ) [format_notification(source, level, ).first, true] end |
#summarize_pending_messages(already_summarized = Set.new) {|msg| ... } ⇒ Set
Processes the exception and job_progress queues, and yields with a message that summarizes the new ones
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/roby/interface/v1/shell_client.rb', line 411 def (already_summarized = Set.new) summarized = Set.new = [] queues = { exception: client.exception_queue, job_progress: client.job_progress_queue, notification: client.notification_queue } queues.each do |type, q| q.delete_if do |id, args| summarized << id unless already_summarized.include?(id) msg, complete = send("summarize_#{type}", *args) << "##{id} #{msg}" complete end end end [summarized, ] end |
#unsafe ⇒ Object
254 255 256 |
# File 'lib/roby/interface/v1/shell_client.rb', line 254 def unsafe @batch = nil end |
#wtf? ⇒ Boolean
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/roby/interface/v1/shell_client.rb', line 221 def wtf? msg = [] client.notification_queue.each do |id, (source, level, )| msg << Roby.color("-- ##{id} (notification) --", :bold) msg.concat format_notification(source, level, ) msg << "\n" end client.job_progress_queue.each do |id, (kind, job_id, job_name, *args)| msg << Roby.color("-- ##{id} (job progress) --", :bold) msg.concat format_job_progress(kind, job_id, job_name, *args) msg << "\n" end client.exception_queue.each do |id, (kind, exception, tasks)| msg << Roby.color("-- ##{id} (#{kind} exception) --", :bold) msg.concat format_exception(kind, exception, tasks) msg << "\n" end client.job_progress_queue.clear client.exception_queue.clear client.notification_queue.clear puts msg.join("\n") nil end |