From 0be6fc9dcb81564f8020a2b774c2c9a7193df878 Mon Sep 17 00:00:00 2001 From: Mark Cavage Date: Fri, 2 Sep 2011 09:29:33 -0400 Subject: [PATCH] GH-10: Client not interacting with AD, and has bad socket.connect() calls --- lib/client.js | 106 ++++----------------------------- lib/messages/search_request.js | 4 +- 2 files changed, 14 insertions(+), 96 deletions(-) diff --git a/lib/client.js b/lib/client.js index 183fc90..92b2793 100644 --- a/lib/client.js +++ b/lib/client.js @@ -104,8 +104,7 @@ function Client(options) { throw new TypeError('options.socketPath must be a string'); if (options.log4js && typeof(options.log4js) !== 'object') throw new TypeError('options.log4s must be an object'); - if (options.numConnections && typeof(options.numConnections) !== 'number') - throw new TypeError('options.numConnections must be a number'); + if (!xor(options.url, options.socketPath)) throw new TypeError('options.url ^ options.socketPath required'); @@ -119,9 +118,6 @@ function Client(options) { } this.log4js = options.log4js || logStub; - this.numConnections = Math.abs(options.numConnections) || 1; - this.connections = []; - this.currentConnection = 0; this.connectOptions = { port: self.url ? self.url.port : options.socketPath, host: self.url ? self.url.hostname : undefined @@ -168,33 +164,16 @@ function Client(options) { self.emit('connect', c.ldap.id); }); c.on('end', function() { - self.log.trace('%s end', c.ldap.id); - c.ldap.connected = false; - if (!self.shutdown) - c.connect(); + self.emit('end'); }); c.addListener('close', function(had_err) { - self.log.trace('%s close; had_err=%j', c.ldap.id, had_err); - c.ldap.connected = false; - if (!self.shutdown) - c.connect(); + self.emit('close', had_err); }); c.on('error', function(err) { - self.log.warn('%s unexpected connection error %s', c.ldap.id, err); - self.emit('error', err, c.ldap.id); - c.ldap.connected = false; - if (!self.shutdown) { - c.end(); - c.connect(); - } + self.emit('error', err); }); c.on('timeout', function() { - self.log.trace('%s timed out', c.ldap.id); - c.ldap.connected = false; - if (!self.shutdown) { - c.end(); - c.connect(); - } + self.emit('timeout'); }); c.on('data', function(data) { if (self.log.isTraceEnabled()) @@ -208,8 +187,8 @@ function Client(options) { var callback = c.ldap.messages[message.messageID]; if (!callback) { - self.log.error('%s: received unsolicited message: %j', - c.ldap.id, message.json); + self.log.error('%s: received unsolicited message: %j', c.ldap.id, + message.json); return; } @@ -218,9 +197,7 @@ function Client(options) { return c; } - for (var i = 0; i < this.numConnections; i++) { - self.connections.push(newConnection()); - } + self.connection = newConnection(); } util.inherits(Client, EventEmitter); module.exports = Client; @@ -258,26 +235,7 @@ Client.prototype.bind = function(name, credentials, controls, callback) { controls: controls }); - var cbIssued = false; - var finished = 0; - function _callback(err, res) { - if (err) { - if (!cbIssued) { - cbIssued = true; - return callback(err); - } - } - - if (++finished >= self.connections.length && !cbIssued) { - cbIssued = true; - self.emit('bind'); - return callback(null, res); - } - } - - this.connections.forEach(function(c) { - return self._send(req, [errors.LDAP_SUCCESS], _callback, c); - }); + return self._send(req, [errors.LDAP_SUCCESS], callback); }; @@ -646,60 +604,20 @@ Client.prototype.unbind = function(callback) { if (!callback) callback = function defUnbindCb() { self.log.trace('disconnected'); }; - this.shutdown = true; var req = new UnbindRequest(); - var finished = 0; - var cbIssued = false; - - function _callback(err, res) { - if (err) { - if (!cbIssued) { - cbIssued = true; - return callback(err); - } - } - - if (++finished >= self.connections.length && !cbIssued) { - cbIssued = true; - self.emit('unbind'); - return callback(null); - } - } - - this.connections.forEach(function(c) { - return self._send(req, 'unbind', _callback, c); - }); + return self._send(req, 'unbind', callback); }; -Client.prototype._send = function(message, expect, callback, conn) { +Client.prototype._send = function(message, expect, callback) { assert.ok(message); assert.ok(expect); assert.ok(callback); var self = this; - // First select a connection - // Note bind and unbind are special in that they will pass in the - // connection since they iterate over the whole pool - if (!conn) { - function nextConn() { - if (++self.currentConnection >= self.connections.length) - self.currentConnection = 0; - - return self.connections[self.currentConnection]; - } - - var save = this.currentConnection; - while ((conn = nextConn()) && save !== this.currentConnection); - - if (!conn) { - self.emit('error', new DisconnectedError('No connections available')); - return; - } - } - assert.ok(conn); + var conn = self.connection; // Now set up the callback in the messages table message.messageID = conn.ldap.nextMessageID; diff --git a/lib/messages/search_request.js b/lib/messages/search_request.js index 5696bf1..bc720c0 100644 --- a/lib/messages/search_request.js +++ b/lib/messages/search_request.js @@ -127,13 +127,13 @@ SearchRequest.prototype._toBer = function(ber) { var f = this.filter || new filters.PresenceFilter({attribute: 'objectclass'}); ber = f.toBer(ber); + ber.startSequence(Ber.Sequence | Ber.Constructor); if (this.attributes && this.attributes.length) { - ber.startSequence(Ber.Sequence | Ber.Constructor); this.attributes.forEach(function(a) { ber.writeString(a); }); - ber.endSequence(); } + ber.endSequence(); return ber; };