Class: SubZero::Socket
- Inherits:
-
Object
- Object
- SubZero::Socket
- Defined in:
- lib/sub_zero/socket.rb,
lib/sub_zero/socket/sender.rb,
lib/sub_zero/socket/server.rb
Defined Under Namespace
Modules: Sender, Server Classes: Error, TimeoutError
Constant Summary
Constants included from Loggable
Loggable::COLORS, Loggable::SEVERITIES, Loggable::SEVERITY_COLORS
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #check!(result_code) ⇒ Object
- #context ⇒ Object
-
#initialize(config = Configuration.default) ⇒ Socket
constructor
A new instance of Socket.
- #receive_message(socket, request) ⇒ Object
- #send_message(socket, message) ⇒ Object
- #socket(context) ⇒ Object
Methods included from Server
#down!, #ping, #poll, #run, #send_server_verb, #up!
Methods included from Sender
Methods included from Loggable
Constructor Details
#initialize(config = Configuration.default) ⇒ Socket
Returns a new instance of Socket.
18 19 20 |
# File 'lib/sub_zero/socket.rb', line 18 def initialize config = Configuration.default @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/sub_zero/socket.rb', line 16 def config @config end |
Instance Method Details
#check!(result_code) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/sub_zero/socket.rb', line 56 def check! result_code return if ZMQ::Util.resultcode_ok? result_code fail Socket::Error, "operation failed, errno [#{ZMQ::Util.errno}], " + "description [#{ZMQ::Util.error_string}]" rescue raise Socket::Error "operation failed, cannot have errno" end |
#context ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/sub_zero/socket.rb', line 22 def context context = ZMQ::Context.create(1) fail Socket::Error, 'failed to create context' unless context yield context check! context.terminate rescue Socket::Error check! context.terminate raise end |
#receive_message(socket, request) ⇒ Object
51 52 53 54 |
# File 'lib/sub_zero/socket.rb', line 51 def socket, request check! socket.recv_strings(frames = []) Message.parse frames, request end |
#send_message(socket, message) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/sub_zero/socket.rb', line 44 def socket, frames = .to_source last = frames.pop frames.each { |f| check! socket.send_string f.to_s, ZMQ::SNDMORE } check! socket.send_string last end |
#socket(context) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sub_zero/socket.rb', line 32 def socket context socket = context.socket ZMQ::DEALER socket.identity = SecureRandom.hex(10) socket.setsockopt(ZMQ::LINGER, 0) check! socket.connect("tcp://#{config[:ip]}:#{config[:port]}") yield socket check! socket.close rescue Socket::Error check! socket.close raise end |