Class: Opee::SocketInspector

Inherits:
OTerm::Executor
  • Object
show all
Defined in:
lib/opee/inspector.rb

Defined Under Namespace

Classes: ActorStat

Constant Summary

BY_NAME =
'n'
BY_ACTIVITY =
'a'
BY_QUEUE =
'q'

Instance Method Summary (collapse)

Constructor Details

- (SocketInspector) initialize(port = 6060)

Returns a new instance of SocketInspector



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/opee/inspector.rb', line 8

def initialize(port=6060)
  super()
  register('actor_count', self, :actor_count, 'Returns the number of active Actors.', nil)
  register('actors', self, :actors, 'Displays a list of all Actor names.', nil)
  register('busy', self, :busy, 'Returns the busy state of the system.', nil)
  register('queued', self, :queued, 'Returns the total number of queued requests.', nil)
  register('show', self, :show, '<actor name> Shows the details of an Actor.',
           %|[<actor name>] Shows the details of an Actor. Attributes are:
name        - identifies the Actor
state       - state of the Actor [ STOPPED \| RUNNING \| STEP \| CLOSING ]
busy        - true if the Actor has requested queued
queued      - number of requested in the queue
max queue   - maximum number of requested that can be queued
ask timeout - seconds to wait before rejecting an ask
|)
  register('start', self, :start, '[<actor name>] Start or restart an Actor.', nil)
  register('status', self, :status, 'Displays status of all Actors.',
           %|Displays the Actor name and queued count. If the terminal supports real time
updates the displays stays active until the X character is pressed. While running
options are available for sorting on name, activity, or queue size.|)
  register('step', self, :step, '[<actor name>] Step once.',
           %|Step once for the actor specfified or once for some actor that is
waiting if not actor is identified.
|)
  register('stop', self, :stop, '[<actor name>] Stops an Actor.', nil)
  register('verbosity', self, :verbosity, '[<level>] Show or set the verbosity or log level.', nil)

  @server = ::OTerm::Server.new(self, port, false)
end

Instance Method Details

- (Object) actor_count(listener, args)



90
91
92
# File 'lib/opee/inspector.rb', line 90

def actor_count(listener, args)
  listener.out.pl("There are currently #{Opee::Env.actor_count()} active Actors.")
end

- (Object) actors(listener, args)



94
95
96
97
98
99
100
101
102
# File 'lib/opee/inspector.rb', line 94

def actors(listener, args)
  max = 0
  ::Opee::Env.each_actor() do |a|
    max = a.name.size if max < a.name.size
  end
  ::Opee::Env.each_actor() do |a|
    listener.out.pl("  %1$*2$s: %3$s" % [a.name, -max, a.class.to_s])
  end
end

- (Object) busy(listener, args)



108
109
110
111
112
113
114
# File 'lib/opee/inspector.rb', line 108

def busy(listener, args)
  if Opee::Env.busy?()
    listener.out.pl("One or more actors is busy.")
  else
    listener.out.pl("All actors are idle.")
  end
end

- (Object) dynamic_status(listener)



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/opee/inspector.rb', line 234

def dynamic_status(listener)
  o = listener.out
  actors = [] # ActorStat
  sort_by = BY_NAME
  rev = false
  delay = 0.4
  max = 6
  ::Opee::Env.each_actor() do |a|
    actors << ActorStat.new(a)
    max = a.name.size if max < a.name.size
  end
  lines = actors.size + 2
  h, w = o.screen_size()
  lines = h - 2 if lines > h - 2
  o.clear_screen()
  max_q = w - max - 4
  done = false
  until done
    actors.each { |as| as.refresh() }
    case sort_by
    when BY_NAME
      if rev
        actors.sort! { |a,b| b.name <=> a.name }
      else
        actors.sort! { |a,b| a.name <=> b.name }
      end
    when BY_ACTIVITY
      if rev
        actors.sort! { |a,b| a.activity <=> b.activity }
      else
        actors.sort! { |a,b| b.activity <=> a.activity }
      end
    when BY_QUEUE
      if rev
        actors.sort! { |a,b| a.queued <=> b.queued }
      else
        actors.sort! { |a,b| b.queued <=> a.queued }
      end
    end
    o.set_cursor(1, 1)
    o.bold()
    o.p("%1$*2$s @ Queued" % ['Actor', -max])
    o.attrs_off()
    i = 2
    actors[0..lines].each do |as|
      o.set_cursor(i, 1)
      o.p("%1$*2$s " % [as.name, -max])
      o.set_cursor(i, max + 2)
      case as.activity
      when 0
        o.p(' ')
      when 1
        o.p('.')
      when 2, 3
        o.p('o')
      else
        o.p('O')
      end
      o.p(' ')
      qlen = as.queued
      qlen = max_q if max_q < qlen
      if 0 < qlen
        o.reverse()
        o.p("%1$*2$d" % [as.queued, -qlen])
        o.attrs_off()
      end
      o.clear_to_end()
      i += 1
    end
    o.set_cursor(i, 1)
    o.bold()
    o.p('E) exit  N) by name  A) by activity  Q) by queued  R) reverse  ')
    o.graphic_font()
    o.p(::OTerm::VT100::PLUS_MINUS)
    o.default_font()
    o.p(") faster/slower [%0.1f]" % [delay])
    o.attrs_off()
    c = o.recv_wait(1, delay, /./)
    unless c.nil?
      case c[0]
      when 'e', 'E'
        done = true
      when 'n', 'N'
        sort_by = BY_NAME
        rev = false
      when 'a', 'A'
        sort_by = BY_ACTIVITY
        rev = false
      when 'q', 'Q'
        sort_by = BY_QUEUE
        rev = false
      when 'r', 'R'
        rev = !rev
      when 'f', 'F', '+'
        delay /= 2.0 unless delay <= 0.1
      when 's', 'S', '-'
        delay *= 2.0 unless 3.0 <= delay
      end
    end
  end
  o.pl()
end

- (Object) greeting



47
48
49
# File 'lib/opee/inspector.rb', line 47

def greeting()
  "Welcome to the Inspector."
end

- (Object) join



38
39
40
# File 'lib/opee/inspector.rb', line 38

def join()
  @server.join()
end

- (Object) queued(listener, args)



104
105
106
# File 'lib/opee/inspector.rb', line 104

def queued(listener, args)
  listener.out.pl("There are currently #{Opee::Env.queue_count()} unprocessed requests queued.")
end

- (Object) show(listener, args)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/opee/inspector.rb', line 200

def show(listener, args)
  if nil == args
    listener.out.pl("--- No Actor specified")
    return
  end
  a = ::Opee::Env.find_actor(args)
  if nil == a
    listener.out.pl("--- Failed to find '#{args}'")
  else
    args.strip!
    listener.out.pl("#{a.name}:")
    listener.out.pl("  state:           #{a.state_string()}")
    listener.out.pl("  busy?:           #{a.busy?()}")
    listener.out.pl("  queued count:    #{a.queue_count()}")
    listener.out.pl("  max queue count: #{a.max_queue_count()}")
    listener.out.pl("  ask timeout:     #{a.ask_timeout()}")
  end
end

- (Object) shutdown(listener, args)



42
43
44
45
# File 'lib/opee/inspector.rb', line 42

def shutdown(listener, args)
  super
  Env.shutdown()
end

- (Object) start(listener, args)



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/opee/inspector.rb', line 132

def start(listener, args)
  if nil == args || 0 == args.size()
    ::Opee::Env.start()
    listener.out.pl("All Actors restarted")
  else
    args.strip!
    a = ::Opee::Env.find_actor(args)
    if nil == a
      listener.out.pl("--- Failed to find '#{args}'")
    else
      a.start()
      listener.out.pl("#{a.name} started")
    end
  end
end

- (Object) status(listener, args)



219
220
221
222
223
224
225
226
227
228
# File 'lib/opee/inspector.rb', line 219

def status(listener, args)
  if listener.out.is_vt100?
    dynamic_status(listener)
  else
    listener.out.pl("  %20s  %-11s  %5s  %5s" % ['Actor Name', 'Q-cnt/max', 'busy?', 'processed'])
    ::Opee::Env.each_actor() do |a|
      listener.out.pl("  %20s  %5d/%-5d  %5s  %5d" % [a.name, a.queue_count(), a.max_queue_count().to_i, a.busy?(), a.proc_count()])
    end
  end
end

- (Object) step(listener, args)



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/opee/inspector.rb', line 148

def step(listener, args)
  la = ::Opee::Env.logger()
  stop_after = false
  if ::Opee::Actor::STOPPED == la.state
    la.start()
    stop_after = true
  end
  if nil == args || 0 == args.size()
    ma = nil
    mb = 0
    ::Opee::Env.each_actor() do |a|
      next unless ::Opee::Actor::STOPPED == a.state
      if ma.nil?
        ma = a
        mb = ma.backed_up()
      else
        b = a.backed_up()
        if mb < b
          ma = a 
          mb = b
        end
      end
    end
    unless ma.nil? || 0 == ma.queue_count()
      ma.step()
      listener.out.pl("#{ma.name} stepped")
    end
  else
    a = ::Opee::Env.find_actor(args)
    if nil == a
      listener.out.pl("--- Failed to find '#{args}'")
    else
      a.step()
      listener.out.pl("#{a.name} stepped")
    end
  end
  la.stop() if stop_after
end

- (Object) stop(listener, args)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/opee/inspector.rb', line 116

def stop(listener, args)
  if nil == args || 0 == args.size()
    ::Opee::Env.stop()
    listener.out.pl("All Actors stopped(paused)")
  else
    args.strip!
    a = ::Opee::Env.find_actor(args)
    if nil == a
      listener.out.pl("--- Failed to find '#{args}'")
    else
      a.stop()
      listener.out.pl("#{a.name} stopped(paused)")
    end
  end
end

- (Object) tab(cmd, listener)



51
52
53
54
55
56
57
58
59
60
61
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
# File 'lib/opee/inspector.rb', line 51

def tab(cmd, listener)
  start = cmd.index(' ')
  if nil == start
    super
  else
    op = cmd[0...start]
    until cmd.size <= start || ' ' != cmd[start]
      start += 1
    end
    arg = cmd[start..-1]

    if 'show' == op || 'start' == op || 'step' == op || 'stop' == op
      names = []
      Env.each_actor { |a| names << a.name if a.name.start_with?(arg) }
      return if 0 == names.size
      if 1 == names.size
        listener.move_col(1000)
        listener.insert(names[0][arg.size..-1])
        listener.out.prompt()
        listener.out.p(listener.buf)
      else
        listener.out.pl()
        names.each do |name|
          listener.out.pl("#{op} #{name}")
        end
        best = best_completion(arg, names)
        if best == arg
          listener.update_cmd(0)
        else
          listener.move_col(1000)
          listener.insert(best[arg.size..-1])
          listener.out.prompt()
          listener.out.p(listener.buf)
        end
      end
    end
  end
end

- (Object) verbosity(listener, args)



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/opee/inspector.rb', line 187

def verbosity(listener, args)
  v = ::Opee::Env.logger.severity
  if nil != args && 0 < args.size()
    args.strip!
    begin
      v = ::Opee::Env.logger.severity = args
    rescue Exception
      listener.out.pl("'#{line}' is not a value verbosity")
    end
  end
  listener.out.pl("verbosity: #{v}")
end