Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

zloop cover + sample

  • Loading branch information...
commit 5f0849037df72c991fe683e75b8a15f56ed7de92 1 parent be4e7b4
@showstopper showstopper authored
Showing with 79 additions and 0 deletions.
  1. +44 −0 samples/loop.ooc
  2. +35 −0 source/czmq.ooc
View
44 samples/loop.ooc
@@ -0,0 +1,44 @@
+use czmq
+import czmq
+
+main: func {
+ ctx := Context new()
+
+ output := Socket new(ctx, ZMQ PAIR)
+ output bind("inproc://zloop.test")
+ input := Socket new(ctx, ZMQ PAIR)
+ input connect("inproc://zloop.test")
+
+ loop := Loop new()
+
+ loop setVerbose(true) // fancy debug messages
+ // After 10 msecs, send a ping message to output
+ loop timer(10, 1, timerEvent, output)
+ // When we get the ping message, end the reactor
+ pollInput := [input, 0, ZMQ POLLIN] as PollItem // struct literal
+
+ pollStdIn := [null, 1, ZMQ POLLIN] as PollItem
+ loop timer(5000, 1, exitEvent, null) // force exit
+ loop poller(pollInput, socketEvent, null)
+ loop start()
+
+ loop destroy()
+ ctx destroy()
+
+}
+
+socketEvent: func(a: Loop, b: PollItem, c: Pointer) {
+ "socket event!" println()
+ "Message received: %s" format(b@ socket recvString()) println()
+}
+
+timerEvent: func(a: Loop, b: PollItem, c: Pointer) {
+ "timer event!" println()
+ c as Socket sendString("hello")
+}
+
+// program hangs somehow
+exitEvent: func(a: Loop, b: PollItem, c: Pointer) {
+ "Goodbye, cruel world!" println()
+ exit(0)
+}
View
35 source/czmq.ooc
@@ -11,6 +11,10 @@ ZMQ: enum {
PUB: extern(ZMQ_PUB)
SUB: extern(ZMQ_SUB)
PAIR: extern(ZMQ_PAIR)
+
+ POLLIN: extern(ZMQ_POLLIN)
+ POLLOUT: extern(ZMQ_POLLOUT)
+ POLLERR: extern(ZMQ_POLLERR)
}
Context: cover from zctx_t* {
@@ -51,3 +55,34 @@ Socket: cover from Pointer {
}
+
+_PollItem: cover from zmq_pollitem_t {
+
+ socket: Socket
+ fd: extern Int
+ events: extern Short
+ revents: extern Short
+
+}
+
+PollItem: cover from _PollItem*
+
+//LoopFn: cover from Func(Loop, PollItem, Pointer)
+
+Loop: cover from Pointer {
+
+ new: extern(zloop_new) static func -> This
+
+ //zloop_poller (zloop_t *self, zmq_pollitem_t *item, zloop_fn handler, void *arg);
+ poller: extern(zloop_poller) func(PollItem, Pointer, Pointer) -> Int
+
+ start: extern(zloop_start) func -> Int
+
+ //zloop_timer (zloop_t *self, size_t delay, size_t times, zloop_fn handler, void *arg);
+ timer: extern(zloop_timer) func(SizeT, SizeT, Pointer, Pointer) -> Int
+
+ destroy: extern(zloop_destroy) func
+
+ setVerbose: extern(zloop_set_verbose) func(Bool) -> Int
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.