From 1a655045951c29edda0b616640b9dac507cda164 Mon Sep 17 00:00:00 2001 From: malosek Date: Wed, 16 Sep 2009 17:06:59 +0200 Subject: ruby directory moved into bindings dir --- Makefile.am | 9 +- bindings/Makefile.am | 8 +- bindings/ruby/Makefile.am | 11 ++ bindings/ruby/extconf.rb | 24 ++++ bindings/ruby/rbzmq.cpp | 277 ++++++++++++++++++++++++++++++++++++++++++++++ configure.in | 2 +- ruby/Makefile.am | 11 -- ruby/extconf.rb | 24 ---- ruby/rbzmq.cpp | 277 ---------------------------------------------- 9 files changed, 322 insertions(+), 321 deletions(-) create mode 100644 bindings/ruby/Makefile.am create mode 100644 bindings/ruby/extconf.rb create mode 100644 bindings/ruby/rbzmq.cpp delete mode 100644 ruby/Makefile.am delete mode 100644 ruby/extconf.rb delete mode 100644 ruby/rbzmq.cpp diff --git a/Makefile.am b/Makefile.am index 82bddfe..b06c00e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,15 +1,12 @@ -if BUILD_RUBY -DIR_R = ruby -endif - if BUILD_PERF DIR_PERF = perf endif -SUBDIRS = src $(DIR_R) $(DIR_PERF) devices bindings -DIST_SUBDIRS = src ruby perf devices bindings +SUBDIRS = src $(DIR_PERF) devices bindings +DIST_SUBDIRS = src perf devices bindings EXTRA_DIST = $(top_srcdir)/foreign/openpgm/@pgm_basename@.tar.bz2 dist-hook: -rm -rf $(distdir)/foreign/openpgm/@pgm_basename@ + diff --git a/bindings/Makefile.am b/bindings/Makefile.am index 2b877ed..77b4ec2 100644 --- a/bindings/Makefile.am +++ b/bindings/Makefile.am @@ -6,6 +6,10 @@ if BUILD_PYTHON DIR_P = python endif -SUBDIRS = $(DIR_J) $(DIR_P) -DIST_SUBDIRS = java python +if BUILD_RUBY +DIR_R = ruby +endif + +SUBDIRS = $(DIR_J) $(DIR_P) $(DIR_R) +DIST_SUBDIRS = java python ruby diff --git a/bindings/ruby/Makefile.am b/bindings/ruby/Makefile.am new file mode 100644 index 0000000..148daf0 --- /dev/null +++ b/bindings/ruby/Makefile.am @@ -0,0 +1,11 @@ +INCLUDES = -I$(top_builddir) -I$(top_srcdir)/include -I$(top_builddir)/include + +rblib_LTLIBRARIES = librbzmq.la +rblibdir = @RUBYDIR@ + +librbzmq_la_SOURCES = rbzmq.cpp + +librbzmq_la_LDFLAGS = -version-info @RBLTVER@ +librbzmq_la_CXXFLAGS = -Wall -pedantic -Werror -Wno-long-long +librbzmq_la_LIBADD = $(top_builddir)/src/libzmq.la + diff --git a/bindings/ruby/extconf.rb b/bindings/ruby/extconf.rb new file mode 100644 index 0000000..f931c5e --- /dev/null +++ b/bindings/ruby/extconf.rb @@ -0,0 +1,24 @@ +# +# Copyright (c) 2007-2009 FastMQ Inc. +# +# This file is part of 0MQ. +# +# 0MQ is free software; you can redistribute it and/or modify it under +# the terms of the Lesser GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# 0MQ is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Lesser GNU General Public License for more details. +# +# You should have received a copy of the Lesser GNU General Public License +# along with this program. If not, see . + +require 'mkmf' +dir_config('libzmq') +have_library('libzmq') +create_makefile("ruby") + + diff --git a/bindings/ruby/rbzmq.cpp b/bindings/ruby/rbzmq.cpp new file mode 100644 index 0000000..bf0d9bc --- /dev/null +++ b/bindings/ruby/rbzmq.cpp @@ -0,0 +1,277 @@ +/* + Copyright (c) 2007-2009 FastMQ Inc. + + This file is part of 0MQ. + + 0MQ is free software; you can redistribute it and/or modify it under + the terms of the Lesser GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + 0MQ is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + Lesser GNU General Public License for more details. + + You should have received a copy of the Lesser GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include + +#include "../c/zmq.h" + +static void context_free (void *ctx) +{ + if (ctx) { + int rc = zmq_term (ctx); + assert (rc == 0); + } +} + +static VALUE context_alloc (VALUE class_) +{ + return rb_data_object_alloc (class_, NULL, 0, context_free); +} + +static VALUE context_initialize (VALUE self_, VALUE app_threads_, + VALUE io_threads_) +{ + assert (!DATA_PTR (self_)); + void *ctx = zmq_init (NUM2INT (app_threads_), NUM2INT (io_threads_)); + if (!ctx) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + + DATA_PTR (self_) = (void*) ctx; + return self_; +} + +static void socket_free (void *s) +{ + if (s) { + int rc = zmq_close (s); + assert (rc == 0); + } +} + +static VALUE socket_alloc (VALUE class_) +{ + return rb_data_object_alloc (class_, NULL, 0, socket_free); +} + +static VALUE socket_initialize (VALUE self_, VALUE context_, VALUE type_) +{ + assert (!DATA_PTR (self_)); + + if (strcmp (rb_obj_classname (context_), "Context") != 0) { + rb_raise (rb_eArgError, "expected Context object"); + return Qnil; + } + + void *s = zmq_socket (DATA_PTR (context_), NUM2INT (type_)); + if (!s) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + + DATA_PTR (self_) = (void*) s; + return self_; +} + + +static VALUE socket_setsockopt (VALUE self_, VALUE option_, + VALUE optval_) +{ + + int rc = 0; + + switch (NUM2INT (option_)) { + case ZMQ_HWM: + case ZMQ_LWM: + case ZMQ_SWAP: + case ZMQ_AFFINITY: + case ZMQ_RATE: + case ZMQ_RECOVERY_IVL: + case ZMQ_MCAST_LOOP: + { + long optval = FIX2LONG (optval_); + + // Forward the code to native 0MQ library. + rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_), + (void *) &optval, 4); + } + + break; + case ZMQ_IDENTITY: + case ZMQ_SUBSCRIBE: + case ZMQ_UNSUBSCRIBE: + + // Forward the code to native 0MQ library. + rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_), + (void *) StringValueCStr (optval_), RSTRING_LEN (optval_)); + break; + + default: + rc = -1; + errno = EINVAL; + } + + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + + return self_; +} + + +static VALUE socket_bind (VALUE self_, VALUE addr_) +{ + assert (DATA_PTR (self_)); + + int rc = zmq_bind (DATA_PTR (self_), rb_string_value_cstr (&addr_)); + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + + return Qnil; +} + +static VALUE socket_connect (VALUE self_, VALUE addr_) +{ + assert (DATA_PTR (self_)); + + int rc = zmq_connect (DATA_PTR (self_), rb_string_value_cstr (&addr_)); + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + + return Qnil; +} + +static VALUE socket_send (VALUE self_, VALUE msg_, VALUE flags_) +{ + assert (DATA_PTR (self_)); + + Check_Type (msg_, T_STRING); + + zmq_msg_t msg; + int rc = zmq_msg_init_size (&msg, RSTRING_LEN (msg_)); + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + memcpy (zmq_msg_data (&msg), RSTRING_PTR (msg_), RSTRING_LEN (msg_)); + + rc = zmq_send (DATA_PTR (self_), &msg, NUM2INT (flags_)); + if (rc != 0 && errno == EAGAIN) { + rc = zmq_msg_close (&msg); + assert (rc == 0); + return Qfalse; + } + + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + rc = zmq_msg_close (&msg); + assert (rc == 0); + return Qnil; + } + + rc = zmq_msg_close (&msg); + assert (rc == 0); + return Qtrue; +} + +static VALUE socket_flush (VALUE self_) +{ + assert (DATA_PTR (self_)); + + int rc = zmq_flush (DATA_PTR (self_)); + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + return Qnil; + } + + return Qnil; +} + +static VALUE socket_recv (VALUE self_, VALUE flags_) +{ + assert (DATA_PTR (self_)); + + zmq_msg_t msg; + int rc = zmq_msg_init (&msg); + assert (rc == 0); + + rc = zmq_recv (DATA_PTR (self_), &msg, NUM2INT (flags_)); + if (rc != 0 && errno == EAGAIN) { + rc = zmq_msg_close (&msg); + assert (rc == 0); + return Qnil; + } + + if (rc != 0) { + rb_raise (rb_eRuntimeError, strerror (errno)); + rc = zmq_msg_close (&msg); + assert (rc == 0); + return Qnil; + } + + VALUE message = rb_str_new ((char*) zmq_msg_data (&msg), + zmq_msg_size (&msg)); + rc = zmq_msg_close (&msg); + assert (rc == 0); + return message; +} + +extern "C" void Init_librbzmq () +{ + VALUE context_type = rb_define_class ("Context", rb_cObject); + rb_define_alloc_func (context_type, context_alloc); + rb_define_method (context_type, "initialize", + (VALUE(*)(...)) context_initialize, 2); + + VALUE socket_type = rb_define_class ("Socket", rb_cObject); + rb_define_alloc_func (socket_type, socket_alloc); + rb_define_method (socket_type, "initialize", + (VALUE(*)(...)) socket_initialize, 2); + rb_define_method (socket_type, "setsockopt", + (VALUE(*)(...)) socket_setsockopt, 2); + rb_define_method (socket_type, "bind", + (VALUE(*)(...)) socket_bind, 1); + rb_define_method (socket_type, "connect", + (VALUE(*)(...)) socket_connect, 1); + rb_define_method (socket_type, "send", + (VALUE(*)(...)) socket_send, 2); + rb_define_method (socket_type, "flush", + (VALUE(*)(...)) socket_flush, 0); + rb_define_method (socket_type, "recv", + (VALUE(*)(...)) socket_recv, 1); + + rb_define_global_const ("HWM", INT2NUM (ZMQ_HWM)); + rb_define_global_const ("LWM", INT2NUM (ZMQ_LWM)); + rb_define_global_const ("SWAP", INT2NUM (ZMQ_SWAP)); + rb_define_global_const ("AFFINITY", INT2NUM (ZMQ_AFFINITY)); + rb_define_global_const ("IDENTITY", INT2NUM (ZMQ_IDENTITY)); + rb_define_global_const ("SUBSCRIBE", INT2NUM (ZMQ_SUBSCRIBE)); + rb_define_global_const ("UNSUBSCRIBE", INT2NUM (ZMQ_UNSUBSCRIBE)); + rb_define_global_const ("RATE", INT2NUM (ZMQ_RATE)); + rb_define_global_const ("RECOVERY_IVL", INT2NUM (ZMQ_RECOVERY_IVL)); + rb_define_global_const ("MCAST_LOOP", INT2NUM (ZMQ_MCAST_LOOP)); + + rb_define_global_const ("NOBLOCK", INT2NUM (ZMQ_NOBLOCK)); + rb_define_global_const ("NOFLUSH", INT2NUM (ZMQ_NOFLUSH)); + + rb_define_global_const ("P2P", INT2NUM (ZMQ_P2P)); + rb_define_global_const ("SUB", INT2NUM (ZMQ_SUB)); + rb_define_global_const ("PUB", INT2NUM (ZMQ_PUB)); + rb_define_global_const ("REQ", INT2NUM (ZMQ_REQ)); + rb_define_global_const ("REP", INT2NUM (ZMQ_REP)); +} diff --git a/configure.in b/configure.in index eee00c3..f518509 100644 --- a/configure.in +++ b/configure.in @@ -489,7 +489,7 @@ AC_TYPE_SIGNAL AC_CHECK_FUNCS(perror gettimeofday memset socket getifaddrs freeifaddrs) AC_OUTPUT(Makefile src/Makefile bindings/python/Makefile \ - bindings/python/setup.py ruby/Makefile \ + bindings/python/setup.py bindings/ruby/Makefile \ bindings/java/Makefile perf/Makefile perf/c/Makefile perf/cpp/Makefile \ perf/python/Makefile perf/ruby/Makefile perf/java/Makefile src/libzmq.pc \ devices/Makefile devices/zmq_forwarder/Makefile bindings/Makefile) diff --git a/ruby/Makefile.am b/ruby/Makefile.am deleted file mode 100644 index 148daf0..0000000 --- a/ruby/Makefile.am +++ /dev/null @@ -1,11 +0,0 @@ -INCLUDES = -I$(top_builddir) -I$(top_srcdir)/include -I$(top_builddir)/include - -rblib_LTLIBRARIES = librbzmq.la -rblibdir = @RUBYDIR@ - -librbzmq_la_SOURCES = rbzmq.cpp - -librbzmq_la_LDFLAGS = -version-info @RBLTVER@ -librbzmq_la_CXXFLAGS = -Wall -pedantic -Werror -Wno-long-long -librbzmq_la_LIBADD = $(top_builddir)/src/libzmq.la - diff --git a/ruby/extconf.rb b/ruby/extconf.rb deleted file mode 100644 index f931c5e..0000000 --- a/ruby/extconf.rb +++ /dev/null @@ -1,24 +0,0 @@ -# -# Copyright (c) 2007-2009 FastMQ Inc. -# -# This file is part of 0MQ. -# -# 0MQ is free software; you can redistribute it and/or modify it under -# the terms of the Lesser GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# 0MQ is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# Lesser GNU General Public License for more details. -# -# You should have received a copy of the Lesser GNU General Public License -# along with this program. If not, see . - -require 'mkmf' -dir_config('libzmq') -have_library('libzmq') -create_makefile("ruby") - - diff --git a/ruby/rbzmq.cpp b/ruby/rbzmq.cpp deleted file mode 100644 index bf0d9bc..0000000 --- a/ruby/rbzmq.cpp +++ /dev/null @@ -1,277 +0,0 @@ -/* - Copyright (c) 2007-2009 FastMQ Inc. - - This file is part of 0MQ. - - 0MQ is free software; you can redistribute it and/or modify it under - the terms of the Lesser GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - 0MQ is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - Lesser GNU General Public License for more details. - - You should have received a copy of the Lesser GNU General Public License - along with this program. If not, see . -*/ - -#include -#include -#include -#include - -#include "../c/zmq.h" - -static void context_free (void *ctx) -{ - if (ctx) { - int rc = zmq_term (ctx); - assert (rc == 0); - } -} - -static VALUE context_alloc (VALUE class_) -{ - return rb_data_object_alloc (class_, NULL, 0, context_free); -} - -static VALUE context_initialize (VALUE self_, VALUE app_threads_, - VALUE io_threads_) -{ - assert (!DATA_PTR (self_)); - void *ctx = zmq_init (NUM2INT (app_threads_), NUM2INT (io_threads_)); - if (!ctx) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - - DATA_PTR (self_) = (void*) ctx; - return self_; -} - -static void socket_free (void *s) -{ - if (s) { - int rc = zmq_close (s); - assert (rc == 0); - } -} - -static VALUE socket_alloc (VALUE class_) -{ - return rb_data_object_alloc (class_, NULL, 0, socket_free); -} - -static VALUE socket_initialize (VALUE self_, VALUE context_, VALUE type_) -{ - assert (!DATA_PTR (self_)); - - if (strcmp (rb_obj_classname (context_), "Context") != 0) { - rb_raise (rb_eArgError, "expected Context object"); - return Qnil; - } - - void *s = zmq_socket (DATA_PTR (context_), NUM2INT (type_)); - if (!s) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - - DATA_PTR (self_) = (void*) s; - return self_; -} - - -static VALUE socket_setsockopt (VALUE self_, VALUE option_, - VALUE optval_) -{ - - int rc = 0; - - switch (NUM2INT (option_)) { - case ZMQ_HWM: - case ZMQ_LWM: - case ZMQ_SWAP: - case ZMQ_AFFINITY: - case ZMQ_RATE: - case ZMQ_RECOVERY_IVL: - case ZMQ_MCAST_LOOP: - { - long optval = FIX2LONG (optval_); - - // Forward the code to native 0MQ library. - rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_), - (void *) &optval, 4); - } - - break; - case ZMQ_IDENTITY: - case ZMQ_SUBSCRIBE: - case ZMQ_UNSUBSCRIBE: - - // Forward the code to native 0MQ library. - rc = zmq_setsockopt (DATA_PTR (self_), NUM2INT (option_), - (void *) StringValueCStr (optval_), RSTRING_LEN (optval_)); - break; - - default: - rc = -1; - errno = EINVAL; - } - - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - - return self_; -} - - -static VALUE socket_bind (VALUE self_, VALUE addr_) -{ - assert (DATA_PTR (self_)); - - int rc = zmq_bind (DATA_PTR (self_), rb_string_value_cstr (&addr_)); - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - - return Qnil; -} - -static VALUE socket_connect (VALUE self_, VALUE addr_) -{ - assert (DATA_PTR (self_)); - - int rc = zmq_connect (DATA_PTR (self_), rb_string_value_cstr (&addr_)); - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - - return Qnil; -} - -static VALUE socket_send (VALUE self_, VALUE msg_, VALUE flags_) -{ - assert (DATA_PTR (self_)); - - Check_Type (msg_, T_STRING); - - zmq_msg_t msg; - int rc = zmq_msg_init_size (&msg, RSTRING_LEN (msg_)); - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - memcpy (zmq_msg_data (&msg), RSTRING_PTR (msg_), RSTRING_LEN (msg_)); - - rc = zmq_send (DATA_PTR (self_), &msg, NUM2INT (flags_)); - if (rc != 0 && errno == EAGAIN) { - rc = zmq_msg_close (&msg); - assert (rc == 0); - return Qfalse; - } - - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - rc = zmq_msg_close (&msg); - assert (rc == 0); - return Qnil; - } - - rc = zmq_msg_close (&msg); - assert (rc == 0); - return Qtrue; -} - -static VALUE socket_flush (VALUE self_) -{ - assert (DATA_PTR (self_)); - - int rc = zmq_flush (DATA_PTR (self_)); - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - return Qnil; - } - - return Qnil; -} - -static VALUE socket_recv (VALUE self_, VALUE flags_) -{ - assert (DATA_PTR (self_)); - - zmq_msg_t msg; - int rc = zmq_msg_init (&msg); - assert (rc == 0); - - rc = zmq_recv (DATA_PTR (self_), &msg, NUM2INT (flags_)); - if (rc != 0 && errno == EAGAIN) { - rc = zmq_msg_close (&msg); - assert (rc == 0); - return Qnil; - } - - if (rc != 0) { - rb_raise (rb_eRuntimeError, strerror (errno)); - rc = zmq_msg_close (&msg); - assert (rc == 0); - return Qnil; - } - - VALUE message = rb_str_new ((char*) zmq_msg_data (&msg), - zmq_msg_size (&msg)); - rc = zmq_msg_close (&msg); - assert (rc == 0); - return message; -} - -extern "C" void Init_librbzmq () -{ - VALUE context_type = rb_define_class ("Context", rb_cObject); - rb_define_alloc_func (context_type, context_alloc); - rb_define_method (context_type, "initialize", - (VALUE(*)(...)) context_initialize, 2); - - VALUE socket_type = rb_define_class ("Socket", rb_cObject); - rb_define_alloc_func (socket_type, socket_alloc); - rb_define_method (socket_type, "initialize", - (VALUE(*)(...)) socket_initialize, 2); - rb_define_method (socket_type, "setsockopt", - (VALUE(*)(...)) socket_setsockopt, 2); - rb_define_method (socket_type, "bind", - (VALUE(*)(...)) socket_bind, 1); - rb_define_method (socket_type, "connect", - (VALUE(*)(...)) socket_connect, 1); - rb_define_method (socket_type, "send", - (VALUE(*)(...)) socket_send, 2); - rb_define_method (socket_type, "flush", - (VALUE(*)(...)) socket_flush, 0); - rb_define_method (socket_type, "recv", - (VALUE(*)(...)) socket_recv, 1); - - rb_define_global_const ("HWM", INT2NUM (ZMQ_HWM)); - rb_define_global_const ("LWM", INT2NUM (ZMQ_LWM)); - rb_define_global_const ("SWAP", INT2NUM (ZMQ_SWAP)); - rb_define_global_const ("AFFINITY", INT2NUM (ZMQ_AFFINITY)); - rb_define_global_const ("IDENTITY", INT2NUM (ZMQ_IDENTITY)); - rb_define_global_const ("SUBSCRIBE", INT2NUM (ZMQ_SUBSCRIBE)); - rb_define_global_const ("UNSUBSCRIBE", INT2NUM (ZMQ_UNSUBSCRIBE)); - rb_define_global_const ("RATE", INT2NUM (ZMQ_RATE)); - rb_define_global_const ("RECOVERY_IVL", INT2NUM (ZMQ_RECOVERY_IVL)); - rb_define_global_const ("MCAST_LOOP", INT2NUM (ZMQ_MCAST_LOOP)); - - rb_define_global_const ("NOBLOCK", INT2NUM (ZMQ_NOBLOCK)); - rb_define_global_const ("NOFLUSH", INT2NUM (ZMQ_NOFLUSH)); - - rb_define_global_const ("P2P", INT2NUM (ZMQ_P2P)); - rb_define_global_const ("SUB", INT2NUM (ZMQ_SUB)); - rb_define_global_const ("PUB", INT2NUM (ZMQ_PUB)); - rb_define_global_const ("REQ", INT2NUM (ZMQ_REQ)); - rb_define_global_const ("REP", INT2NUM (ZMQ_REP)); -} -- cgit v1.2.3