Class: Connection
- Inherits:
-
Object
- Object
- Connection
- Defined in:
- lib/angstrom/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.
9 10 11 12 13 14 15 |
# File 'lib/angstrom/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_id ⇒ Object (readonly)
Returns the value of attribute app_id.
7 8 9 |
# File 'lib/angstrom/connection.rb', line 7 def app_id @app_id end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
7 8 9 |
# File 'lib/angstrom/connection.rb', line 7 def context @context end |
#pub_addr ⇒ Object (readonly)
Returns the value of attribute pub_addr.
7 8 9 |
# File 'lib/angstrom/connection.rb', line 7 def pub_addr @pub_addr end |
#request_sock ⇒ Object (readonly)
Returns the value of attribute request_sock.
7 8 9 |
# File 'lib/angstrom/connection.rb', line 7 def request_sock @request_sock end |
#response_sock ⇒ Object (readonly)
Returns the value of attribute response_sock.
7 8 9 |
# File 'lib/angstrom/connection.rb', line 7 def response_sock @response_sock end |
#sub_addr ⇒ Object (readonly)
Returns the value of attribute sub_addr.
7 8 9 |
# File 'lib/angstrom/connection.rb', line 7 def sub_addr @sub_addr end |
Instance Method Details
#connect ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/angstrom/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 |
#receive ⇒ Object
parse the request, this is the best way to get stuff back, as a Hash
36 37 38 |
# File 'lib/angstrom/connection.rb', line 36 def receive parse(recv) end |
#recv ⇒ Object
raw recv, unparsed message
28 29 30 31 32 33 |
# File 'lib/angstrom/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/angstrom/connection.rb', line 50 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
55 56 57 |
# File 'lib/angstrom/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/angstrom/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 |