Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen von Takach committed Aug 15, 2013
1 parent a0bae8a commit 8b10c92
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 120 deletions.
20 changes: 0 additions & 20 deletions Formula/libuv.rb

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2004-2012 Cotag Media
Copyright (c) 2004-2013 Cotag Media

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,55 @@
# uv.rb - libuv FFI bindings for Ruby

[Libuv](https://github.com/cotag/libuv) is a cross platform asynchronous IO implementation that powers NodeJS. It supports sockets, both UDP and TCP, filesystem operations, TTY, Pipes and other asynchronous primitives like timer, check, prepare and idle.
[Libuv](https://github.com/joyent/libuv) is a cross platform asynchronous IO implementation that powers NodeJS. It supports sockets, both UDP and TCP, filesystem operations, TTY, Pipes and other asynchronous primitives like timer, check, prepare and idle.

Libuv.rb is FFI Ruby bindings for libuv.
The Libuv gem contains Libuv and a Ruby wrapper.

## Usage

Create a uv loop or use a default one
Create a new libuv loop or use a default one

```ruby
require 'uv'
require 'libuv'

loop = UV::Loop.default
loop = Libuv::Loop.default
# or
# loop = UV::Loop.new

timer = loop.timer
timer.start(50000, 0) do |error|
p error if error
puts "50 seconds passed"
timer.close
# loop = Libuv::Loop.new

loop.run do
timer = loop.timer
timer.start(50000, 0) do |error|
p error if error
puts "50 seconds passed"
timer.close
loop.stop
end
end

loop.run
```

Find more examples in examples directory
Find more examples in examples directory and check out the [yard documentation](http://rubydoc.info/gems/libuv/Libuv/Loop)

## Installation

```Shell
gem install uvrb
gem install libuv
```

or

```shell
git clone ...
cd ...
git clone https://github.com/cotag/libuv.git
cd libuv
bundle install
rake compile
```

### Prerequisites

* The installation requires subversion to be installed on your system and available on the PATH
* Windows users will require a copy of Visual Studio 2010 or later installed. {Express}[http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products] works fine.
* The installation requires __subversion__ to be installed on your system and available on the PATH
* Windows users will require a copy of Visual Studio 2010 or later. [Visual Studio Express](http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products) works fine.

or if you have a compatible libuv.(so|dylib|dll) on the PATH already, setting the environmental variable `USE_GLOBAL_LIBUV` will prevent compliling the packaged version.


## What's supported

Expand All @@ -62,3 +67,5 @@ or
* FSEvent
* Errors
* Work queue (thread pool)


9 changes: 3 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'cucumber'
require 'cucumber/rake/task'
require 'rdoc/task'
require 'yard'
require 'ffi'
require 'rake/clean'
require 'libuv/ext/tasks'
Expand All @@ -14,11 +14,8 @@ task :default => :test
RSpec::Core::RakeTask.new(:spec)
Cucumber::Rake::Task.new(:features)

RDoc::Task.new(:rdoc => "rdoc", :clobber_rdoc => "rdoc:clean", :rerdoc => "rdoc:force") do |rd|
rd.main = "README.rdoc"
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
rd.options << "--title=UV.rb - libuv bindings for Ruby"
rd.options << "--markup=tomdoc"
YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', '-', 'ext/README.md', 'README.md']
end

task :test => [:spec, :features]
Expand Down
6 changes: 3 additions & 3 deletions ext/Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
if ENV['USE_GLOBAL_LIBUV'].nil?

if ENV.has_key?('USE_GLOBAL_LIBUV')
exit(0)
else
require 'rubygems'
require 'ffi'
require 'rake/clean'
Expand All @@ -14,5 +15,4 @@ if ENV['USE_GLOBAL_LIBUV'].nil?
task :libuv => ["ext/libuv.#{FFI::Platform::LIBSUFFIX}"]

CLOBBER.include("ext/libuv.#{FFI::Platform::LIBSUFFIX}")

end
15 changes: 8 additions & 7 deletions features/async.feature
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Feature: wake up another event loop

UV::Loop cannot be shared by multiple threads. To wake up a control loop in a different
thread, use UV::Loop#async, which is thread safe
Libuv::Loop cannot be shared by multiple threads. To wake up a control loop in a different
thread, use Libuv::Loop#async, which is thread safe

Scenario: wake up an event loop from a different thread
Given a file named "async_example.rb" with:
"""
require 'uvrb'
require 'libuv'
count = 0
loop = UV::Loop.default
loop = Libuv::Loop.default
timer = loop.timer
timer.start(0, 100) do |e|
Expand All @@ -20,9 +20,10 @@ Feature: wake up another event loop
callback = loop.async do |e|
stopper = loop.timer
stopper.start(1000, 0) do |e|
timer.close {}
callback.close {}
stopper.close {}
timer.close
callback.close
stopper.close
loop.stop
end
end
Expand Down
11 changes: 6 additions & 5 deletions features/idle.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Feature: triggering callbacks while nothing else is happening
Scenario: when you've got nothing better to do...
Given a file named "idle_example.rb" with:
"""
require 'uvrb'
require 'libuv'
idle_calls = 0
loop = UV::Loop.default
loop = Libuv::Loop.default
idle = loop.idle
idle.start do |e|
Expand All @@ -31,9 +31,10 @@ Feature: triggering callbacks while nothing else is happening
stopper = loop.timer
stopper.start(100, 0) do |e|
raise e if e
idle.close {}
timer.close {}
stopper.close {}
idle.close
timer.close
stopper.close
loop.stop
end
loop.run
Expand Down
89 changes: 42 additions & 47 deletions features/pipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,73 @@ Feature: Named pipes
Scenario: bidirectional inter process communication
Given a file named "ipc_server_example.rb" with:
"""
require 'uvrb'
require 'libuv'
pong = "pong"
loop = UV::Loop.default
loop = Libuv::Loop.default
server = loop.pipe
unless_error = proc { |error|
loop.stop
abort "the following error occurred '#{error}'"
}
server.bind("/tmp/ipc-example.ipc")
server.listen(128) do |e|
raise e if e
server.listen(128).then unless_error do
client = server.accept
client.start_read do |e, data|
raise e if e
client.write(pong) do |e|
raise e if e
unless_error.call(e) if e
client.close {}
server.close {}
client.write(pong).then unless_error do
client.close
server.close
loop.stop
end
end
end
stopper = loop.timer
stopper.start(5000, 0) do |e|
raise e if e
unless_error.call(e) if e
server.close {}
stopper.close {}
server.close
stopper.close
loop.stop
end
begin
loop.run
end
loop.run
"""
And a file named "ipc_client_example.rb" with:
"""
require 'uvrb'
require 'libuv'
ping = "ping"
loop = UV::Loop.default
loop = Libuv::Loop.default
client = loop.pipe
unless_error = proc { |error|
loop.stop
abort "the following error occurred '#{error}'"
}
client.connect("/tmp/ipc-example.ipc") do |e|
raise e if e
client.connect("/tmp/ipc-example.ipc").then unless_error do
client.start_read do |e, pong|
raise e if e
unless_error.call(e) if e
puts "received #{pong} from server"
client.close {}
client.close
loop.stop
end
client.write(ping) do |e|
raise e if e
client.write(ping).then unless_error do
puts "sent #{ping} to server"
end
end
begin
loop.run
rescue UV::Error::EOF, UV::Error::EBADF => e
exit 0
end
loop.run
"""
When I run `ruby ipc_server_example.rb` interactively
And I wait for 1 seconds
Expand All @@ -85,8 +82,8 @@ Feature: Named pipes
Given a named pipe "/tmp/exchange-pipe.pipe"
And a file named "pipe_producer_example.rb" with:
"""
require 'uvrb'
loop = UV::Loop.default
require 'libuv'
loop = Libuv::Loop.default
pipe = File.open("/tmp/exchange-pipe.pipe", File::RDWR|File::NONBLOCK)
producer = loop.pipe
Expand All @@ -106,19 +103,18 @@ Feature: Named pipes
stopper.start(3000, 0) do |e|
raise e if e
heartbeat.close {}
producer.close {}
stopper.close {}
heartbeat.close
producer.close
stopper.close
loop.stop
end
begin
loop.run
end
loop.run
"""
And a file named "pipe_consumer_example.rb" with:
"""
require 'uvrb'
loop = UV::Loop.default
require 'libuv'
loop = Libuv::Loop.default
pipe = File.open("/tmp/exchange-pipe.pipe", File::RDWR|File::NONBLOCK)
consumer = loop.pipe
Expand All @@ -136,13 +132,12 @@ Feature: Named pipes
stopper.start(2000, 0) do |e|
raise e if e
consumer.close {}
stopper.close {}
consumer.close
stopper.close
loop.stop
end
begin
loop.run
end
loop.run
"""
When I run `ruby pipe_producer_example.rb` interactively
And I run `ruby pipe_consumer_example.rb`
Expand Down
Loading

0 comments on commit 8b10c92

Please sign in to comment.