Permalink
Browse files

Refactoring for Node 3.x

  • Loading branch information...
Judson Stephenson authored and jfd committed Jan 31, 2011
1 parent 2678b7c commit 7a62b5d8abfebfee9c9f31dcb066b7410f9e7e75
Showing with 107 additions and 74 deletions.
  1. +107 −74 fleetdb.js
View
@@ -1,90 +1,123 @@
//
// node-fleet
-// A FleetDB client for Node.js
+// A fleetdb client for Node.js
//
// Read README for instructions and LICENSE license.
//
-// Copyright (c) 2010 Johan Dahlberg
+// Copyright (c) 2010 Johan Dahlberg
+// Copyright (c) 2011 Judson Stephenson
//
-var tcp = require('tcp'),
- uri = require('uri');
+var net = require('net'),
+ url = require('url'),
+ sys = require('sys');
-var KILL_TIMEOUT = 2000;
+var KILL_TIMEOUT = 6000;
-exports.open = function(uri_addr, err_callback) {
- var addr = uri.parse(uri_addr);
- var port = addr.port || 3400,
- addr = addr.url || '127.0.0.1',
- queue = [],
- q_stack = [],
- conn = tcp.createConnection(port, addr),
- kill_signal = false,
- auto_kill_pid = 0;
+var fleetdb = exports.fleetdb = function ()
+{
+ var self = this;
+ var options = {
+ queue: [],
+ q_stack: [],
+ conn: null,
+ kill_signal: false,
+ auto_kill_pid: false
+ };
+
+ self.options = options;
- conn.setEncoding('utf8');
+ return self;
+};
- conn.addListener('connect', function() {
- var q = null;
- while ((q = queue.pop())) {
- conn.send(q);
- }
- });
+fleetdb.prototype = {
+ constructor: fleetdb
+};
- conn.addListener('receive', function(data) {
- var obj = JSON.parse(data);
- var callback = q_stack.pop();
- callback(obj[0], obj[1]);
- if (kill_signal && q_stack.length == 0) {
- conn.close();
- conn = null;
- if (auto_kill_pid != 0) {
- clearTimeout(auto_kill_pid);
- }
- }
- });
+fleetdb.prototype.open = function (uri, callback)
+{
+ var self = this;
+ var o = self.options;
+
+ var addr = url.parse(uri);
+ var port = addr.port || 3400,
+ addr = addr.url || '127.0.0.1';
+
+ o.conn = net.createConnection(port, addr);
+ o.conn.setEncoding('utf8');
- conn.addListener('timeout', function(data) {
- err_callback('timeout');
- });
+ o.conn.on('connect', function() {
+ var q = null;
+ while ((q = o.queue.pop())) {
+ o.conn.write(q);
+ }
+ });
- return {
+ o.conn.on('data', function(data) {
+ var obj = JSON.parse(data);
+ var callback = o.q_stack.pop();
+ callback(obj[0], obj[1]);
+
+ if (o.kill_signal && o.q_stack.length == 0) {
+ if(o.conn != null)
+ {
+ o.conn.destroy();
+ o.conn = null;
+ }
+ if (o.auto_kill_pid != 0) {
+ clearTimeout(o.auto_kill_pid);
+ }
+ }
+ });
- /**
- * Query the FleetDB server. The optional ´´callback´´ is called when
- * the server returns a response.
- */
- query: function(q, callback) {
- var data = JSON.stringify(q) + '\r\n';
- q_stack.push(callback || function() {});
- if (conn.readyState == 'open') {
- conn.send(data);
- } else {
- queue.push(data);
- }
- },
+ o.conn.addListener('timeout', function(data) {
+ err_callback('timeout');
+ });
+
+};
- /**
- * Close the established connection with the server.
- */
- close: function() {
- if (q_stack.length) {
- // We are still waiting for some server responses. Handle them, then
- // kill the connection.
- kill_signal = true;
-
- // Kill anyway after KILL_TIMEOUT seconds.
- auto_kill_pid = setTimeout(function() {
- if (conn != null) {
- conn.close();
- conn = null;
- }
- }, KILL_TIMEOUT);
- } else {
- conn.close();
- conn = null;
- }
- }
-
- }
+/**
+ * Query the fleetdb server. The optional ««callback«« is called when
+ * the server returns a response.
+ */
+fleetdb.prototype.query = function(q, callback)
+{
+ var self = this;
+ var o = self.options;
+
+ var data = JSON.stringify(q) + '\r\n';
+ o.q_stack.push(callback);
+
+ if (o.conn.readyState == 'open') {
+ o.conn.write(data);
+ } else {
+ o.queue.push(data);
+ }
+}
-}
+/**
+ * Close the established connection with the server.
+ */
+fleetdb.prototype.close = function()
+{
+ var self = this;
+ var o = self.options;
+
+ if (o.q_stack.length) {
+ // We are still waiting for some server responses. Handle them, then
+ // kill the connection.
+ o.kill_signal = true;
+
+ // Kill anyway after KILL_TIMEOUT seconds.
+ o.auto_kill_pid = setTimeout(function() {
+ if (o.conn != null) {
+ o.conn.destroy();
+ o.conn = null;
+ }
+ }, KILL_TIMEOUT);
+ } else {
+ if (o.conn != null) {
+ o.conn.destroy();
+ o.conn = null;
+ }
+ }
+}

0 comments on commit 7a62b5d

Please sign in to comment.