Class: Perennial::Protocols::PureRuby::JSONTransport
- Defined in:
- lib/perennial/protocols/pure_ruby/json_transport.rb
Defined Under Namespace
Classes: BufferedIO, Error, NoConnection
Constant Summary collapse
- RETRY_DELAY =
30.0- SEPERATOR =
"\r\n".freeze
- @@callbacks =
{}
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#retry ⇒ Object
readonly
Returns the value of attribute retry.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #close ⇒ Object
-
#initialize(host, port, timeout = nil) ⇒ JSONTransport
constructor
A new instance of JSONTransport.
- #read_message(timeout = nil) ⇒ Object
- #write_message(action, payload = {}, &callback) ⇒ Object
Constructor Details
#initialize(host, port, timeout = nil) ⇒ JSONTransport
39 40 41 42 43 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 39 def initialize(host, port, timeout = nil) @host = host @port = port @timeout = timeout end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
37 38 39 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 37 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
37 38 39 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 37 def port @port end |
#retry ⇒ Object (readonly)
Returns the value of attribute retry.
37 38 39 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 37 def retry @retry end |
Instance Method Details
#alive? ⇒ Boolean
82 83 84 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 82 def alive? !!socket end |
#close ⇒ Object
86 87 88 89 90 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 86 def close @socket.close if @socket && !@socket.closed? @socket = nil @retry = nil end |
#read_message(timeout = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 58 def (timeout = nil) with_socket do |s| raise NoConnection, "no connection the server at #{@host}:#{@port}" if s.nil? = nil begin Perennial::TimerImplementation.timeout(timeout || @timeout) do = JSON.parse(s.gets.strip) end rescue Timeout::Error return nil, nil end return nil, nil if !.is_a?(Hash) action, payload = ["action"], ["payload"] return nil, nil if !action.is_a?(String) payload = {} unless payload.is_a?(Hash) # We have a processed callback - huzzah! if payload.has_key?("callback-id") callback = @@callbacks.delete(payload["callback-id"]) callback.call(action, payload) if callback end return action, payload end end |
#write_message(action, payload = {}, &callback) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/perennial/protocols/pure_ruby/json_transport.rb', line 45 def (action, payload = {}, &callback) # TODO: Print message. = JSON.dump({ "action" => action.to_s, "payload" => payload, "sent-at" => Time.now }.merge((callback))) + SEPERATOR with_socket do |s| raise NoConnection, "no connection the server at #{@host}:#{@port}" if s.nil? s.write() end end |