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.



9
10
11
12
13
14
15
# File 'lib/armstrong/connection.rb', line 9

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.



7
8
9
# File 'lib/armstrong/connection.rb', line 7

def app_id
  @app_id
end

#contextObject (readonly)

Returns the value of attribute context.



7
8
9
# File 'lib/armstrong/connection.rb', line 7

def context
  @context
end

#pub_addrObject (readonly)

Returns the value of attribute pub_addr.



7
8
9
# File 'lib/armstrong/connection.rb', line 7

def pub_addr
  @pub_addr
end

#request_sockObject (readonly)

Returns the value of attribute request_sock.



7
8
9
# File 'lib/armstrong/connection.rb', line 7

def request_sock
  @request_sock
end

#response_sockObject (readonly)

Returns the value of attribute response_sock.



7
8
9
# File 'lib/armstrong/connection.rb', line 7

def response_sock
  @response_sock
end

#sub_addrObject (readonly)

Returns the value of attribute sub_addr.



7
8
9
# File 'lib/armstrong/connection.rb', line 7

def sub_addr
  @sub_addr
end

Instance Method Details

#connectObject



17
18
19
20
21
22
23
24
25
# File 'lib/armstrong/connection.rb', line 17

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



36
37
38
# File 'lib/armstrong/connection.rb', line 36

def receive
  parse(recv)
end

#recvObject

raw recv, unparsed message



28
29
30
31
32
33
# File 'lib/armstrong/connection.rb', line 28

def recv
  msg = ""
  rc = @request_sock.recv_string(msg)
  puts "errno [#{ZMQ::Util.errno}] with description [#{ZMQ::Util.error_string}]" unless ZMQ::Util.resultcode_ok?(rc)
  msg
end

#reply(env, message) ⇒ Object

reply to an env with ‘message` string



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

def reply(env, message)
  self.send(env[:sender], [env[:conn_id]], message)
end

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

reply to a req with a valid http header



55
56
57
# File 'lib/armstrong/connection.rb', line 55

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

#send(uuid, conn_id, msg) ⇒ Object

sends the message off, formatted for Mongrel2 to understand



41
42
43
44
45
46
47
# File 'lib/armstrong/connection.rb', line 41

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
  rc = @response_sock.send_string string, ZMQ::NOBLOCK
  puts "errno [#{ZMQ::Util.errno}] with description [#{ZMQ::Util.error_string}]" unless ZMQ::Util.resultcode_ok?(rc)
end