forked from booksbyus/zguide
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add weather client/server examples for Felix.
- Loading branch information
skaller
authored and
skaller
committed
Jan 19, 2012
1 parent
43efe0b
commit 25d2531
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Weather update client | ||
// Connects SUB socket to tcp://localhost:5556 | ||
// Collects weather updates and finds avg temp in zipcode | ||
// | ||
open ZeroMQ; | ||
|
||
fun parse_int(s:string,var i:int) = { | ||
var acc = 0; | ||
while s.[i] \in "0123456789" do | ||
acc = acc * 10 + s.[i].ord - "0".char.ord; | ||
++i; | ||
done | ||
return i,acc; | ||
} | ||
|
||
fun parse_space(s:string, i:int)=> i+1; | ||
|
||
fun parse_weather(s:string) = { | ||
var i = 0; | ||
def i, val zipcode = parse_int (s,i); | ||
i = parse_space(s,i); | ||
def i, val temperature = parse_int (s,i); | ||
i = parse_space(s,i); | ||
def i, val relhumidity= parse_int (s,i); | ||
return zipcode, temperature, relhumidity; | ||
} | ||
|
||
var context = zmq_init 1; | ||
|
||
// Socket to talk to server | ||
println "Collecting updates from weather server..."; | ||
var subscriber = context.mk_socket ZMQ_SUB; | ||
zmq_validate$ subscriber.connect "tcp://localhost:5556"; | ||
|
||
// Subscribe to zipcode 100 | ||
filter := if System::argc > 1 then System::argv 1 else "1001" endif; | ||
zmq_validate$ subscriber.set_sockopt$ zmq_subscribe filter; | ||
|
||
// Process 100 updates | ||
var total_temp = 0; | ||
for var update_nbr in 0 upto 99 do | ||
e,s := subscriber.recv_string; | ||
zmq_validate$ e; | ||
|
||
zipcode, temperature, relhumidity := parse_weather s; | ||
total_temp += temperature; | ||
done | ||
println$ | ||
f"Average temperature for zipcode '%S' was %d C\n"$ | ||
filter, total_temp / update_nbr | ||
; | ||
|
||
zmq_validate$ subscriber.close; | ||
zmq_validate$ context.term; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Weather update server | ||
// Binds PUB socket to tcp://*:5556 | ||
// Publishes random weather updates | ||
// | ||
open ZeroMQ; | ||
// Prepare our context and publisher | ||
var context = zmq_init 1; | ||
var publisher = context.mk_socket ZMQ_PUB; | ||
zmq_validate$ publisher.bind "tcp://*:5556"; | ||
zmq_validate$ publisher.bind "ipc://weather.ipc"; | ||
|
||
while true do | ||
// Get values that will fool the boss | ||
zipcode := #rand % 1000+1000; | ||
temperature := #rand % 80 - 20; // Oztraila mate! | ||
relhumidity := #rand % 50 + 10; | ||
|
||
// Send message to all subscribers | ||
var update = f"%03d %d %d" (zipcode, temperature, relhumidity); | ||
zmq_validate$ publisher.send_string update; | ||
done |