drb-websocket 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89726ee2059536ad9626423ad6414b9e1ce71861
4
- data.tar.gz: af8a405c335b1c845fb0bae3d4976a63a8b368f9
3
+ metadata.gz: f5f989e53a1e77689135c986a7e50e88d7624df6
4
+ data.tar.gz: d1b5c5e1ff6c7a835652e8ce69abf98337eb6a2b
5
5
  SHA512:
6
- metadata.gz: 21a810716e7ea6cada26e4e98a3b08d2bc20c2148a1d719b5f9e07b474f627e5876b4dce28861eb08ee833b9f79ee6156ab5060a197e00b79ff6e9fcb5763a17
7
- data.tar.gz: dc78d12a2a03a992a9c25a147f1bf2a7d894ff3c78be3cd85865d4c0f435743504c52998b5105fb62025850447f3c1cc462a8a88599deb168d9ab199d52f5030
6
+ metadata.gz: 7dfee7db6bd60abfa61a095a75b2a9c8546ccd4b1cf28d0e4fb01fe75ad9b62a341e534d5c44eb5ad51d39f82616f3fe5bd38e37d163c4561ad6b76805f8b8e0
7
+ data.tar.gz: d71444cee9c8b450555d8bbaac5342e3efbafc5bc63e2cd1093466ff24b55fd9de8d05cc3d3a378a36a1ec313ca9d4312e96075c8298dbe830678697fdc1f59a
data/lib/drb/websocket.rb CHANGED
@@ -35,16 +35,14 @@ module DRb
35
35
  raise(DRbBadURI, 'can\'t parse uri: ' + uri)
36
36
  end
37
37
 
38
- path = $4
38
+ path, uuid = $4.split('/')
39
39
 
40
- callback_handler = nil
41
- if path == 'callback'
42
- callback_handler = CallbackHandler.new(uri)
43
- RackApp.register(uri, callback_handler)
44
- elsif path != nil
40
+ unless path.nil? || path == 'callback'
45
41
  raise(DRbBadURI, 'can\'t parse uri: ' + uri)
46
42
  end
47
43
 
44
+ handler = RackApp.handler(uri)
45
+ callback_handler = handler if CallbackHandler === handler
48
46
  ClientSide.new(uri, config, callback_handler)
49
47
  end
50
48
 
@@ -58,15 +56,18 @@ module DRb
58
56
  sio = StrStream.new
59
57
  sio.write(data.pack('C*'))
60
58
  @queue.push sio
61
- nil
62
59
  end
63
60
 
64
- def pop
61
+ def on_session_start(ws)
62
+ @ws = ws
63
+ end
64
+
65
+ def stream
65
66
  @queue.pop
66
67
  end
67
68
 
68
- def send(data)
69
- RackApp.sockets[@uri].send(data.bytes)
69
+ def send(url, data)
70
+ @ws.send(data.bytes)
70
71
  end
71
72
  end
72
73
 
@@ -78,6 +79,7 @@ module DRb
78
79
  @msg = DRbMessage.new(config)
79
80
  @proxy = ENV['HTTP_PROXY']
80
81
  @handler = handler
82
+ @queue = Thread::Queue.new
81
83
  end
82
84
 
83
85
  def close
@@ -91,19 +93,22 @@ module DRb
91
93
  stream = StrStream.new
92
94
  @msg.send_request(stream, ref, msg_id, *arg, &b)
93
95
  if @handler
94
- @handler.send(stream.buf)
96
+ @handler.send(@uri, stream.buf)
95
97
  else
96
98
  send(@uri, stream.buf)
97
99
  end
98
100
  end
99
101
 
100
102
  def recv_reply
101
- @reply_stream = @handler.pop if @handler
102
- begin
103
- @msg.recv_reply(@reply_stream)
104
- rescue
105
- close
106
- raise $!
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
107
112
  end
108
113
  end
109
114
 
@@ -113,23 +118,22 @@ module DRb
113
118
  path = [(it.path=='' ? '/' : it.path), it.query].compact.join('?')
114
119
 
115
120
  EM.run do
116
- sio = StrStream.new
117
- @ws = Faye::WebSocket::Client.new(uri + path)
121
+ ws = Faye::WebSocket::Client.new(uri + path)
118
122
 
119
- @ws.on :message do |event|
123
+ ws.on :message do |event|
124
+ sio = StrStream.new
120
125
  sio.write(event.data.pack('C*'))
121
126
 
122
127
  if @config[:load_limit] < sio.buf.size
123
128
  raise TypeError, 'too large packet'
124
129
  end
125
130
 
126
- @reply_stream = sio
127
- @ws.close
131
+ ws.close
128
132
 
129
133
  EM.stop
130
134
  end
131
135
 
132
- @ws.send(data.bytes)
136
+ ws.send(data.bytes)
133
137
  end
134
138
  end
135
139
  end
@@ -7,20 +7,14 @@ module DRb
7
7
  end
8
8
 
9
9
  @handlers = {}
10
- @sockets = {}
11
10
 
12
11
  def self.handler(key)
13
12
  @handlers[key]
14
13
  end
15
14
 
16
- def self.sockets
17
- @sockets
18
- end
19
-
20
15
  def self.close(key)
21
- if @sockets.has_key?(key)
22
- @sockets[key].close
23
- @sockets.delete(key)
16
+ if @handlers.has_key?(key)
17
+ @handlers.delete(key)
24
18
  end
25
19
  end
26
20
 
@@ -33,17 +27,18 @@ module DRb
33
27
  ws = Faye::WebSocket.new(env)
34
28
  req = Rack::Request.new(env)
35
29
  uri = "ws://#{req.host}:#{req.port}#{req.path == '/' ? nil : req.path}"
36
- RackApp.sockets[uri] = ws
30
+
31
+ handler = req.path.start_with?('/callback') ? RackApp.register(uri, CallbackHandler.new(uri)) : RackApp.handler(uri)
32
+ handler.on_session_start(ws)
37
33
 
38
34
  ws.on :message do |event|
39
- Thread.new do
40
- res = RackApp.handler(uri).on_message(event.data)
41
- ws.send(res.bytes) if res
42
- end.run
35
+ handler.on_message(event.data)
43
36
  end
44
37
 
45
38
  ws.on :close do |event|
46
- RackApp.close(uri)
39
+ if CallbackHandler === handler
40
+ RackApp.close(uri)
41
+ end
47
42
  ws = nil
48
43
  end
49
44
 
@@ -63,8 +63,7 @@ module DRb
63
63
  end
64
64
 
65
65
  def close
66
- u = URI.parse(@uri)
67
- RackApp.close("#{u.host}:#{u.port}")
66
+ RackApp.close(@uri)
68
67
  end
69
68
 
70
69
  def push(callback)
@@ -72,13 +71,18 @@ module DRb
72
71
  end
73
72
 
74
73
  def accept
75
- client = @queue.pop
76
- ServerSide.new(client, @config, uri)
74
+ callback = @queue.pop
75
+ ServerSide.new(callback, @config, uri)
77
76
  end
78
77
 
79
78
  def on_message(data)
80
79
  callback = Callback.new(self)
81
- callback.recv_mesg(data.pack('C*'))
80
+ res = callback.recv_mesg(data.pack('C*'))
81
+ @ws.send(res.bytes)
82
+ end
83
+
84
+ def on_session_start(ws)
85
+ @ws = ws
82
86
  end
83
87
  end
84
88
 
@@ -1,5 +1,5 @@
1
1
  module Drb
2
2
  module Websocket
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
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.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-15 00:00:00.000000000 Z
11
+ date: 2017-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faye-websocket