Sha256: fa49e4ab0b2f00358243326207f3471ab3e8b022f20aa66dd62b943b732a4acf

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

# encoding: utf-8

# weather_upd_client.rb

# This example is used in conjunction with weather_upd_server.rb.
# Run this program in a terminal/console window and then run
# weather_upd_server.rb in another terminal/console window and
# observe the output.
#
# This program subscribes to a feed of weather updates published
# by weather_upd_server.rb, collects the first 100 updates that
# match the subscription filter and displays the average temperature
# for that zipcode.
#
# Usage: ruby weather_upd_client.rb [zip code (default=10001)]
#
# If you supply a zip code argument then the maximum value that will
# be recognized is 11000.

require File.join(File.dirname(__FILE__), '..', 'lib', 'ffi-rxs')

COUNT = 100

context = XS::Context.create()

# Socket to talk to server
puts "Collecting updates from weather server..."
subscriber = context.socket(XS::SUB)
subscriber.connect("tcp://127.0.0.1:5556")

# Subscribe to zipcode, default is NYC, 10001
filter = ARGV.size > 0 ? ARGV[0] : "10001"
subscriber.setsockopt(XS::SUBSCRIBE, filter)

# Process 100 updates
total_temp = 0
1.upto(COUNT) do |update_nbr|
  s = ''
  subscriber.recv_string(s)
  
  zipcode, temperature, relhumidity = s.split.map(&:to_i)
  total_temp += temperature
  puts "Update #{update_nbr.to_s}: #{temperature.to_s}F"
end

puts "Average temperature for zipcode '#{filter}' was #{total_temp / COUNT}F"

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ffi-rxs-1.2.1 examples/weather_upd_client.rb
ffi-rxs-1.2.0 examples/weather_upd_client.rb
ffi-rxs-1.1.0 examples/weather_upd_client.rb
ffi-rxs-1.0.1 examples/weather_upd_client.rb