Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions examples/lookup2.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
var sys = require('sys'),
geoip = require('../lib/geoip');
var geoip = require('../lib/geoip');

var dbpath = '/usr/local/share/GeoIP/GeoLiteCity.dat';
var ip = '216.236.135.152';

sys.puts('Looking up ip: ' + ip + '...\n');
var con = new geoip.Connection(dbpath, function(con) {
con.query(ip, function(result) {
for (var attr in result) {
sys.puts(attr + ' : ' + result[attr]);
}
con.close();
});
console.log('Looking up ip: ' + ip + '...\n');

var con = geoip.createConnection();

con.on("error", function(err) {
console.log(err);
});

con.query(ip, function(result) {
for (var attr in result) {
console.log(attr + ' : ' + result[attr]);
}
con.close();
});

con.connect(dbpath);
116 changes: 76 additions & 40 deletions lib/geoip.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,93 @@
var sys = require('sys'),
ceoip = require('./ceoip');
var inherits = require('util').inherits,
EventEmitter = require("events").EventEmitter,
Connection = require('./ceoip').Connection;


exports.createConnection = function() {
return new GeoIpConnection();
}

function GeoIpConnection() {
EventEmitter.call(this);

this._queue = [];
this._currentQuery = null;

function Connection(dbpath, callback) {
this.con = new ceoip.Connection();
this.queue = [];
this.connected = false;
this.currentQuery = null;
this.callback = callback;
this.closing = false;

this._connection = null;
}

var self = this;
exports.GeoIpConnection = GeoIpConnection;
inherits(GeoIpConnection, EventEmitter);

this.con.addListener('closed', function() {
GeoIpConnection.prototype.connect = function(dbpath) {
var self = this;

if (this._connection) {
return;
}

if (typeof dbpath !== "string" || dbpath.length == 0) {
throw new Error("Expected dbpath");
}

this._connection = new Connection();

this._connection.on("connected", function() {
self.connected = true;
self._processQueue();
self.emit("connect");
});

this._connection.on("closed", function() {
self.connected = false;
self.emit('closed');
self.closing = false;
self._connection = null;
self.emit("close");
});

this.con.addListener('connected', function() {
self.connected = true;
self.callback(self);
self.processQueue();
this._connection.on("result", function(result) {
self._currentQuery[0](result);
self._currentQuery = null;
self._processQueue();
});

this.con.addListener('result', function(result) {
self.currentQuery[0](result);
self.currentQuery = null;
self.processQueue();
this._connection.on("error", function(error) {
self.emit("error", error);
});

this.con.connect(dbpath);

try {
this._connection.connect(dbpath);
} catch (connectException) {
process.nextTick(function() {
self.emit("error", connectException);
});
}
}

sys.inherits(Connection, process.EventEmitter);

Connection.prototype.addJob = function(callback, ipAddress) {
this.queue.push([callback, ipAddress]);
this.processQueue();
};
GeoIpConnection.prototype.query = function(address, callback) {
this._queue.push([callback, address]);
this._processQueue();
}

Connection.prototype.query = function(ipAddress, callback) {
this.addJob(callback, ipAddress);
};
GeoIpConnection.prototype._processQueue = function() {

Connection.prototype.processQueue = function () {
if (!this.queue.length || !this.connected || this.currentQuery) {
if (!this._queue.length || !this._connection || this._currentQuery) {
return;
}
this.currentQuery = this.queue.shift();
this.con.query(this.currentQuery[1]);
};

Connection.prototype.close = function () {
// Emit failure on all promises in queue?
this.con.close();
};
this._currentQuery = this._queue.shift();
this._connection.query(this._currentQuery[1]);
}

GeoIpConnection.prototype.close = function() {

exports.Connection = Connection;
if (this.closing || !this._connection) {
return;
}

this.closing = true;
this._connection.close();
}