Class: Connection
- Inherits:
-
Object
- Object
- Connection
- Defined in:
- lib/armstrong/connection.rb
Instance Attribute Summary collapse
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#pub_addr ⇒ Object
readonly
Returns the value of attribute pub_addr.
-
#request_sock ⇒ Object
readonly
Returns the value of attribute request_sock.
-
#response_sock ⇒ Object
readonly
Returns the value of attribute response_sock.
-
#sub_addr ⇒ Object
readonly
Returns the value of attribute sub_addr.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(app_id, zmq_sub_pub_addr = ["tcp://127.0.0.1", 9999, "tcp://127.0.0.1", 9998]) ⇒ Connection
constructor
A new instance of Connection.
-
#receive ⇒ Object
parse the request, this is the best way to get stuff back, as a Hash.
-
#recv ⇒ Object
raw recv, unparsed message.
-
#reply(env, message) ⇒ Object
reply to an env with ‘message` string.
-
#reply_http(env, body, code = 200, headers = {"Content-type" => "text/html"}) ⇒ Object
reply to a req with a valid http header.
-
#send(uuid, conn_id, msg) ⇒ Object
sends the message off, formatted for Mongrel2 to understand.
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.
8 9 10 11 12 13 14 |
# File 'lib/armstrong/connection.rb', line 8 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_id ⇒ Object (readonly)
Returns the value of attribute app_id.
6 7 8 |
# File 'lib/armstrong/connection.rb', line 6 def app_id @app_id end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/armstrong/connection.rb', line 6 def context @context end |
#pub_addr ⇒ Object (readonly)
Returns the value of attribute pub_addr.
6 7 8 |
# File 'lib/armstrong/connection.rb', line 6 def pub_addr @pub_addr end |
#request_sock ⇒ Object (readonly)
Returns the value of attribute request_sock.
6 7 8 |
# File 'lib/armstrong/connection.rb', line 6 def request_sock @request_sock end |
#response_sock ⇒ Object (readonly)
Returns the value of attribute response_sock.
6 7 8 |
# File 'lib/armstrong/connection.rb', line 6 def response_sock @response_sock end |
#sub_addr ⇒ Object (readonly)
Returns the value of attribute sub_addr.
6 7 8 |
# File 'lib/armstrong/connection.rb', line 6 def sub_addr @sub_addr end |
Instance Method Details
#connect ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/armstrong/connection.rb', line 16 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 |
#receive ⇒ Object
parse the request, this is the best way to get stuff back, as a Hash
35 36 37 |
# File 'lib/armstrong/connection.rb', line 35 def receive parse(recv) end |
#recv ⇒ Object
raw recv, unparsed message
27 28 29 30 31 32 |
# File 'lib/armstrong/connection.rb', line 27 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
48 49 50 |
# File 'lib/armstrong/connection.rb', line 48 def reply(env, ) self.send(env[:sender], [env[:conn_id]], ) end |
#reply_http(env, body, code = 200, headers = {"Content-type" => "text/html"}) ⇒ Object
reply to a req with a valid http header
53 54 55 |
# File 'lib/armstrong/connection.rb', line 53 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
40 41 42 43 44 45 |
# File 'lib/armstrong/connection.rb', line 40 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 @response_sock.send_string string, ZMQ::NOBLOCK end |