Class: Mongrel2::Connection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, sub, pub) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mongrel2/connection.rb', line 13

def initialize(uuid, sub, pub)
  @uuid, @sub, @pub = uuid, sub, pub

  # Connect to receive requests
  @reqs = self.class.context.socket(ZMQ::PULL)
  @reqs.connect(sub)

  # Connect to send responses
  @resp = self.class.context.socket(ZMQ::PUB)
  @resp.connect(pub)
  @resp.setsockopt(ZMQ::IDENTITY, uuid)
end

Class Method Details

.contextObject



9
10
11
# File 'lib/mongrel2/connection.rb', line 9

def self.context
  @context ||= ZMQ::Context.new(1)
end

Instance Method Details

#closeObject



37
38
39
40
# File 'lib/mongrel2/connection.rb', line 37

def close
  # I think I should be able to just close the context
  self.class.context.close rescue nil
end

#recvObject



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

def recv
  msg = @reqs.recv_string(0)
  msg.nil? ? nil : Request.parse(msg)
end

#reply(req, body, status = 200, headers = {}) ⇒ Object



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

def reply(req, body, status = 200, headers = {})
  resp = Response.new(@resp)
  resp.send_http(req, body, status, headers)
  resp.close(req) if req.close?
end