Sha256: f7edd600f1e88c665c04448d1ae1b2765d75eeee51ea18989dcbbc137b09a375

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require 'drb'
require 'yaml'

module Adaptation

  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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adaptation-0.0.1 lib/adaptation/mom.rb
adaptation-0.0.2 lib/adaptation/mom.rb