Class: Roby::Interface::V2::ShellClient
- Defined in:
- lib/roby/interface/v2/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
- #display_action(act) ⇒ Object
- #display_action_model(act) ⇒ 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/v2/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
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/roby/interface/v2/shell_client.rb', line 347 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) act 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/v2/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/v2/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/v2/shell_client.rb', line 10 def remote_name @remote_name end |
Instance Method Details
#__jobs ⇒ Object
115 116 117 |
# File 'lib/roby/interface/v2/shell_client.rb', line 115 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/v2/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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/roby/interface/v2/shell_client.rb', line 174 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
341 342 343 344 345 |
# File 'lib/roby/interface/v2/shell_client.rb', line 341 def cancel @batch = client.create_batch review nil end |
#close ⇒ Object
56 57 58 59 60 |
# File 'lib/roby/interface/v2/shell_client.rb', line 56 def close client.close @job_manager = nil @client = nil end |
#closed? ⇒ Boolean
52 53 54 |
# File 'lib/roby/interface/v2/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/v2/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
144 145 146 147 148 149 150 151 |
# File 'lib/roby/interface/v2/shell_client.rb', line 144 def describe(matcher) if matcher.kind_of?(Protocol::Action) display_action(matcher) elsif matcher.kind_of?(Protocol::ActionModel) display_action_model(matcher) end nil end |
#display_action(act) ⇒ Object
167 168 169 170 171 172 |
# File 'lib/roby/interface/v2/shell_client.rb', line 167 def display_action(act) puts act.model.name act.arguments.each do |arg| puts " #{arg}" end end |
#display_action_model(act) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/roby/interface/v2/shell_client.rb', line 153 def display_action_model(act) puts "#{act.name} #{act.doc}" act.arguments.each do |arg| puts " #{arg.name} - #{arg.doc}" puts " #{arg.required ? 'required' : 'optional'}" unless arg.default.kind_of?(Protocol::VoidClass) puts " default: #{arg.default}" end unless arg.example.kind_of?(Protocol::VoidClass) puts " example: #{arg.example}" end end end |
#drop_job(job_id) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/roby/interface/v2/shell_client.rb', line 294 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 |
# File 'lib/roby/interface/v2/shell_client.rb', line 105 def format_arguments(hash) hash.map do |k, v| 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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/roby/interface/v2/shell_client.rb', line 210 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
127 128 129 130 131 132 133 134 |
# File 'lib/roby/interface/v2/shell_client.rb', line 127 def format_job_info(id, state, task, planning_task) if planning_task.arguments[:action_model] name = "#{planning_task.arguments[:action_model].name}(#{format_arguments(planning_task.arguments[:action_arguments])})" else name = "#{task.model}<id:#{task.id}>" end format("[%4d] (%s) %s", id, state.to_s, name) end |
#format_job_progress(kind, job_id, job_name, *args) ⇒ Object
202 203 204 |
# File 'lib/roby/interface/v2/shell_client.rb', line 202 def format_job_progress(kind, job_id, job_name, *args) ["[#{job_id}] #{job_name}: #{kind}"] end |
#format_notification(source, level, message) ⇒ Object
194 195 196 |
# File 'lib/roby/interface/v2/shell_client.rb', line 194 def format_notification(source, level, ) ["[#{level}] #{source}: #{}"] end |
#help(subcommand = client) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/roby/interface/v2/shell_client.rb', line 381 def help(subcommand = client) # IRB nowadays converts `help syskit` into `help "syskit"` if subcommand.kind_of?(String) subcommand_name = subcommand subcommand = client.find_subcommand_by_name(subcommand_name) unless subcommand raise ArgumentError, "no subcommand '#{subcommand}'" end end 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 |sub_name| next if sub_name.empty? puts "#{sub_name}: #{subcommand.commands[sub_name].description.first}" end end nil end |
#jobs ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/roby/interface/v2/shell_client.rb', line 119 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
282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/roby/interface/v2/shell_client.rb', line 282 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
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
# File 'lib/roby/interface/v2/shell_client.rb', line 468 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/v2/shell_client.rb', line 26 def path [] end |
#process ⇒ Object
331 332 333 334 335 336 337 338 339 |
# File 'lib/roby/interface/v2/shell_client.rb', line 331 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
512 513 514 |
# File 'lib/roby/interface/v2/shell_client.rb', line 512 def quit call({}, [], :quit) end |
#resolve_job_id(job_id) ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/roby/interface/v2/shell_client.rb', line 274 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
136 137 138 139 140 141 142 |
# File 'lib/roby/interface/v2/shell_client.rb', line 136 def retry_on_com_error yield rescue ComError Roby::Interface.warn "Lost communication with remote, retrying command after reconnection" connect retry end |
#review ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/roby/interface/v2/shell_client.rb', line 306 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 |path, m, args, kw| if %i[drop_job kill_job].include?(m) job_id = args.first if (job_info = @batch_job_info[job_id]) formatted_job_info = format_job_info(job_id, *job_info) else formatted_job_info = "##{job_id}" end puts "#{Roby.color(m.to_s, :bold, :bright_red)} " \ "#{formatted_job_info}" elsif m == :start_job puts "#{Roby.color("#{args[0]}!", :bright_blue)}(#{format_arguments(kw)})" else puts "#{Roby.color("#{m}!", :bright_blue)}(#{args.first})" end end end nil end |
#safe ⇒ Object
265 266 267 268 |
# File 'lib/roby/interface/v2/shell_client.rb', line 265 def safe @batch ||= client.create_batch nil end |
#safe? ⇒ Boolean
261 262 263 |
# File 'lib/roby/interface/v2/shell_client.rb', line 261 def safe? !!@batch end |
#silent(silent) ⇒ Object
Whether the shell should stop displaying any notification
504 505 506 |
# File 'lib/roby/interface/v2/shell_client.rb', line 504 def silent(silent) # rubocop:disable Style/TrivialAccessors @silent = silent end |
#summarize_exception(kind, error, *args) ⇒ Object
232 233 234 235 |
# File 'lib/roby/interface/v2/shell_client.rb', line 232 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
206 207 208 |
# File 'lib/roby/interface/v2/shell_client.rb', line 206 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
198 199 200 |
# File 'lib/roby/interface/v2/shell_client.rb', line 198 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
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'lib/roby/interface/v2/shell_client.rb', line 442 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
270 271 272 |
# File 'lib/roby/interface/v2/shell_client.rb', line 270 def unsafe @batch = nil end |
#wtf? ⇒ Boolean
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/roby/interface/v2/shell_client.rb', line 237 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 |