jstp 1.2.0 → 1.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.
data/lib/jstp/engine.rb CHANGED
@@ -4,6 +4,7 @@ module JSTP
4
4
 
5
5
  def initialize test = false
6
6
  @config = Configuration.instance
7
+ @logger = @config.logger
7
8
  from.send @config.strategy.inbound unless test
8
9
  end
9
10
 
@@ -60,6 +61,10 @@ module JSTP
60
61
  @clients ||= {}
61
62
  end
62
63
 
64
+ def logger
65
+ @logger
66
+ end
67
+
63
68
  class NotPermittedError < RuntimeError; end
64
69
  class NotAControllerError < RuntimeError; end
65
70
  class ClientNotFoundError < RuntimeError; end
data/lib/jstp/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module JSTP
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
@@ -39,20 +39,49 @@ module Reader
39
39
 
40
40
  # Start the server with the TCP strategy
41
41
  def tcp
42
- @server = TCPServer.open @config.port.inbound
42
+ @server = TCPServer.new @config.port.inbound
43
+
44
+ @config.logger.info "JSTP node running on TCP mode in port #{@config.port.inbound}"
43
45
  loop {
44
46
  Thread.start(@server.accept) { |client|
45
- begin
46
- @source.clients[UUID.new.generate] = client
47
- @message = ::JSTP::Dispatch.new client.gets
48
- @source.dispatch @message, client
47
+ begin
48
+ # Opening routine
49
+ token = UUID.new.generate
50
+ @source.clients[token] = client
51
+ if @source.respond_to? :open
52
+ begin
53
+ @source.open client, token
54
+ rescue Exception => e
55
+ @config.logger.error "On open hook: #{e.class}: #{e.message}"
56
+ @config.logger.debug e.backtrace.to_s
57
+ end
58
+ end
59
+
60
+ # Message loop
61
+ while line = client.gets
62
+ begin
63
+ @message = ::JSTP::Dispatch.new line
64
+ @source.dispatch @message, client
65
+ rescue Exception => e
66
+ log_exception e, @message
67
+ end
68
+ end
69
+
70
+ # Closing routine
49
71
  client.close
72
+ @source.clients.delete token
73
+ if @source.respond_to? :close
74
+ @source.close client, token
75
+ end
50
76
  rescue Exception => exception
51
- log_exception exception, @message
77
+ @config.logger.error "Client #{token} is DOWN: #{exception.class}: #{exception.message}"
78
+ @config.logger.debug exception.backtrace.to_s
52
79
  client.close
53
80
  end
54
81
  }
55
82
  }
83
+ rescue Exception => e
84
+ @config.logger.fatal "Could not initialize TCP server on port #{@config.port.inbound}"
56
85
  end
57
86
 
58
87
  private
@@ -1,10 +1,12 @@
1
1
  module Writer
2
2
  module JSTP
3
3
  class Dispatch < Discoverer::Pattern
4
+ attr_accessor :pool
4
5
 
5
6
  def initialize source
6
7
  super source
7
8
  @config = ::JSTP::Configuration.instance
9
+ @pool = {}
8
10
  end
9
11
 
10
12
  # Dispatch this applying the websocket strategy
@@ -41,17 +43,22 @@ module Writer
41
43
  end
42
44
  end
43
45
 
44
- # Dispatch thid applying the TCP strategy
46
+ # Dispatch applying the TCP strategy
45
47
  def tcp port = nil
46
- if port.nil?
47
- client = TCPSocket.open @source["resource"].first, @config.port.outbound
48
- else
49
- client = TCPSocket.open @source["resource"].first, port
48
+ port = @config.port.outbound if port.nil?
49
+ host = @source['resource'].first
50
+
51
+ @pool[host] ||= TCPSocket.new host, port
52
+
53
+ if @pool[host].closed?
54
+ @pool[host] = TCPSocket.new host, port
55
+ elsif not @pool[host].stat.readable?
56
+ @pool[host].close
57
+ @pool[host] = TCPSocket.new host, port
50
58
  end
51
59
 
52
- client.puts @source.to.json
60
+ @pool[host].puts @source.to.json
53
61
  @config.logger.debug @source.to_s
54
- client.close
55
62
  end
56
63
 
57
64
  def json
@@ -0,0 +1,17 @@
1
+ require 'jstp'
2
+
3
+ class Eng < JSTP::Engine
4
+ def open client, token
5
+ logger.info "Open #{token}"
6
+ end
7
+
8
+ def close client, token
9
+ logger.warn "Close #{token}"
10
+ end
11
+
12
+ class User < JSTP::Controller
13
+ def get
14
+ engine.logger.info query.first
15
+ end
16
+ end
17
+ end
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: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-02-07 00:00:00.000000000 Z
14
+ date: 2013-02-18 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: em-websocket
@@ -172,6 +172,7 @@ files:
172
172
  - samples/api-2.0/middleware.rb
173
173
  - samples/api-2.0/references.rb
174
174
  - samples/diff.rb
175
+ - samples/hook-steroid.rb
175
176
  - samples/hooks.rb
176
177
  - samples/micro.rb
177
178
  - samples/new_api.rb