Skip to content

Commit

Permalink
add examples to test the zeromq flow
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed May 26, 2011
1 parent 5e7b864 commit e92a382
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ src/
.DS_Store
.git*
*.md
examples
18 changes: 18 additions & 0 deletions examples/tasker.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# tasker
# Connects to PULL socket on tcp://*:5555
# Pushes a flurry of jobs

port = process.env.PORT or 5555
jobs = process.env.NUM or 1000
sleep = process.env.SLEEP or 0

context = require 'zeromq'
socket = context.createSocket 'push'
socket.connect "tcp://127.0.0.1:#{port}"

payload = (new Buffer 100).toString("base64")
socket.send "start:#{jobs}:#{sleep}"
for num in [1..jobs]
socket.send "job:#{num}:#{payload}"

socket.close()
31 changes: 31 additions & 0 deletions examples/worker.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# worker
# Binds PULL socket on tcp://*:5555
# Receives a flurry of jobs from the tasker.

port = process.env.PORT or 5555

context = require 'zeromq'
socket = context.createSocket 'pull'

countdown = 0
sleep = 0
socket.on 'message', (buf) ->
[command,num,extra] = buf.toString().split(":")
if command == 'start'
countdown = parseInt num
sleep = parseInt extra
console.time "#{num} messages"
else
setTimeout ->
if countdown < 2
console.timeEnd "#{num} messages"
else
countdown -= 1
, sleep

socket.bindSync "tcp://*:#{port}"
console.log "Listening..."

process.on 'SIGINT', ->
console.log "Hanging it up..."
socket.close()

0 comments on commit e92a382

Please sign in to comment.