Module: Horobi::Pub

Defined in:
lib/horobi/pub.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#sockObject (readonly)

Returns the value of attribute sock.



5
6
7
# File 'lib/horobi/pub.rb', line 5

def sock
  @sock
end

Class Method Details

.initObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/horobi/pub.rb', line 7

def self.init
  @context ||= ZMQ::Context.new
  options = {
    "logfile" => STDERR,
    "outputs" => [],
  }
  OptionParser.new do |op|
    op.on('-p VAL','--pidfile=VAL','pidfile path'){|v| options["pidfile"] = v}
    op.on('-l VAL','--logfile=VAL','logfile path'){|v| options["logfile"] = (v == "-" ? STDOUT : v)}
    op.on('-o VAL','--ouput-points=VAL',
      "output(hub's input) point(s) such as 'tcp://127.0.0.1:5551,tcp://127.0.11.1:5551'"){|v| options["outputs"] = v.split(",")}
    op.on('-d','--daemonize','daemonize this script'){ options["daemon"] = true }
    op.parse!(ARGV)
  end
  @options = options
  if @options["outputs"].compact.length < 1
    raise "pub output points are undefined"
  end
  @logger = Logger.new(options["logfile"])
  @sock ||= begin
    sock = @context.socket(ZMQ::PUSH)
    @options["outputs"].each do |point|
      @logger.info("connecting to #{point}")
      sock.connect(point)
    end
    sock.setsockopt(ZMQ::LINGER, 100)
    sock
  end
end

.send(msg, flags = nil) ⇒ Object

flags = ZMQ::NOBLOCK | ZMQ::SNDMORE



37
38
39
40
41
42
43
44
# File 'lib/horobi/pub.rb', line 37

def self.send(msg, flags=nil) # flags = ZMQ::NOBLOCK | ZMQ::SNDMORE
  if @options.nil?
    init
  end

  @logger.debug("message send to #{@options["inputs"]}: " + msg)
  @sock.send(msg, flags)
end