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.
Merge pull request booksbyus#157 from skaller/master
Felix binding examples for Hello World case of the Zguide
- Loading branch information
Showing
5 changed files
with
156 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,37 @@ | ||
// Hello World client | ||
// Connects REQ socket to tcp://localhost:5555 | ||
// Sends "Hello" to server, expects "World" back | ||
|
||
open ZeroMQ; | ||
println "hwclient, Felix version"; | ||
|
||
var context = zmq_init (1); | ||
|
||
// Socket to talk to server | ||
println "Connecting to hello world server"; | ||
|
||
var requester = context.mk_socket ZMQ_REQ; | ||
if not requester.valid do | ||
println "Can't create socket"; | ||
System::exit(Errno::errno.int); | ||
done | ||
|
||
zmq_validate$ requester.connect "tcp://localhost:5555"; | ||
var request = #zmq_msg_t; | ||
var reply = #zmq_msg_t; | ||
|
||
for var request_nbr in 0 upto 9 do | ||
zmq_validate$ request.init_size 5.size; | ||
memcpy (zmq_msg_data request, c"Hello".address, 5.size); | ||
print$ f"Sending Hello %d\n" request_nbr; | ||
zmq_validate$ requester.send request; | ||
zmq_validate$ request.close; | ||
|
||
zmq_validate$ reply.init_size 5.size; | ||
zmq_validate$ requester.recv reply; | ||
println$ f"Received World %d" request_nbr; | ||
zmq_validate$ reply.close; | ||
done | ||
zmq_validate$ requester.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,39 @@ | ||
// Hello World server | ||
// Binds REP socket to tcp://*:5555 | ||
// Expects "Hello" from client, replies with "World" | ||
|
||
include "std/posix/errno"; | ||
|
||
open ZeroMQ; | ||
println "hwserver, Felix version"; | ||
|
||
var context = zmq_init (1); | ||
|
||
// Socket to talk to clients | ||
var responder = context.mk_socket ZMQ_REP; | ||
if not responder.valid do | ||
println "Can't create socket"; | ||
System::exit(Errno::errno.int); | ||
done | ||
|
||
zmq_validate$ responder.bind "tcp://*:5555"; | ||
var request = #zmq_msg_t; | ||
var reply = #zmq_msg_t; | ||
|
||
while true do | ||
// Wait for next request from client | ||
request.init_string "Hello"; | ||
zmq_validate$ responder.recv request; | ||
println "Received Hello"; | ||
zmq_validate$ request.close; | ||
|
||
// Do some 'work' | ||
Faio::sleep (sys_clock,1.0); | ||
|
||
// Send reply back to client | ||
zmq_validate$ reply.init_size 5.size; | ||
memcpy (zmq_msg_data reply, c"World".address, 5.size); | ||
zmq_validate$ responder.send reply; | ||
zmq_validate$ reply.close; | ||
done | ||
|
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 |
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,2 @@ | ||
println$ f"Current 0MQ version is %d.%d.%d" #ZeroMQ::zmq_version; | ||
|