Class: Goat::StateSrvConnection

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::P::LineText2
Defined in:
lib/goat/state-srv.rb

Constant Summary collapse

@@connection =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pusherObject

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

Returns:

  • (Boolean)


56
# File 'lib/goat/state-srv.rb', line 56

def self.connected?; @@connection != nil; end

.connectionObject



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

.reconnectObject



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.send_message(*args)
  if self.connected?
    self.connection.send_message(*args)
  else
    raise NoStateSrvConnectionError
  end
end

.send_message_sync(msg) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/goat/state-srv.rb', line 69

def self.send_message_sync(msg)
  s = TCPSocket.open(@host, @port)
  s.write(msg.to_json + "\n")
  resp = s.readline
  Goat.logd("=> #{resp.inspect}") if $verbose
  s.close
  resp
end

Instance Method Details

#closeObject



118
119
120
# File 'lib/goat/state-srv.rb', line 118

def close
  close_connection
end

#connection_completedObject



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| message_received(m)}
  else
    message_received(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 send_message(t, msg, sync=false)
  msg = msg.merge('type' => t)

  Goat.logd(">> #{msg.inspect}") if $verbose

  if sync
    self.class.send_message_sync(msg) # TODO better way to do this?
  else
    send_data(msg.to_json + "\n")
  end
end

#unbindObject



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