forked from celluloid/dcell
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
111 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,74 @@ | ||
module DCell | ||
# Node metadata helper | ||
class DirectoryMeta | ||
attr_reader :id, :address, :actors | ||
|
||
def initialize(id, meta) | ||
@id = id | ||
if meta | ||
@address = meta[:address] | ||
@actors = meta[:actors].map {|a| a.to_sym} | ||
else | ||
@actors = Array.new | ||
end | ||
end | ||
|
||
def address=(address) | ||
@address = address | ||
DCell.registry.set_node @id, self | ||
end | ||
|
||
def actors=(actors) | ||
@actors = actors.map {|a| a.to_sym} | ||
DCell.registry.set_node @id, self | ||
end | ||
|
||
def add_actor(actor) | ||
@actors << actor.to_sym | ||
DCell.registry.set_node @id, self | ||
end | ||
alias_method :<<, :add_actor | ||
|
||
def to_msgpack(pk=nil) | ||
{ | ||
:address => @address, | ||
:actors => @actors, | ||
}.to_msgpack(pk) | ||
end | ||
end | ||
|
||
# Directory of nodes connected to the DCell cluster | ||
module Directory | ||
include Enumerable | ||
extend self | ||
|
||
# Get the URL for a particular Node ID | ||
def get(node_id) | ||
DCell.registry.get_node node_id | ||
# Get the address for a particular Node ID | ||
def find(id) | ||
meta = DCell.registry.get_node(id) | ||
DirectoryMeta.new(id, meta) | ||
end | ||
alias_method :[], :get | ||
|
||
# Set the address of a particular Node ID | ||
def set(node_id, addr) | ||
DCell.registry.set_node node_id, addr | ||
end | ||
alias_method :[]=, :set | ||
alias_method :[], :find | ||
|
||
# List all of the node IDs in the directory | ||
def all | ||
DCell.registry.nodes | ||
end | ||
|
||
# Iterates over all registered nodes | ||
def each | ||
DCell.registry.nodes.each do |id| | ||
yield Directory[id] | ||
end | ||
end | ||
|
||
# Remove all nodes in the directory | ||
def clear_all | ||
DCell.registry.clear_all_nodes | ||
end | ||
|
||
def remove(node) | ||
DCell.registry.remove_node node | ||
# Remove information for a give Node ID | ||
def remove(id) | ||
DCell.registry.remove_node id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
describe DCell::Directory do | ||
it "stores node addresses" do | ||
DCell::Directory["foobar"] = "tcp://localhost:1870" | ||
DCell::Directory["foobar"].should == "tcp://localhost:1870" | ||
DCell::Directory["foobar"].address = "tcp://localhost:1870" | ||
DCell::Directory["foobar"].address.should == "tcp://localhost:1870" | ||
end | ||
|
||
it "stores node actors" do | ||
DCell::Directory["foobar"].actors = [] | ||
DCell::Directory["foobar"] << :one | ||
DCell::Directory["foobar"] << :two | ||
DCell::Directory["foobar"].actors.should == [:one, :two] | ||
|
||
DCell::Directory["foobar"].actors = [:three, :four] | ||
DCell::Directory["foobar"].actors.should == [:three, :four] | ||
end | ||
|
||
it "presents all stored addresses" do | ||
DCell::Directory["foo"] = "tcp://fooaddress" | ||
DCell::Directory["bar"] = "tcp://baraddress" | ||
DCell::Directory["foo"].address = "tcp://fooaddress" | ||
DCell::Directory["bar"].address = "tcp://baraddress" | ||
DCell::Directory.all.should include("foo") | ||
DCell::Directory.all.should include("bar") | ||
DCell::Directory.map{|node| node.id}.should include("foo") | ||
DCell::Directory.map{|node| node.id}.should include("bar") | ||
end | ||
|
||
it "clears node addresses" do | ||
DCell::Directory["foo"] = "tcp://fooaddress" | ||
DCell::Directory["foobar"].should == "tcp://localhost:1870" | ||
DCell::Directory["foo"].address = "tcp://fooaddress" | ||
DCell::Directory["foobar"].address.should == "tcp://localhost:1870" | ||
["foo", "foobar"].each do |node| | ||
DCell::Directory.remove node | ||
end | ||
DCell::Directory["foobar"].should_not == "tcp://localhost:1870" | ||
DCell::Directory["foobar"].address.should_not == "tcp://localhost:1870" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters