drb-websocket 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0e85bc4e5a760ce0a2db683ff1e2ce1c8e669b7a
4
+ data.tar.gz: '04272481701b4ccec845f66876ca8899f27a90eb'
5
+ SHA512:
6
+ metadata.gz: 00c8520e27a1c60c12a58a07dff4d3012a0a28413b18c0de269ca0c63800d3537aa9e8e9ce052467e63c831e7e820a28baa04aa9c42a7cf095df9b30426c685a
7
+ data.tar.gz: 9f0b53eb4678cf698d5c57c3bb316fade1148300e379fc1182c72c1fd72839e63f901261a6793425bc50f834bc4d0ece0bd607eb11d04eeec072ce38979fb866
data/.gitignore ADDED
@@ -0,0 +1,52 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ .byebug_history
14
+
15
+ # Used by dotenv library to load environment variables.
16
+ # .env
17
+
18
+ ## Specific to RubyMotion:
19
+ .dat*
20
+ .repl_history
21
+ build/
22
+ *.bridgesupport
23
+ build-iPhoneOS/
24
+ build-iPhoneSimulator/
25
+
26
+ ## Specific to RubyMotion (use of CocoaPods):
27
+ #
28
+ # We recommend against adding the Pods directory to your .gitignore. However
29
+ # you should judge for yourself, the pros and cons are mentioned at:
30
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
31
+ #
32
+ # vendor/Pods/
33
+
34
+ ## Documentation cache and generated files:
35
+ /.yardoc/
36
+ /_yardoc/
37
+ /doc/
38
+ /rdoc/
39
+
40
+ ## Environment normalization:
41
+ /.bundle/
42
+ /vendor/bundle
43
+ /lib/bundler/man/
44
+
45
+ # for a library or gem, you might want to ignore these files since the code is
46
+ # intended to run in multiple environments; otherwise, check them in:
47
+ # Gemfile.lock
48
+ # .ruby-version
49
+ # .ruby-gemset
50
+
51
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
52
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ daemons (1.2.4)
5
+ eventmachine (1.2.3)
6
+ faye-websocket (0.10.7)
7
+ eventmachine (>= 0.12.0)
8
+ websocket-driver (>= 0.5.1)
9
+ rack (2.0.3)
10
+ thin (1.7.2)
11
+ daemons (~> 1.0, >= 1.0.9)
12
+ eventmachine (~> 1.0, >= 1.0.4)
13
+ rack (>= 1, < 3)
14
+ websocket-driver (0.6.5)
15
+ websocket-extensions (>= 0.1.0)
16
+ websocket-extensions (0.1.2)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ faye-websocket
23
+ rack
24
+ thin
25
+
26
+ BUNDLED WITH
27
+ 1.14.6
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 youchan
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ drb-websocket
2
+ =====
3
+
4
+ A druby protocol of WebSocket
5
+
6
+ This is experimental implementation of druby protocol with WebSocket.
7
+
8
+ How to run
9
+ =====
10
+
11
+ run server
12
+ ruby -I . server.rb
13
+
14
+ run client
15
+ ruby -I . client.rb
16
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "drb/websockets"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'drb/websocket/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "drb-websocket"
7
+ spec.version = Drb::Websocket::VERSION
8
+ spec.authors = ["youchan"]
9
+ spec.email = ["youchan01@gmail.com"]
10
+
11
+ spec.summary = %q{A druby protocol of WebSocket.}
12
+ spec.description = %q{A druby protocol of WebSocket.}
13
+ spec.homepage = "https://github.com/youchan/drb-websocket"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'faye-websocket'
23
+ spec.add_dependency 'rack'
24
+ spec.add_dependency 'thin'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
data/examples/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "drb-websocket", path: '..'
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ drb-websocket (0.1.0)
5
+ faye-websocket
6
+ rack
7
+ thin
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ daemons (1.2.4)
13
+ eventmachine (1.2.3)
14
+ faye-websocket (0.10.7)
15
+ eventmachine (>= 0.12.0)
16
+ websocket-driver (>= 0.5.1)
17
+ rack (2.0.3)
18
+ thin (1.7.2)
19
+ daemons (~> 1.0, >= 1.0.9)
20
+ eventmachine (~> 1.0, >= 1.0.4)
21
+ rack (>= 1, < 3)
22
+ websocket-driver (0.6.5)
23
+ websocket-extensions (>= 0.1.0)
24
+ websocket-extensions (0.1.2)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ drb-websocket!
31
+
32
+ BUNDLED WITH
33
+ 1.14.6
@@ -0,0 +1,6 @@
1
+ require 'drb/websocket'
2
+
3
+ factory = DRbObject.new_with_uri "ws://127.0.0.1:1234"
4
+
5
+ remote = factory.get
6
+ puts remote.test
@@ -0,0 +1,17 @@
1
+ require 'drb/drb'
2
+ require 'drb/websocket/server'
3
+
4
+ class SampleObject
5
+ def test
6
+ "ACK!"
7
+ end
8
+ end
9
+
10
+ class SampleFactory
11
+ def self.get
12
+ DRbObject.new(SampleObject.new)
13
+ end
14
+ end
15
+
16
+ DRb.start_service("ws://127.0.0.1:1234", SampleFactory)
17
+ DRb.thread.join
@@ -0,0 +1,143 @@
1
+ require 'drb/drb'
2
+ require 'drb/websocket'
3
+ require 'thread'
4
+ require 'rack'
5
+ require 'thin'
6
+ require 'faye/websocket'
7
+
8
+ module DRb
9
+ module WebSocket
10
+
11
+ def self.open_server(uri, config)
12
+ unless uri =~ /^ws:\/\/(.*?):(\d+)(\?(.*))?$/
13
+ raise(DRbBadScheme, uri) unless uri =~ /^ws:/
14
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
15
+ end
16
+ Server.new(uri, config)
17
+ end
18
+
19
+ class Callback
20
+ def initialize(drb)
21
+ @drb = drb
22
+ @queue = Thread::Queue.new
23
+ end
24
+
25
+ def recv_mesg(msg)
26
+ @msg = msg
27
+ @drb.push(self)
28
+ @queue.pop
29
+ end
30
+
31
+ def message
32
+ @msg
33
+ end
34
+
35
+ def reply(body)
36
+ @queue.push(body)
37
+ end
38
+
39
+ def close; end
40
+ end
41
+
42
+ class Server
43
+ attr_reader :uri
44
+
45
+ def initialize(uri, config)
46
+ @uri = uri
47
+ @config = config
48
+ @queue = Thread::Queue.new
49
+ setup_websock(uri)
50
+ end
51
+
52
+ def close
53
+ @ws.close if @ws
54
+ @ws = nil
55
+ end
56
+
57
+ def push(callback)
58
+ @queue.push(callback)
59
+ end
60
+
61
+ def accept
62
+ client = @queue.pop
63
+ ServerSide.new(client, @config, uri)
64
+ end
65
+
66
+ def setup_websock(uri)
67
+ Faye::WebSocket.load_adapter('thin')
68
+ thin = Rack::Handler.get('thin')
69
+
70
+ u = URI.parse(uri)
71
+
72
+ app = lambda do |env|
73
+ if Faye::WebSocket.websocket?(env)
74
+ ws = Faye::WebSocket.new(env)
75
+
76
+ ws.on :message do |event|
77
+ callback = Callback.new(self)
78
+ @queue.push(callback)
79
+ res = callback.recv_mesg(event.data.pack('C*'))
80
+ ws.send res.bytes
81
+ end
82
+
83
+ ws.on :close do |event|
84
+ ws = nil
85
+ end
86
+
87
+ @ws = ws
88
+
89
+ # Return async Rack response
90
+ ws.rack_response
91
+ else
92
+ # Normal HTTP request
93
+ [400, {}, []]
94
+ end
95
+ end
96
+
97
+ Thread.new do
98
+ thin.run(app, Host: u.host, Port: u.port)
99
+ end.run
100
+ end
101
+ end
102
+
103
+ class ServerSide
104
+ attr_reader :uri
105
+
106
+ def initialize(callback, config, uri)
107
+ @uri = uri
108
+ @callback = callback
109
+ @config = config
110
+ @msg = DRbMessage.new(@config)
111
+ @req_stream = StrStream.new(@callback.message)
112
+ end
113
+
114
+ def close
115
+ @callback.close if @callback
116
+ @callback = nil
117
+ end
118
+
119
+ def alive?; false; end
120
+
121
+ def recv_request
122
+ begin
123
+ @msg.recv_request(@req_stream)
124
+ rescue
125
+ close
126
+ raise $!
127
+ end
128
+ end
129
+
130
+ def send_reply(succ, result)
131
+ begin
132
+ return unless @callback
133
+ stream = StrStream.new
134
+ @msg.send_reply(stream, succ, result)
135
+ @callback.reply(stream.buf)
136
+ rescue
137
+ close
138
+ raise $!
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,5 @@
1
+ module Drb
2
+ module Websocket
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,95 @@
1
+ require "drb/websocket/version"
2
+ require 'drb/drb'
3
+ require 'uri'
4
+ require 'eventmachine'
5
+ require 'faye/websocket'
6
+
7
+ module DRb
8
+ module WebSocket
9
+ class StrStream
10
+ def initialize(str='')
11
+ @buf = str
12
+ end
13
+ attr_reader :buf
14
+
15
+ def read(n)
16
+ begin
17
+ return @buf[0,n]
18
+ ensure
19
+ @buf[0,n] = ''
20
+ end
21
+ end
22
+
23
+ def write(s)
24
+ @buf.concat s
25
+ end
26
+ end
27
+
28
+ def self.uri_option(uri, config)
29
+ return uri, nil
30
+ end
31
+
32
+ def self.open(uri, config)
33
+ unless uri =~ /^ws:\/\/(.*?):(\d+)(\?(.*))?$/
34
+ raise(DRbBadScheme, uri) unless uri =~ /^ws:/
35
+ raise(DRbBadURI, 'can\'t parse uri:' + uri)
36
+ end
37
+ ClientSide.new(uri, config)
38
+ end
39
+
40
+ class ClientSide
41
+ def initialize(uri, config)
42
+ @uri = uri
43
+ @res = nil
44
+ @config = config
45
+ @msg = DRbMessage.new(config)
46
+ @proxy = ENV['HTTP_PROXY']
47
+ end
48
+
49
+ def close
50
+ end
51
+
52
+ def alive?
53
+ false
54
+ end
55
+
56
+ def send_request(ref, msg_id, *arg, &b)
57
+ stream = StrStream.new
58
+ @msg.send_request(stream, ref, msg_id, *arg, &b)
59
+ @reply_stream = StrStream.new
60
+ post(@uri, stream.buf)
61
+ end
62
+
63
+ def recv_reply
64
+ @msg.recv_reply(@reply_stream)
65
+ end
66
+
67
+ def post(uri, data)
68
+ it = URI.parse(uri)
69
+ path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
70
+
71
+ EM.run do
72
+ sio = StrStream.new
73
+ @ws = Faye::WebSocket::Client.new(uri + path)
74
+
75
+ @ws.on :message do |event|
76
+ sio.write(event.data.pack('C*'))
77
+
78
+ if @config[:load_limit] < sio.buf.size
79
+ raise TypeError, 'too large packet'
80
+ end
81
+
82
+ @reply_stream = sio
83
+ @ws.close
84
+
85
+ EM.stop
86
+ end
87
+
88
+ @ws.send(data.bytes)
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ DRbProtocol.add_protocol(WebSocket)
95
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drb-websocket
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - youchan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faye-websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thin
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: A druby protocol of WebSocket.
98
+ email:
99
+ - youchan01@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - drb-websocket.gemspec
113
+ - examples/Gemfile
114
+ - examples/Gemfile.lock
115
+ - examples/client.rb
116
+ - examples/server.rb
117
+ - lib/drb/websocket.rb
118
+ - lib/drb/websocket/server.rb
119
+ - lib/drb/websocket/version.rb
120
+ homepage: https://github.com/youchan/drb-websocket
121
+ licenses: []
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.6.11
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: A druby protocol of WebSocket.
143
+ test_files: []