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 ;
0 commit comments