drb-websocket 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/drb/websocket.rb +4 -112
- data/lib/drb/websocket/client.rb +130 -0
- data/lib/drb/websocket/server.rb +31 -34
- data/lib/drb/websocket/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fe7890accdf437f9175be51a811147a0284968e7
|
4
|
+
data.tar.gz: c584b7b51179842a460f9dc1f5c28fbdc94793aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ae6b7b7cb4255a8a610a7059910a190d0870a2a4839239db4ccf39322776434f587db5c02b526757a6f1d57b9f1e845ae4874c01bda24db7269ab80d0d8292d
|
7
|
+
data.tar.gz: f34974a952a74ec4cf4c5fcc9ae3b9ae24b1e965d57a7be96237e971dd19e64fed2e019b966e9d8e9c9b27b10ea4104836e8555e2250968c7e5345dc650499a3
|
data/lib/drb/websocket.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
require "drb/websocket/version"
|
2
2
|
require 'drb/drb'
|
3
|
-
require '
|
4
|
-
require 'eventmachine'
|
5
|
-
require 'faye/websocket'
|
3
|
+
require 'drb/websocket/rack_app'
|
6
4
|
|
7
5
|
module DRb
|
8
6
|
module WebSocket
|
@@ -28,116 +26,10 @@ module DRb
|
|
28
26
|
def self.uri_option(uri, config)
|
29
27
|
return uri, nil
|
30
28
|
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
|
-
|
38
|
-
path, uuid = $4.split('/')
|
39
|
-
|
40
|
-
unless path.nil? || path == 'callback'
|
41
|
-
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
42
|
-
end
|
43
|
-
|
44
|
-
handler = RackApp.handler(uri)
|
45
|
-
callback_handler = handler if CallbackHandler === handler
|
46
|
-
ClientSide.new(uri, config, callback_handler)
|
47
|
-
end
|
48
|
-
|
49
|
-
class CallbackHandler
|
50
|
-
def initialize(uri)
|
51
|
-
@uri = uri
|
52
|
-
@queue = Thread::Queue.new
|
53
|
-
end
|
54
|
-
|
55
|
-
def on_message(data)
|
56
|
-
sio = StrStream.new
|
57
|
-
sio.write(data.pack('C*'))
|
58
|
-
@queue.push sio
|
59
|
-
end
|
60
|
-
|
61
|
-
def on_session_start(ws)
|
62
|
-
@ws = ws
|
63
|
-
end
|
64
|
-
|
65
|
-
def stream
|
66
|
-
@queue.pop
|
67
|
-
end
|
68
|
-
|
69
|
-
def send(url, data)
|
70
|
-
@ws.send(data.bytes)
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
class ClientSide
|
75
|
-
def initialize(uri, config, handler)
|
76
|
-
@uri = uri
|
77
|
-
@res = nil
|
78
|
-
@config = config
|
79
|
-
@msg = DRbMessage.new(config)
|
80
|
-
@proxy = ENV['HTTP_PROXY']
|
81
|
-
@handler = handler
|
82
|
-
@queue = Thread::Queue.new
|
83
|
-
end
|
84
|
-
|
85
|
-
def close
|
86
|
-
end
|
87
|
-
|
88
|
-
def alive?
|
89
|
-
false
|
90
|
-
end
|
91
|
-
|
92
|
-
def send_request(ref, msg_id, *arg, &b)
|
93
|
-
stream = StrStream.new
|
94
|
-
@msg.send_request(stream, ref, msg_id, *arg, &b)
|
95
|
-
if @handler
|
96
|
-
@handler.send(@uri, stream.buf)
|
97
|
-
else
|
98
|
-
send(@uri, stream.buf)
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def recv_reply
|
103
|
-
Thread.start do
|
104
|
-
@reply_stream = @handler.stream if @handler
|
105
|
-
|
106
|
-
begin
|
107
|
-
@msg.recv_reply(@reply_stream)
|
108
|
-
rescue
|
109
|
-
close
|
110
|
-
raise $!
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
def send(uri, data)
|
116
|
-
@reply_stream = StrStream.new
|
117
|
-
it = URI.parse(uri)
|
118
|
-
path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
|
119
|
-
|
120
|
-
EM.run do
|
121
|
-
ws = Faye::WebSocket::Client.new(uri + path)
|
122
|
-
|
123
|
-
ws.on :message do |event|
|
124
|
-
sio = StrStream.new
|
125
|
-
sio.write(event.data.pack('C*'))
|
126
|
-
|
127
|
-
if @config[:load_limit] < sio.buf.size
|
128
|
-
raise TypeError, 'too large packet'
|
129
|
-
end
|
130
|
-
|
131
|
-
ws.close
|
132
|
-
|
133
|
-
EM.stop
|
134
|
-
end
|
135
|
-
|
136
|
-
ws.send(data.bytes)
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
29
|
end
|
141
30
|
|
142
31
|
DRbProtocol.add_protocol(WebSocket)
|
143
32
|
end
|
33
|
+
|
34
|
+
require 'drb/websocket/client'
|
35
|
+
require 'drb/websocket/server'
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'faye/websocket'
|
4
|
+
|
5
|
+
module DRb
|
6
|
+
module WebSocket
|
7
|
+
def self.open(uri, config)
|
8
|
+
unless uri =~ /^ws:\/\/(.*?):(\d+)(\/(.*))?$/
|
9
|
+
raise(DRbBadScheme, uri) unless uri =~ /^ws:/
|
10
|
+
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
11
|
+
end
|
12
|
+
|
13
|
+
path, uuid = $4.split('/') if $4
|
14
|
+
|
15
|
+
unless path.nil? || path == 'callback'
|
16
|
+
raise(DRbBadURI, 'can\'t parse uri: ' + uri)
|
17
|
+
end
|
18
|
+
|
19
|
+
handler = RackApp.handler(uri)
|
20
|
+
callback_handler = handler || StandaloneCallbackHandler.new(uri, config)
|
21
|
+
ClientSide.new(uri, config, callback_handler)
|
22
|
+
end
|
23
|
+
|
24
|
+
class CallbackHandler
|
25
|
+
def initialize(uri)
|
26
|
+
@uri = uri
|
27
|
+
@queue = Thread::Queue.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def on_message(data)
|
31
|
+
sio = StrStream.new
|
32
|
+
sio.write(data.pack('C*'))
|
33
|
+
@queue.push sio
|
34
|
+
end
|
35
|
+
|
36
|
+
def on_session_start(ws)
|
37
|
+
@ws = ws
|
38
|
+
end
|
39
|
+
|
40
|
+
def stream
|
41
|
+
@queue.pop
|
42
|
+
end
|
43
|
+
|
44
|
+
def send(url, data)
|
45
|
+
@ws.send(data.bytes)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class StandaloneCallbackHandler
|
50
|
+
def initialize(uri, config)
|
51
|
+
@uri = uri
|
52
|
+
@config = config
|
53
|
+
@queue = Thread::Queue.new
|
54
|
+
end
|
55
|
+
|
56
|
+
def stream
|
57
|
+
@queue.pop
|
58
|
+
end
|
59
|
+
|
60
|
+
def fiber=(fiber)
|
61
|
+
@fiber = fiber
|
62
|
+
end
|
63
|
+
|
64
|
+
def send(uri, data)
|
65
|
+
it = URI.parse(uri)
|
66
|
+
path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
|
67
|
+
|
68
|
+
Thread.new do
|
69
|
+
EM.run do
|
70
|
+
ws = Faye::WebSocket::Client.new(uri + path)
|
71
|
+
|
72
|
+
ws.on :message do |event|
|
73
|
+
sio = StrStream.new
|
74
|
+
sio.write(event.data.pack('C*'))
|
75
|
+
@queue.push sio
|
76
|
+
|
77
|
+
if @config[:load_limit] < sio.buf.size
|
78
|
+
raise TypeError, 'too large packet'
|
79
|
+
end
|
80
|
+
|
81
|
+
ws.close
|
82
|
+
|
83
|
+
EM.stop
|
84
|
+
@fiber.resume
|
85
|
+
end
|
86
|
+
|
87
|
+
ws.send(data.bytes)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class ClientSide
|
94
|
+
def initialize(uri, config, handler)
|
95
|
+
@uri = uri
|
96
|
+
@res = nil
|
97
|
+
@config = config
|
98
|
+
@msg = DRbMessage.new(config)
|
99
|
+
@proxy = ENV['HTTP_PROXY']
|
100
|
+
@handler = handler
|
101
|
+
@queue = Thread::Queue.new
|
102
|
+
end
|
103
|
+
|
104
|
+
def close
|
105
|
+
end
|
106
|
+
|
107
|
+
def alive?
|
108
|
+
false
|
109
|
+
end
|
110
|
+
|
111
|
+
def send_request(ref, msg_id, *arg, &b)
|
112
|
+
stream = StrStream.new
|
113
|
+
@msg.send_request(stream, ref, msg_id, *arg, &b)
|
114
|
+
@handler.send(@uri, stream.buf)
|
115
|
+
end
|
116
|
+
|
117
|
+
def recv_reply
|
118
|
+
@reply_stream = @handler.stream
|
119
|
+
|
120
|
+
begin
|
121
|
+
@msg.recv_reply(@reply_stream)
|
122
|
+
rescue
|
123
|
+
close
|
124
|
+
raise $!
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
data/lib/drb/websocket/server.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'drb/drb'
|
2
|
-
require_relative './rack_app'
|
3
|
-
require 'drb/websocket'
|
4
1
|
require 'thread'
|
5
2
|
require 'rack'
|
6
3
|
require 'thin'
|
@@ -17,27 +14,24 @@ module DRb
|
|
17
14
|
Server.new(uri, config)
|
18
15
|
end
|
19
16
|
|
20
|
-
class
|
21
|
-
def initialize
|
22
|
-
@
|
23
|
-
@
|
17
|
+
class Messages
|
18
|
+
def initialize
|
19
|
+
@request_message = Thread::Queue.new
|
20
|
+
@reply_message = Thread::Queue.new
|
24
21
|
end
|
25
22
|
|
26
|
-
def
|
27
|
-
@
|
28
|
-
@
|
29
|
-
@queue.pop
|
23
|
+
def recv_message(message)
|
24
|
+
@request_message.push message
|
25
|
+
@reply_message.pop
|
30
26
|
end
|
31
27
|
|
32
|
-
def
|
33
|
-
@
|
28
|
+
def request_message
|
29
|
+
@request_message.pop
|
34
30
|
end
|
35
31
|
|
36
32
|
def reply(body)
|
37
|
-
@
|
33
|
+
@reply_message.push(body)
|
38
34
|
end
|
39
|
-
|
40
|
-
def close; end
|
41
35
|
end
|
42
36
|
|
43
37
|
class Server
|
@@ -63,49 +57,52 @@ module DRb
|
|
63
57
|
end
|
64
58
|
|
65
59
|
def close
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
def push(callback)
|
70
|
-
@queue.push(callback)
|
60
|
+
@ws.close
|
61
|
+
@ws = nil
|
71
62
|
end
|
72
63
|
|
73
64
|
def accept
|
74
|
-
|
75
|
-
ServerSide.new(
|
65
|
+
messages = @queue.pop
|
66
|
+
ServerSide.new(messages, @config, uri)
|
76
67
|
end
|
77
68
|
|
78
69
|
def on_message(data)
|
79
|
-
callback = Callback.new(self)
|
80
|
-
res = callback.recv_mesg(data.pack('C*'))
|
81
|
-
@ws.send(res.bytes)
|
82
70
|
end
|
83
71
|
|
84
72
|
def on_session_start(ws)
|
85
73
|
@ws = ws
|
74
|
+
messages = Messages.new
|
75
|
+
@ws.on(:message) do |event|
|
76
|
+
Thread.new do
|
77
|
+
res = messages.recv_message(event.data.pack('C*'))
|
78
|
+
@ws.send(res.bytes)
|
79
|
+
end.run
|
80
|
+
end
|
81
|
+
@queue.push(messages)
|
86
82
|
end
|
87
83
|
end
|
88
84
|
|
89
85
|
class ServerSide
|
90
86
|
attr_reader :uri
|
91
87
|
|
92
|
-
def initialize(
|
88
|
+
def initialize(messages, config, uri)
|
93
89
|
@uri = uri
|
94
|
-
@
|
90
|
+
@messages = messages
|
95
91
|
@config = config
|
96
92
|
@msg = DRbMessage.new(@config)
|
97
|
-
@req_stream = StrStream.new(@callback.message)
|
98
93
|
end
|
99
94
|
|
100
95
|
def close
|
101
|
-
@
|
102
|
-
@callback = nil
|
96
|
+
@messages = nil
|
103
97
|
end
|
104
98
|
|
105
|
-
def alive
|
99
|
+
def alive?
|
100
|
+
!!@messages
|
101
|
+
end
|
106
102
|
|
107
103
|
def recv_request
|
108
104
|
begin
|
105
|
+
@req_stream = StrStream.new(@messages.request_message)
|
109
106
|
@msg.recv_request(@req_stream)
|
110
107
|
rescue
|
111
108
|
close
|
@@ -115,10 +112,10 @@ module DRb
|
|
115
112
|
|
116
113
|
def send_reply(succ, result)
|
117
114
|
begin
|
118
|
-
return unless
|
115
|
+
return unless alive?
|
119
116
|
stream = StrStream.new
|
120
117
|
@msg.send_reply(stream, succ, result)
|
121
|
-
@
|
118
|
+
@messages.reply(stream.buf)
|
122
119
|
rescue
|
123
120
|
close
|
124
121
|
raise $!
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: drb-websocket
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- youchan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- examples/client.rb
|
116
116
|
- examples/server.rb
|
117
117
|
- lib/drb/websocket.rb
|
118
|
+
- lib/drb/websocket/client.rb
|
118
119
|
- lib/drb/websocket/rack_app.rb
|
119
120
|
- lib/drb/websocket/server.rb
|
120
121
|
- lib/drb/websocket/version.rb
|
@@ -137,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
138
|
version: '0'
|
138
139
|
requirements: []
|
139
140
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.6.
|
141
|
+
rubygems_version: 2.6.13
|
141
142
|
signing_key:
|
142
143
|
specification_version: 4
|
143
144
|
summary: A druby protocol of WebSocket.
|