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 export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin
cd `dirname $0` 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.' echo 'Cannot find ./cluster-geo containing NodeJS script files.'
exit 1 exit 1
fi fi

View file

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

View file

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