Skip to content

Commit

Permalink
Using HEAD requests to Google to monitor connectivity, slow status no…
Browse files Browse the repository at this point in the history
…w supported
  • Loading branch information
flesler committed Oct 24, 2016
1 parent 9629281 commit 420ddf7
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 14 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf

[*.js]
indent_size = 2
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true
File renamed without changes.
File renamed without changes
58 changes: 50 additions & 8 deletions app/lib/connectivity.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
const Monitor = new require('electron-online');
const dns = require('dns');
const http = require('http');
const status = require('./status');

const HOST = {method: 'HEAD', host: 'google.com', path: '/'};
const INTERVAL = 3000;
const MAX_MS = 800;
// How many SLOW in a row to conclude
const SLOW_ATTEMPTS = 5;

let last;
let count = 0;

exports.monitor = function(cb) {
const monitor = new Monitor();
monitor.on('online', function() {
cb(status.ONLINE);
});
monitor.on('offline', function() {
cb(status.OFFLINE);
});
setInterval(function() { check(cb); }, INTERVAL);
check(cb);
};

function check(cb) {
ping(function(err, ms) {
const curr = getStatus(err, ms);
if (curr !== last) {
last = curr;
cb(curr);
}
});
}

function getStatus(err, ms) {
if (err) return status.OFFLINE;
if (ms <= MAX_MS) {
count = 0;
return status.ONLINE;
}
if (++count >= SLOW_ATTEMPTS || !last) {
return status.SLOW;
}
return last;
}

function ping(done) {
// Using DNS is not good enough because it has caching
// Using native ping I don't like because it means more running processes
const start = Date.now();
const req = http.request(HOST);
req.on('response', function(res) {
// Flush
res.resume();
done(null, Date.now() - start);
})
req.on('error', done);
req.end();
}

2 changes: 1 addition & 1 deletion app/lib/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const platform = require('os').platform();

exports.UNKNOWN = createStatus('unknown');
exports.OFFLINE = createStatus('offline');
exports.LOW = createStatus('low');
exports.SLOW = createStatus('slow');
exports.ONLINE = createStatus('online');

function createStatus(id) {
Expand Down
2 changes: 1 addition & 1 deletion app/lib/tray.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const electron = require('electron');
const Tray = electron.Tray;

const DEFAULT = require('./status').LOW;
const DEFAULT = require('./status').UNKNOWN;

let icon;

Expand Down
2 changes: 1 addition & 1 deletion app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ app.on('ready', function () {
});

app.on('window-all-closed', function () {
app.quit();
//app.quit();
});

process.on('uncaughtException', function(err) {
Expand Down
3 changes: 1 addition & 2 deletions app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wifine",
"productName": "Wi-Fine",
"version": "1.1.0",
"version": "2.0.0",
"author": "Ariel Flesler <aflesler@gmail.com>",
"description": "A connectivity monitoring app, built on Electron",
"homepage": "https://github.com/flesler/electron-wi-fine",
Expand All @@ -15,7 +15,6 @@
"electron-config": "0.2.1",
"electron-gh-releases": "2.0.4",
"electron-notifications": "0.1.1",
"electron-online": "1.0.0",
"electron-squirrel-startup": "1.0.0",
"request": "2.75.0"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wifine",
"version": "1.1.0",
"version": "2.0.0",
"scripts": {
"start": "electron app/ --enable-logging",
"pack": "build --dir",
Expand Down

0 comments on commit 420ddf7

Please sign in to comment.