Permalink
Browse files

implement TIME SERVER

  • Loading branch information...
rvagg committed Feb 16, 2014
1 parent 60be3c3 commit d76b501dda26c35754a3257049774e614c90aa3e
@@ -1 +1,80 @@
-// code stuff here
+var net = require('net')
+ , exercise = require('workshopper-exercise')()
+ , filecheck = require('workshopper-exercise/filecheck')
+ , execute = require('workshopper-exercise/execute')
+ , comparestdout = require('workshopper-exercise/comparestdout')
+ , through2 = require('through2')
+ , rndport = require('../../lib/rndport')
+
+
+// checks that the submission file actually exists
+exercise = filecheck(exercise)
+
+// execute the solution and submission in parallel with spawn()
+exercise = execute(exercise)
+
+
+// assign ports for the child processes to listen to
+exercise.addSetup(function (mode, callback) {
+ this.submissionPort = rndport()
+ this.solutionPort = this.submissionPort + 1
+
+ // set child process arguments
+ this.submissionArgs = [ this.submissionPort ]
+ this.solutionArgs = [ this.solutionPort ]
+
+ process.nextTick(callback)
+})
+
+
+// add a processor for both run and verify calls, added *before*
+// the comparestdout processor so we can mess with the stdouts
+exercise.addProcessor(function (mode, callback) {
+ this.submissionStdout.pipe(process.stdout)
+
+ // replace stdout with our own streams
+ this.submissionStdout = through2()
+ if (mode == 'verify')
+ this.solutionStdout = through2()
+
+ setTimeout(query.bind(this, mode), 500)
+
+ process.nextTick(function () {
+ callback(null, true)
+ })
+})
+
+// compare stdout of solution and submission
+exercise = comparestdout(exercise)
+
+
+// delayed for 500ms to wait for servers to start so we can start
+// playing with them
+function query (mode) {
+ var exercise = this
+
+ // on error, write to the stream so that'll also be verified
+
+ // connect to localhost:<port> and pipe results to <stream>
+ function connect (port, stream) {
+ net.connect(port)
+ .on('error', function (err) {
+ stream.end()
+ setImmediate(function () {
+ exercise.emit(
+ 'fail'
+ , 'Error connecting to localhost:' + port + ': ' + err.message
+ )
+ })
+ })
+ .pipe(stream)
+ }
+
+ connect(this.submissionPort, this.submissionStdout)
+
+ if (mode == 'verify')
+ connect(this.solutionPort, this.solutionStdout)
+}
+
+
+module.exports = exercise
@@ -1,19 +0,0 @@
-var net = require('net')
-
-function zeroFill(i) {
- return (i < 10 ? '0' : '') + i
-}
-
-function now () {
- var d = new Date()
- return d.getFullYear() + '-'
- + zeroFill(d.getMonth() + 1) + '-'
- + zeroFill(d.getDate()) + ' '
- + zeroFill(d.getHours()) + ':'
- + zeroFill(d.getMinutes())
-}
-
-var server = net.createServer(function (socket) {
- socket.end(now() + '\n')
-})
-server.listen(Number(process.argv[2]))
@@ -1 +1,20 @@
-// code stuff here
+var net = require('net')
+
+function zeroFill(i) {
+ return (i < 10 ? '0' : '') + i
+}
+
+function now () {
+ var d = new Date()
+ return d.getFullYear() + '-'
+ + zeroFill(d.getMonth() + 1) + '-'
+ + zeroFill(d.getDate()) + ' '
+ + zeroFill(d.getHours()) + ':'
+ + zeroFill(d.getMinutes())
+}
+
+var server = net.createServer(function (socket) {
+ socket.end(now() + '\n')
+})
+
+server.listen(Number(process.argv[2]))
View
@@ -0,0 +1,3 @@
+module.exports = function rndport () {
+ return 1024 + Math.floor(Math.random() * 64511)
+}
View
@@ -18,7 +18,7 @@
"license": "MIT",
"dependencies": {
"workshopper": "~1.0.0-alpha05",
- "workshopper-exercise": "~0.2.0",
+ "workshopper-exercise": "~0.2.2",
"workshopper-wrappedexec": "~0.0.0",
"workshopper-boilerplate": "~0.0.0",
"concat-stream": "~1.4.1",
@@ -31,7 +31,8 @@
"colors-tmpl": "~0.1.0",
"after": "~0.8.1",
"rimraf": "~2.2.6",
- "chalk": "~0.4.0"
+ "chalk": "~0.4.0",
+ "through2": "~0.4.1"
},
"bin": "./learnyounode.js",
"preferGlobal": true

0 comments on commit d76b501

Please sign in to comment.