Skip to content

Instantly share code, notes, and snippets.

@sanitz
Created August 29, 2011 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanitz/1178532 to your computer and use it in GitHub Desktop.
Save sanitz/1178532 to your computer and use it in GitHub Desktop.
Simple example for ccc-kata
dgram = require "dgram"
server = dgram.createSocket "udp4"
client = dgram.createSocket "udp4"
finish = ->
client.close()
server.close()
process.exit()
operators =
ADD : (a, b) -> a + b
SUBTRACT: (a, b) -> a - b
MULTIPLY: (a, b) -> a * b
send = (uuid, result) ->
message = new Buffer "#{uuid}:#{result}", encoding='utf8'
client.send message, 0, message.length, 9001, "localhost"
server.on 'message', (msg,rinfo) ->
args = msg.toString('utf8').split(':')
name = args.shift()
finish() if name is "__SHUTDOWN__"
uuid = args.shift()
send uuid, (parseInt(x, 10) for x in args).reduce operators[name]
server.bind(9000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment