chattyproc 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +57 -0
- data/Rakefile +1 -0
- data/chattyproc.gemspec +19 -0
- data/lib/chattyproc.rb +51 -0
- data/lib/chattyproc/version.rb +3 -0
- data/spec/pipe_server_spec.rb +23 -0
- data/spec/spec_helper.rb +4 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alan Johnson
|
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,57 @@
|
|
1
|
+
# Chattyproc
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'chattyproc'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install chattyproc
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Right now ChattyProc only includes a single process communication method - a pipe server. The idea behind the pipe server is that a process can communicate back and forth with processes that it forks. Here's a quick example:
|
22
|
+
|
23
|
+
require "chattyproc"
|
24
|
+
|
25
|
+
# Set up the pipe server
|
26
|
+
ps = ChattyProc::PipeServer.new
|
27
|
+
|
28
|
+
# Fork a new process
|
29
|
+
fork do
|
30
|
+
# Mark this process as the callee
|
31
|
+
ps.callee!
|
32
|
+
|
33
|
+
message = ps.read
|
34
|
+
ps.write("Hello #{message}")
|
35
|
+
end
|
36
|
+
|
37
|
+
# Mark this process as the caller
|
38
|
+
ps.caller!
|
39
|
+
|
40
|
+
# Send a message over to the other process
|
41
|
+
ps.write("Alan")
|
42
|
+
|
43
|
+
# Print the response
|
44
|
+
response = ps.read
|
45
|
+
|
46
|
+
# Wait for the child process to exit
|
47
|
+
Process.waitall
|
48
|
+
|
49
|
+
This style of pipe server is in use in [tconsole](http://github.com/commondream/tconsole) and has worked amazingly well. It's faster than DRb for communication between forked processes, and has shown itself to be less error prone.
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
57
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/chattyproc.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'chattyproc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "chattyproc"
|
8
|
+
gem.version = ChattyProc::VERSION
|
9
|
+
gem.authors = ["Alan Johnson"]
|
10
|
+
gem.email = ["alan@commondream.net"]
|
11
|
+
gem.description = %q{Library for communicating between forked processes.}
|
12
|
+
gem.summary = %q{Library for communicating between forked processes.}
|
13
|
+
gem.homepage = "http://github.com/commondream/chattyproc"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
data/lib/chattyproc.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "chattyproc/version"
|
2
|
+
|
3
|
+
module ChattyProc
|
4
|
+
class PipeServer
|
5
|
+
|
6
|
+
# Public: Sets up a new PipeServer
|
7
|
+
def initialize
|
8
|
+
@callee = []
|
9
|
+
@caller = []
|
10
|
+
@callee[0], @caller[1] = IO.pipe
|
11
|
+
@caller[0], @callee[1] = IO.pipe
|
12
|
+
|
13
|
+
@me = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
# Public: Identifies the current process as the callee process
|
17
|
+
def callee!
|
18
|
+
@me = @callee
|
19
|
+
|
20
|
+
@caller.each do |io|
|
21
|
+
io.close
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Identifies the current process as the caller process
|
26
|
+
def caller!
|
27
|
+
@me = @caller
|
28
|
+
|
29
|
+
@callee.each do |io|
|
30
|
+
io.close
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# Public: Writes a message to the appropriate pipe. The message can be
|
35
|
+
# anything that will Marshal cleanly.
|
36
|
+
def write(message)
|
37
|
+
encoded_message = [Marshal.dump(message)].pack("m0")
|
38
|
+
@me[1].puts(encoded_message)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Public: Reads a message from the appropriate pipe and unmarshalls it
|
42
|
+
def read
|
43
|
+
raw_message = @me[0].gets
|
44
|
+
|
45
|
+
return nil if raw_message.nil?
|
46
|
+
|
47
|
+
Marshal.load(raw_message.unpack("m")[0])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ChattyProc::PipeServer do
|
4
|
+
before do
|
5
|
+
@ps = ChattyProc::PipeServer.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "can send and respond to messages" do
|
9
|
+
test_message = "This is a test"
|
10
|
+
fork do
|
11
|
+
@ps.callee!
|
12
|
+
|
13
|
+
message = @ps.read
|
14
|
+
@ps.write(message)
|
15
|
+
end
|
16
|
+
|
17
|
+
@ps.caller!
|
18
|
+
@ps.write(test_message)
|
19
|
+
response = @ps.read
|
20
|
+
|
21
|
+
expect(response).to eq(test_message)
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chattyproc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan Johnson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Library for communicating between forked processes.
|
15
|
+
email:
|
16
|
+
- alan@commondream.net
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- chattyproc.gemspec
|
27
|
+
- lib/chattyproc.rb
|
28
|
+
- lib/chattyproc/version.rb
|
29
|
+
- spec/pipe_server_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
31
|
+
homepage: http://github.com/commondream/chattyproc
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Library for communicating between forked processes.
|
55
|
+
test_files:
|
56
|
+
- spec/pipe_server_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc:
|