lib/appcelerator/dispatcher.rb in appcelerator-2.0.1.1 vs lib/appcelerator/dispatcher.rb in appcelerator-2.0.2
- old
+ new
@@ -1,68 +1,68 @@
module Appcelerator
- class Dispatcher
- include Singleton
+ module Dispatcher
+
+ # dispatch a standard request, called by the service_broker_controller
+ #
+ def self.dispatch_request(request, response, session)
+ message_queue = []
+ extract_messages(request, session) do |in_message|
+ out_messages = ServiceBroker.send(in_message)
+ message_queue.concat(out_messages)
+ end
+ serialize(message_queue, session.session_id, response.body)
+ end
- def serialize(session,body)
- queue = session['_app_mq']
- body << '<?xml version="1.0"?>'
- body << "<messages version='1.0' sessionid='#{session.session_id}'>"
- if queue
- queue.each do |msg|
- body << msg
- end
- session['_app_mq']=[]
- end
- body << '</messages>'
- end
-
- def outgoing(obj,type,message)
- queue = obj['session']['_app_mq']
- if !queue
- queue = [];
- obj['session']['_app_mq'] = queue
- end
- #TODO requestid
- queue << "<message requestid='' direction='OUTGOING' datatype='JSON' type='#{type}'><![CDATA["
- queue << message.to_json
- queue << ']]></message>'
- end
-
- def incoming(session,request,response)
- body = request.env['RAW_POST_DATA']
- if body and body != ''
- node = REXML::Document.new(body)
- node.root.each_element('//message') do |message|
- requestid = message.attributes['requestid']
- type = message.attributes['type']
- text = message.text
- msg = JSON.parse(text)
- req = {'requestid'=>requestid, 'session'=>session, 'orig_request'=>request}
- ServiceBroker.send(req,type,msg)
- end
- end
- end
- end
-end
-
-
-
-# this is the rails dispatcher
-require 'dispatcher'
-
-module ClassMethods
- def reset_application_and_services!
- Appcelerator::Service.load_services
- reset_application_but_not_services!
- end
-end
-
-Dispatcher.extend(ClassMethods)
-Dispatcher.class_eval do
- class << self
- unless method_defined? :reset_application_but_not_services!
- alias_method :reset_application_but_not_services!, :reset_application!
+ # dispatch a message that was generated in some other way, like the upload_controller
+ #
+ def self.dispatch_message(request, response, session, message_type, params, request_id)
+ msg = Message.new(request, session, message_type, params, request_id)
+
+ message_queue = ServiceBroker.send(msg)
+
+ serialize(message_queue, session.session_id, response.body)
end
- alias_method :reset_application!, :reset_application_and_services!
+
+ def self.extract_messages(request, session)
+ body = request.env['RAW_POST_DATA']
+ if body and body != ''
+ node = REXML::Document.new(body)
+ node.root.each_element('//message') do |message|
+
+ request_id = message.attributes['requestid']
+ message_type = message.attributes['type']
+ params = JSON.parse(message.text)
+
+ yield Message.new(request, session, message_type, params, request_id)
+ end
+ end
+ end
+
+ def self.serialize(message_queue, session_id, output)
+ output << '<?xml version="1.0"?>'
+ output << "<messages version='1.0' sessionid='#{session_id}'>"
+
+ message_queue.compact!
+ message_queue.each do |msg|
+ if msg.response_type
+ output << "<message requestid='#{msg.request_id}' direction='OUTGOING' datatype='JSON' type='#{msg.response_type}'><![CDATA["
+ output << msg.response.to_json
+ output << ']]></message>'
+ end
+ end
+
+ output << '</messages>'
+ end
+
+ class Message
+ attr_accessor :request, :session, :message_type, :params, :request_id, :response_type, :response
+ def initialize(request, session, message_type, params, request_id)
+ @request = request
+ @session = session
+ @message_type = message_type
+ @params = params
+ @request_id = request_id
+ end
+ end
end
end