Class: AppMgr

Inherits:
Object
  • Object
show all
Defined in:
lib/app-mgr.rb

Overview

file: app-mgr.rb

Instance Method Summary collapse

Constructor Details

#initializeAppMgr

Returns a new instance of AppMgr.



5
6
7
8
9
# File 'lib/app-mgr.rb', line 5

def initialize()
  @app = {}
  @content_type = 'text/xml'
  super()
end

Instance Method Details

#availableObject



61
62
63
# File 'lib/app-mgr.rb', line 61

def available()
  @app.select {|k,v| v[:available] == true}.keys
end

#connect(app_name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/app-mgr.rb', line 32

def connect(app_name)
  app = @app[app_name]
  if app and app[:running] then 
    yield(app[:public])
  else
    return "app %s not running" % app_name
  end
end

#execute(app_name, method, params = '') ⇒ Object



41
42
43
# File 'lib/app-mgr.rb', line 41

def execute(app_name, method, params='')
  @app[app_name][:running].method('call_' + method.gsub(/-/,'_').to_sym).call(params)
end

#load(opt = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/app-mgr.rb', line 11

def load(opt={})        
  o = {name: '', object: nil, config: {}, public: {}}.merge!(opt)

  key, name = o.shift
  @app[name] = o
  "'%s' loaded" % name
end

#projectx_handler(xml_project) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/app-mgr.rb', line 95

def projectx_handler(xml_project)
  out = ''
  doc = Document.new(xml_project)
  project_name = doc.root.attribute('name').to_s

  if self.running? project_name then

    out = XPath.match(doc.root, 'methods/method').map  do |node_method|
      methodx = node_method.attributes.get_attribute('name').to_s
      params = node_method.elements['params'].to_s

      method_out, @content_type = self.execute(project_name, methodx, params)
      method_out
    end

    out = out.first if out.length == 1
  else
    out = "that project doesn't exist"
  end
  
  @content_type ||= 'text/xml'

  [out, @content_type]
end

#run(app_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/app-mgr.rb', line 19

def run(app_name)
  app = @app[app_name]
  if app then
    options = {public: app[:public]}
    options[:config] = app[:config] unless app[:config].empty?
    app[:running] = app[:object].new(options, self)

    return "'%s' running ..." % app_name
  else
    return "app %s not available" % app_name
    end      
end

#run_projectx(project_name, method_name, qparams = []) ⇒ Object



88
89
90
91
92
93
# File 'lib/app-mgr.rb', line 88

def run_projectx(project_name, method_name, qparams=[])

  params = "<params>%s</params>" % qparams.map{|k,v| "<param var='%s'>%s</param>" % [k,v]}.join
  xml_project = project = "<project name='%s'><methods><method name='%s'>%s</method></methods></project>" % [project_name, method_name, params]
  projectx_handler(xml_project)
end

#runningObject



53
54
55
# File 'lib/app-mgr.rb', line 53

def running()
  @app.select {|k,v| v[:running]}.keys
end

#running?(app_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/app-mgr.rb', line 57

def running?(app_name)
  @app[app_name][:running]
end

#show_public_methods(app_name) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/app-mgr.rb', line 78

def show_public_methods(app_name)
  app = @app[app_name]
  if app and app[:running] then
    return app[:running].public_methods.map(&:to_s).grep(/call_/).map {|x| x.gsub(/call_/,'').gsub(/_/,'-')}.sort.join(', ')
  else
    return "app %s not running" % app_name
  end
end

#stop(app_name) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/app-mgr.rb', line 45

def stop(app_name)
  if @app[app_name].delete(:running) then
    return "app %s stopped" % app_name
  else
    return "couldn't find app %s" % app_name
  end
end

#unload(app_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/app-mgr.rb', line 65

def unload(app_name)

  handler_name = @app[app_name][:running].name
  
  if handler_name then
    Object.send(:remove_const, handler_name)
    @app.delete app_name
    return "app %s unloaded" % app_name
  else
    return "couldn't find app %s" % app_name
  end
end