ruby-fifo 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 9fcf27bae77b111caf248abe556705cd06b1560b
4
- data.tar.gz: ec8e787ebcdbae3c6b1a27fe84fd398749379ce1
2
+ SHA256:
3
+ metadata.gz: 4fab97cf3842f0e9ef32f5b9a2b1fc9c275f5e912d4d05cffb30465eb74e547e
4
+ data.tar.gz: a29449a81938a26cbf7e8cfc70b4314fcfd477925153474f927fb4f760b00f3d
5
5
  SHA512:
6
- metadata.gz: 902afd3add127ef2aaf730f5b03a63ed91e9be8c72386fd78dc6a30d52de7530f23bf73c22ade093ad40b00d990b2ed3711d9370defd63da730afc93ac9bdbf1
7
- data.tar.gz: d77fa087541717df067b42983ca0b63f540fb87b624374aac0dadf54c864f37bc1b34da1a6b6704324105cb0049a0b651ad3b60d3713fdaaf2ea8e3ab7edadeb
6
+ metadata.gz: 50b08c65ec8ec99589ab6bba1cd2fc068df634f98c2241d4f009bbd31641447700a2303e88df3320118098e6f0398bbbe2e7631006b6c78627e44a1a847b7756
7
+ data.tar.gz: 7c726e78f61a499d4c31f3dbab827f3c064e1750a6e9009b3e0e9ba20ed0b5804512fd01355e19a350036565e0f165473464c49faf02ddb1db34d9d4419ba263
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source :rubygems
2
+
3
+ if RUBY_PLATFORM =~ /mswin/
4
+ gem 'win32-pipe'
5
+ else
6
+ gem 'mkfifo'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rspec'
11
+ gem 'rake'
12
+ end
13
+
14
+ group :development do
15
+ gem 'pry'
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ coderay (1.0.6)
5
+ diff-lcs (1.1.3)
6
+ method_source (0.7.1)
7
+ mkfifo (0.0.1)
8
+ pry (0.9.9.6)
9
+ coderay (~> 1.0.5)
10
+ method_source (~> 0.7.1)
11
+ slop (>= 2.4.4, < 3)
12
+ rake (0.9.2.2)
13
+ rspec (2.10.0)
14
+ rspec-core (~> 2.10.0)
15
+ rspec-expectations (~> 2.10.0)
16
+ rspec-mocks (~> 2.10.0)
17
+ rspec-core (2.10.0)
18
+ rspec-expectations (2.10.0)
19
+ diff-lcs (~> 1.1.3)
20
+ rspec-mocks (2.10.1)
21
+ slop (2.4.4)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ mkfifo
28
+ pry
29
+ rake
30
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2
+ Version 2, December 2004
3
+
4
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5
+
6
+ Everyone is permitted to copy and distribute verbatim or modified
7
+ copies of this license document, and changing it is allowed as long
8
+ as the name is changed.
9
+
10
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12
+
13
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # ruby-fifo [![Build Status](https://travis-ci.org/shurizzle/ruby-fifo.svg?branch=master)](https://travis-ci.org/shurizzle/ruby-fifo)
2
+
3
+ A small, simple library for using Fifos in Ruby. A Fifo is traditionally a Unix
4
+ idea that lets processes communicate by writing to and reading from a special
5
+ kind of file in the filesystem. More information on fifos can be found here:
6
+ [http://en.wikipedia.org/wiki/Named_pipe](http://en.wikipedia.org/wiki/Named_pipe).
7
+
8
+ # Installation
9
+
10
+ To install ruby-fifo, execute the following command at your terminal:
11
+
12
+ $ gem install ruby-fifo
13
+
14
+ Being sure not to include the dollar sign. The dollar sign is simply convention
15
+ for denoting a terminal command.
16
+
17
+ # Usage
18
+
19
+ To use a fifo, you need both a reader and a writer (the POSIX standard does not
20
+ define the behaviour from using the same file handle as both a reader and a
21
+ writer so this library does not allow it).
22
+
23
+ Here's some example code that will simply write to a fifo and read from it, all
24
+ in the same process:
25
+
26
+ ``` ruby
27
+ reader = Fifo.new('path/to/fifo', :r, :nowait)
28
+ writer = Fifo.new('path/to/fifo', :w, :nowait)
29
+
30
+ writer.puts "Hello, world!"
31
+ reader.gets
32
+ #=> "Hello, world!\n"
33
+ ```
34
+
35
+ Notice that we pass in `:r` and `:w` for the reader and writer respectively.
36
+ Also, we have this `:nowait` symbol in there. This tells the library that we
37
+ don't want to use "blocking" fifos.
38
+
39
+ ## Blocking vs Non-blocking
40
+
41
+ A blocking fifo will block the current thread of execution until the other end
42
+ is opened. For example, the following code will never finish executing:
43
+
44
+ ``` ruby
45
+ reader = Fifo.new('path/to/fifo', :r, :wait)
46
+ writer = Fifo.new('path/to/fifo', :w, :wait)
47
+ ```
48
+
49
+ The thread will be blocked after the first line and it will wait until the
50
+ writing end of the fifo is opened before allowing execution to continue. This
51
+ also works exactly the same way in reverse (if you opened the writer before the
52
+ reader).
53
+
54
+ The following code should work fine:
55
+
56
+ ``` ruby
57
+ fork do
58
+ reader = Fifo.new('path/to/fifo', :r, :wait)
59
+ reader.gets
60
+ #=> Eventually, this will return "Hello, fork!\n"
61
+ end
62
+
63
+ fork do
64
+ writer = Fifo.new('path/to/fifo', :w, :wait)
65
+ writer.puts "Hello, fork!"
66
+ end
67
+ ```
68
+
69
+ ### Non-blocking
70
+
71
+ Alternately, you can use non-blocking pipes. These pipes don't wait for the
72
+ other end to be open before doing there work. The following code will work just
73
+ fine all in the same process:
74
+
75
+ ``` ruby
76
+ writer = Fifo.new('path/to/fifo', :w, :nowait)
77
+ writer.puts "Testing"
78
+
79
+ reader = Fifo.new('path/to/fifo', :r, :nowait)
80
+ reader.gets
81
+ #=> "Testing\n"
82
+ ```
83
+
84
+ ### Defaults
85
+
86
+ Because of this, non-blocking is the default type of fifo that this library will
87
+ create.
88
+
89
+ ``` ruby
90
+ fifo = Fifo.new('path/to/fifo')
91
+ # This is a non-blocking reader by default
92
+ ```
93
+
94
+ ## Other methods for reading and writing
95
+
96
+ There are other forms of reading and writing that will be familiar to you if you
97
+ have used the Ruby File object:
98
+
99
+ ``` ruby
100
+ reader = Fifo.new('path/to/fifo', :r, :nowait)
101
+ writer = Fifo.new('path/to/fifo', :w, :nowait)
102
+
103
+ writer.puts "Two", "Lines"
104
+ reader.gets
105
+ #=> "Two\n"
106
+ reader.gets
107
+ #=> "Lines\n"
108
+
109
+ writer.print "12345"
110
+ # reader.gets would block forever here, no new line
111
+ reader.getc
112
+ #=> "1"
113
+ reader.read(1)
114
+ #=> "2"
115
+ reader.read(3)
116
+ #=> "345"
117
+
118
+ # reader.read(1)
119
+ #=> Blocks until something is written
120
+
121
+ writer.print "Same as puts\n"
122
+ reader.readline
123
+ #=> "Same as puts\n"
124
+ ```
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ task :default => [:test]
4
+
5
+ desc "Run all tests."
6
+ RSpec::Core::RakeTask.new(:test) do |t|
7
+ t.rspec_opts = '-cfs'
8
+ end
9
+
10
+ desc "Opens up an irb session with the load path and library required."
11
+ task :console do
12
+ exec "irb -I lib/ -r ./lib/ruby-fifo.rb"
13
+ end
14
+
15
+ desc "Alias for rake console."
16
+ task :c => [:console]
@@ -0,0 +1,131 @@
1
+ class Fifo
2
+ # Module to allow delegation
3
+ include Forwardable
4
+
5
+ # Constructs a new Fifo. Can open either a read or write fifo in either
6
+ # blocking or non-blocking mode.
7
+ #
8
+ # Examples:
9
+ #
10
+ # # Non-blocking read Fifo (default)
11
+ # r = Fifo.new('path/to/fifo')
12
+ #
13
+ # # Blocking read Fifo
14
+ # r = Fifo.new('path/to/fifo', :r, :wait)
15
+ #
16
+ # # Non blocking write Fifo
17
+ # w = Fifo.new('path/to/fifo', :w, :nowait)
18
+ def initialize(file, perms = :r, mode = :nowait)
19
+ unless [:r, :w].include?(perms)
20
+ raise "Unknown file permission. Must be either :r or :w."
21
+ end
22
+
23
+ unless [:wait, :nowait].include?(mode)
24
+ raise "Unknown file mode. Must be either :wait or :nowait for blocking" +
25
+ " or non-blocking respectively."
26
+ end
27
+
28
+ if not $POSIX
29
+ include Win32
30
+
31
+ mode = mode == :wait ? Pipe::WAIT : Pipe::NOWAIT
32
+ @pipe = perms == :r ? Pipe.new_server(file, mode) : Pipe.new_client(file)
33
+ @pipe.connect if perms == :r
34
+ else
35
+ unless File.exists?(file)
36
+ File.mkfifo(file)
37
+ File.chmod(0666, file)
38
+ end
39
+
40
+ perms = perms.to_s + (mode == :wait ? '' : '+')
41
+ @pipe = File.open(file, perms)
42
+ end
43
+
44
+ def_delegators :@pipe, :read, :write, :close, :to_io, :flush
45
+ end
46
+
47
+ # Prints the arguments passed in to the fifo. to_s is called on either
48
+ # argument passed in.
49
+ #
50
+ # Example:
51
+ #
52
+ # f = Fifo.new('path/to/fifo', :w)
53
+ # f.print "Hello!"
54
+ # f.print "Multiple", "Arguments"
55
+ # f.puts "!" # Need a puts because fifos are line buffered
56
+ #
57
+ # r = Fifo.new('path/to/fifo', :r)
58
+ # r.gets
59
+ # #=> "Hello!MultipleArugments!\n"
60
+ def print(*args)
61
+ args.each do |obj|
62
+ self.write obj.to_s
63
+ end
64
+
65
+ write $OUTPUT_RECORD_SEPARATOR
66
+ flush
67
+ end
68
+
69
+ # Works the same as Kernel::puts, writes a string or multiple strings to the
70
+ # Fifo and then appends a new line. In the case of multiple arguments, a new
71
+ # line is printed after each one.
72
+ #
73
+ # Examples:
74
+ #
75
+ # w = Fifo.new('path/to/fifo', :w)
76
+ # r = Fifo.new('path/to/fifo', :r)
77
+ #
78
+ # w.puts "1", "2", "3", "4"
79
+ #
80
+ # r.gets
81
+ # #=> "1\n"
82
+ #
83
+ # r.gets
84
+ # #=> "2\n"
85
+ #
86
+ # r.gets
87
+ # #=> "3\n"
88
+ #
89
+ # r.gets
90
+ # #=> "4\n"
91
+ def puts(*args)
92
+ args.each do |obj|
93
+ self.write "#{obj.to_s.sub(/\n$/, '')}\n"
94
+ flush
95
+ end
96
+ end
97
+
98
+ # Alias for read(1).
99
+ def getc
100
+ self.read(1)
101
+ end
102
+
103
+ # Works in the same way as gets does but uses the $_ global variable for
104
+ # reading in each character. There is no functional difference between this
105
+ # and gets.
106
+ def readline
107
+ str = ""
108
+ while ($_ = self.read(1)) != "\n"
109
+ str << $_
110
+ end
111
+ str << "\n"
112
+ end
113
+
114
+ # Reads from the Fifo until it encounters a new line. Will block the current
115
+ # thread of execution until it hits a new line. This includes when the fifo is
116
+ # empty and nothing is writing to it.
117
+ #
118
+ # Example:
119
+ #
120
+ # w = Fifo.new('path/to/fifo', :w)
121
+ # r = Fifo.new('path/to/fifo', :r)
122
+ #
123
+ # w.puts "Hello, world!"
124
+ # r.gets
125
+ # #=> "Hello, world!\n"
126
+ def gets
127
+ str = ""
128
+ str << (r = self.read(1)) until r == "\n"
129
+ str
130
+ end
131
+ end
data/ruby-fifo.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+
3
+ Gem::Specification.new {|s|
4
+ s.name = 'ruby-fifo'
5
+ s.version = '0.2.0'
6
+ s.author = 'shura'
7
+ s.email = 'shura1991@gmail.com'
8
+ s.homepage = 'http://github.com/shurizzle/ruby-fifo'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.required_ruby_version = '>= 1.9.2'
11
+ s.summary = 'A cross-platform library to use named pipe'
12
+ s.description = s.summary
13
+ s.require_paths = ['lib']
14
+ s.has_rdoc = true
15
+ s.license = 'WTFPL'
16
+
17
+ # exclude these files and directories from the gem
18
+ dir_exclude = Regexp.new(%r{^(test|spec|features)/})
19
+ file_exclude = Regexp.new(/^(\.gitignore|\.travis|\.rubocop|\.rspec|Guardfile)/)
20
+ excludes = Regexp.union(dir_exclude, file_exclude)
21
+
22
+ # add all files in the repository not matching any of the above
23
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(excludes) }
24
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-fifo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shura
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-15 00:00:00.000000000 Z
11
+ date: 2024-08-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A cross-platform library to use named pipe
14
14
  email: shura1991@gmail.com
@@ -16,28 +16,35 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - Gemfile
20
+ - Gemfile.lock
21
+ - LICENSE
22
+ - README.md
23
+ - Rakefile
19
24
  - lib/ruby-fifo.rb
25
+ - lib/ruby-fifo/fifo.rb
26
+ - ruby-fifo.gemspec
20
27
  homepage: http://github.com/shurizzle/ruby-fifo
21
- licenses: []
28
+ licenses:
29
+ - WTFPL
22
30
  metadata: {}
23
- post_install_message:
31
+ post_install_message:
24
32
  rdoc_options: []
25
33
  require_paths:
26
34
  - lib
27
35
  required_ruby_version: !ruby/object:Gem::Requirement
28
36
  requirements:
29
- - - '>='
37
+ - - ">="
30
38
  - !ruby/object:Gem::Version
31
39
  version: 1.9.2
32
40
  required_rubygems_version: !ruby/object:Gem::Requirement
33
41
  requirements:
34
- - - '>='
42
+ - - ">="
35
43
  - !ruby/object:Gem::Version
36
44
  version: '0'
37
45
  requirements: []
38
- rubyforge_project:
39
- rubygems_version: 2.0.14
40
- signing_key:
46
+ rubygems_version: 3.3.25
47
+ signing_key:
41
48
  specification_version: 4
42
49
  summary: A cross-platform library to use named pipe
43
50
  test_files: []