Skip to content

Commit 19c0d0e

Browse files
committed
ok now i can ping fff
1 parent 09893f2 commit 19c0d0e

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

lib/connection.js

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var _ = require('underscore');
55
var EventEmitter = require('events');
66
var msgpack = require('msgpack');
77

8-
const states = {
8+
var states = {
99
CONNECTING: 0,
1010
CONNECTED: 1,
1111
AWAITING: 2,
@@ -16,7 +16,7 @@ const states = {
1616
PREHELLO: 7
1717
};
1818

19-
const commandType = {
19+
var commandType = {
2020
CONNECT: 0,
2121
REQUEST: 1
2222
};
@@ -58,7 +58,7 @@ TarantoolConnection.prototype.onData = function(data){
5858
console.log(data.length, data.toString());
5959
switch(this.state){
6060
case states.PREHELLO:
61-
for (let i = 0; i<this.commandsQueue.length; i++)
61+
for (var i = 0; i<this.commandsQueue.length; i++)
6262
{
6363
if (this.commandsQueue[i][0] == commandType.CONNECT)
6464
{
@@ -69,6 +69,9 @@ TarantoolConnection.prototype.onData = function(data){
6969
}
7070
this.state = states.CONNECTED;
7171
break;
72+
case states.CONNECTED:
73+
new TarantoolResponse(data);
74+
break;
7275
}
7376
};
7477

@@ -77,7 +80,7 @@ TarantoolConnection.prototype.onConnect = function(){
7780
};
7881

7982
TarantoolConnection.prototype.onError = function(error){
80-
for (let i=0; i<this.commandsQueue.length; i++)
83+
for (var i=0; i<this.commandsQueue.length; i++)
8184
this.commandsQueue[i][2](error);
8285
this.commandsQueue = [];
8386
};
@@ -92,13 +95,39 @@ TarantoolConnection.prototype.connect = function(){
9295
};
9396

9497
TarantoolConnection.prototype.ping = function(){
98+
console.log('start ping');
99+
var header = this._header(tarantoolConstants.RequestCode.rqPing);
100+
console.log('header', header);
101+
var body = new Buffer(0);
102+
this._request(header, body);
103+
};
95104

105+
106+
TarantoolConnection.prototype._header = function(command){
107+
try {
108+
var header = new Buffer([0x82, tarantoolConstants.KeysCode.code, command,
109+
tarantoolConstants.KeysCode.sync, 0xce, 0, 0, 0, 0]);
110+
header.writeUIntBE(requestId.getId(), 5, 4);
111+
return header;
112+
} catch(e){
113+
console.log(e, e.stack);
114+
}
96115
};
97116

98117
TarantoolConnection.prototype._request = function(header, body){
118+
console.log('start request');
99119
var sumL = header.length + body.length;
100120
var prefixSizeBuffer = new Buffer(5);
101-
socket.write()
121+
prefixSizeBuffer[0] = 0xCE;
122+
prefixSizeBuffer.writeUIntBE(sumL, 1, 4);
123+
try {
124+
var buffer = Buffer.concat([prefixSizeBuffer, header, body]);
125+
console.log(buffer);
126+
this.socket.write(buffer);
127+
} catch (e){
128+
console.log(e, e.stack);
129+
}
130+
102131
};
103132

104133
TarantoolConnection.prototype.destroy = function(interupt){
@@ -112,4 +141,35 @@ TarantoolConnection.prototype.destroy = function(interupt){
112141
}
113142
};
114143

144+
//@InputType(Buffer)
145+
function TarantoolResponse(buffer){
146+
this.ended = false;
147+
var length = buffer.readUIntBE(1, 4);
148+
console.log('len', length);
149+
this.buffer = buffer;
150+
if (this.buffer.length - 5 == length)
151+
{
152+
this.ended = true;
153+
this.processing();
154+
}
155+
}
156+
157+
TarantoolResponse.prototype.processing = function(){
158+
try{
159+
this.success = this.buffer[6] == 0x00;
160+
console.log(this.buffer);
161+
if (this.success){
162+
var dataBuffer = this.buffer.slice(13);
163+
console.log(dataBuffer);
164+
var obj = msgpack.unpack(dataBuffer);
165+
console.log(obj);
166+
}
167+
else{
168+
console.log('its error');
169+
}
170+
} catch(e){
171+
console.log(e, e.stack);
172+
}
173+
};
174+
115175
module.exports = TarantoolConnection;

test/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var conn = new TarantoolConnection({port: 33013, host: '95.85.55.64'});
77
conn.connect()
88
.then(function(){
99
console.log('resolve');
10+
conn.ping();
1011
}, function(e){
1112
console.log('reject');
1213
})

0 commit comments

Comments
 (0)