From 4b4463ad8ea54e1280074c3dac8fc3b1d2a34b2a Mon Sep 17 00:00:00 2001 From: Johan Dahlberg Date: Fri, 28 Jan 2011 12:41:34 +0100 Subject: [PATCH 1/2] `Nodified ` javascript connection a little bit --- examples/lookup2.js | 26 ++++++---- lib/geoip.js | 112 ++++++++++++++++++++++++++++---------------- 2 files changed, 88 insertions(+), 50 deletions(-) diff --git a/examples/lookup2.js b/examples/lookup2.js index ed54b05..c5a3ad2 100644 --- a/examples/lookup2.js +++ b/examples/lookup2.js @@ -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); diff --git a/lib/geoip.js b/lib/geoip.js index 98b6c9e..e4680df 100644 --- a/lib/geoip.js +++ b/lib/geoip.js @@ -1,57 +1,89 @@ -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; + } + + 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(); +} \ No newline at end of file From 02d2b3c7008328810b766b025c6f1326ae1825de Mon Sep 17 00:00:00 2001 From: Johan Dahlberg Date: Fri, 28 Jan 2011 12:59:56 +0100 Subject: [PATCH 2/2] added dbpath error check on connect --- lib/geoip.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/geoip.js b/lib/geoip.js index e4680df..7b659ea 100644 --- a/lib/geoip.js +++ b/lib/geoip.js @@ -29,6 +29,10 @@ GeoIpConnection.prototype.connect = function(dbpath) { return; } + if (typeof dbpath !== "string" || dbpath.length == 0) { + throw new Error("Expected dbpath"); + } + this._connection = new Connection(); this._connection.on("connected", function() {