Skip to content

Commit

Permalink
Implemented the msreader example.
Browse files Browse the repository at this point in the history
Signed-off-by: Lewis Gunsch <lewis@gunsch.ca>
  • Loading branch information
lgunsch committed Jun 21, 2011
1 parent a351d32 commit 05128e8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/msreader.vala
@@ -0,0 +1,40 @@
//
// Reading from multiple sockets
// This version uses a simple recv loop
//
using ZMQ;

public static int main(string [] argv) {
// Prepare our context and sockets
var context = new Context (1);

// Connect to task ventilator
var receiver = new Socket (context, SocketType.PULL);
receiver.connect ("tcp://localhost:5557");

// Connect to weather server
var subscriber = new Socket (context, SocketType.SUB);
subscriber.connect ("tcp://localhost:5556");
subscriber.setsockopt (SocketOption.SUBSCRIBE, "10001 ", 6);

// Process messages from both sockets
// We prioritize traffic from the task ventilator
while (true) {
// Process any waiting tasks
int rc = 0;
do {
if ((rc = receiver.recv ( MSG.Msg (), SendRecvOption.NOBLOCK)) == 0) {
// process task
}
} while (rc == 0);

// Process any waiting weather updates
do {
if ((rc = subscriber.recv ( MSG.Msg (), SendRecvOption.NOBLOCK)) == 0) {
// process weather update
}
} while (rc == 0);
// No activity, so sleep for 1 msec
Thread.usleep (1000);
}
}
8 changes: 8 additions & 0 deletions examples/wscript_build
Expand Up @@ -64,3 +64,11 @@ bld.program(
source = ['zhelpers.vala', 'wuclient.vala'],
vapi_dirs = ['../']
)

bld.program(
packages = 'libzmq',
target = 'msreader',
uselib = 'GTK GLIB ZMQ',
source = ['msreader.vala'],
vapi_dirs = ['../']
)

0 comments on commit 05128e8

Please sign in to comment.