GeoIP cluster service works.

This commit is contained in:
Adam Ierymenko 2015-10-22 10:41:15 -07:00
parent e07bae2525
commit 1bc451ed10
3 changed files with 19 additions and 14 deletions

View File

@ -3,7 +3,7 @@
export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
cd `dirname $0`
if [ ! -d cluster-geo -o ! -f cluster-geo/index.js ]; then
if [ ! -d cluster-geo -o ! -f cluster-geo/cluster-geo.js ]; then
echo 'Cannot find ./cluster-geo containing NodeJS script files.'
exit 1
fi

View File

@ -1,3 +1,5 @@
"use strict";
//
// GeoIP lookup service
//
@ -20,10 +22,10 @@ function lookup(ip,callback)
cache.get(ip,function(err,cachedEntryJson) {
if ((!err)&&(cachedEntryJson)) {
try {
var cachedEntry = JSON.parse(cachedEntryJson.toString());
let cachedEntry = JSON.parse(cachedEntryJson.toString());
if (cachedEntry) {
var ts = cachedEntry.ts;
var r = cachedEntry.r;
let ts = cachedEntry.ts;
let r = cachedEntry.r;
if ((ts)&&(r)) {
if ((Date.now() - ts) < CACHE_TTL) {
r._cached = true;
@ -57,24 +59,24 @@ process.stdin.on('readable',function() {
var chunk;
while (null !== (chunk = process.stdin.read())) {
for(var i=0;i<chunk.length;++i) {
var c = chunk[i];
let c = chunk[i];
if ((c == 0x0d)||(c == 0x0a)) {
if (linebuf.length > 0) {
var ip = linebuf;
let ip = linebuf;
lookup(ip,function(err,result) {
if ((err)||(!result)||(!result.location)) {
return process.stdout.write(ip+',0,0,0,0,0,0\n');
} else {
var lat = parseFloat(result.location.latitude);
var lon = parseFloat(result.location.longitude);
let lat = parseFloat(result.location.latitude);
let lon = parseFloat(result.location.longitude);
// Convert to X,Y,Z coordinates from Earth's origin, Earth-as-sphere approximation.
var latRadians = lat * 0.01745329251994; // PI / 180
var lonRadians = lon * 0.01745329251994; // PI / 180
var cosLat = Math.cos(latRadians);
var x = Math.round((-6371.0) * cosLat * Math.cos(lonRadians)); // 6371 == Earth's approximate radius in kilometers
var y = Math.round(6371.0 * Math.sin(latRadians));
var z = Math.round(6371.0 * cosLat * Math.sin(lonRadians));
let latRadians = lat * 0.01745329251994; // PI / 180
let lonRadians = lon * 0.01745329251994; // PI / 180
let cosLat = Math.cos(latRadians);
let x = Math.round((-6371.0) * cosLat * Math.cos(lonRadians)); // 6371 == Earth's approximate radius in kilometers
let y = Math.round(6371.0 * Math.sin(latRadians));
let z = Math.round(6371.0 * cosLat * Math.sin(lonRadians));
return process.stdout.write(ip+',1,'+lat+','+lon+','+x+','+y+','+z+'\n');
}

View File

@ -37,6 +37,8 @@
#include <signal.h>
#include <errno.h>
#include <iostream>
#include "ClusterGeoIpService.hpp"
#include "../node/Utils.hpp"
#include "../osdep/OSUtils.hpp"
@ -163,6 +165,7 @@ void ClusterGeoIpService::threadMain()
{
Mutex::Lock _l2(_cache_m);
_cache[rip] = ce;
std::cout << ">> " << linebuf << std::endl;
}
}
}