Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 44 lines (35 sloc) 0.657 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
require 'set'

class Supervisor
  include DRbUndumped

  attr_reader :replicas

  def initialize
    @next_id = 1
    @replicas = Set.new
    @id_map = {}
  end

  def add_replica(replica, id=@next_id)
    if @replicas.add?(replica)
      @id_map[id.to_s] = replica
      @next_id += 1
    end
  end

  def remove_replica(replica)
    if @replicas.delete?(replica)
      @id_map.each do |k,v|
        if v == replica
          @id_map.delete(k)
          break
        end
      end
    end
  end
  
  def replica_by_id(id)
    @id_map[id]
  end
  
  def [](id)
    replica_by_id(id)
  end
  
  def replicas_by_id(&block)
    @id_map.each(&block)
  end
end
Something went wrong with that request. Please try again.