jstp 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.add_dependency 'em-websocket'
12
12
  gem.add_dependency 'em-websocket-client'
13
13
  gem.add_dependency 'discoverer'
14
+ gem.add_dependency 'symbolmatrix'
14
15
 
15
16
  gem.add_development_dependency 'rspec'
16
17
 
@@ -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'
@@ -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 number
14
- Connector.instance.port = number
13
+ def port data
14
+ Connector.instance.port = SymbolMatrix data
15
15
  end
16
16
 
17
- def strategy symbol
18
- Connector.instance.strategy = symbol
17
+ def strategy data
18
+ Connector.instance.strategy = SymbolMatrix data
19
19
  end
20
20
  end
21
21
  end
@@ -0,0 +1,7 @@
1
+ module JSTP
2
+ class Base
3
+ class << self
4
+ include ::JSTP::API
5
+ end
6
+ end
7
+ end
@@ -10,8 +10,8 @@ module JSTP
10
10
  attr_accessor :port, :strategy, :block
11
11
 
12
12
  def initialize
13
- @port = 33333
14
- @strategy = :tcp
13
+ @port = SymbolMatrix inbound: 33333, outbound: 33333
14
+ @strategy = SymbolMatrix inbound: :tcp, outbound: :tcp
15
15
  end
16
16
  end
17
17
  end
@@ -1,5 +1,12 @@
1
1
  module JSTP
2
2
  class TCP
3
3
  include Singleton
4
+ def initialize
5
+ @client_pool = {}
6
+ end
7
+
8
+ def client hostname, port
9
+ @client_pool["#{hostname}:#{port}"] ||= TCPSocket.open hostname, port
10
+ end
4
11
  end
5
12
  end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module JSTP
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -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}:33333/#{resource[1..-1].join('/')}"
27
+ .connect "ws://#{resource.first}:#{Connector.instance.port.outbound}/#{resource[1..-1].join('/')}"
28
28
  end
29
29
  end
30
30
  end
@@ -14,7 +14,7 @@ module Reader
14
14
 
15
15
  # Start the server with the TCP strategy
16
16
  def tcp
17
- @server = TCPServer.open @source.port
17
+ @server = TCPServer.open @source.port.inbound
18
18
  loop {
19
19
  Thread.start(@server.accept) { |client|
20
20
  @source.block.call JSON.parse client.gets
@@ -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
@@ -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
- context 'websocket strategy' do
18
- before do
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 'tcp strategy' do
59
- before do
60
- JSTP::Connector.instance.strategy = :tcp
61
- end
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
- JSTP::Connector.instance.should_receive(:block=)
69
- .with block
27
+ JSTP::Connector.instance.should_receive(:block=)
28
+ .with block
70
29
 
71
- JSTP::Connector.instance.should_receive(:from)
72
- .and_return reader
73
- reader.should_receive :tcp
30
+ JSTP::Connector.instance.should_receive(:from)
31
+ .and_return reader
32
+ reader.should_receive :tcp
74
33
 
75
- o = Object.new
76
- o.extend JSTP::API
77
- o.dispatch &block
78
- end
34
+ o = Object.new
35
+ o.extend JSTP::API
36
+ o.dispatch &block
79
37
  end
38
+ end
80
39
 
81
- context 'an argument is passed' do
82
- it 'should dispatch the message via the Connector' do
83
- message = stub 'message'
84
- writer = stub 'writer'
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
- JSTP::Connector.instance.should_receive(:to)
87
- .and_return writer
45
+ JSTP::Connector.instance.should_receive(:to)
46
+ .and_return writer
88
47
 
89
- writer.should_receive(:tcp)
90
- .with message
48
+ writer.should_receive(:websocket)
49
+ .with message
91
50
 
92
- o = Object.new
93
- o.extend JSTP::API
94
- o.dispatch message
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
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSTP::Base do
4
+
5
+ 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: 33333 }, &JSTP::WebSocket.instance.server_setup)
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}:33333/#{@resource[1..-1].join('/')}")
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.should_receive(:port).
58
- and_return 4444
57
+ JSTP::Connector.instance.port = SymbolMatrix inbound: 4444, outbound: 3333
59
58
 
60
- TCPSocket.should_receive(:open).with(hostname, 4444)
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.2.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