Class: Perennial::Protocols::PureRuby::JSONTransport

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#hostObject (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

#portObject (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

#retryObject (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

#closeObject



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 read_message(timeout = nil)
  with_socket do |s|
    raise NoConnection, "no connection the server at #{@host}:#{@port}" if s.nil?
    message = nil
    begin
      Perennial::TimerImplementation.timeout(timeout || @timeout) do
        message = JSON.parse(s.gets.strip)
      end
    rescue Timeout::Error
      return nil, nil
    end
    return nil, nil if !message.is_a?(Hash)
    action, payload = message["action"], message["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 write_message(action, payload = {}, &callback)
  # TODO: Print message.
  message = JSON.dump({
    "action"  => action.to_s,
    "payload" => payload,
    "sent-at" => Time.now
  }.merge(callback_options(callback))) + SEPERATOR
  with_socket do |s|
    raise NoConnection, "no connection the server at #{@host}:#{@port}" if s.nil?
    s.write(message)
  end
end