adaptation 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -39,3 +39,10 @@
39
39
  will send messages to other subscribers.
40
40
  * 0.1.3
41
41
  - solved loop that comsuned lots of cpu in mom command
42
+ * 0.1.4
43
+ - subscribers with a message buffer too; mom just delivers messages without waiting
44
+ subscriber to finnish oprocessing it
45
+ - if when subscribing desired address is in use just log it and return
46
+ - settings.yml template improved
47
+ - added support to subscribe/publish to Xmlblaster (http://www.xmlblaster.org)
48
+ via XML-RPC
data/bin/mom CHANGED
@@ -24,25 +24,16 @@ if %w(--file -f).include? ARGV.first
24
24
  file = ARGV[1]
25
25
  end
26
26
 
27
- #if %w(--environment -e).include? ARGV.first
28
- # environment = ARGV[1]
29
- #end
30
- environment = "development"
27
+ mom = "druby"
31
28
 
32
29
  if file.nil?
33
- config = {"mom" => {"type" => "druby", "host" => DEFAULT_HOST, "port" => DEFAULT_PORT}}
30
+ config = {"mom" => {"host" => DEFAULT_HOST, "port" => DEFAULT_PORT, "topics" => "all"}}
34
31
  else
35
- config = YAML::load(File.open(file))[environment]
32
+ config = YAML::load(File.open(file))[mom]
36
33
  end
37
34
 
38
- Signal.trap("INT") { puts "Shutting down MOM server (#{config["mom"]["type"]})"; exit }
35
+ Signal.trap("INT") { puts "Shutting down MOM server (#{mom})"; exit }
39
36
 
40
- case config["mom"]["type"]
41
- when "druby"
42
- mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
43
- mom = Adaptation::Mom::Mom.new mom_uri
44
- mom.start
45
- #when "xmlblaster"
46
- else
47
- puts "Unknown MOM server type: #{config["mom"]["type"]}"
48
- end
37
+ mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
38
+ mom = Adaptation::Mom::Mom.new mom_uri
39
+ mom.start
data/bin/publish ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ unless defined? ADAPTOR_ROOT
3
+ ADAPTOR_ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
4
+ end
5
+ require 'rubygems'
6
+ require 'adaptation'
7
+
8
+ require 'commands/publish'
data/configs/mom.yml CHANGED
@@ -1,10 +1,23 @@
1
- development:
1
+ druby:
2
2
  mom:
3
- type: druby
4
3
  port: 8080
5
4
  host: 127.0.0.1
6
5
  subscriber:
7
- type: druby
8
6
  port: 8081
9
7
  host: 127.0.0.1
10
8
  topics: all
9
+
10
+ xmlblaster:
11
+ mom:
12
+ port: 8080
13
+ host: 127.0.0.1
14
+ publish_user: publisher
15
+ publish_password: secret
16
+ subscriber:
17
+ port: 8081
18
+ host: 127.0.0.1
19
+ public_host:
20
+ public_port:
21
+ topics: TOPIC1, TOPIC2
22
+ subscriber_user: subscriber
23
+ subscriber_password: secret
data/configs/settings.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  development:
2
- group: group_id
3
- application: APPLICATION_ID
2
+ # setting: value
3
+ # The line above makes possible to access the setting
4
+ # in the Adaptor class with: $config["setting"]
4
5
  test:
5
- group: group_id
6
- application: APPLICATION_ID
6
+ # setting: test value
@@ -29,9 +29,12 @@ module Adaptation
29
29
  end
30
30
 
31
31
  xml = message_object.to_xml.to_s.gsub("'", "\"")
32
- unless system("#{$config["oappublish"]} '#{$config["application"]}' '#{xml}'")
33
- puts "Problem publishing: #{xml}"
32
+ publish_method = $config["oappublish"] || "#{ADAPTOR_ROOT}/script/publish"
33
+ topic = $config["application"] || "ADAPTATION"
34
+ unless system("#{publish_method} '#{$config["application"]}' '#{xml}'")
35
+ logger.error "Problem publishing: #{xml}"
34
36
  end
37
+
35
38
  end
36
39
 
37
40
  def self.get_class_object(adaptor_class) # nodoc
@@ -7,39 +7,33 @@ module Adaptation
7
7
 
8
8
  class DrubySubscriber
9
9
 
10
+ # constructor, called from the subscribe command
10
11
  def initialize subscriber_uri, mom_uri, topics
11
12
  @subscriber_uri = subscriber_uri
12
13
  @mom_uri = mom_uri
13
14
  @topics = topics
14
- @adaptors_list = Array.new
15
+ @messages = []
15
16
  end
16
17
 
17
- def process message, topic
18
- if ( (@topics.include?(topic)) or (@topics.include?("all")) )
19
- system("ruby public/dispatch.rb '#{message}'")
20
- end
21
- puts "#{topic} => #{message}"
18
+ # method to receive messages, called from the mom
19
+ def send_message message, topic
20
+ # Insert message into messages buffer, and awake
21
+ # message processor (@sleeper) if paused
22
+ puts "-----------------------------------"
23
+ puts "Received message in topic: #{topic}"
24
+ puts "#{message}"
25
+ puts "-----------------------------------"
26
+ @messages << {:message => message, :topic => topic}
27
+ @sleeper.run if @sleeper.stop?
22
28
  end
23
-
29
+
24
30
  def subscription_result subscribed
25
31
  if subscribed
26
32
  puts "Subscribed to mom (#{@mom_uri}). Listening at #{@subscriber_uri}"
27
33
  end
28
34
  end
29
35
 
30
- # TODO: Think what exactly is this subscriber:
31
- # - path to file
32
- # - combination of a path and a set of conditions <- looks better...
33
- # - ...
34
- def note_me_down adaptor
35
- unless @adaptors_list.include? adaptor
36
- @adaptors_list << adaptor
37
- puts "Added adaptor: #{adaptor}"
38
- end
39
- end
40
-
41
36
  def start
42
- uri_owner = false
43
37
  begin
44
38
  # try to start the subscriber service,
45
39
  # using the uri specified in config/mom.yml
@@ -48,33 +42,33 @@ module Adaptation
48
42
  # subscribe that uri to the mom
49
43
  mom = DRbObject.new(nil, @mom_uri)
50
44
  mom.subscribe @subscriber_uri
51
- uri_owner = true
52
45
  rescue Exception => e
53
- # desired uri already in use...
54
- # if the process using it is a subscriber, this
55
- # shouldn't be a problem
56
- uri_owner = false
46
+ # desired uri already in use
47
+ puts "Couldn't start subscriber at #{@subscriber_uri}. Address already in use?"
48
+ return
57
49
  end
50
+
51
+ @sleeper = Thread.new{
52
+ loop do
58
53
 
59
- # try to tell to the subscriber using the uri
60
- # (may be this self instance of DrubySubscriber),
61
- # that we want to be executed when the MOM
62
- # calls its process method
63
- begin
64
- subscriber = DRbObject.new(nil, @subscriber_uri)
65
- routes_file = File.expand_path(File.dirname(__FILE__) + '/../config/routes.rb')
66
- subscriber.note_me_down routes_file
67
- rescue Exception => e
68
- # the process using @subscriber_uri is not
69
- # an instance of DrubySubsciber...
70
- puts "Couldn't start or find subscriber at #{@subscriber_uri}:"
71
- puts "#{e}"
72
- return
73
- end
54
+ # process all messages
55
+ while !@messages.empty?
56
+ @messages.each do |message|
57
+ if ( (@topics.include?(message[:topic])) or (@topics.include?("all")) )
58
+ system("ruby public/dispatch.rb '#{message[:message]}'")
59
+ end
60
+ @messages.delete message
61
+ end
62
+ end
74
63
 
75
- if uri_owner
76
- DRb.thread.join # Don't exit just yet!
77
- end
64
+ # go to sleep
65
+ Thread.stop
66
+
67
+ end
68
+ }
69
+
70
+ @sleeper.join
71
+ Drb.thread.join
78
72
 
79
73
  end
80
74
 
@@ -16,13 +16,15 @@ module Adaptation
16
16
  unless get_subscribers.include?(drb_uri)
17
17
  add_subscriber drb_uri
18
18
  puts "Added new subscriber: #{drb_uri}"
19
- oapdaemon = DRbObject.new(nil, drb_uri)
20
- oapdaemon.subscription_result true
21
19
  end
20
+
21
+ oapdaemon = DRbObject.new(nil, drb_uri)
22
+ oapdaemon.subscription_result(true)
22
23
  end
23
24
 
24
25
  def publish message, topic
25
- # Tell subscribed hosts to execute their adaptors
26
+ # Insert message into messages buffer, and awake
27
+ # deliverer process (@sleeper) if paused
26
28
  puts "-----------------------------------"
27
29
  puts "Received message in topic: #{topic}"
28
30
  puts "#{message}"
@@ -46,7 +48,7 @@ module Adaptation
46
48
  puts "Calling #{uri}"
47
49
  DRb.start_service
48
50
  oapdaemon = DRbObject.new(nil, uri)
49
- oapdaemon.process message[0], message[1]
51
+ oapdaemon.send_message message[0], message[1]
50
52
  rescue
51
53
  puts "Couldn't send message to subscriber: #{uri}"
52
54
  end
@@ -0,0 +1,141 @@
1
+ require "adaptation/xmlblaster/xmlblaster_client.rb"
2
+ require "adaptation/xmlblaster/xmlblaster_callback_server.rb"
3
+
4
+ class XmlblasterCallbackClient < XmlblasterClient
5
+
6
+ attr_reader :callback_server
7
+
8
+ def initialize( xmlblaster_ip = nil, xmlblaster_port = "8080", callback_ip = "127.0.0.1", callback_port = "8081", callback_public_ip = nil, callback_public_port = nil, audit = nil)
9
+ super xmlblaster_ip, xmlblaster_port, audit
10
+ @xmlblaster_ip = xmlblaster_ip
11
+ @xmlblaster_port = xmlblaster_port
12
+ @callback_ip = callback_ip
13
+ @callback_port = callback_port
14
+ @callback_public_ip = callback_public_ip || @callback_ip
15
+ @callback_public_port = callback_public_port || @callback_port
16
+ @callback_server = nil
17
+ @audit = audit
18
+
19
+ begin
20
+ @callback_server = XmlblasterCallbackServer.new( @callback_ip, @callback_port, @callback_public_ip, @callback_public_port, self, @audit )
21
+ rescue => e
22
+ @audit.warn( "XMLBlasterCallbackClient: Could not create CallbackServer" )
23
+ raise e
24
+ end
25
+
26
+ begin
27
+ @callback_server.start()
28
+ rescue => e
29
+ @audit.warn( "XMLBlasterCallbackClient: Error creating XMLRPC Server" )
30
+ raise e
31
+ end
32
+ end
33
+
34
+ def login( username='guest', password='guest' )
35
+ qos = "<qos>
36
+ <securityService type='htpasswd' version='1.0'>
37
+ <![CDATA[
38
+ <user>#{username}</user>
39
+ <passwd>#{password}</passwd>
40
+ ]]>
41
+ </securityService>
42
+ <session name ='#{username}/1' timeout='0' maxSessions='1' clearSessions='true' />
43
+ <persistent/>
44
+ <callback type='XMLRPC' retries='-1' delay='60000'>#{@callback_server.callback_url}</callback>
45
+ </qos>"
46
+ @audit.debug( "XmlblasterCallbackClient: authenticate.connect QoS = #{qos}" )
47
+ returnQos = @proxy.call("authenticate.connect", qos)
48
+ @audit.debug( "XmlblasterCallbackClient: authenticate.connect returnQos = #{returnQos}" )
49
+ xml = REXML::Document.new(returnQos)
50
+ @sessionId = xml.elements["//session"].attributes["sessionId"]
51
+ end
52
+
53
+ def subscribe( xmlKey, qos )
54
+ begin
55
+ returnValue = @proxy.call("xmlBlaster.subscribe", @sessionId, xmlKey, qos )
56
+ puts "Subscribed to mom (xmlblaster at #{@xmlblaster_ip}:#{@xmlblaster_port}). Listening at #{@callback_public_ip || @callback_ip}:#{@callback_public_port || @callback_port}"
57
+ @audit.info( "==> ::SUBSCRIBE:: <== Success subscribing with sessionID #{@sessionId}" )
58
+ rescue => e
59
+ @audit.warn( "XMLBlasterClient: Error subscribing to MOM: #{e}" )
60
+ raise e
61
+ end
62
+ return returnValue
63
+ end
64
+
65
+ def unsubscribe( xmlKey, qos )
66
+ begin
67
+ returnValue = @proxy.call("xmlBlaster.unSubscribe", @sessionId, xmlKey, qos )
68
+ @audit.info( "==> ::UNSUBSCRIBE:: <== Success unSubscribing with sessionID #{@sessionId}" )
69
+ rescue => e
70
+ @audit.warn( "XMLBlasterClient: Error unsubscribing from MOM: #{e}" )
71
+ raise e
72
+ end
73
+ return true
74
+ end
75
+
76
+ def update( *args )
77
+ key =args[0]
78
+ content = args[1]
79
+ qos = args[2]
80
+ @audit.info( "XMLBlasterCallbackClient: Received UPDATE." )
81
+
82
+ begin
83
+ qos_xml = REXML::Document.new args[2]
84
+ rescue => e
85
+ @audit.warn( "XMLBlasterCallbackClient: Could not open QOS of message." )
86
+ end
87
+
88
+ if qos_xml.elements['qos'].elements['state'] != nil then
89
+ begin
90
+ value = qos_xml.elements['qos'].elements['state'].attributes['id'].to_s
91
+ if value == "ERASED" then
92
+ @audit.debug( "XMLBlasterCallbackClient: TOPIC GOT ERASED" )
93
+ else
94
+ @audit.debug( "XMLBlasterCallbackClient: SOMETHING STRANGE" )
95
+ end
96
+ rescue => e
97
+ @audit.warn( "XMLBlasterCallbackClient: Error: #{e}" )
98
+ end
99
+ end
100
+
101
+ begin
102
+ key_xml = REXML::Document.new key
103
+ topic = key_xml.elements['key'].attributes["oid"]
104
+ @audit.debug( "Topic #{topic}" )
105
+ puts "-----------------------------------"
106
+ puts "Received message in topic: #{topic}"
107
+ puts "#{content}"
108
+ puts "-----------------------------------"
109
+ # process message
110
+ Adaptation::Base.new.process content
111
+ rescue => e
112
+ @audit.warn( "XMLBlasterCallbackClient: Could not access content of message." )
113
+ end
114
+
115
+ return "<qos><state>OK</state></qos>"
116
+ end
117
+
118
+ def ping( *args )
119
+ @audit.debug( "XMLBlasterClient: received PING - PONG" )
120
+ return "<qos><state>OK</state></qos>"
121
+ end
122
+
123
+ def logout
124
+ super
125
+ if @callback_server then
126
+ begin
127
+ @callback_server.shutdown()
128
+ rescue => e
129
+ @audit.warn( "XMLBlasterCallbackClient: Error could not stop CallbackServer" )
130
+ raise e
131
+ end
132
+ else
133
+ return false
134
+ end
135
+ return true
136
+ end
137
+
138
+ end
139
+
140
+
141
+
@@ -0,0 +1,79 @@
1
+ require 'xmlrpc/server'
2
+ require 'socket'
3
+ require 'logger'
4
+
5
+ class XmlblasterCallbackServer
6
+
7
+ attr_reader :thread
8
+ attr_reader :callback_url
9
+
10
+ def initialize( ip, port, public_ip, public_port, callbackInstance, audit = nil )
11
+ if audit == nil then
12
+ @audit = Logger.new($stdout)
13
+ else
14
+ @audit = audit
15
+ end
16
+ @thread = nil
17
+ @callback_server = nil
18
+ @callback_url = "http://#{public_ip}:#{public_port}/RPC2"
19
+ @port = port
20
+ @ip = ip
21
+ @callback_instance = callbackInstance
22
+ end
23
+
24
+ def start
25
+
26
+ begin
27
+ @callback_server = XMLRPC::Server.new( @port, @ip, 4, @audit, false )
28
+ @audit.debug( "CallBackServer started")
29
+ rescue => e
30
+ msg = e.message + ": " + e.backtrace.join("\n")
31
+ @audit.error( "CallBackServer: Could not create XMLRPC Server: " + msg)
32
+ return false
33
+ end
34
+
35
+ @thread = Thread.new( @port, @callback_instance ) { | port, callback_instance |
36
+ Thread.current['name'] = "MOM-CallbackServer"
37
+ STDOUT.sync = true
38
+
39
+
40
+ if @callback_server then
41
+
42
+ @audit.debug( "MOM-CallBackServer: " + @callback_url )
43
+
44
+ @callback_server.add_handler("ping") do |name, *args|
45
+ @callback_instance.ping( *args )
46
+ end
47
+
48
+ @callback_server.add_handler("update") do |name, *args|
49
+ @callback_instance.update( *args )
50
+ end
51
+
52
+ @callback_server.set_default_handler do |name, *args|
53
+ raise XMLRPC::FaultException.new(-99, "MOM-CallBackServer: Method #{name} missing or wrong number of parameters!")
54
+ end
55
+
56
+ # listening
57
+ @audit.debug( "MOM-CallBackServer: XMLRPC Server serving." )
58
+ @callback_server.serve()
59
+
60
+ else
61
+ return false
62
+ end
63
+
64
+ }
65
+
66
+ return true
67
+ end
68
+
69
+
70
+ def shutdown
71
+ if @callback_server then
72
+ @callback_server.shutdown()
73
+ @thread.kill
74
+ end
75
+ return true
76
+ end
77
+
78
+ end
79
+
@@ -0,0 +1,122 @@
1
+ require 'xmlrpc/client'
2
+ require 'rexml/document'
3
+ require 'logger'
4
+
5
+ # Implements http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.html
6
+
7
+ class XmlblasterClient
8
+
9
+ attr_reader :xmlblaster_ip
10
+
11
+ def initialize( xmlblaster_ip = nil, xmlblaster_port = nil, audit = nil )
12
+
13
+ if audit == nil then
14
+ @audit = Logger.new(STDOUT)
15
+ @audit.level = Logger::INFO
16
+ else
17
+ @audit = audit
18
+ end
19
+
20
+ @xmlblaster_ip = xmlblaster_ip
21
+ @xmlblaster_port = xmlblaster_port
22
+
23
+ @proxy = nil
24
+ @sessionId = nil
25
+
26
+ if @xmlblaster_ip then
27
+ begin
28
+ self.connect(@xmlblaster_ip, @xmlblaster_port)
29
+ rescue => e
30
+ @audit.warn( "XMLBlasterClient: Error connecting to XMLBlasterServer." )
31
+ raise e
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ def connect( ip, port )
38
+ rpc="/RPC2"
39
+ begin
40
+ @proxy = XMLRPC::Client.new( ip, rpc, port, nil, nil, nil, nil, nil,nil)
41
+ rescue => e
42
+ @audit.warn( "XMLBlasterClient: new XMLRPC client creation failed.")
43
+ raise e
44
+ end
45
+ @audit.debug( "XMLBlasterClient: XMLBlaster client to http://#{ip}:#{port}#{rpc}")
46
+ return true
47
+ end
48
+
49
+ def login( username='guest', password='guest')
50
+ qos = "<qos>
51
+ <securityService type='htpasswd' version='1.0'>
52
+ <![CDATA[
53
+ <user>#{username}</user>
54
+ <passwd>#{password}</passwd>
55
+ ]]>
56
+ </securityService>
57
+ <session timeout='30000' maxSessions='99' clearSessions='false' />
58
+ </qos>"
59
+ @audit.debug( "XmlbasterClient: authenticate.connect QoS = #{qos}" )
60
+ returnQos = @proxy.call("authenticate.connect", qos)
61
+ @audit.debug( "XmlblasterClient:: authenticate.connect returnQos = #{returnQos}" )
62
+ xml = REXML::Document.new(returnQos)
63
+ @sessionId = xml.elements["//session"].attributes["sessionId"]
64
+ end
65
+
66
+ def logout
67
+ begin
68
+ returnValue = @proxy.call("authenticate.logout", @sessionId)
69
+ @audit.info( "==> ::LOGOUT:: <== Success with sessionID #{@sessionId}, return Value: #{returnValue.to_s}" )
70
+ rescue => e
71
+ @audit.warn( "XMLBlasterClient: Error logging out: #{e}" )
72
+ raise e
73
+ end
74
+ return true
75
+ end
76
+
77
+ def publish( xmlKey, content, qos )
78
+ begin
79
+ returnValue = @proxy.call("xmlBlaster.publish", @sessionId, xmlKey, content, qos )
80
+ @audit.info( "==> ::PUBLISH:: <== Success publishing with sessionID #{@sessionId}")
81
+ rescue => e
82
+ @audit.warn( "XMLBlasterClient: Error publishing to MOM: #{e}" )
83
+ raise e
84
+ end
85
+ return true
86
+ end
87
+
88
+ def get( xmlKey, qos )
89
+ begin
90
+ returnValue = @proxy.call("xmlBlaster.get", @sessionId, xmlKey, qos )
91
+ @audit.info( "==> ::GET:: <== Success get with sessionID #{@sessionId}" )
92
+ rescue => e
93
+ @audit.warn( "XMLBlasterClient: Error get from MOM: #{e}" )
94
+ raise e
95
+ end
96
+ return returnValue
97
+ end
98
+
99
+ def erase( xmlKey, qos )
100
+ begin
101
+ returnValue = @proxy.call("xmlBlaster.erase", @sessionId, xmlKey, qos )
102
+ @audit.info( "==> ::ERASE:: <== Success get with sessionID #{@sessionId}" )
103
+ rescue XMLRPC::FaultException => e
104
+ @audit.warn( "XMLBlasterClient: Error erase from MOM: #{e}" )
105
+ raise e
106
+ end
107
+ return true
108
+ end
109
+
110
+ def printMessage( messages )
111
+ @audit.info( "Received #{messages.length} messages.")
112
+ messages.each{ | message |
113
+ key = message[0]
114
+ content = message[1]
115
+ qos = message[2]
116
+ @audit.info " Key = " + key.to_s
117
+ @audit.info " Content = " + content.lenght + " bytes"
118
+ @audit.info " QOS = " + qos.to_s
119
+ }
120
+ end
121
+
122
+ end
@@ -4,17 +4,19 @@ unless defined? ADAPTOR_ROOT
4
4
  ADAPTOR_ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
5
5
  end
6
6
 
7
- environment = "development"
7
+ args = ARGV
8
+ mom = "druby"
8
9
  ARGV.each do |arg|
9
- if arg[0..14] == "ADAPTATION_ENV="
10
- environment = arg[15..arg.length]
10
+ if arg[0..3] == "MOM="
11
+ mom = arg[4..arg.length]
12
+ args = ARGV.reject{|p| p == "MOM=#{mom}"}
11
13
  end
12
14
  end
13
15
 
14
16
  require 'yaml'
15
- config = YAML::load(File.open("#{ADAPTOR_ROOT}/config/mom.yml"))[environment.to_s]
17
+ config = YAML::load(File.open("#{ADAPTOR_ROOT}/config/mom.yml"))[mom]
16
18
 
17
- case config["mom"]["type"]
19
+ case mom
18
20
  when "druby"
19
21
  require 'drb'
20
22
 
@@ -25,11 +27,13 @@ case config["mom"]["type"]
25
27
 
26
28
  when "xmlblaster"
27
29
  require "rubygems"
28
- require "adaptation/xmlblaster_client"
30
+ require "adaptation"
31
+ require "adaptation/xmlblaster/xmlblaster_client"
29
32
 
30
33
  xbc = XmlblasterClient.new(config["mom"]["host"], config["mom"]["port"])
31
- xbc.login( "OAP_USER", "OAP_PASS" )
32
- xbc.publish( "<key oid='#{ARGV[0]}' contentMime='text/xml'/>", ARGV[1] , "<qos></qos>" )
34
+ xbc.login( config["mom"]["publish_user"], config["mom"]["publish_password"] )
35
+ puts args
36
+ xbc.publish( "<key oid='#{args[0]}' contentMime='text/xml'/>", args[1] , "<qos></qos>" )
33
37
  xbc.logout
34
38
  end
35
39
 
@@ -1,25 +1,82 @@
1
1
  require 'adaptation'
2
2
 
3
- environment = "development" # TODO: "un-hardcode" this
4
- config = YAML::load(File.open("config/mom.yml"))["development"]
3
+ args = ARGV
4
+ mom = "druby"
5
+ ARGV.each do |arg|
6
+ if arg[0..3] == "MOM="
7
+ mom = arg[4..arg.length]
8
+ args = ARGV.reject{|p| p == "MOM=#{mom}"}
9
+ end
10
+ end
11
+
12
+ config = YAML::load(File.open("config/mom.yml"))[mom]
5
13
 
6
- case config["mom"]["type"]
14
+ case mom
7
15
  when "druby"
8
16
  require 'adaptation/druby_subscriber'
9
17
 
10
18
  mom_uri = "druby://#{config["mom"]["host"]}:#{config["mom"]["port"]}"
11
19
  subscriber_uri = "druby://#{config["subscriber"]["host"]}:#{config["subscriber"]["port"]}"
12
- topics = config["subscriber"]["topics"].split(' ')
20
+ topics = config["subscriber"]["topics"].gsub(" ", "").split(',')
13
21
 
14
- Signal.trap("INT") { puts "Shutting down subscriber (#{config["mom"]["type"]})"; exit }
22
+ Signal.trap("INT") { puts "Shutting down subscriber (#{mom})"; exit }
15
23
 
16
24
  oapdaemon = Adaptation::Mom::DrubySubscriber.new subscriber_uri, mom_uri, topics
17
25
  oapdaemon.start
18
26
 
19
- #when "xmlblaster"
20
- #
27
+ when "xmlblaster"
28
+ require 'adaptation/xmlblaster/xmlblaster_callback_client'
29
+
30
+ xbcc = XmlblasterCallbackClient.new(
31
+ config["mom"]["host"],
32
+ config["mom"]["port"],
33
+ config["subscriber"]["host"],
34
+ config["subscriber"]["port"],
35
+ config["subscriber"]["public_host"],
36
+ config["subscriber"]["public_port"],
37
+ Logger.new("#{ADAPTOR_ROOT}/log/subscriber.log")
38
+ )
39
+
40
+ Signal.trap("INT") {
41
+ puts "Shutting down subscriber (#{mom})"
42
+ begin
43
+ xbcc.unsubscribe(
44
+ "<key oid='#{topic}'/>",
45
+ "<qos><persistent/><initialUpdate>false</initialUpdate>
46
+ <duplicateUpdates>false</duplicateUpdates><multiSubscribe>false</multiSubscribe>
47
+ <history numEntries='1'/></qos>"
48
+ )
49
+ rescue
50
+ end
51
+ xbcc.logout
52
+ exit
53
+ }
54
+
55
+ user = config["subscriber"]["subscriber_user"].nil? ? config["subscriber"]["host"] : config["subscriber"]["subscriber_user"]
56
+ password = config["subscriber"]["subscriber_password"].nil? ? "empty" : config["subscriber"]["subscriber_password"]
57
+ xbcc.login(user, password)
58
+
59
+ topics = (config["subscriber"]["topics"].nil? or config["subscriber"]["topics"] == "all") ? nil : config["subscriber"]["topics"].gsub(" ","").split(',')
60
+ if topics.nil?
61
+ xbcc.subscribe(
62
+ "<key oid='' queryType='XPATH'> /xmlBlaster/key[starts-with(@oid,'.')] </key>",
63
+ "<qos><persistent/><initialUpdate>false</initialUpdate>
64
+ <duplicateUpdates>false</duplicateUpdates><multiSubscribe>false</multiSubscribe>
65
+ <history numEntries='1'/></qos>"
66
+ )
67
+ else
68
+ topics.each do |topic|
69
+ xbcc.subscribe(
70
+ "<key oid='#{topic}'/>",
71
+ "<qos><persistent/><initialUpdate>false</initialUpdate>
72
+ <duplicateUpdates>false</duplicateUpdates><multiSubscribe>false</multiSubscribe>
73
+ <history numEntries='1'/></qos>"
74
+ )
75
+ end
76
+ end
77
+
78
+ sleep
21
79
 
22
80
  else
23
- puts "Unknown MOM server type: #{config["mom"]["type"]}"
81
+ puts "Unknown MOM server type: #{mom}"
24
82
  end
25
-
@@ -58,19 +58,18 @@ class AppGenerator < Rails::Generator::Base
58
58
  #m.file "environments/test.rb", "config/environments/test.rb"
59
59
 
60
60
  # Scripts
61
- %w( generate destroy about subscribe breakpointer console ).each do |file|
61
+ %w( generate destroy about publish subscribe breakpointer console ).each do |file|
62
62
  m.file "bin/#{file}", "script/#{file}", script_options
63
63
  end
64
64
 
65
65
  # Dispatches
66
66
  m.file "dispatches/dispatch.rb", "public/dispatch.rb", script_options
67
- m.file "dispatches/publish.rb", "public/publish.rb", script_options
68
67
 
69
68
  # Docs
70
69
  m.file "doc/README_FOR_APP", "doc/README_FOR_APP"
71
70
 
72
71
  # Logs
73
- %w(server production development test).each { |file|
72
+ %w(subscriber development test).each { |file|
74
73
  m.file "configs/empty.log", "log/#{file}.log", :chmod => 0666
75
74
  }
76
75
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adaptation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavi Vila Morell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-23 00:00:00 +01:00
12
+ date: 2009-01-27 00:00:00 +01:00
13
13
  default_executable: adaptation
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,7 @@ files:
47
47
  - bin/breakpointer
48
48
  - bin/about
49
49
  - bin/adaptation
50
+ - bin/publish
50
51
  - bin/generate
51
52
  - bin/console
52
53
  - bin/mom
@@ -58,6 +59,10 @@ files:
58
59
  - lib/adaptation/mom.rb
59
60
  - lib/adaptation/message.rb
60
61
  - lib/adaptation/adaptor.rb
62
+ - lib/adaptation/xmlblaster
63
+ - lib/adaptation/xmlblaster/xmlblaster_client.rb
64
+ - lib/adaptation/xmlblaster/xmlblaster_callback_server.rb
65
+ - lib/adaptation/xmlblaster/xmlblaster_callback_client.rb
61
66
  - lib/adaptation/druby_subscriber.rb
62
67
  - lib/adaptation/test
63
68
  - lib/adaptation/test/fake_fixtures.rb
@@ -117,6 +122,7 @@ files:
117
122
  - lib/commands/subscribe.rb
118
123
  - lib/commands/destroy.rb
119
124
  - lib/commands/generate.rb
125
+ - lib/commands/publish.rb
120
126
  - helpers/application.rb
121
127
  - helpers/test.rb
122
128
  - helpers/publish.rb
@@ -129,7 +135,6 @@ files:
129
135
  - configs/empty.log
130
136
  - configs/boot.rb
131
137
  - dispatches/dispatch.rb
132
- - dispatches/publish.rb
133
138
  - README
134
139
  - CHANGELOG
135
140
  - fresh_rakefile