Permalink
Browse files

trying to make sense of ZDCF

  • Loading branch information...
igrigorik committed Nov 14, 2010
1 parent 372364f commit 54960b4ad9c394e84ffa1bda599ed7afb709e54d
Showing with 135 additions and 3 deletions.
  1. +1 −0 autotest/discover.rb
  2. +65 −2 lib/zdevice.rb
  3. +65 −0 spec/zdevice_spec.rb
  4. +4 −1 zdevice.gemspec
View
@@ -0,0 +1 @@
+Autotest.add_discovery { 'rspec2' }
View
@@ -1,3 +1,66 @@
-module Zdevice
- # Your code goes here...
+require 'ffi-rzmq'
+
+module ZMQ
+ module Device
+
+ class Builder
+ def initialize(conf = {})
+ @context = Context.new(conf.delete(:context))
+ @devices = {}
+
+ conf.each do |name, c|
+ p [name, c]
+ @devices[name] = Device.new(name, c)
+ end
+ end
+
+ def method_missing(method, *args, &blk)
+ if d = @devices[method]
+ return d
+ end
+
+ super
+ end
+ end
+
+ class Context
+ attr_reader :iothreads, :verbose
+ def initialize(conf = {})
+ @iothreads = conf[:iothreads] || 1
+ @verbose = conf[:verbose] || false
+
+ @ctx = ZMQ::Context.new(@iothreads)
+ end
+ end
+
+ class Device
+ attr_reader :name, :type
+ def initialize(name, conf = {})
+ raise 'invalid name' if name == 'context'
+ raise 'missing type' if !conf.key? :type
+
+ @name = name
+ @type = conf.delete(:type)
+ # TODO: open sockets...
+ end
+ end
+
+ class ZSocket
+ attr_reader :name, :type
+ def initialize(name, conf = {})
+ raise 'invalid name' if name == 'type'
+ raise 'missing type' if !conf.key? :type
+
+ # [:type, :bind, :connect, :option].each do |var|
+ # raise "missing #{var}" if !conf.key? var
+ # end
+
+ @name = name
+ @type = conf.delete(:type)
+
+ end
+ end
+
+ end
+
end
View
@@ -0,0 +1,65 @@
+require 'lib/zdevice'
+
+include ZMQ::Device
+describe ZMQ::Device do
+
+ context Builder do
+ it "should assemble a ZMQ Device" do
+ b = Builder.new(
+ context: { iothreads: 5},
+ main: {
+ type: :queue,
+ frontend: { type: :SUB, option: { hwm: 1, swap: 25}, bind: "tcp://127.0.0.1:5555" },
+ backend: { bind: "tcp://eth0:5556" }
+ }
+ )
+
+ b.main.class.should == Device
+ b.main.type.should == :queue
+
+ end
+ end
+
+ context Context do
+ it "should accept optional iothread count" do
+ c = Context.new(iothreads: 5)
+ c.iothreads.should == 5
+ end
+
+ it "should accept optional verbose flag" do
+ c = Context.new(verbose: true)
+ c.verbose.should be_true
+ end
+ end
+
+ context Device do
+ it "should validate type" do
+ lambda { Device.new('context') }.should raise_error
+ end
+
+ it "should validate name" do
+ lambda { Device.new('context', type: :a) }.should raise_error
+ d = Device.new('valid', type: :a)
+ d.name.should == 'valid'
+ end
+ end
+
+ context ZSocket do
+ it "should have any name except 'type'" do
+ lambda { ZSocket.new('type') }.should raise_error('invalid name')
+
+ s = ZSocket.new('valid', type: :queue)
+ s.name.should == 'valid'
+ end
+
+ it "should have a type" do
+ s = ZSocket.new('valid', type: :queue)
+ s.type.should == :queue
+ end
+
+ # it "should have zero or more endpoints to bind the socket to"
+ # it "should have zero or more endpoints to connect the socket to"
+ # it "should have socket options"
+ end
+
+end
View
@@ -11,9 +11,12 @@ Gem::Specification.new do |s|
s.homepage = "http://rubygems.org/gems/zdevice"
s.summary = %q{TODO: Write a gem summary}
s.description = %q{TODO: Write a gem description}
-
s.rubyforge_project = "zdevice"
+ s.add_dependency "ffi"
+ s.add_dependency "ffi-rzmq"
+ s.add_development_dependency "rspec"
+
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }

0 comments on commit 54960b4

Please sign in to comment.