Skip to content

Commit 08a4570

Browse files
author
Alan Shaw
committed
Move broadcaster into library, use in all exercises, refactor notifier slightly
1 parent 92e14ae commit 08a4570

File tree

12 files changed

+82
-68
lines changed

12 files changed

+82
-68
lines changed

exercises/blink_blink/exercise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var filecheck = require('workshopper-exercise/filecheck')
77
var execute = require('workshopper-exercise/execute')
88
var wrappedexec = require('workshopper-wrappedexec')
99
var path = require('path')
10-
var notifier = require('../../lib/notifier')('Blink blink')
10+
var notifier = require('../../lib/notifier')
11+
var broadcaster = require('../../lib/broadcaster')
1112

1213

1314
// checks that the submission file actually exists
@@ -57,9 +58,9 @@ exercise.addVerifyProcessor(function (callback) {
5758
expect(io.digitalWrite.calledWith(13, io.HIGH)).to.be.true
5859
expect(io.digitalWrite.calledWith(13, io.LOW)).to.be.true
5960

60-
notifier(callback)
61+
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
6162
} catch(error) {
62-
notifier(error, callback)
63+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
6364
}
6465
})
6566

exercises/fire_alarm/exercise.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var filecheck = require('workshopper-exercise/filecheck')
77
var execute = require('workshopper-exercise/execute')
88
var wrappedexec = require('workshopper-wrappedexec')
99
var path = require('path')
10-
var notifier = require('../../lib/notifier')('Fire alarm')
10+
var notifier = require('../../lib/notifier')
11+
var broadcaster = require('../../lib/broadcaster')
1112

1213
// checks that the submission file actually exists
1314
exercise = filecheck(exercise)
@@ -63,14 +64,14 @@ exercise.addVerifyProcessor(function (callback) {
6364
expect(io.digitalWrite.called, 'Fire alarm went off before a temperature was received!').to.be.false
6465

6566
testAlarmTurnsOff(analogReadListener, io, function (error) {
66-
if (error) return notifier(error, callback)
67+
if (error) return broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
6768

6869
testAlarmResets(analogReadListener, io, function (error) {
69-
notifier(error, callback)
70+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
7071
})
7172
})
7273
} catch (error) {
73-
notifier(error, callback)
74+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
7475
}
7576
})
7677

exercises/light_switch/exercise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var filecheck = require('workshopper-exercise/filecheck')
77
var execute = require('workshopper-exercise/execute')
88
var wrappedexec = require('workshopper-wrappedexec')
99
var path = require('path')
10-
var notifier = require('../../lib/notifier')('Light switch')
10+
var notifier = require('../../lib/notifier')
11+
var broadcaster = require('../../lib/broadcaster')
1112

1213
// checks that the submission file actually exists
1314
exercise = filecheck(exercise)
@@ -108,9 +109,9 @@ exercise.addVerifyProcessor(function (callback) {
108109
expect(led.off.callCount, 'LED did not turn off after fourth button press').to.be.equal(initial.off.callCount + 2)
109110
}
110111

111-
notifier(callback)
112+
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
112113
} catch(error) {
113-
notifier(error, callback)
114+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
114115
}
115116
})
116117

exercises/ping_bell/exercise.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var filecheck = require('workshopper-exercise/filecheck')
88
var execute = require('workshopper-exercise/execute')
99
var wrappedexec = require('workshopper-wrappedexec')
1010
var path = require('path')
11-
var notifier = require('../../lib/notifier')('Ping bell')
11+
var notifier = require('../../lib/notifier')
12+
var broadcaster = require('../../lib/broadcaster')
1213

1314
// checks that the submission file actually exists
1415
exercise = filecheck(exercise)
@@ -63,14 +64,14 @@ exercise.addVerifyProcessor(function (callback) {
6364
'Piezo didn\'t play a tone when sent a UDP message')
6465
.to.be.gt(initial.tone.callCount)
6566

66-
notifier(callback)
67+
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
6768
} catch (error) {
68-
notifier(error, callback)
69+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
6970
}
7071
}, 500)
7172

7273
} catch (error) {
73-
notifier(error, callback)
74+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
7475
}
7576
})
7677

exercises/remote_temperature/exercise.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ var filecheck = require('workshopper-exercise/filecheck')
99
var execute = require('workshopper-exercise/execute')
1010
var wrappedexec = require('workshopper-wrappedexec')
1111
var path = require('path')
12-
var notifier = require('../../lib/notifier')('Remote temperature')
12+
var notifier = require('../../lib/notifier')
13+
var broadcaster = require('../../lib/broadcaster')
1314

1415
// checks that the submission file actually exists
1516
exercise = filecheck(exercise)
@@ -64,7 +65,10 @@ exercise.addVerifyProcessor(function (callback) {
6465

6566
d.on('remote', function (remote) {
6667
if (!remote.getTemperature) {
67-
return callback(new Error('Remote has no method \'getTemperature\''), false)
68+
return broadcaster(exercise)(
69+
new Error('Remote has no method \'getTemperature\''),
70+
function (er) { notifier(exercise)(er, callback) }
71+
)
6872
}
6973

7074
async.eachSeries(
@@ -90,16 +94,16 @@ exercise.addVerifyProcessor(function (callback) {
9094
}, 1000)
9195
},
9296
function (error) {
93-
notifier(error, callback)
97+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
9498
})
9599
})
96100

97101
d.on('error', function (error) {
98-
notifier(error, callback)
102+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
99103
})
100104

101105
} catch (error) {
102-
notifier(error, callback)
106+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
103107
}
104108
})
105109

exercises/robot_arm/exercise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var filecheck = require('workshopper-exercise/filecheck')
88
var execute = require('workshopper-exercise/execute')
99
var wrappedexec = require('workshopper-wrappedexec')
1010
var path = require('path')
11-
var notifier = require('../../lib/notifier')('Robot arm')
11+
var notifier = require('../../lib/notifier')
12+
var broadcaster = require('../../lib/broadcaster')
1213

1314
// checks that the submission file actually exists
1415
exercise = filecheck(exercise)
@@ -100,11 +101,11 @@ exercise.addVerifyProcessor(function (callback) {
100101
}, sensor.freq + 0)
101102
},
102103
function (error) {
103-
notifier(error, callback)
104+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
104105
})
105106

106107
} catch(error) {
107-
notifier(error, callback)
108+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
108109
}
109110
})
110111

exercises/servo_wave/exercise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var filecheck = require('workshopper-exercise/filecheck')
77
var execute = require('workshopper-exercise/execute')
88
var wrappedexec = require('workshopper-wrappedexec')
99
var path = require('path')
10-
var notifier = require('../../lib/notifier')('Servo wave')
10+
var notifier = require('../../lib/notifier')
11+
var broadcaster = require('../../lib/broadcaster')
1112

1213
// checks that the submission file actually exists
1314
exercise = filecheck(exercise)
@@ -60,9 +61,9 @@ exercise.addVerifyProcessor(function (callback) {
6061
expect(stop0.calledBefore(toLast), 'servo did not stop before returning to center').to.be.true
6162
expect(toLast.args[0], 'servo did not return to center').to.equal(90)
6263

63-
notifier(callback)
64+
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
6465
} catch(error) {
65-
notifier(error, callback)
66+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
6667
}
6768
})
6869

exercises/spin_motor_spin/exercise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var filecheck = require('workshopper-exercise/filecheck')
77
var execute = require('workshopper-exercise/execute')
88
var wrappedexec = require('workshopper-wrappedexec')
99
var path = require('path')
10-
var notifier = require('../../lib/notifier')('Spin motor spin')
10+
var notifier = require('../../lib/notifier')
11+
var broadcaster = require('../../lib/broadcaster')
1112

1213
// checks that the submission file actually exists
1314
exercise = filecheck(exercise)
@@ -73,9 +74,9 @@ exercise.addVerifyProcessor(function (callback) {
7374
expect(start1.calledAfter(wait1), 'motor didn\'t start again after 1 second')
7475
expect(start1.args[0], 'motor not started again at 200').to.equal(200)
7576

76-
notifier(callback)
77+
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
7778
} catch(error) {
78-
notifier(error, callback)
79+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
7980
}
8081
})
8182

exercises/street_lamp/exercise.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var filecheck = require('workshopper-exercise/filecheck')
77
var execute = require('workshopper-exercise/execute')
88
var wrappedexec = require('workshopper-wrappedexec')
99
var path = require('path')
10-
var notifier = require('../../lib/notifier')('Street lamp')
10+
var notifier = require('../../lib/notifier')
11+
var broadcaster = require('../../lib/broadcaster')
1112

1213
// checks that the submission file actually exists
1314
exercise = filecheck(exercise)
@@ -69,7 +70,7 @@ exercise.addVerifyProcessor(function (callback) {
6970
try {
7071
expect(led.on.called, 'led was not turned on when resistor value high').to.be.true
7172
} catch (er) {
72-
callback(er, false)
73+
return broadcaster(exercise)(er, function (er) { notifier(exercise)(er, callback) })
7374
}
7475

7576
analogReadListener(random(0, 600))
@@ -82,7 +83,7 @@ exercise.addVerifyProcessor(function (callback) {
8283
'led was not turned off after it was turned on'
8384
).to.be.true
8485
} catch (er) {
85-
callback(er, false)
86+
return broadcaster(exercise)(er, function (er) { notifier(exercise)(er, callback) })
8687
}
8788

8889
analogReadListener(random(600, 900))
@@ -93,16 +94,16 @@ exercise.addVerifyProcessor(function (callback) {
9394
led.on.lastCall.calledAfter(led.off.lastCall),
9495
'led was not turned on after it was turned off'
9596
).to.be.true
96-
notifier(callback)
97+
broadcaster(exercise)(function (er) { notifier(exercise)(er, callback) })
9798
} catch (error) {
98-
notifier(error, callback)
99+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
99100
}
100101
}, freq)
101102
}, freq)
102103
}, freq)
103104

104105
} catch (error) {
105-
notifier(error, callback)
106+
broadcaster(exercise)(error, function (er) { notifier(exercise)(er, callback) })
106107
}
107108
})
108109

lib/broadcaster.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var dgram = require('dgram')
2+
3+
module.exports = function (exercise) {
4+
return function (er, cb) {
5+
if (!cb) {
6+
cb = er
7+
er = null
8+
}
9+
10+
var sock = dgram.createSocket('udp4')
11+
, packet = new Buffer(JSON.stringify({
12+
workshop: exercise.workshopper.title,
13+
exercise: exercise.name,
14+
event: er ? 'fail' : 'pass',
15+
timestamp: Date.now()
16+
}))
17+
, port = process.env.PROGRESS_BROADCAST_PORT || 1138
18+
19+
try {
20+
sock.bind(port, '0.0.0.0', function (er) {
21+
if (er) return;
22+
sock.setBroadcast(true)
23+
24+
sock.send(packet, 0, packet.length, port, '255.255.255.255', function () {
25+
sock.close()
26+
})
27+
})
28+
} catch (er) {}
29+
30+
cb(er)
31+
}
32+
}

0 commit comments

Comments
 (0)