eventkit-eventloop 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 252201710cd3e7692c813b5ec3d9f1ee3b8e3797
4
+ data.tar.gz: fae28a14ee68d67e1cb2a7fc283bc7c8765efdf7
5
+ SHA512:
6
+ metadata.gz: 436a8cf96fdfcf0d4adb5d9b53314c9a66757f4d3cd6cd18617eb016a90addd3d700d94173d2655134bc36b215cccdc928c4154f3542912626ef4832a8b54c32
7
+ data.tar.gz: 42424b8334681b6d179112c09c4a5d5f504bee1337b612943289d8a63effdbd1a27bff33138421780c110e6f271ee3c3b779fed877daa388fa2bf6c1373ae30f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in eventkit-promise.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake', '~> 10.4'
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Oliver Martell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,86 @@
1
+ # Eventkit::Eventloop
2
+
3
+ [![Build Status](https://travis-ci.org/omartell/eventkit-eventloop.svg?branch=master)](https://travis-ci.org/omartell/eventkit-eventloop)
4
+
5
+ A basic event loop implemented with Ruby's IO.select to perform non blocking IO.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'eventkit-eventloop'
13
+ ```
14
+
15
+ ## Event Loop Usage
16
+
17
+ Eventkit provides a basic event loop on top of Ruby's IO.select.
18
+ Callbacks can be registered to monitor the readability or writability of IO objects.
19
+ These callbacks are executed when the IO object is ready to be read or written to.
20
+
21
+ Another feature is timers, which allows you to execute code at some point in the future.
22
+
23
+ ```ruby
24
+ require 'eventkit/event_loop'
25
+
26
+ # Getting notified when an IO object is ready to be read or written
27
+ event_loop = Eventkit::EventLoop.new
28
+
29
+ server = TCPServer.new('localhost', 9595)
30
+
31
+ client = TCPSocket.new('localhost', 9595)
32
+
33
+ event_loop.register_read(server) do |server|
34
+ # This will be executed every time a new connection is ready to be accepted
35
+ connection, _ = server.accept_nonblock
36
+ event_loop.register_write(connection) do |connection|
37
+ bytes_written = connection.write_nonblock('hello world')
38
+ end
39
+ end
40
+
41
+ event_loop.start
42
+
43
+
44
+ # Unsubscribing from notifications
45
+ # A single read
46
+ event_loop.deregister_read(io_object, handler)
47
+
48
+ # A single write
49
+ event_loop.deregister_write(io_object, handler)
50
+
51
+ # All handlers
52
+ event_loop.deregister_write(io_object)
53
+ event_loop.deregister_read(io_object)
54
+
55
+
56
+ # Registering a handler to be run on the next tick
57
+ event_loop = Eventkit::EventLoop.new
58
+
59
+ event_loop.on_next_tick do
60
+ puts 'hello world'
61
+ event_loop.stop
62
+ end
63
+
64
+ event_loop.start
65
+
66
+
67
+ # Registering timers
68
+
69
+ event_loop = Eventkit::EventLoop.new
70
+
71
+ event_loop.register_timer(run_in: 5) do
72
+ # Block executes after 5 seconds have passed
73
+ puts 'hello world'
74
+ event_loop.stop
75
+ end
76
+
77
+ event_loop.start
78
+ ```
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/[my-github-username]/eventkit-eventloop/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
@@ -0,0 +1,4 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'eventkit/event_loop'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eventkit-eventloop"
8
+ spec.version = Eventkit::EventLoop::VERSION
9
+ spec.authors = ["Oliver Martell"]
10
+ spec.email = ["oliver.martell@gmail.com"]
11
+
12
+ spec.summary = "Event loop for non blocking IO"
13
+ spec.description =
14
+ "Event loop on top of IO.select for non blocking IO"
15
+ spec.homepage = "http://github.com/omartell/eventkit-eventloop"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2.0"
25
+ end
@@ -0,0 +1,95 @@
1
+ require 'set'
2
+ require 'eventkit/timer'
3
+
4
+ module Eventkit
5
+ class EventLoopAlreadyStartedError < StandardError; end
6
+
7
+ class EventLoop
8
+ VERSION = "0.1.0"
9
+ attr_reader :select_interval
10
+ private :select_interval
11
+
12
+ def initialize(config = {})
13
+ @read_handlers = Hash.new { |h, k| h[k] = [] }
14
+ @write_handlers = Hash.new { |h, k| h[k] = [] }
15
+ @select_interval = config.fetch(:select_interval, 1 / 100_000)
16
+ @timers = SortedSet.new
17
+ @stopped = false
18
+ @started = false
19
+ end
20
+
21
+ def start(&_block)
22
+ if @started
23
+ fail EventLoopAlreadyStartedError, 'This event loop instance has already started running'
24
+ else
25
+ @started = true
26
+ end
27
+
28
+ loop do
29
+ if stop_scheduled?
30
+ @stopped = false
31
+ break
32
+ end
33
+ tick
34
+ end
35
+ end
36
+
37
+ def stop
38
+ @stopped = true
39
+ @started = false
40
+ end
41
+
42
+ def stop_scheduled?
43
+ @stopped
44
+ end
45
+
46
+ def tick
47
+ ready_read, ready_write, _ = IO.select(@read_handlers.keys, @write_handlers.keys, [], select_interval)
48
+ ready_read.each { |io|
49
+ @read_handlers.fetch(io).each { |handler| handler.call(io) }
50
+ } if ready_read
51
+
52
+ ready_write.each { |io|
53
+ @write_handlers.fetch(io).each { |handler| handler.call(io) }
54
+ } if ready_write
55
+
56
+ @timers.select(&:expired?).each { |timer|
57
+ timer.handler.call
58
+ @timers.delete(timer)
59
+ }
60
+ nil
61
+ end
62
+
63
+ def on_next_tick(&handler)
64
+ register_timer(run_in: 0, &handler)
65
+ end
66
+
67
+ def register_timer(options, &handler)
68
+ @timers << Timer.new(options.fetch(:run_in), handler)
69
+ end
70
+
71
+ def register_read(io, &listener)
72
+ @read_handlers[io] += [listener]
73
+ end
74
+
75
+ def deregister_read(io, listener = nil)
76
+ if listener
77
+ @read_handlers[io] -= [listener]
78
+ else
79
+ @read_handlers[io] = []
80
+ end
81
+ end
82
+
83
+ def register_write(io, &listener)
84
+ @write_handlers[io] += [listener]
85
+ end
86
+
87
+ def deregister_write(io, listener = nil)
88
+ if listener
89
+ @write_handlers[io] -= [listener]
90
+ else
91
+ @write_handlers[io] = []
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,15 @@
1
+ module Eventkit
2
+ Timer = Struct.new(:expires_in, :handler) do
3
+ def initialize(seconds, handler)
4
+ super(Time.now.to_f + seconds, handler)
5
+ end
6
+
7
+ def <=>(other)
8
+ expires_in <=> other.expires_in
9
+ end
10
+
11
+ def expired?
12
+ expires_in <= Time.now.to_f
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventkit-eventloop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Martell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
41
+ description: Event loop on top of IO.select for non blocking IO
42
+ email:
43
+ - oliver.martell@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - eventkit-eventloop.gemspec
56
+ - lib/eventkit/event_loop.rb
57
+ - lib/eventkit/timer.rb
58
+ homepage: http://github.com/omartell/eventkit-eventloop
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Event loop for non blocking IO
82
+ test_files: []