Permalink
Browse files

adding a start and block

  • Loading branch information...
tenderlove committed Sep 22, 2008
1 parent 6a95190 commit e853cee491f159b60902613203dc99bffd389a9f
View
No changes.
View
@@ -1,18 +1,32 @@
#include <quail_handle.h>
-static void dealloc(void * handle)
+static VALUE native_bind(VALUE self, VALUE exchange_name, VALUE queue_name)
{
+ void * handle;
+
+ Data_Get_Struct(rb_iv_get(self, "@handle"), void, handle);
+ czmq_bind(handle, StringValuePtr(exchange_name), StringValuePtr(queue_name));
+}
+
+static VALUE destroy_handle(VALUE self)
+{
+ void * handle;
+
+ Data_Get_Struct(rb_iv_get(self, "@handle"), void, handle);
czmq_destroy(handle);
}
-static VALUE new(VALUE klass, VALUE host)
+static VALUE create_handle(VALUE self, VALUE host)
{
void * handle = czmq_create(StringValuePtr(host));
- return Data_Wrap_Struct(klass, NULL, dealloc, handle);
+ rb_iv_set(self, "@handle", Data_Wrap_Struct(rb_cObject, NULL, NULL, handle));
+ return self;
}
void Init_Quail_Handle(VALUE mQuail)
{
VALUE cQuailHandle = rb_define_class_under(mQuail, "Handle", rb_cObject);
- rb_define_singleton_method(cQuailHandle, "new", new, 1);
+ rb_define_private_method(cQuailHandle, "create_handle", create_handle, 1);
+ rb_define_private_method(cQuailHandle, "destroy_handle", destroy_handle, 0);
+ rb_define_private_method(cQuailHandle, "native_bind", native_bind, 2);
}
View
@@ -1,3 +1,4 @@
require 'quail/native'
require 'quail/exchange'
require 'quail/queue'
+require 'quail/handle'
View
@@ -5,10 +5,12 @@ class Exchange
def initialize handle,
exchange_name = 'E',
scope = Quail::GLOBAL,
- address = '127.0.0.1:5000'
+ address = '127.0.0.1:5555'
+
+ raise unless handle.started?
+ @handle = handle.handle
@name = exchange_name
- @handle = handle
- @eid = create_exchange(handle, exchange_name, scope, address)
+ @eid = create_exchange(@handle, exchange_name, scope, address)
end
end
end
View
@@ -0,0 +1,37 @@
+module Quail
+ class Handle
+ class << self
+ def start host, &block
+ new(host).start(host, &block)
+ end
+ end
+
+ attr_accessor :host, :handle
+ def initialize host
+ @host = host
+ @started = false
+ @handle = nil
+ end
+
+ def started?
+ @started
+ end
+
+ def start host = @host, &block
+ create_handle(host)
+ @started = true
+ block.call(self)
+ finish
+ end
+
+ def finish
+ destroy_handle
+ @started = false
+ end
+
+ def bind exchange, queue
+ raise "not started" unless @started
+ native_bind(exchange.name, queue.name)
+ end
+ end
+end
View
@@ -5,11 +5,12 @@ class Queue
def initialize handle,
name = 'Q',
scope = Quail::GLOBAL,
- address = '127.0.0.1:5001'
+ address = '127.0.0.1:5556'
- @handle = handle
+ raise unless handle.started?
+ @handle = handle.handle
@name = name
- create_queue(handle, name, scope, address)
+ create_queue(@handle, name, scope, address)
end
end
end
View
@@ -4,8 +4,6 @@
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), path)))
end
-Thread.new { `vendor/bin/zmq_server` }
-
require 'quail'
module Quail
@@ -3,9 +3,10 @@
module Quail
class TestExchange < Quail::TestCase
def test_initialize
- handle = Quail::Handle.new('localhost')
assert_nothing_raised {
- exchange = Quail::Exchange.new(handle)
+ Quail::Handle.start('localhost') { |handle|
+ exchange = Quail::Exchange.new(handle)
+ }
}
end
end
@@ -7,5 +7,13 @@ def test_initialize
Quail::Handle.new('localhost.local')
}
end
+
+ def test_bind
+ Quail::Handle.start('localhost') do |handle|
+ exchange = Quail::Exchange.new(handle)
+ queue = Quail::Queue.new(handle)
+ handle.bind(exchange, queue)
+ end
+ end
end
end
View
@@ -3,9 +3,10 @@
module Quail
class TestQueue < Quail::TestCase
def test_initialize
- handle = Quail::Handle.new('localhost')
assert_nothing_raised {
- queue = Quail::Queue.new(handle)
+ Quail::Handle.start('localhost') do |handle|
+ queue = Quail::Queue.new(handle)
+ end
}
end
end

0 comments on commit e853cee

Please sign in to comment.