]> gitweb.factorcode.org Git - factor.git/blob - extra/zeromq/examples/taskvent.factor
e4629359502b57718d0bbbcc8c0279b2e95682ce
[factor.git] / extra / zeromq / examples / taskvent.factor
1 ! Copyright (C) 2012 Eungju PARK.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-arrays calendar destructors formatting io kernel
4 math namespaces random threads zeromq zeromq.ffi ;
5 IN: zeromq.examples.taskvent
6
7 : taskvent ( -- )
8     [
9         <zmq-context> &dispose
10
11         [
12             ! Socket to send messages on
13             ZMQ_PUSH <zmq-socket> &dispose
14             dup "tcp://*:5557" zmq-bind
15         ] [
16             ! Socket to send start of batch message on
17             ZMQ_PUSH <zmq-socket> &dispose
18             dup "tcp://localhost:5558" zmq-connect
19         ] bi
20
21         "Press Enter when the workers are ready: " write flush
22         read1 drop
23         "Sending tasks to workers…\n" write flush
24
25         ! The first message is "0" and signals start of batch
26         dup "0" >byte-array 0 zmq-send
27
28         ! Send 100 tasks
29         0 100 [
30             ! Random workload from 1 to 100msecs
31             100 random 1 +
32             dup [ + ] dip
33             [ pick ] dip "%d" sprintf >byte-array 0 zmq-send
34         ] times
35         "Total expected cost: %d msec\n" printf
36
37         ! Give 0MQ time to deliver
38         1 seconds sleep
39
40         drop
41         drop
42     ] with-destructors ;
43
44 MAIN: taskvent