Class: Flatware::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/flatware/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



60
61
62
63
# File 'lib/flatware/socket.rb', line 60

def initialize
  @c = ZMQ::Context.new
  @sockets = []
end

Instance Attribute Details

#cObject (readonly)

Returns the value of attribute c.



58
59
60
# File 'lib/flatware/socket.rb', line 58

def c
  @c
end

#socketsObject (readonly)

Returns the value of attribute sockets.



58
59
60
# File 'lib/flatware/socket.rb', line 58

def sockets
  @sockets
end

Instance Method Details

#close(force: false) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/flatware/socket.rb', line 77

def close(force: false)
  sockets.each do |socket|
    socket.setsockopt ZMQ::LINGER, 0
  end if force
  sockets.each(&:close)
  Flatware::socket_error unless LibZMQ.zmq_term(c.context) == 0
  Flatware.log "terminated context"
end

#socket(zmq_type, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flatware/socket.rb', line 65

def socket(zmq_type, options={})
  Socket.new(c.socket(zmq_type)).tap do |socket|
    sockets.push socket
    if port = options[:connect]
      socket.connect port
    end
    if port = options[:bind]
      socket.bind port
    end
  end
end