Class: Deployable::Zmq::Publish

Inherits:
Object
  • Object
show all
Defined in:
lib/deployable/zmq/publish.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Publish

Returns a new instance of Publish.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#endObject

End the connection



65
66
67
# File 'lib/deployable/zmq/publish.rb', line 65

def end
  @publisher.close
end

#send_arr(label, array) ⇒ Object

Send an array of messages



46
47
48
49
50
51
52
53
54
# 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) ⇒ Object

Send an object in json



57
58
59
60
61
62
# 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) ⇒ Object

String a simple string



40
41
42
43
# File 'lib/deployable/zmq/publish.rb', line 40

def send_string label, message
  @publisher.send_string label, ZMQ::SNDMORE
  @publisher.send_string message
end