Skip to content

Commit 09893f2

Browse files
committed
test connection
1 parent f79ca91 commit 09893f2

File tree

4 files changed

+130
-2
lines changed

4 files changed

+130
-2
lines changed

lib/connection.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
'use strict';
2+
var tarantoolConstants = require('./const');
3+
var net = require('net');
4+
var _ = require('underscore');
5+
var EventEmitter = require('events');
6+
var msgpack = require('msgpack');
7+
8+
const states = {
9+
CONNECTING: 0,
10+
CONNECTED: 1,
11+
AWAITING: 2,
12+
REQUESTING: 3,
13+
GETTING: 4,
14+
INITED: 5,
15+
DISONECTED: 6,
16+
PREHELLO: 7
17+
};
18+
19+
const commandType = {
20+
CONNECT: 0,
21+
REQUEST: 1
22+
};
23+
24+
var requestId = {
25+
_id: 0,
26+
getId: function(){
27+
if (this._id > 1000000)
28+
this._id = 0;
29+
return this._id++;
30+
}
31+
};
32+
33+
var defaultOptions = {
34+
host: 'localhost',
35+
port: '3301',
36+
username: null,
37+
password: null,
38+
timeout: 5000,
39+
reconnect: true
40+
};
41+
42+
43+
function TarantoolConnection (options){
44+
this.socket = new net.Socket({
45+
readable: true,
46+
writable: true
47+
});
48+
this.state = states.INITED;
49+
this.emitter = new EventEmitter();
50+
this.options = _.extend(defaultOptions, options);
51+
this.commandsQueue = [];
52+
this.socket.on('connect', this.onConnect.bind(this));
53+
this.socket.on('error', this.onError.bind(this));
54+
this.socket.on('data', this.onData.bind(this));
55+
}
56+
57+
TarantoolConnection.prototype.onData = function(data){
58+
console.log(data.length, data.toString());
59+
switch(this.state){
60+
case states.PREHELLO:
61+
for (let i = 0; i<this.commandsQueue.length; i++)
62+
{
63+
if (this.commandsQueue[i][0] == commandType.CONNECT)
64+
{
65+
this.commandsQueue[i][1](true);
66+
this.commandsQueue.splice(i, 1);
67+
i--;
68+
}
69+
}
70+
this.state = states.CONNECTED;
71+
break;
72+
}
73+
};
74+
75+
TarantoolConnection.prototype.onConnect = function(){
76+
this.state = states.PREHELLO;
77+
};
78+
79+
TarantoolConnection.prototype.onError = function(error){
80+
for (let i=0; i<this.commandsQueue.length; i++)
81+
this.commandsQueue[i][2](error);
82+
this.commandsQueue = [];
83+
};
84+
85+
TarantoolConnection.prototype.connect = function(){
86+
console.log('pre connect');
87+
this.state = states.CONNECTING;
88+
return new Promise(function(resolve, reject){
89+
this.commandsQueue.push([commandType.CONNECT, resolve, reject]);
90+
this.socket.connect({port: this.options.port, host: this.options.host});
91+
}.bind(this));
92+
};
93+
94+
TarantoolConnection.prototype.ping = function(){
95+
96+
};
97+
98+
TarantoolConnection.prototype._request = function(header, body){
99+
var sumL = header.length + body.length;
100+
var prefixSizeBuffer = new Buffer(5);
101+
socket.write()
102+
};
103+
104+
TarantoolConnection.prototype.destroy = function(interupt){
105+
if (interupt)
106+
{
107+
this.socket.destroy();
108+
}
109+
else
110+
{
111+
this.commandsQueue.push('destroy');
112+
}
113+
};
114+
115+
module.exports = TarantoolConnection;

lib/const.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const PacketLengthBytes = 5;
5252
const ExportPackage = {
5353
RequestCode: RequestCode,
5454
KeysCode: KeysCode,
55-
IteratorsType: IteratrosType,
55+
IteratorsType: IteratorsType,
5656
OkCode: OkCode,
5757
NetErrCode: NetErrCode,
5858
TimeoutErrCode: TimeoutErrCode,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"homepage": "https://github.com/KlonD90/node-tarantool-driver",
2424
"dependencies": {
25-
"msgpack5": "^2.0.0"
25+
"msgpack5": "^2.0.0",
26+
"underscore": "^1.8.3"
2627
}
2728
}

test/app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Created by klond on 05.04.15.
3+
*/
4+
var TarantoolConnection = require('../lib/connection');
5+
6+
var conn = new TarantoolConnection({port: 33013, host: '95.85.55.64'});
7+
conn.connect()
8+
.then(function(){
9+
console.log('resolve');
10+
}, function(e){
11+
console.log('reject');
12+
})

0 commit comments

Comments
 (0)