Class: AgentXmpp::BaseController

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_xmpp/client/controller.rb

Overview


Direct Known Subclasses

Controller

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipe, params) ⇒ BaseController

.….….….….….….….….….….….….….….….….….….….….….….….….….….



101
102
103
104
# File 'lib/agent_xmpp/client/controller.rb', line 101

def initialize(pipe, params)
  @params, @pipe = params, pipe
  @params_list, @submits = [params], []
end

Class Attribute Details

.before_filtersObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



17
18
19
# File 'lib/agent_xmpp/client/controller.rb', line 17

def before_filters
  @before_filters
end

.commands_listObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



17
18
19
# File 'lib/agent_xmpp/client/controller.rb', line 17

def commands_list
  @commands_list
end

.commands_list_mutexObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



17
18
19
# File 'lib/agent_xmpp/client/controller.rb', line 17

def commands_list_mutex
  @commands_list_mutex
end

.routesObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



17
18
19
# File 'lib/agent_xmpp/client/controller.rb', line 17

def routes
  @routes
end

Instance Attribute Details

#paramsObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



98
99
100
# File 'lib/agent_xmpp/client/controller.rb', line 98

def params
  @params
end

#params_listObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



98
99
100
# File 'lib/agent_xmpp/client/controller.rb', line 98

def params_list
  @params_list
end

#pipeObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



98
99
100
# File 'lib/agent_xmpp/client/controller.rb', line 98

def pipe
  @pipe
end

#routeObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



98
99
100
# File 'lib/agent_xmpp/client/controller.rb', line 98

def route
  @route
end

#submitsObject (readonly)

.….….….….….….….….….….….….….….….….….….….….….….….….….….



98
99
100
# File 'lib/agent_xmpp/client/controller.rb', line 98

def submits
  @submits
end

Class Method Details

.add_before_filter(msg_type, nodes) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



56
57
58
# File 'lib/agent_xmpp/client/controller.rb', line 56

def add_before_filter(msg_type, nodes)
  (before_filters[msg_type] ||= []).push(nodes).last
end

.add_command_to_list(session_id, controller) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



81
82
83
84
85
# File 'lib/agent_xmpp/client/controller.rb', line 81

def add_command_to_list(session_id, controller)
  commands_list_mutex.synchronize do
    commands_list[session_id] = {:controller=>controller, :created_at=>Time.now}
  end
end

.before(args = nil, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



38
39
40
41
# File 'lib/agent_xmpp/client/controller.rb', line 38

def before(args=nil, &blk)
  args = {:command => :all, :event => :all, :chat => :all} if args.nil? or args.eql?(:all)
  args.each {|(msg_type, nodes)| add_before_filter(msg_type, {:nodes => nodes, :blk => blk})}
end

.chat(opts = {}, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



33
34
35
# File 'lib/agent_xmpp/client/controller.rb', line 33

def chat(opts = {}, &blk) 
  route(:chat, {:opts => opts, :blk => blk})
end

.command(node, opts = {}, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….…. application interface .….….….….….….….….….….….….….….….….….….….….….….….….….….



22
23
24
# File 'lib/agent_xmpp/client/controller.rb', line 22

def command(node, opts = {}, &blk) 
  route(:command, {:node => node, :opts => opts, :blk => blk}) 
end

.command_nodes(jid = nil) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



61
62
63
64
65
66
67
68
# File 'lib/agent_xmpp/client/controller.rb', line 61

def command_nodes(jid=nil)
  (routes[:command] ||= []).inject([]) do |nodes, route|
    if jid and not AgentXmpp.(jid)
      groups, access = Contact.find_by_jid(jid)[:groups], [route[:opts][:access] || []].flatten
      (access.empty? or access.any?{|a| groups.include?(a)}) ? nodes << route[:node] : nodes
    else; nodes << route[:node]; end
  end
end

.event(jid, node, opts = {}, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



27
28
29
30
# File 'lib/agent_xmpp/client/controller.rb', line 27

def event(jid, node, opts = {}, &blk) 
  j = Xmpp::Jid.new(jid)
  route(:event, {:node => "/home/#{j.domain}/#{j.node}/#{node}", :domain => j.domain, :opts => opts, :blk => blk}) 
end

.event_domainsObject

.….….….….….….….….….….….….….….….….….….….….….….….….….….



76
77
78
# File 'lib/agent_xmpp/client/controller.rb', line 76

def event_domains
  (routes[:event] ||= []).map{|r| r[:domain]}.uniq
end

.include_module(mod) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



44
45
46
# File 'lib/agent_xmpp/client/controller.rb', line 44

def include_module(mod)
  include(mod)
end

.remove_command_from_list(session_id) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



88
89
90
91
92
# File 'lib/agent_xmpp/client/controller.rb', line 88

def remove_command_from_list(session_id)
  if commands_list[session_id]
    commands_list_mutex.synchronize{commands_list.delete(session_id)}
  end
end

.route(msg_type, nroute) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….…. managment .….….….….….….….….….….….….….….….….….….….….….….….….….….



51
52
53
# File 'lib/agent_xmpp/client/controller.rb', line 51

def route(msg_type, nroute)
  (routes[msg_type] ||= []).push(nroute).last
end

.subscriptions(service) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



71
72
73
# File 'lib/agent_xmpp/client/controller.rb', line 71

def subscriptions(service)
  (routes[:event] ||= []).inject([]){|s,r| /#{r[:domain]}/.match(service) ? s << r[:node] : s}
end

Instance Method Details

#apply_before_filters(msg_type, node = nil) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….…. filters .….….….….….….….….….….….….….….….….….….….….….….….….….….



368
369
370
371
372
373
374
375
376
# File 'lib/agent_xmpp/client/controller.rb', line 368

def apply_before_filters(msg_type, node=nil)
  (BaseController.before_filters[msg_type] || []).inject([]) do |fs, f|
    nodes = [f[:nodes]].flatten
    (nodes.include?(node) or nodes.include?(:all)) ? fs << f : fs
  end.inject(true) do |r,f|
    define_meta_class_method(:filter, &f[:blk])
    r and filter
  end
end

#command_completedObject

.….….….….….….….….….….….….….….….….….….….….….….….….….….



215
216
217
218
# File 'lib/agent_xmpp/client/controller.rb', line 215

def command_completed
  Xmpp::IqCommand.send_command(:to=>params[:from], :node=>params[:node], :iq_type=>:result, :status=>:completed, 
                               :id => params[:id], :sessionid => params[:sessionid])
end

#command_request(args, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



221
222
223
224
# File 'lib/agent_xmpp/client/controller.rb', line 221

def command_request(args, &blk)
  raise ArgmentError ':to and :node are required' unless args[:to] and args[:node]
  Xmpp::IqCommand.send_command(:to=>args[:to], :node=>args[:node], :iq_type=>:set, :action=>:execute, :payload=>args[:payload], &blk)
end

#delegate_methodsObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



174
175
176
# File 'lib/agent_xmpp/client/controller.rb', line 174

def delegate_methods
  @delegate_methods ||= AgentXmpp::Delegate.new
end

#delegate_to(methods) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



210
211
212
# File 'lib/agent_xmpp/client/controller.rb', line 210

def delegate_to(methods)
  delegate_methods.add_delegate_methods(methods); delegate_methods
end

#error(err, *args) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….…. application interface .….….….….….….….….….….….….….….….….….….….….….….….….……



191
192
193
# File 'lib/agent_xmpp/client/controller.rb', line 191

def error(err, *args)
  AgentXmpp::Error.new(err, *args)
end

#flush_messagesObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



184
185
186
# File 'lib/agent_xmpp/client/controller.rb', line 184

def flush_messages
  pipe.send_resp(messages); messages.clear
end

#invoke_chatObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/agent_xmpp/client/controller.rb', line 158

def invoke_chat
  @route = chat_route
  unless route.nil?
    define_meta_class_method(:request_handler, &route[:blk])
  else
    define_meta_class_method(:request_handler) do
      "#{AgentXmpp::AGENT_XMPP_NAME} #{AgentXmpp::VERSION}, #{AgentXmpp::OS_VERSION}"
    end
  end
  define_meta_class_method(:request_callback) do |result|
    add_payload_to_container(result) if result.kind_of?(String)
  end
  process_request
end

#invoke_commandObject

.….….….….….….….….….….….….….….….….….….….….….….….….….…. internal interface .….….….….….….….….….….….….….….….….….….….….….….….….……



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/agent_xmpp/client/controller.rb', line 116

def invoke_command
  params[:sessionid] ||= Xmpp::IdGenerator.generate_id
  invoke_command_on_route do
    define_meta_class_method(:request, &route[:blk])
    define_meta_class_method(:request_handler) do  
      run_command(request)  
    end
    define_meta_class_method(:request_callback) do |*resp|
      resp = resp.length.eql?(1)  ? resp.first : resp  
      add_payload_to_container(resp)
    end
    process_request
  end
end

#invoke_command_nextObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



132
133
134
135
136
137
138
139
140
141
# File 'lib/agent_xmpp/client/controller.rb', line 132

def invoke_command_next
  invoke_command_on_route do
    define_meta_class_method(:request_handler) do  
      if params[:x_data_type].eql?(:submit)
        on_submit
      end
    end
    process_request
  end
end

#invoke_eventObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/agent_xmpp/client/controller.rb', line 144

def invoke_event
  @route = get_route(:event)
  unless route.nil?
    define_meta_class_method(:request, &route[:blk])
    define_meta_class_method(:request_handler) do
      request; delegate_methods.delegate(pipe, self); flush_messages
    end
    process_request
  else
    AgentXmpp.logger.error "ROUTING ERROR: no route for {:node => '#{params[:node]}'}."
  end
end

#messagesObject

.….….….….….….….….….….….….….….….….….….….….….….….….……



179
180
181
# File 'lib/agent_xmpp/client/controller.rb', line 179

def messages
  @messages ||= []
end

#next(params) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….….….



107
108
109
110
111
# File 'lib/agent_xmpp/client/controller.rb', line 107

def next(params)
  @params = params
  @params_list << params
  self
end

#on(action, opts = {}, &blk) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



196
197
198
199
200
201
202
# File 'lib/agent_xmpp/client/controller.rb', line 196

def on(action, opts= {}, &blk)
  if action.eql?(:submit)
    @submits << {:opts=>opts, :blk=>blk}
  else
    define_meta_class_method(("on_"+action.to_s).to_sym, &blk)
  end
end

#on_submitObject

.….….….….….….….….….….….….….….….….….….….….….….….….….…. managment .….….….….….….….….….….….….….….….….….….….….….….….….……



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/agent_xmpp/client/controller.rb', line 229

def on_submit
  unless(sub = submits.shift).nil?
    update_command_list 
    blk, guard = sub[:blk], sub[:opts][:guard] 
    call_blk = lambda{|| blk.arity.eql?(1) ? blk.call(Xmpp::XData.new('form')) : blk.call}
    result = if guard.nil?
               call_blk[]
             else  
               (guard.arity.eql?(1) ? guard.call(self) : guard.call) ? call_blk[] : on_submit
             end
    result.nil? ? on_submit : result         
  end
end

#xmpp_msg(msg) ⇒ Object

.….….….….….….….….….….….….….….….….….….….….….….….….……



205
206
207
# File 'lib/agent_xmpp/client/controller.rb', line 205

def xmpp_msg(msg)
  messages << msg
end