Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
604 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,2 @@ | ||
Eungju PARK | ||
John Benediktsson |
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,25 @@ | ||
! Copyright (C) 2013 John Benediktsson. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
|
||
USING: byte-arrays calendar calendar.format destructors io | ||
kernel present strings threads zeromq zeromq.ffi ; | ||
|
||
IN: zeromq.examples.echo-client | ||
|
||
: echo-client ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
ZMQ_REQ <zmq-socket> &dispose | ||
dup "tcp://127.0.0.1:5000" zmq-connect | ||
[ | ||
now present | ||
[ "Sending " write print flush ] | ||
[ >byte-array dupd 0 zmq-send ] bi | ||
dup 0 zmq-recv >string | ||
"Received " write print flush | ||
1 seconds sleep | ||
t | ||
] loop drop | ||
] with-destructors ; | ||
|
||
MAIN: echo-client |
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,21 @@ | ||
! Copyright (C) 2013 John Benediktsson. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
|
||
USING: destructors io kernel strings zeromq zeromq.ffi ; | ||
|
||
IN: zeromq.examples.echoserver | ||
|
||
: echo-server ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
ZMQ_REP <zmq-socket> &dispose | ||
dup "tcp://127.0.0.1:5000" zmq-bind | ||
[ | ||
dup 0 zmq-recv | ||
[ >string "Received " write print flush ] | ||
[ dupd 0 zmq-send ] bi | ||
t | ||
] loop drop | ||
] with-destructors ; | ||
|
||
MAIN: echo-server |
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,23 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays destructors formatting io kernel sequences | ||
strings zeromq zeromq.ffi ; | ||
IN: zeromq.examples.hwclient | ||
|
||
: hwclient ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
"Connecting to hello world server…" print | ||
ZMQ_REQ <zmq-socket> &dispose | ||
dup "tcp://localhost:5555" zmq-connect | ||
10 iota [ | ||
[ "Hello" dup rot "Sending %s %d...\n" printf | ||
dupd >byte-array 0 zmq-send ] | ||
[ [ dup 0 zmq-recv >string ] dip | ||
"Received %s %d\n" printf flush ] | ||
bi | ||
] each drop | ||
] with-destructors ; | ||
|
||
MAIN: hwclient | ||
|
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,21 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays calendar destructors io kernel strings | ||
threads zeromq zeromq.ffi ; | ||
IN: zeromq.examples.hwserver | ||
|
||
: hwserver ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
ZMQ_REP <zmq-socket> &dispose | ||
dup "tcp://*:5555" zmq-bind | ||
[ t ] [ | ||
dup | ||
[ 0 zmq-recv >string "Received " write print flush ] | ||
[ drop 1 seconds sleep ] | ||
[ "World" >byte-array 0 zmq-send ] | ||
tri | ||
] while drop | ||
] with-destructors ; | ||
|
||
MAIN: hwserver |
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,26 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays calendar destructors formatting io kernel | ||
math strings sequences zeromq zeromq.ffi ; | ||
IN: zeromq.examples.tasksink | ||
|
||
: tasksink ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
ZMQ_PULL <zmq-socket> &dispose | ||
dup "tcp://*:5558" zmq-bind | ||
! Wait for start of batch | ||
dup 0 zmq-recv drop | ||
! Start our clock now | ||
now | ||
! Process 100 confirmations | ||
100 iota [ | ||
pick 0 zmq-recv drop | ||
10 rem zero? [ ":" ] [ "." ] if write flush | ||
] each | ||
! Calculate and report duration of batch | ||
now swap time- duration>milliseconds "Total elapsed time: %d msec\n" printf | ||
drop | ||
] with-destructors ; | ||
|
||
MAIN: tasksink |
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,44 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays calendar destructors formatting io kernel | ||
math namespaces random threads zeromq zeromq.ffi ; | ||
IN: zeromq.examples.taskvent | ||
|
||
: taskvent ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
|
||
[ | ||
! Socket to send messages on | ||
ZMQ_PUSH <zmq-socket> &dispose | ||
dup "tcp://*:5557" zmq-bind | ||
] [ | ||
! Socket to send start of batch message on | ||
ZMQ_PUSH <zmq-socket> &dispose | ||
dup "tcp://localhost:5558" zmq-connect | ||
] bi | ||
|
||
"Press Enter when the workers are ready: " write flush | ||
read1 drop | ||
"Sending tasks to workers…\n" write flush | ||
|
||
! The first message is "0" and signals start of batch | ||
dup "0" >byte-array 0 zmq-send | ||
|
||
! Send 100 tasks | ||
0 100 [ | ||
! Random workload from 1 to 100msecs | ||
100 random 1 + | ||
dup [ + ] dip | ||
[ pick ] dip "%d" sprintf >byte-array 0 zmq-send | ||
] times | ||
"Total expected cost: %d msec\n" printf | ||
|
||
! Give 0MQ time to deliver | ||
1 seconds sleep | ||
|
||
drop | ||
drop | ||
] with-destructors ; | ||
|
||
MAIN: taskvent |
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 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays calendar destructors formatting io kernel | ||
math.parser strings threads zeromq zeromq.ffi ; | ||
IN: zeromq.examples.taskwork | ||
|
||
: taskwork ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
|
||
[ | ||
! Socket to receive messages on | ||
ZMQ_PULL <zmq-socket> &dispose | ||
dup "tcp://localhost:5557" zmq-connect | ||
] [ | ||
! Socket to send messages to | ||
ZMQ_PUSH <zmq-socket> &dispose | ||
dup "tcp://localhost:5558" zmq-connect | ||
] bi | ||
|
||
! Process tasks forever | ||
[ | ||
over 0 zmq-recv >string | ||
! Simple progress indicator for the viewer | ||
dup "%s." printf flush | ||
! Do the work | ||
string>number milliseconds sleep | ||
! Send results to sink | ||
dup "" >byte-array 0 zmq-send | ||
t | ||
] loop | ||
|
||
drop | ||
drop | ||
] with-destructors ; | ||
|
||
MAIN: taskwork |
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,26 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays command-line destructors formatting io kernel | ||
math math.parser namespaces sequences splitting strings zeromq | ||
zeromq.ffi ; | ||
IN: zeromq.examples.wuclient | ||
|
||
: wuclient ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
"Collecting updates from weather server…" print | ||
ZMQ_SUB <zmq-socket> &dispose | ||
dup "tcp://localhost:5556" zmq-connect | ||
command-line get [ "10001 " ] [ first ] if-empty | ||
2dup >byte-array ZMQ_SUBSCRIBE swap zmq-setopt | ||
0 100 dup [ | ||
[ pick 0 zmq-recv | ||
>string " " split [ string>number ] map second + | ||
] times | ||
] dip | ||
/ "Average temperature for zipcode '%s' was %dF\n" printf | ||
drop | ||
] with-destructors ; | ||
|
||
MAIN: wuclient | ||
|
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,25 @@ | ||
! Copyright (C) 2012 Eungju PARK. | ||
! See http://factorcode.org/license.txt for BSD license. | ||
USING: byte-arrays calendar destructors formatting kernel math | ||
namespaces random zeromq zeromq.ffi ; | ||
IN: zeromq.examples.wuserver | ||
|
||
: wuserver ( -- ) | ||
[ | ||
<zmq-context> &dispose | ||
ZMQ_PUB <zmq-socket> &dispose | ||
dup "tcp://*:5556" zmq-bind | ||
dup "ipc://weather.ipc" zmq-bind | ||
random-generator get now timestamp>unix-time >fixnum seed-random [ | ||
[ t ] [ | ||
dup | ||
100000 random | ||
215 random 80 - | ||
50 random 10 + | ||
"%05d %d %d" sprintf | ||
>byte-array 0 zmq-send | ||
] while | ||
] with-random drop | ||
] with-destructors ; | ||
|
||
MAIN: wuserver |
Oops, something went wrong.