class Deployable::Zmq::Publish

Deployable::Zmq provides a generic set of helpers do you don't have to do so much leg work.

Public Class Methods

new(options = {}) click to toggle source
# File lib/deployable/zmq/publish.rb, line 19
def initialize options = {}

  @context   = ZMQ::Context.new
  @publisher = @context.socket ZMQ::PUB

  @port      = options.fetch :port, __class_ivg( 'port' )
  @port      = DEFAULT_BIND_PORT if @port.nil?

  @address   = options.fetch :address, __class_ivg( 'address' )
  @address   = DEFAULT_BIND_ADDRESS if @address.nil?

  uri = "tcp://#{@address}:#{@port}"
  
  unless rc = @publisher.bind( uri ) == 0
    raise "zmq bind failed [#{rc}] [#{uri}]" 
  end

  #log.debug "zmq publishing on [#{uri}]"
end

Public Instance Methods

end() click to toggle source

End the connection

# File lib/deployable/zmq/publish.rb, line 65
def end
  @publisher.close
end
send_arr(label, array) click to toggle source

Send an array of messages

# File lib/deployable/zmq/publish.rb, line 46
def send_arr label, array
  @publisher.send_string label, ZMQ::SNDMORE

  # Everything but the last element
  array[0..-2].each do |e|
    @publisher.send_string e.to_s, ZMQ::SNDMORE
  end
  @publisher.send_string array.last.to_s
end
send_json(label, obj) click to toggle source

Send an object in json

# File lib/deployable/zmq/publish.rb, line 57
def send_json label, obj
  # parse before send in case of issues
  message = obj.to_json
  @publisher.send_string label, ZMQ::SNDMORE
  @publisher.send_string message
end
send_string(label, message) click to toggle source

String a simple string

# File lib/deployable/zmq/publish.rb, line 40
def send_string label, message
  @publisher.send_string label, ZMQ::SNDMORE
  @publisher.send_string message
end