Created
August 29, 2011 14:43
-
-
Save sanitz/1178532 to your computer and use it in GitHub Desktop.
Simple example for ccc-kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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