posix-mqueue 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1d43379130238e323d67a9fdee4cfabd81a72661
4
+ data.tar.gz: 8f61ef81dcacc70618cd4a61d6828d6321004e5f
5
+ SHA512:
6
+ metadata.gz: d90c99dd26fcbb147042fbfa5301eccbb71401359d2e0a7a8c621483dcab297833526fc4b89b8fd5ac26dcd4272bc5652da3ba93e6de3aa0d520c93f4e32133d
7
+ data.tar.gz: 9aba8450a25e9045605c9f9effa72d5c240953b4c53b451e3b58bd5abdfe25b38b880d6e211363d702db7489762e601d004df41b034d702a7a3913d9d60131d6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in posix-mqueue.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Simon Eskildsen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Posix::Mqueue
2
+
3
+ Minimal wrapper around the [POSIX message queue](pmq).
4
+
5
+ [pmq]: http://man7.org/linux/man-pages/man7/mq_overview.7.html
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ext/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ have_header('mqueue.h')
3
+ create_makefile('mqueue')
data/ext/mqueue.c ADDED
@@ -0,0 +1,147 @@
1
+ #include <ruby.h>
2
+
3
+ #include <mqueue.h>
4
+ #include <fcntl.h>
5
+ #include <errno.h>
6
+
7
+ #include <stdlib.h>
8
+ #include <stdio.h>
9
+
10
+ mqd_t
11
+ rb_mqueue_fd(const char *queue, const struct mq_attr *attr) {
12
+ mqd_t fd = mq_open("/mqueue", O_CREAT | O_RDWR, S_IRWXU | S_IRWXO | S_IRWXG, attr);
13
+
14
+ if (fd == (mqd_t)-1) {
15
+ rb_sys_fail("fak rubby");
16
+ }
17
+
18
+ return fd;
19
+ }
20
+
21
+ typedef struct {
22
+ mqd_t fd;
23
+ struct mq_attr attr;
24
+ }
25
+ mqueue_t;
26
+
27
+ static void
28
+ mqueue_mark(void* ptr)
29
+ {
30
+ (void)ptr;
31
+ }
32
+
33
+ static void
34
+ mqueue_free(void* ptr)
35
+ {
36
+ mqueue_t* data = ptr;
37
+ mq_close(data->fd);
38
+ xfree(ptr);
39
+ }
40
+
41
+ static size_t
42
+ mqueue_memsize(const void* ptr)
43
+ {
44
+ return sizeof(mqueue_t);
45
+ }
46
+
47
+ static const rb_data_type_t
48
+ mqueue_type = {
49
+ "mqueue_type",
50
+ {
51
+ mqueue_mark,
52
+ mqueue_free,
53
+ mqueue_memsize
54
+ }
55
+ };
56
+
57
+ static VALUE
58
+ posix_mqueue_alloc(VALUE klass)
59
+ {
60
+ mqueue_t* data;
61
+ return TypedData_Make_Struct(klass, mqueue_t, &mqueue_type, data);
62
+ }
63
+
64
+ VALUE posix_mqueue_send(VALUE self, VALUE message)
65
+ {
66
+ int err;
67
+ mqueue_t* data;
68
+
69
+ TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);
70
+
71
+ if (!RB_TYPE_P(message, T_STRING)) {
72
+ rb_raise(rb_eTypeError, "fak u rubby");
73
+ }
74
+
75
+ // FIXME: is rstring_len with or without \0?
76
+ // TODO: Custom priority
77
+ err = mq_send(data->fd, RSTRING_PTR(message), RSTRING_LEN(message), 10);
78
+
79
+ if (err < 0) {
80
+ rb_sys_fail("fak rubby");
81
+ }
82
+
83
+ return Qtrue;
84
+ }
85
+
86
+ VALUE posix_mqueue_receive(VALUE self)
87
+ {
88
+ int err;
89
+ size_t buf_size;
90
+ char *buf;
91
+ VALUE str;
92
+
93
+ mqueue_t* data;
94
+
95
+ TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);
96
+
97
+ buf_size = data->attr.mq_msgsize + 1;
98
+
99
+ // Make sure the buffer is capable
100
+ buf = (char*)malloc(buf_size);
101
+
102
+ // TODO: Specify priority
103
+ err = mq_receive(data->fd, buf, buf_size, NULL);
104
+
105
+ if (err < 0) {
106
+ rb_sys_fail("fak rubby");
107
+ }
108
+
109
+ str = rb_str_new(buf, err);
110
+ free(buf);
111
+
112
+ return str;
113
+ }
114
+
115
+ VALUE posix_mqueue_initialize(VALUE self, VALUE queue)
116
+ {
117
+ // TODO: Modify these options from initialize arguments
118
+ // TODO: Set nonblock and handle error in #push
119
+ struct mq_attr attr = {
120
+ .mq_flags = 0, // Flags, 0 or O_NONBLOCK
121
+ .mq_maxmsg = 100, // Max messages in queue
122
+ .mq_msgsize = 512, // Max message size (bytes)
123
+ .mq_curmsgs = 0 // # currently in queue
124
+ };
125
+
126
+ mqueue_t* data;
127
+ TypedData_Get_Struct(self, mqueue_t, &mqueue_type, data);
128
+
129
+ data->attr = attr;
130
+
131
+ // FIXME: This is probably dangerous since I don't whether the value is
132
+ // actually a string.
133
+ data->fd = rb_mqueue_fd(StringValueCStr(queue), &data->attr);
134
+
135
+ return self;
136
+ }
137
+
138
+ void Init_mqueue()
139
+ {
140
+ VALUE posix = rb_define_module("POSIX");
141
+ VALUE mqueue = rb_define_class_under(posix, "Mqueue", rb_cObject);
142
+ rb_define_alloc_func(mqueue, posix_mqueue_alloc);
143
+ rb_define_method(mqueue, "initialize", posix_mqueue_initialize, 1);
144
+ rb_define_method(mqueue, "send", posix_mqueue_send, 1);
145
+ rb_define_method(mqueue, "receive", posix_mqueue_receive, 0);
146
+ }
147
+
@@ -0,0 +1,6 @@
1
+ require "posix/mqueue/version"
2
+
3
+ module POSIX
4
+ class Mqueue
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module POSIX
2
+ class Mqueue
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'posix/mqueue/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "posix-mqueue"
8
+ spec.version = POSIX::Mqueue::VERSION
9
+ spec.authors = ["Simon Eskildsen"]
10
+ spec.email = ["sirup@sirupsen.com"]
11
+ spec.description = %q{posix-mqueue is a simple wrapper around the mqueue(7).}
12
+ spec.summary = %q{posix-mqueue is a simple wrapper around the mqueue(7). It only works on Linux.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.extensions = ["ext/extconf.rb"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/unit'
2
+ require 'minitest/autorun'
3
+
4
+ $: << File.dirname(__FILE__) + '/../ext'
5
+
6
+ require 'mqueue.so'
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class MqueueTest < MiniTest::Unit::TestCase
4
+ def test_send_and_receive
5
+ m = POSIX::Mqueue.new("/whatever")
6
+ m.send("hello")
7
+ assert_equal "hello", m.receive
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: posix-mqueue
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Eskildsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: posix-mqueue is a simple wrapper around the mqueue(7).
42
+ email:
43
+ - sirup@sirupsen.com
44
+ executables: []
45
+ extensions:
46
+ - ext/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - ext/extconf.rb
55
+ - ext/mqueue.c
56
+ - lib/posix/mqueue.rb
57
+ - lib/posix/mqueue/version.rb
58
+ - posix-mqueue.gemspec
59
+ - test/test_helper.rb
60
+ - test/test_mqueue.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: posix-mqueue is a simple wrapper around the mqueue(7). It only works on Linux.
85
+ test_files:
86
+ - test/test_helper.rb
87
+ - test/test_mqueue.rb