Browse files

stylistic clean-up

  • Loading branch information...
1 parent 811385f commit faee1363ed49f3ab3c57d4263adecfc7cb7ed70d @agnat committed Jan 14, 2012
Showing with 22 additions and 19 deletions.
  1. +22 −19 wake_on_lan.js
View
41 wake_on_lan.js
@@ -2,27 +2,29 @@ var dgram = require('dgram')
, Buffer = require('buffer').Buffer
;
+var num_mac_octets = 6;
+
exports.createMagicPacket = function(mac) {
- var num_mac_octets = 6;
+ var mac_buffer = new Buffer(num_mac_octets)
+ , i
+ ;
if (mac.length == 2 * num_mac_octets + (num_mac_octets - 1)) {
- var sep = mac[2];
- mac = mac.replace(new RegExp(sep, 'g'), '');
+ mac = mac.replace(new RegExp(mac[2], 'g'), '');
}
if (mac.length != 2 * num_mac_octets || mac.match(/[^a-fA-F0-9]/)) {
throw new Error("malformed MAC address '" + mac + "'");
}
- var mac_buffer = new Buffer(num_mac_octets);
- for (var i = 0; i < num_mac_octets; ++i) {
+ for (i = 0; i < num_mac_octets; ++i) {
mac_buffer[i] = parseInt(mac.substr(2 * i, 2), 16);
}
- var num_macs = 16;
- var buffer = new Buffer((1 + num_macs) * num_mac_octets);
- for (var i = 0; i < num_mac_octets; ++i) {
+ var num_macs = 16
+ , buffer = new Buffer((1 + num_macs) * num_mac_octets);
+ for (i = 0; i < num_mac_octets; ++i) {
buffer[i] = 0xff;
}
- for (var i = 0; i < num_macs; ++i) {
+ for (i = 0; i < num_macs; ++i) {
mac_buffer.copy(buffer, (i + 1) * num_mac_octets, 0, mac_buffer.length)
}
return buffer;
@@ -36,17 +38,18 @@ exports.wake = function(mac, opts, callback) {
opts = opts || {};
- var address = opts['address'] || '255.255.255.255';
- var num_packets = opts['num_packets'] || 3;
- var interval = opts['interval'] || 100;
- var port = opts['port'] || 9;
+ var address = opts['address'] || '255.255.255.255'
+ , num_packets = opts['num_packets'] || 3
+ , interval = opts['interval'] || 100
+ , port = opts['port'] || 9
+ , magic_packet = exports.createMagicPacket(mac)
+ , socket = dgram.createSocket('udp4')
+ , i = 0
+ , timer_id
+ ;
- var magic_packet = exports.createMagicPacket(mac);
- var socket = dgram.createSocket('udp4');
socket.setBroadcast(true);
- var i = 0;
- var timer_id;
- var handler = function(error) {
+ function handler(error) {
if (error || i === num_packets) {
socket.close();
if (timer_id) {
@@ -57,7 +60,7 @@ exports.wake = function(mac, opts, callback) {
}
}
}
- var sendWoL = function() {
+ function sendWoL() {
i += 1;
socket.send(magic_packet, 0, magic_packet.length, port, address, handler);
if (i < num_packets) {

0 comments on commit faee136

Please sign in to comment.