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

- (BaseController) initialize(pipe, params)

.........................................................................................................



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

+ (Object) before_filters (readonly)

.........................................................................................................



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

def before_filters
  @before_filters
end

+ (Object) commands_list (readonly)

.........................................................................................................



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

def commands_list
  @commands_list
end

+ (Object) commands_list_mutex (readonly)

.........................................................................................................



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

def commands_list_mutex
  @commands_list_mutex
end

+ (Object) routes (readonly)

.........................................................................................................



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

def routes
  @routes
end

Instance Attribute Details

- (Object) params (readonly)

.........................................................................................................



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

def params
  @params
end

- (Object) params_list (readonly)

.........................................................................................................



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

def params_list
  @params_list
end

- (Object) pipe (readonly)

.........................................................................................................



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

def pipe
  @pipe
end

- (Object) route (readonly)

.........................................................................................................



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

def route
  @route
end

- (Object) submits (readonly)

.........................................................................................................



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

def submits
  @submits
end

Class Method Details

+ (Object) add_before_filter(msg_type, nodes)

.........................................................................................................



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

+ (Object) add_command_to_list(session_id, controller)

.........................................................................................................



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

+ (Object) before(args = nil, &blk)

.........................................................................................................



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

+ (Object) chat(opts = {}, &blk)

.........................................................................................................



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

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

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

......................................................................................................... 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

+ (Object) command_nodes(jid = nil)

.........................................................................................................



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

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

.........................................................................................................



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

+ (Object) event_domains

.........................................................................................................



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

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

+ (Object) include_module(mod)

.........................................................................................................



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

def include_module(mod)
  include(mod)
end

+ (Object) remove_command_from_list(session_id)

.........................................................................................................



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

+ (Object) route(msg_type, nroute)

......................................................................................................... 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

+ (Object) subscriptions(service)

.........................................................................................................



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

- (Object) apply_before_filters(msg_type, node = nil)

......................................................................................................... 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

- (Object) command_completed

.........................................................................................................



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

- (Object) command_request(args, &blk)

.........................................................................................................



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

- (Object) delegate_methods

.......................................................................................................



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

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

- (Object) delegate_to(methods)

.......................................................................................................



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

- (Object) error(err, *args)

......................................................................................................... application interface .......................................................................................................



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

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

- (Object) flush_messages

.......................................................................................................



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

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

- (Object) invoke_chat

.......................................................................................................



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

- (Object) invoke_command

......................................................................................................... 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

- (Object) invoke_command_next

.......................................................................................................



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

- (Object) invoke_event

.......................................................................................................



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

- (Object) messages

.......................................................................................................



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

def messages
  @messages ||= []
end

- (Object) next(params)

.........................................................................................................



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

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

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

.......................................................................................................



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

- (Object) on_submit

......................................................................................................... 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

- (Object) xmpp_msg(msg)

.......................................................................................................



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

def xmpp_msg(msg)
  messages << msg
end