Class: Goat::StateSrvConnection
- Includes:
- EM::P::LineText2
- Defined in:
- lib/goat/state-srv.rb
Constant Summary collapse
- @@connection =
nil
Instance Attribute Summary collapse
-
#pusher ⇒ Object
Returns the value of attribute pusher.
Class Method Summary collapse
- .connect(host, port, &dlg) ⇒ Object
- .connected? ⇒ Boolean
- .connection ⇒ Object
- .connection=(c) ⇒ Object
- .reconnect ⇒ Object
- .send_message(*args) ⇒ Object
- .send_message_sync(msg) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #connection_completed ⇒ Object
- #receive_line(line) ⇒ Object
- #send_message(t, msg, sync = false) ⇒ Object
- #unbind ⇒ Object
Instance Attribute Details
#pusher ⇒ Object
Returns the value of attribute pusher.
86 87 88 |
# File 'lib/goat/state-srv.rb', line 86 def pusher @pusher end |
Class Method Details
.connect(host, port, &dlg) ⇒ Object
58 59 60 61 62 |
# File 'lib/goat/state-srv.rb', line 58 def self.connect(host, port, &dlg) @host = host @port = port EM.connect(host, port, self) end |
.connected? ⇒ Boolean
56 |
# File 'lib/goat/state-srv.rb', line 56 def self.connected?; @@connection != nil; end |
.connection ⇒ Object
54 |
# File 'lib/goat/state-srv.rb', line 54 def self.connection; @@connection; end |
.connection=(c) ⇒ Object
55 |
# File 'lib/goat/state-srv.rb', line 55 def self.connection=(c); @@connection = c; end |
.reconnect ⇒ Object
64 65 66 67 |
# File 'lib/goat/state-srv.rb', line 64 def self.reconnect raise 'Reconnect called before connection made' if @host.nil? || @port.nil? self.connect(@host, @port) end |
.send_message(*args) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/goat/state-srv.rb', line 78 def self.(*args) if self.connected? self.connection.(*args) else raise NoStateSrvConnectionError end end |
Instance Method Details
#close ⇒ Object
118 119 120 |
# File 'lib/goat/state-srv.rb', line 118 def close close_connection end |
#connection_completed ⇒ Object
88 89 90 91 92 |
# File 'lib/goat/state-srv.rb', line 88 def connection_completed @was_connected = true Goat.logw "Connected to StateSrv" StateSrvConnection.connection = self end |
#receive_line(line) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/goat/state-srv.rb', line 94 def receive_line(line) msg = JSON.load(line) Goat.logd("=> #{msg.inspect}") if $verbose if msg.is_a?(Array) msg.each{|m| (m)} else (msg) end end |
#send_message(t, msg, sync = false) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/goat/state-srv.rb', line 106 def (t, msg, sync=false) msg = msg.merge('type' => t) Goat.logd(">> #{msg.inspect}") if $verbose if sync self.class.(msg) # TODO better way to do this? else send_data(msg.to_json + "\n") end end |
#unbind ⇒ Object
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/goat/state-srv.rb', line 122 def unbind if @was_connected Goat.logw "Lost StateSrv connection" else Goat.logw "Couldn't open StateSrv connection" end StateSrvConnection.connection = nil EM.add_timer(5) { self.class.reconnect } end |