require 'drb' require 'yaml' module Adaptation module Mom class Mom def initialize mom_uri @mom_uri = mom_uri end def subscribe drb_uri unless get_subscribers.include?(drb_uri) add_subscriber drb_uri puts "Added new subscriber: #{drb_uri}" oapdaemon = DRbObject.new(nil, drb_uri) oapdaemon.subscription_result true end end def publish message, topic # Tell subscribed hosts to execute their adaptors puts "-----------------------------------" puts "Received message in topic: #{topic}" puts "#{message}" puts "-----------------------------------" get_subscribers.each do |uri| puts "Calling #{uri}" DRb.start_service oapdaemon = DRbObject.new(nil, uri) oapdaemon.call_adaptor message, topic end end def start DRb.start_service(@mom_uri, self) puts "MOM started. Listening at #{@mom_uri}" DRb.thread.join # Don't exit just yet end def list puts "MOM subscriptions:" get_subscribers.each do |s| puts " #{s}" end return end private def add_subscriber drb_uri subscribers = get_subscribers subscribers << drb_uri unless subscribers.include?(drb_uri) sf = File.new('subscribers.yml', 'w') sf.write(YAML::dump(subscribers)) sf.close end def get_subscribers if File.exists?('subscribers.yml') subscribers = YAML::load(File.open('subscribers.yml')) else subscribers = Array.new end subscribers end end end end