Class: Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/armstrong/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, zmq_sub_pub_addr = ["tcp://127.0.0.1", 9999, "tcp://127.0.0.1", 9998]) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
12
# File 'lib/armstrong/connection.rb', line 6

def initialize(app_id, zmq_sub_pub_addr=["tcp://127.0.0.1", 9999, "tcp://127.0.0.1", 9998])
  @app_id = app_id
  @sub_addr = zmq_sub_pub_addr[0..1].join(":")
  @pub_addr = zmq_sub_pub_addr[2..3].join(":")
  
  @request_sock = @response_sock = nil
end

Instance Attribute Details

#app_idObject (readonly)

Returns the value of attribute app_id.



4
5
6
# File 'lib/armstrong/connection.rb', line 4

def app_id
  @app_id
end

#pub_addrObject (readonly)

Returns the value of attribute pub_addr.



4
5
6
# File 'lib/armstrong/connection.rb', line 4

def pub_addr
  @pub_addr
end

#request_sockObject (readonly)

Returns the value of attribute request_sock.



4
5
6
# File 'lib/armstrong/connection.rb', line 4

def request_sock
  @request_sock
end

#response_sockObject (readonly)

Returns the value of attribute response_sock.



4
5
6
# File 'lib/armstrong/connection.rb', line 4

def response_sock
  @response_sock
end

#sub_addrObject (readonly)

Returns the value of attribute sub_addr.



4
5
6
# File 'lib/armstrong/connection.rb', line 4

def sub_addr
  @sub_addr
end

Instance Method Details

#connectObject



14
15
16
17
18
19
20
21
22
# File 'lib/armstrong/connection.rb', line 14

def connect
  context = ZMQ::Context.new 1
  @request_sock = context.socket ZMQ::PULL
  @request_sock.connect @sub_addr
  
  @response_sock = context.socket ZMQ::PUB
  @response_sock.setsockopt ZMQ::IDENTITY, @app_id
  @response_sock.connect @pub_addr
end

#receiveObject

parse the request, this is the best way to get stuff back, as a Hash



32
33
34
35
# File 'lib/armstrong/connection.rb', line 32

def receive
  data = parse(self.recv)
  return data
end

#recvObject

raw recv



25
26
27
28
29
# File 'lib/armstrong/connection.rb', line 25

def recv
  msg = ""
  @request_sock.recv_string msg
  return msg
end

#reply(request, message) ⇒ Object



45
46
47
# File 'lib/armstrong/connection.rb', line 45

def reply(request, message)
  self.send(request[:uuid], [request[:id]], message)
end

#reply_http(req, body, code = 200, headers = {"Content-type" => "text/html"}) ⇒ Object



49
50
51
# File 'lib/armstrong/connection.rb', line 49

def reply_http(req, body, code=200, headers={"Content-type" => "text/html"})
  self.reply(req, http_response(body, code, headers))
end

#send(uuid, conn_id, msg) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/armstrong/connection.rb', line 37

def send(uuid, conn_id, msg)
  header = "%s %d:%s" % [uuid, conn_id.join(' ').length, conn_id.join(' ')]
  string =  header + ', ' + msg 
  puts "\t\treplying to #{conn_id} with: ", string.inspect
  @response_sock.send_string string, ZMQ::NOBLOCK
  puts "send string"
end