jstp 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/jstp.gemspec +1 -0
- data/lib/jstp.rb +2 -0
- data/lib/jstp/api.rb +6 -6
- data/lib/jstp/base.rb +7 -0
- data/lib/jstp/connector.rb +2 -2
- data/lib/jstp/tcp.rb +7 -0
- data/lib/jstp/version.rb +1 -1
- data/lib/jstp/web_socket.rb +2 -2
- data/lib/reader/jstp/connector.rb +1 -1
- data/lib/writer/jstp/connector.rb +2 -1
- data/spec/jstp/api_spec.rb +32 -73
- data/spec/jstp/base_spec.rb +5 -0
- data/spec/jstp/web_socket_spec.rb +4 -3
- data/spec/reader/jstp/connector_spec.rb +1 -1
- data/spec/writer/jstp/connector_spec.rb +3 -3
- metadata +20 -1
data/jstp.gemspec
CHANGED
data/lib/jstp.rb
CHANGED
@@ -4,11 +4,13 @@ require 'em-websocket'
|
|
4
4
|
require 'em-websocket-client'
|
5
5
|
require 'json'
|
6
6
|
require 'discoverer'
|
7
|
+
require 'symbolmatrix'
|
7
8
|
|
8
9
|
require 'jstp/web_socket'
|
9
10
|
require 'jstp/tcp'
|
10
11
|
|
11
12
|
require 'jstp/api'
|
13
|
+
require 'jstp/base'
|
12
14
|
require 'jstp/connector'
|
13
15
|
|
14
16
|
require 'writer/jstp/connector'
|
data/lib/jstp/api.rb
CHANGED
@@ -4,18 +4,18 @@ module JSTP
|
|
4
4
|
def dispatch *args, &block
|
5
5
|
if args.empty? and block
|
6
6
|
Connector.instance.block = block
|
7
|
-
Connector.instance.from.send Connector.instance.strategy
|
7
|
+
Connector.instance.from.send Connector.instance.strategy.inbound
|
8
8
|
else
|
9
|
-
Connector.instance.to.send Connector.instance.strategy, args.first
|
9
|
+
Connector.instance.to.send Connector.instance.strategy.outbound, args.first
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
def port
|
14
|
-
Connector.instance.port =
|
13
|
+
def port data
|
14
|
+
Connector.instance.port = SymbolMatrix data
|
15
15
|
end
|
16
16
|
|
17
|
-
def strategy
|
18
|
-
Connector.instance.strategy =
|
17
|
+
def strategy data
|
18
|
+
Connector.instance.strategy = SymbolMatrix data
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/jstp/base.rb
ADDED
data/lib/jstp/connector.rb
CHANGED
data/lib/jstp/tcp.rb
CHANGED
data/lib/jstp/version.rb
CHANGED
data/lib/jstp/web_socket.rb
CHANGED
@@ -11,7 +11,7 @@ module JSTP
|
|
11
11
|
def event_machine
|
12
12
|
@event_machine ||= proc {
|
13
13
|
::EM::WebSocket.start host: "0.0.0.0",
|
14
|
-
port: Connector.instance.port,
|
14
|
+
port: Connector.instance.port.inbound,
|
15
15
|
&WebSocket.instance.server_setup
|
16
16
|
}
|
17
17
|
end
|
@@ -24,7 +24,7 @@ module JSTP
|
|
24
24
|
|
25
25
|
def client resource
|
26
26
|
::EM::WebSocketClient
|
27
|
-
.connect "ws://#{resource.first}
|
27
|
+
.connect "ws://#{resource.first}:#{Connector.instance.port.outbound}/#{resource[1..-1].join('/')}"
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -19,8 +19,9 @@ module Writer
|
|
19
19
|
|
20
20
|
# Dispatch thid applying the TCP strategy
|
21
21
|
def tcp message
|
22
|
-
client = TCPSocket.open message["resource"].first, @source.port
|
22
|
+
client = TCPSocket.open message["resource"].first, @source.port.outbound
|
23
23
|
client.puts message.to_json
|
24
|
+
client.close
|
24
25
|
end
|
25
26
|
end
|
26
27
|
end
|
data/spec/jstp/api_spec.rb
CHANGED
@@ -7,92 +7,50 @@ describe JSTP::API do
|
|
7
7
|
o = Object.new
|
8
8
|
o.extend JSTP::API
|
9
9
|
|
10
|
-
o.strategy :websocket
|
10
|
+
o.strategy inbound: :websocket, outbound: :tcp
|
11
11
|
|
12
|
-
JSTP::Connector.instance.strategy.should == :websocket
|
12
|
+
JSTP::Connector.instance.strategy.inbound.should == :websocket
|
13
|
+
JSTP::Connector.instance.strategy.outbound.should == :tcp
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
16
17
|
describe '#dispatch' do
|
17
|
-
|
18
|
-
|
19
|
-
JSTP::Connector.instance.strategy = :websocket
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'a block is passed' do
|
23
|
-
it 'should register the block and send JSTP::Server to the EventMachine reactor' do
|
24
|
-
block = proc { "lalal" }
|
25
|
-
reader = stub 'reader'
|
26
|
-
|
27
|
-
JSTP::Connector.instance.should_receive(:block=)
|
28
|
-
.with block
|
29
|
-
|
30
|
-
JSTP::Connector.instance.should_receive(:from)
|
31
|
-
.and_return reader
|
32
|
-
reader.should_receive :websocket
|
33
|
-
|
34
|
-
o = Object.new
|
35
|
-
o.extend JSTP::API
|
36
|
-
o.dispatch &block
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'an argument is passed' do
|
41
|
-
it 'should dispatch the message via the Connector' do
|
42
|
-
message = stub 'message'
|
43
|
-
writer = stub 'writer'
|
44
|
-
|
45
|
-
JSTP::Connector.instance.should_receive(:to)
|
46
|
-
.and_return writer
|
47
|
-
|
48
|
-
writer.should_receive(:websocket)
|
49
|
-
.with message
|
50
|
-
|
51
|
-
o = Object.new
|
52
|
-
o.extend JSTP::API
|
53
|
-
o.dispatch message
|
54
|
-
end
|
55
|
-
end
|
18
|
+
before do
|
19
|
+
JSTP::Connector.instance.strategy = SymbolMatrix inbound: :tcp, outbound: :websocket
|
56
20
|
end
|
57
21
|
|
58
|
-
context '
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
context 'a block is passed' do
|
64
|
-
it 'should register the block and send JSTP::Server to the EventMachine reactor' do
|
65
|
-
block = proc { "lalal" }
|
66
|
-
reader = stub 'reader'
|
22
|
+
context 'a block is passed' do
|
23
|
+
it 'should register the block and send JSTP::Server to the EventMachine reactor' do
|
24
|
+
block = proc { "lalal" }
|
25
|
+
reader = stub 'reader'
|
67
26
|
|
68
|
-
|
69
|
-
|
27
|
+
JSTP::Connector.instance.should_receive(:block=)
|
28
|
+
.with block
|
70
29
|
|
71
|
-
|
72
|
-
|
73
|
-
|
30
|
+
JSTP::Connector.instance.should_receive(:from)
|
31
|
+
.and_return reader
|
32
|
+
reader.should_receive :tcp
|
74
33
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
end
|
34
|
+
o = Object.new
|
35
|
+
o.extend JSTP::API
|
36
|
+
o.dispatch &block
|
79
37
|
end
|
38
|
+
end
|
80
39
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
40
|
+
context 'an argument is passed' do
|
41
|
+
it 'should dispatch the message via the Connector' do
|
42
|
+
message = stub 'message'
|
43
|
+
writer = stub 'writer'
|
85
44
|
|
86
|
-
|
87
|
-
|
45
|
+
JSTP::Connector.instance.should_receive(:to)
|
46
|
+
.and_return writer
|
88
47
|
|
89
|
-
|
90
|
-
|
48
|
+
writer.should_receive(:websocket)
|
49
|
+
.with message
|
91
50
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
51
|
+
o = Object.new
|
52
|
+
o.extend JSTP::API
|
53
|
+
o.dispatch message
|
96
54
|
end
|
97
55
|
end
|
98
56
|
end
|
@@ -104,9 +62,10 @@ describe JSTP::API do
|
|
104
62
|
}
|
105
63
|
o = Object.new
|
106
64
|
o.extend JSTP::API
|
107
|
-
o.port 3000
|
65
|
+
o.port inbound: 3000, outbound: 4000
|
108
66
|
|
109
|
-
JSTP::Connector.instance.port.should == 3000
|
67
|
+
JSTP::Connector.instance.port.inbound.should == 3000
|
68
|
+
JSTP::Connector.instance.port.outbound.should == 4000
|
110
69
|
end
|
111
70
|
end
|
112
71
|
end
|
@@ -7,7 +7,7 @@ describe JSTP::WebSocket do
|
|
7
7
|
|
8
8
|
describe '#event_machine' do
|
9
9
|
before do
|
10
|
-
JSTP::Connector.instance.port = 33333
|
10
|
+
JSTP::Connector.instance.port = SymbolMatrix inbound: 33333, outbound: 44444
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'should start a websocket server in 33333' do
|
@@ -15,7 +15,8 @@ describe JSTP::WebSocket do
|
|
15
15
|
.should_receive :call
|
16
16
|
|
17
17
|
EventMachine::WebSocket.should_receive(:start)
|
18
|
-
.with({ host: '0.0.0.0', port:
|
18
|
+
.with({ host: '0.0.0.0', port: JSTP::Connector.instance.port.inbound },
|
19
|
+
&JSTP::WebSocket.instance.server_setup)
|
19
20
|
|
20
21
|
JSTP::WebSocket.instance.event_machine.call
|
21
22
|
end
|
@@ -61,7 +62,7 @@ describe JSTP::WebSocket do
|
|
61
62
|
|
62
63
|
it 'should initialize the websocket client' do
|
63
64
|
EventMachine::WebSocketClient.should_receive(:connect)
|
64
|
-
.with("ws://#{@resource.first}
|
65
|
+
.with("ws://#{@resource.first}:#{JSTP::Connector.instance.port.outbound}/#{@resource[1..-1].join('/')}")
|
65
66
|
.and_return @web_socket_client
|
66
67
|
|
67
68
|
JSTP::WebSocket.instance
|
@@ -20,7 +20,7 @@ describe Reader::JSTP::Connector do
|
|
20
20
|
tcp_server = stub 'tcp server'
|
21
21
|
|
22
22
|
TCPServer.should_receive(:open)
|
23
|
-
.with(JSTP::Connector.instance.port)
|
23
|
+
.with(JSTP::Connector.instance.port.inbound)
|
24
24
|
.and_return tcp_server
|
25
25
|
|
26
26
|
tcp_server.should_receive(:accept)
|
@@ -54,15 +54,15 @@ describe Writer::JSTP::Connector do
|
|
54
54
|
|
55
55
|
resource.should_receive(:first).and_return hostname
|
56
56
|
|
57
|
-
JSTP::Connector.instance.
|
58
|
-
and_return 4444
|
57
|
+
JSTP::Connector.instance.port = SymbolMatrix inbound: 4444, outbound: 3333
|
59
58
|
|
60
|
-
TCPSocket.should_receive(:open).with(hostname,
|
59
|
+
TCPSocket.should_receive(:open).with(hostname, JSTP::Connector.instance.port.outbound)
|
61
60
|
.and_return socket
|
62
61
|
|
63
62
|
message.should_receive(:to_json).and_return message_json
|
64
63
|
|
65
64
|
socket.should_receive(:puts).with message_json
|
65
|
+
socket.should_receive(:close)
|
66
66
|
|
67
67
|
writer = Writer::JSTP::Connector.new ::JSTP::Connector.instance
|
68
68
|
writer.tcp message
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jstp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -61,6 +61,22 @@ dependencies:
|
|
61
61
|
- - ! '>='
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: '0'
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
name: symbolmatrix
|
66
|
+
requirement: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
type: :runtime
|
73
|
+
prerelease: false
|
74
|
+
version_requirements: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
64
80
|
- !ruby/object:Gem::Dependency
|
65
81
|
name: rspec
|
66
82
|
requirement: !ruby/object:Gem::Requirement
|
@@ -93,6 +109,7 @@ files:
|
|
93
109
|
- jstp.gemspec
|
94
110
|
- lib/jstp.rb
|
95
111
|
- lib/jstp/api.rb
|
112
|
+
- lib/jstp/base.rb
|
96
113
|
- lib/jstp/connector.rb
|
97
114
|
- lib/jstp/tcp.rb
|
98
115
|
- lib/jstp/version.rb
|
@@ -100,6 +117,7 @@ files:
|
|
100
117
|
- lib/reader/jstp/connector.rb
|
101
118
|
- lib/writer/jstp/connector.rb
|
102
119
|
- spec/jstp/api_spec.rb
|
120
|
+
- spec/jstp/base_spec.rb
|
103
121
|
- spec/jstp/connector_spec.rb
|
104
122
|
- spec/jstp/tcp_spec.rb
|
105
123
|
- spec/jstp/web_socket_spec.rb
|
@@ -132,6 +150,7 @@ specification_version: 3
|
|
132
150
|
summary: Reference implementation for the sketch protocol JSTP
|
133
151
|
test_files:
|
134
152
|
- spec/jstp/api_spec.rb
|
153
|
+
- spec/jstp/base_spec.rb
|
135
154
|
- spec/jstp/connector_spec.rb
|
136
155
|
- spec/jstp/tcp_spec.rb
|
137
156
|
- spec/jstp/web_socket_spec.rb
|