connection.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The MemDB Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var P = require('bluebird');
  17. var EventEmitter = require('events').EventEmitter;
  18. var util = require('util');
  19. var clientPool = require('./clientpool');
  20. var consts = require('./consts');
  21. var logger = require('memdb-logger').getLogger('memdb-client', __filename);
  22. var Connection = function(opts){
  23. EventEmitter.call(this);
  24. opts = opts || {};
  25. this.config = opts;
  26. this._client = null;
  27. this._connId = null;
  28. this._collections = {};
  29. };
  30. util.inherits(Connection, EventEmitter);
  31. var proto = Connection.prototype;
  32. proto.connect = function(){
  33. if(this._connId){
  34. throw new Error('already connected');
  35. }
  36. var key = this.config.host + ':' + this.config.port;
  37. return P.bind(this)
  38. .then(function(){
  39. return clientPool.getClient(this.config.host, this.config.port);
  40. })
  41. .then(function(client){
  42. this._client = client;
  43. this._client.on('close', this._close.bind(this));
  44. return this._client.request(null, 'connect', [consts.version]);
  45. })
  46. .then(function(ret){
  47. this._connId = ret.connId;
  48. logger.info('[conn:%s] connected on %s:%s', this._connId, this.config.host, this.config.port);
  49. return this._connId;
  50. });
  51. };
  52. proto.close = function(){
  53. if(!this._connId){
  54. throw new Error('not connected');
  55. }
  56. return P.bind(this)
  57. .then(function(){
  58. return this._client.request(this._connId, 'disconnect', []);
  59. })
  60. .then(function(){
  61. this._close();
  62. });
  63. };
  64. proto._close = function(){
  65. logger.info('[conn:%s] closed on %s:%s', this._connId, this.config.host, this.config.port);
  66. this._client = null;
  67. this._connId = null;
  68. this.emit('close');
  69. };
  70. proto.collection = function(name){
  71. var self = this;
  72. if(!this._collections[name]){
  73. var collection = {};
  74. consts.collMethods.forEach(function(method){
  75. collection[method] = function(){
  76. var args = [name].concat([].slice.call(arguments));
  77. return self[method].apply(self, args);
  78. };
  79. });
  80. this._collections[name] = collection;
  81. }
  82. return this._collections[name];
  83. };
  84. consts.connMethods.concat(consts.collMethods).forEach(function(method){
  85. proto[method] = function(){
  86. if(!this._connId){
  87. throw new Error('not connected');
  88. }
  89. var args = [].slice.call(arguments);
  90. return this._client.request(this._connId, method, args);
  91. };
  92. });
  93. module.exports = Connection;