tribe_em 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +13 -0
- data/lib/tribe_em.rb +31 -0
- data/lib/tribe_em/connection.rb +46 -0
- data/lib/tribe_em/tcp_server.rb +55 -0
- data/lib/tribe_em/version.rb +5 -0
- data/tribe_em.gemspec +22 -0
- metadata +87 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chad Remesch
|
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,30 @@
|
|
1
|
+
# Tribe EM
|
2
|
+
|
3
|
+
Tribe EM is a Ruby gem that adds network IO to the [Tribe] (https://github.com/chadrem/tribe "Tribe") gem.
|
4
|
+
It is based on [EventMachine] (http://rubyeventmachine.com/ "EventMachine").
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'tribe_em'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install tribe_em
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Coming soon.
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/tribe_em.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'tribe'
|
2
|
+
require 'eventmachine'
|
3
|
+
|
4
|
+
require 'tribe_em/version'
|
5
|
+
require 'tribe_em/connection'
|
6
|
+
require 'tribe_em/tcp_server'
|
7
|
+
|
8
|
+
module Tribe
|
9
|
+
module EM
|
10
|
+
def self.start
|
11
|
+
@em_thread = Thread.new { ::EM.run {} }
|
12
|
+
|
13
|
+
return nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.stop
|
17
|
+
::EM.stop_event_loop
|
18
|
+
@em_thread.join if @em_thread
|
19
|
+
@em_thread = nil
|
20
|
+
|
21
|
+
return nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.running?
|
25
|
+
return ::EM.reactor_running?
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Force EM to run.
|
31
|
+
Tribe::EM.start
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Tribe
|
2
|
+
module EM
|
3
|
+
class Connection < ::EM::Connection
|
4
|
+
include Tribe::Actable
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
init_actable(options)
|
8
|
+
end
|
9
|
+
|
10
|
+
def post_init
|
11
|
+
enqueue(:post_init, nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
def receive_data(data)
|
15
|
+
enqueue(:receive_data, data)
|
16
|
+
end
|
17
|
+
|
18
|
+
def unbind
|
19
|
+
enqueue(:unbind, nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
def process_event(event)
|
23
|
+
case event.command
|
24
|
+
when :post_init
|
25
|
+
post_init_handler(event)
|
26
|
+
when :receive_data
|
27
|
+
receive_data_handler(event)
|
28
|
+
when :unbind
|
29
|
+
unbind_handler(event)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def post_init_handler(event)
|
34
|
+
puts "Actor (#{identifier}) connected to client using thread (#{Thread.current.object_id})."
|
35
|
+
end
|
36
|
+
|
37
|
+
def receive_data_handler(event)
|
38
|
+
puts "Actor (#{identifier}) received data (#{event.data}) using thread (#{Thread.current.object_id})."
|
39
|
+
end
|
40
|
+
|
41
|
+
def unbind_handler(event)
|
42
|
+
puts "Actor (#{identifier}) disconnected from client using thread (#{Thread.current.object_id})."
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Tribe
|
2
|
+
module EM
|
3
|
+
class TcpServer < Tribe::Actor
|
4
|
+
private
|
5
|
+
|
6
|
+
def initialize(ip, port, conn_class, options = {})
|
7
|
+
super(options)
|
8
|
+
|
9
|
+
@ip = ip
|
10
|
+
@port = port
|
11
|
+
@conn_class = conn_class
|
12
|
+
|
13
|
+
start_listener
|
14
|
+
end
|
15
|
+
|
16
|
+
def process_event(event)
|
17
|
+
case event.command
|
18
|
+
when :start_listener
|
19
|
+
start_listener_handler(event)
|
20
|
+
when :stop_listener
|
21
|
+
stop_listener_handler(event)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def start_listener_handler(event)
|
26
|
+
start_listener
|
27
|
+
end
|
28
|
+
|
29
|
+
def stop_listener_handler(event)
|
30
|
+
stop_listener
|
31
|
+
end
|
32
|
+
|
33
|
+
def shutdown_handler(event)
|
34
|
+
stop_server
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_server_sig_handler(event)
|
38
|
+
@server_sig = event.data
|
39
|
+
end
|
40
|
+
|
41
|
+
def start_listener
|
42
|
+
return if @server_sig
|
43
|
+
|
44
|
+
@server_sig = ::EM.start_server(@ip, @port, @conn_class)
|
45
|
+
end
|
46
|
+
|
47
|
+
def stop_listener
|
48
|
+
return unless @server_sig
|
49
|
+
|
50
|
+
::EM.stop_server(@server_sig)
|
51
|
+
@server_sig = nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/tribe_em.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tribe_em/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "tribe_em"
|
8
|
+
gem.version = Tribe::EM::VERSION
|
9
|
+
gem.authors = ["Chad Remesch"]
|
10
|
+
gem.email = ["chad@remesch.com"]
|
11
|
+
gem.description = %q{Event driven network IO for the Tribe gem.}
|
12
|
+
gem.summary = %q{Quickly create network servers using EventMachine and Tribe.}
|
13
|
+
gem.homepage = "https://github.com/chadrem/tribe_em"
|
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
|
+
|
20
|
+
gem.add_dependency('tribe', ['0.0.7'])
|
21
|
+
gem.add_dependency('eventmachine', '>= 1.0.0')
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tribe_em
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chad Remesch
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: tribe
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - '='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.7
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - '='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.0.7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: eventmachine
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.0.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
46
|
+
description: Event driven network IO for the Tribe gem.
|
47
|
+
email:
|
48
|
+
- chad@remesch.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/tribe_em.rb
|
59
|
+
- lib/tribe_em/connection.rb
|
60
|
+
- lib/tribe_em/tcp_server.rb
|
61
|
+
- lib/tribe_em/version.rb
|
62
|
+
- tribe_em.gemspec
|
63
|
+
homepage: https://github.com/chadrem/tribe_em
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Quickly create network servers using EventMachine and Tribe.
|
87
|
+
test_files: []
|