Sha256: 43979ed04ec587fad66485c1da77253205c41e6f93f136d5e9b5a10ffb9ab9ae

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

require 'noam_lemma'

# This is an example of a Ruby Lemma that publishes messages. It expects a Noam
# server to be running. It also expects that the noam-lemma.rb file is in the
# search path. If you run this example from the project root, the following
# command should work:
#
#   ruby -Ilib example/publisher.rb
#
# This example _will not_ work on the same machine running the Noam server as
# both programs need to bind to UDP port 1030.

publisher = Noam::Lemma.new('example-publisher', [], ["e1", "e2"])

# Using the `discover` method asks the Lemma to proactively try and discover a
# server to connect to on the local network. Once the server is discovered, it
# will connect and send a Noam 'register' message. When `discover` returns, the
# Lemma is ready to send events.
publisher.discover

seq = 0
loop do
  # This method block picks an event to send. It's randomized a bit so that we
  # can see things change over time.
  e = if 0.5 < rand()
        "e1"
      else
        "e2"
      end

  # Next, package the event sequence and the current time into a value.
  v = {"seq" => seq, "time" => Time.now.to_s}

  # Attempt to speak the chosen event with the value. Note, the event is either
  # "e1" or "e2" based on how rand() returned.
  unless publisher.speak(e, v)
    # If `speak` returns false, we're unable to speak the message likely because
    # the socket has closed. The connection would have to be restarted.
    puts "Done"
    break
  end
  puts "Wrote: #{e} -> #{v.inspect}"

  seq += 1
  # Sleep for a while so that we don't bog down the network.
  sleep(1)
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
noam_lemma-0.2.1.2 examples/publisher.rb
noam_lemma-0.2.1.1 examples/publisher.rb
noam_lemma-0.2.1 examples/publisher.rb