Class: Eve::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/eve/application.rb

Constant Summary collapse

@@cmd_data =
{
  "version" => [],
  "help" => [],
  "appid" => [],
  "shell" => [],
  "poll" => [ "set", "add", "rm" ],
  "event" => [],
  "remote" => [],
  "interrupt" => [],
  "register" => [ "add", "rm", "reload", "add-path", "rm-path", "path" ],
  "print" => [],
  "exit" => [],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Application

Returns a new instance of Application.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/eve/application.rb', line 22

def initialize(data = {})
  @redis = data[:redis] || Redis.new
  @console = Messenger.new( :display => data[:display] )
  @system_calls = Messenger.new( :display => data[:system_calls_display] || data[:display] )
  
  @message = Message.new(self)
  @poll = 5*60
  @poll_cmds = []
  @cmds = []
  @big_t = data[:big_t] || "1month"
  @mode = data[:mode] || :queue
  @server = data[:server] || "0.0.0.0"
  @port = data[:port] || "4848"
  @running = false
end

Instance Attribute Details

#big_tObject

Returns the value of attribute big_t.



6
7
8
# File 'lib/eve/application.rb', line 6

def big_t
  @big_t
end

#cmdsObject

Returns the value of attribute cmds.



6
7
8
# File 'lib/eve/application.rb', line 6

def cmds
  @cmds
end

#consoleObject

Returns the value of attribute console.



6
7
8
# File 'lib/eve/application.rb', line 6

def console
  @console
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/eve/application.rb', line 6

def id
  @id
end

#messageObject

Returns the value of attribute message.



6
7
8
# File 'lib/eve/application.rb', line 6

def message
  @message
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/eve/application.rb', line 6

def mode
  @mode
end

#pollObject

Returns the value of attribute poll.



6
7
8
# File 'lib/eve/application.rb', line 6

def poll
  @poll
end

#poll_cmdsObject

Returns the value of attribute poll_cmds.



6
7
8
# File 'lib/eve/application.rb', line 6

def poll_cmds
  @poll_cmds
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/eve/application.rb', line 6

def port
  @port
end

#redisObject

Returns the value of attribute redis.



6
7
8
# File 'lib/eve/application.rb', line 6

def redis
  @redis
end

#serverObject

Returns the value of attribute server.



6
7
8
# File 'lib/eve/application.rb', line 6

def server
  @server
end

#system_callsObject

Returns the value of attribute system_calls.



6
7
8
# File 'lib/eve/application.rb', line 6

def system_calls
  @system_calls
end

Class Method Details

.alive?(id) ⇒ Boolean

Returns:

  • (Boolean)


420
421
422
423
424
425
426
427
428
# File 'lib/eve/application.rb', line 420

def self.alive?(id)
  return false if id == "" || id.nil?
  begin
    Process.kill 0, id.to_i
    true
  rescue Exception => ex
    false
  end
end

.extract_arg(name, args, default = [], has_data = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eve/application.rb', line 43

def self.extract_arg(name,args,default = [],has_data = true)
  is_multi = default.kind_of?(Array)
  index = args.index(name)
  return default if index.nil?
  return is_multi ? [true] : true unless has_data
  
  results = []
  args.each_with_index.collect { |d,i| d == name ? i : nil }.compact.each do |index|
    val = ""
    args[index+1..-1].each do |data|
      break if data.start_with?("--")
      val += " #{data}"
    end
    results << val.strip
  end
  is_multi ? results : results.first
end

.ids(redis = Redis.new) ⇒ Object



416
417
418
# File 'lib/eve/application.rb', line 416

def self.ids(redis = Redis.new)
  redis.smembers("Eve.ApplicationIds")
end

.methodize(input) ⇒ Object



61
62
63
# File 'lib/eve/application.rb', line 61

def self.methodize(input)
  input.gsub(/-/,"_")
end

Instance Method Details

#call_appid(result, appid, display_messages) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/eve/application.rb', line 203

def call_appid(result,appid,display_messages)
  @id = appid == "" || appid.nil? ? $$.to_s : appid
  
  is_existing = @redis.sismember("Eve.ApplicationIds",@id)
  @redis.sadd("Eve.ApplicationIds",@id)
  result[:data] = @id
  @message.print_appid(@id)
  cmd("register reload",display_messages) if is_existing
  result
end

#call_event(result, data, display_messages) ⇒ Object



294
295
296
297
298
299
300
301
302
# File 'lib/eve/application.rb', line 294

def call_event(result, data, display_messages)
  @mode = :event
  @server,@port = data.split(":")
  @server ||= "0.0.0.0"
  @port ||= "4848"
  result[:data] = { :server => @server, :port => @port }
  @message.print_event_mode("#{@server}:#{@port}") if display_messages
  result
end

#call_exit(result, data, display_messages) ⇒ Object



251
252
253
254
255
# File 'lib/eve/application.rb', line 251

def call_exit(result,data,display_messages)
  result[:status] = :exited
  @message.print_exit if display_messages
  result
end

#call_help(result, data, display_messages) ⇒ Object



220
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/eve/application.rb', line 220

def call_help(result,data,display_messages)

  if data.nil? || data == ""
    result[:data] = @@cmd_data.dup
    @redis.smembers(lookup_id("registers")).each do |name|
      clazz = reg(name)
      if clazz.nil?
        @redis.srem(lookup_id("registers"),name)
      else
        result[:data].merge!(clazz.available)
      end
    end
  else
    clazz = reg(data)
    if clazz.nil?
      @message.print_unknown_handler(data) if display_messages
      result[:data] = {}
    else
      result[:data] = clazz.available  
    end
  end
  @message.print_help(result[:data],data) if display_messages && !result[:data].empty?
  result
end

#call_interrupt(result, appid, display_messages) ⇒ Object



98
99
100
101
102
# File 'lib/eve/application.rb', line 98

def call_interrupt(result,appid,display_messages)
  @redis.set(lookup_id("mode",appid),"shell")
  @message.print_app_interrupted(appid) if display_messages
  result
end

#call_poll(result, duration, display_messages) ⇒ Object



257
258
259
260
261
# File 'lib/eve/application.rb', line 257

def call_poll(result,duration,display_messages)
  result[:data] = @poll_cmds
  @message.print_poll_details(@poll,result[:data])
  result
end

#call_poll_add(result, cmd, display_messages) ⇒ Object



263
264
265
266
# File 'lib/eve/application.rb', line 263

def call_poll_add(result,cmd,display_messages)
  @poll_cmds << cmd
  result
end

#call_poll_rm(result, index, display_messages) ⇒ Object



268
269
270
271
272
273
274
275
276
# File 'lib/eve/application.rb', line 268

def call_poll_rm(result,index,display_messages)
  result[:data] = []
  if index.to_i.to_s == index
    @poll_cmds.delete_at(index.to_i)
  else
    @poll_cmds.delete(index)
  end
  result
end

#call_poll_set(result, duration, display_messages) ⇒ Object



278
279
280
281
282
283
284
285
# File 'lib/eve/application.rb', line 278

def call_poll_set(result,duration,display_messages)
  @mode = :poll
  in_secs = WatchMe::Timer.to_seconds(duration)
  result[:data] = in_secs
  @poll = in_secs
  @message.print_poll_mode(@id,in_secs) if display_messages
  result
end

#call_print(result, msg, display_messages) ⇒ Object



245
246
247
248
249
# File 'lib/eve/application.rb', line 245

def call_print(result,msg,display_messages)
  result[:data] = msg
  @console.print("#{msg}\n") if display_messages
  result
end

#call_register(result, data, display_messages) ⇒ Object


CALLER METHODS




137
138
139
140
141
142
143
144
# File 'lib/eve/application.rb', line 137

def call_register(result,data,display_messages)
  result[:data] = {}
  @redis.smembers(lookup_id("registers")).collect do |classname|
    result[:data][classname] = @redis.smembers(lookup_id("register.#{classname}.files"))
  end
  @message.print_registry(result[:data])
  result
end

#call_register_add(result, data, display_messages) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/eve/application.rb', line 146

def call_register_add(result,data,display_messages)
  parts = data.split
  classname = parts[0]
  filename = parts[1]
  if filename.nil? || filename == ""
    (cmd("register path",false)[:data]).each do |path|
      filename = "#{path}/#{classname.split("::").last.underscore}.rb"
      next unless p?(filename)
      filename = p(filename)
      break
    end
  end
  load filename  
  @redis.sadd(lookup_id("registers"),classname)
  @redis.sadd(lookup_id("register.#{classname}.files"),filename)
  result[:data] = true
  result
end

#call_register_add_path(result, path, display_messages) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/eve/application.rb', line 165

def call_register_add_path(result,path,display_messages)
  if p?(path)
    result[:data] = @redis.sadd(lookup_id("register.paths"),p(path))
  else
    result[:data] = false
  end
  result
end

#call_register_path(result, path, display_messages) ⇒ Object



180
181
182
183
184
# File 'lib/eve/application.rb', line 180

def call_register_path(result,path,display_messages)
  result[:data] = [ p("#{File.dirname(__FILE__)}/../registry") ] + @redis.smembers(lookup_id("register.paths"))
  @message.print_registry_path(result[:data]) if display_messages
  result
end

#call_register_reload(result, data, display_messages) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/eve/application.rb', line 192

def call_register_reload(result,data,display_messages)
  @redis.smembers(lookup_id("registers")).each do |classname|
    @redis.smembers(lookup_id("register.#{classname}.files")).each do |filename|
      load filename
    end
  end
  @message.print_registry_reloaded if display_messages
  result[:data] = true
  result
end

#call_register_rm(result, classname, display_messages) ⇒ Object



186
187
188
189
190
# File 'lib/eve/application.rb', line 186

def call_register_rm(result,classname,display_messages)
  result[:data] = @redis.srem(lookup_id("registers"),classname)
  @redis.del(lookup_id("register.#{classname}.files"))
  result
end

#call_register_rm_path(result, path, display_messages) ⇒ Object



174
175
176
177
178
# File 'lib/eve/application.rb', line 174

def call_register_rm_path(result,path,display_messages)
  normalized_path = p?(path) ? p(path) : path
  result[:data] = @redis.srem(lookup_id("register.paths"),normalized_path)
  result
end

#call_remote(result, cmd, display_messages) ⇒ Object



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
# File 'lib/eve/application.rb', line 304

def call_remote(result, cmd, display_messages)
  should_exit = false
  begin
    @telnet ||= Net::Telnet::new("Host" => @server, "Port" => @port, "Timeout" => 10, "Telnetmode" => false) # debugging "Output_log"=>"application_telnet.log"
    if cmd == "exit"
      should_exit = true
      @telnet.close
    else
      result[:data] = @telnet.cmd(cmd) do |data|
        data.sub!(/[^\n]*> $/,"") unless @running
        @console.print data  
      end
    end
  rescue Exception => ex
    @message.print_error("Telnet session failed", ex)
    should_exit = true
  end
  
  if should_exit
    @mode = :shell
    cmd("exit",display_messages)
    result[:status] = :exited
  end
  
  result
end

#call_shell(result, data, display_messages) ⇒ Object



287
288
289
290
291
292
# File 'lib/eve/application.rb', line 287

def call_shell(result,data,display_messages)
  @mode = :shell
  result[:data] = @mode
  @message.print_shell_mode if display_messages
  result
end

#call_version(result, data, display_messages) ⇒ Object



214
215
216
217
218
# File 'lib/eve/application.rb', line 214

def call_version(result,data,display_messages)
  result[:data] = Eve::VERSION
  @message.print_version(@id) if display_messages
  result
end

#cmd(raw, display_messages = true, external_cmd = false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/eve/application.rb', line 104

def cmd(raw,display_messages = true,external_cmd = false)
  result = default_result
  raw = "remote #{raw}" if @mode == :event
  begin
    action, data = action_and_input(raw)
  
    num_times_processed = 0
    if @@cmd_data.keys.include?(action)
      inner_action, inner_data = action_and_input(data)
      if @@cmd_data[action].include?(inner_action)
        result = send(Application.methodize("call_#{action}_#{inner_action}"), result, inner_data, display_messages)  
      else
        result = send(Application.methodize("call_#{action}"), result, data, display_messages)  
      end
      num_times_processed += 1
    end
    num_times_processed, result = delegate(num_times_processed,result,raw,display_messages) unless @id.nil?
    
    if num_times_processed == 0 && !action.empty?
      @message.print_unknown_cmd(raw)
    end
    
    @cmds << raw if external_cmd && result[:track_cmd]
  rescue Exception => ex
    @message.print_cmd_error(raw,ex)
  end
  result
end

#default_resultObject



65
66
67
# File 'lib/eve/application.rb', line 65

def default_result
  { :data => nil, :status => :continue, :track_cmd => true }
end

#delegate(num_times_processed, result, cmd, display_messages) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/eve/application.rb', line 344

def delegate(num_times_processed,result,cmd,display_messages)
  all_results = num_times_processed > 0 ? [ result.dup ] : []
  @redis.smembers(lookup_id("registers")).each do |classname|
    all_names = classname.split("::")
    clazz = reg(classname)
    next unless clazz.handle?(cmd)
    all_results << clazz.handle(self,cmd,display_messages)
    num_times_processed += 1
  end
  result[:data] = nil if num_times_processed == 0
  result[:data] = all_results[0][:data] if num_times_processed == 1
  result[:data] = all_results if num_times_processed > 1
  [ num_times_processed, result ]
end

#enqueue_cmd(raw) ⇒ Object



359
360
361
# File 'lib/eve/application.rb', line 359

def enqueue_cmd(raw)
  @redis.rpush(lookup_id("next_cmds"),raw)
end

#handle_args(args) ⇒ Object



379
380
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
# File 'lib/eve/application.rb', line 379

def handle_args(args)
  scripts = []
  
  is_event = false
  Application.extract_arg("--event",args).each do |event|
    is_event = true
    scripts << "event #{event}"
  end

  index = args.index("--interrupt")
  unless index.nil?
    scripts << "interrupt #{args[index+1]}"
    scripts << "exit"
    return scripts
  end

  index = args.index("--id")
  if !index.nil?
    scripts << "appid #{args[index + 1]}"
  elsif @id.nil? && !is_event
    scripts << "appid"
  end
  
  Application.extract_arg("--register",args).each { |register| scripts << "register add #{register}" }

  unless is_event
    Application.extract_arg("--poll",args).each { |poll| scripts << "poll #{poll}" }
    scripts << "shell" unless Application.extract_arg("--shell",args,nil,false).nil?
  end

  Application.extract_arg("--run",args).each do |cmd|
    scripts << cmd
  end

  scripts
end

#historyObject



436
437
438
# File 'lib/eve/application.rb', line 436

def history
  @console.history
end

#lookup_id(sub_id, appid = nil) ⇒ Object



430
431
432
433
434
# File 'lib/eve/application.rb', line 430

def lookup_id(sub_id, appid = nil)
  raise "Application ID not set yet" if appid.nil? && @id.nil?
  appid ||= @id
  "Eve.#{appid}.#{sub_id}"
end

#next_cmds(scripts = []) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/eve/application.rb', line 363

def next_cmds(scripts = [])
  case(@mode)
    when :event
      scripts << user_input
    when :poll
      scripts += @poll_cmds
      scripts << "sleep #{@poll}"
    when :shell
      scripts << user_input
    when :queue
      the_cmd = @redis.lpop(lookup_id("next_cmds")) || "exit"
      scripts << the_cmd
  end
  scripts
end

#post_initObject


EVENT MACHINE




73
74
75
# File 'lib/eve/application.rb', line 73

def post_init
  @message.print_server_init
end

#receive_data(data) ⇒ Object



77
78
79
# File 'lib/eve/application.rb', line 77

def receive_data(data)
  cmd(data,true,true)
end

#reg(classname) ⇒ Object


HELPER METHODS




335
336
337
338
339
340
341
342
# File 'lib/eve/application.rb', line 335

def reg(classname)
  begin
    all_names = classname.split("::")
    all_names.size == 1 ? Object::const_get(classname) : eval(all_names[0...-1].join("::")).const_get(all_names.last)
  rescue
    nil
  end
end

#resetObject



38
39
40
41
# File 'lib/eve/application.rb', line 38

def reset
  @cmds = []
  @console.reset
end

#run(args = []) ⇒ Object


MAIN DRIVER - RUN / CMD




85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/eve/application.rb', line 85

def run(args = [])
  @running = true
  scripts = ["version"] + handle_args(args)
  loop do
    scripts.each do |script|
      status = cmd(script,true,true)[:status]
      status = cmd("exit") if status == :should_exit
      return if status == :exited
    end
    scripts = next_cmds
  end
end

#system_call(input) ⇒ Object



440
441
442
443
# File 'lib/eve/application.rb', line 440

def system_call(input)
  @message.print_system_call(input)
  `#{input}`
end

#user_inputObject



445
446
447
448
449
# File 'lib/eve/application.rb', line 445

def user_input
  @console.print "> " unless @mode == :event
  STDOUT.flush
  STDIN.gets.chomp
end