Skip to content

Commit

Permalink
zeromq: bindings for ZeroMQ.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Sep 18, 2013
1 parent 9faefe7 commit c398b24
Show file tree
Hide file tree
Showing 14 changed files with 604 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extra/zeromq/authors.txt
@@ -0,0 +1,2 @@
Eungju PARK
John Benediktsson
25 changes: 25 additions & 0 deletions extra/zeromq/examples/echo-client.factor
@@ -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
21 changes: 21 additions & 0 deletions extra/zeromq/examples/echo-server.factor
@@ -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
23 changes: 23 additions & 0 deletions extra/zeromq/examples/hwclient.factor
@@ -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

21 changes: 21 additions & 0 deletions extra/zeromq/examples/hwserver.factor
@@ -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
26 changes: 26 additions & 0 deletions extra/zeromq/examples/tasksink.factor
@@ -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
44 changes: 44 additions & 0 deletions extra/zeromq/examples/taskvent.factor
@@ -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
37 changes: 37 additions & 0 deletions extra/zeromq/examples/taskwork.factor
@@ -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
26 changes: 26 additions & 0 deletions extra/zeromq/examples/wuclient.factor
@@ -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

25 changes: 25 additions & 0 deletions extra/zeromq/examples/wuserver.factor
@@ -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

0 comments on commit c398b24

Please sign in to comment.