ichannel 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - ruby-head
5
+ # UNIXSocket#recvmsg iz not implemented on Rubinius yet.
6
+ # - rbx-19mode
7
+
8
+ notifications:
9
+ email: true
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+ gem 'rake'
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Robert Gleeson
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,70 @@
1
+ __OVERVIEW__
2
+
3
+
4
+ | Project | IChannel
5
+ |:----------------|:--------------------------------------------------
6
+ | Homepage | https://github.com/robgleeson/ichannel
7
+ | Documentation | http://rubydoc.info/gems/ichannel/frames
8
+ | CI | [![Build Status](https://travis-ci.org/robgleeson/ichannel.png)](https://travis-ci.org/robgleeson/ichannel)
9
+ | Author | Robert Gleeson
10
+
11
+
12
+ __DESCRIPTION__
13
+
14
+ Provides a modern and easy to use interprocess communication(IPC) primitive.
15
+ It can be used to transport Ruby objects between Ruby processes running on the
16
+ same machine. The serialization options are flexible: any serializer that
17
+ implements `#dump` & `#load` can be used -- this covers Marshal, YAML, & JSON
18
+ out of the box but not MsgPack -- although you could easily write a wrapper
19
+ around msgpack.
20
+
21
+ __EXAMPLES__
22
+
23
+ __1.__
24
+
25
+ The first example shows off how you'd pass Ruby objects through a channel.
26
+ The serializer of choice is `Marshal` but it could just as easily be `JSON` or
27
+ `YAML`.
28
+
29
+ channel = IChannel.new Marshal
30
+ channel.put 'hello'
31
+ channel.put 'goodbye'
32
+ pid = fork do
33
+ channel.get # => 'hello'
34
+ channel.get # => 'goodbye'
35
+ end
36
+ Process.wait pid
37
+
38
+ __REAL WORLD EXAMPLES__
39
+
40
+ I am using IChannel in a couple of my own personal projects:
41
+
42
+ - [IProcess](https://github.com/robgleeson/iprocess)
43
+ Provides a number of abstractions on top of spawning subprocesses and
44
+ interprocess communication. IChannel was born inside IProcess but later
45
+ extracted into its own project when I realised it could be useful on its
46
+ own.
47
+
48
+ - [XPool](https://github.com/robgleeson/xpool)
49
+ A UNIX(X) Process Pool.
50
+
51
+ __PLATFORM SUPPORT__
52
+
53
+ _supported_
54
+
55
+ * CRuby (1.9+)
56
+
57
+ _unsupported_
58
+
59
+ * CRuby 1.8
60
+ * MacRuby
61
+ * JRuby
62
+ * Rubinius (support for Rubinius will come sometime in the future).
63
+
64
+ __INSTALL__
65
+
66
+ $ gem install ichannel
67
+
68
+ __LICENSE__
69
+
70
+ MIT. See LICENSE.txt.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ desc 'Run the test suite.'
3
+ task :test do
4
+ $LOAD_PATH.unshift 'lib'
5
+ Dir["test/*_test.rb"].each do |file|
6
+ require_relative file
7
+ end
8
+ end
9
+ task :default => :test
data/ichannel.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require './lib/ichannel/version'
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "ichannel"
5
+ gem.version = IChannel::VERSION
6
+ gem.authors = ["Robert Gleeson"]
7
+ gem.email = ["rob@flowof.info"]
8
+ gem.description = %q{A simple and modern interprocess communication
9
+ primitive.}
10
+ gem.summary = gem.description
11
+ gem.homepage = "https://github.com/robgleeson/ichannel"
12
+
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+ end
@@ -0,0 +1,3 @@
1
+ class IChannel
2
+ VERSION = "1.0.0"
3
+ end
data/lib/ichannel.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'socket'
2
+ class IChannel
3
+ #
4
+ # @param [#dump,#load}] serializer
5
+ # Any object that implements dump, & load.
6
+ #
7
+ def initialize(serializer)
8
+ @reader, @writer = UNIXSocket.pair Socket::SOCK_DGRAM
9
+ @serializer = serializer
10
+ end
11
+
12
+ #
13
+ # Close the channel.
14
+ #
15
+ # @return [void]
16
+ #
17
+ def close
18
+ [@reader.close,@writer.close]
19
+ end
20
+
21
+ #
22
+ # @return [Boolean]
23
+ # Returns true if the channel is closed.
24
+ #
25
+ def closed?
26
+ @reader.closed? && @writer.closed?
27
+ end
28
+
29
+ #
30
+ # @return [Boolean]
31
+ # Returns true if the channel is open.
32
+ #
33
+ def open?
34
+ !closed?
35
+ end
36
+
37
+ #
38
+ # Add an object to the channel.
39
+ #
40
+ # @param [Object] object
41
+ # An object to add to the channel.
42
+ #
43
+ def write(object)
44
+ _, writable, _ = IO.select [], [@writer], [], 0.1
45
+ if writable
46
+ @writer.send @serializer.dump(object), 0
47
+ end
48
+ end
49
+ alias_method :put, :write
50
+
51
+ #
52
+ # Receive a object from the channel.
53
+ #
54
+ # @return [Object]
55
+ # The object added to the channel.
56
+ #
57
+ def recv
58
+ readable, _ = IO.select [@reader], [], [], 0.1
59
+ if readable
60
+ msg, _ = @reader.recvmsg
61
+ @serializer.load msg
62
+ end
63
+ end
64
+ alias_method :get, :recv
65
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'setup'
2
+ class IChannelTest < Test::Unit::TestCase
3
+ def setup
4
+ @channel = IChannel.new Marshal
5
+ end
6
+
7
+ def teardown
8
+ @channel.close
9
+ end
10
+
11
+ def test_put_and_get
12
+ pid = fork do
13
+ @channel.put %w(a b c)
14
+ end
15
+ Process.wait pid
16
+ assert_equal %w(a b c), @channel.get
17
+ end
18
+
19
+ def test_queued_messages
20
+ pid = fork do
21
+ @channel.put %w(a)
22
+ @channel.put %w(b)
23
+ end
24
+ Process.wait pid
25
+ assert_equal %w(a), @channel.get
26
+ assert_equal %w(b), @channel.get
27
+ end
28
+ end
data/test/setup.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'ichannel'
2
+ require 'test/unit'
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ichannel
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Gleeson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "A simple and modern interprocess communication \n primitive."
15
+ email:
16
+ - rob@flowof.info
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .travis.yml
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - ichannel.gemspec
28
+ - lib/ichannel.rb
29
+ - lib/ichannel/version.rb
30
+ - test/ichannel_class_test.rb
31
+ - test/setup.rb
32
+ homepage: https://github.com/robgleeson/ichannel
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ segments:
45
+ - 0
46
+ hash: -3802321299613243923
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ segments:
54
+ - 0
55
+ hash: -3802321299613243923
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.23
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: A simple and modern interprocess communication primitive.
62
+ test_files:
63
+ - test/ichannel_class_test.rb
64
+ - test/setup.rb