@@ -14,7 +14,8 @@ var states = {
1414 GETTING : 4 ,
1515 INITED : 5 ,
1616 DISONECTED : 6 ,
17- PREHELLO : 7
17+ PREHELLO : 7 ,
18+ AWAITING_LENGTH : 8
1819} ;
1920
2021var commandType = {
@@ -73,50 +74,96 @@ TarantoolConnection.prototype.onData = function(data){
7374 break ;
7475 case states . CONNECTED :
7576 //new TarantoolResponse(data);
76- this . responseEnded = false ;
77- this . curResponseLength = buffer . readUIntBE ( 1 , 4 ) ;
78- console . log ( 'len' , this . curResponseLength ) ;
79- this . buffer = buffer . slice ( 5 ) ;
80- if ( this . buffer . length >= this . curResponseLength )
77+ var trackResult = this . _responseBufferTrack ( data ) ;
78+ if ( trackResult . length == 2 )
8179 {
82- this . ended = true ;
83- if ( this . buffer . length > length ) {
84- var plusResponse = new TarantoolResponse ( this . buffer . slice ( this . curResponseLength ) ) ;
85- if ( plusResponse . ended )
86- {
87-
88- }
89- this . buffer = this . buffer . slice ( 0 , length ) ;
90- }
91- this . processing ( ) ;
80+ this . state = states . AWAITING ;
81+ this . awaitingResponseLength = trackResult [ 1 ] ;
82+ this . buffer = trackResult [ 0 ] ;
83+ }
84+ else
85+ {
86+ this . buffer = null ;
87+ this . state = states . CONNECTED ;
88+ }
89+ break ;
90+ case states . AWAITING :
91+ var trackResult = this . _responseBufferTrack ( Buffer . concat ( this . buffer , data ) , this . awaitingResponseLength ) ;
92+ if ( trackResult . length == 2 )
93+ {
94+ this . state = states . AWAITING ;
95+ this . awaitingResponseLength = trackResult [ 1 ] ;
96+ this . buffer = trackResult [ 0 ] ;
97+ }
98+ else
99+ {
100+ this . buffer = null ;
101+ this . state = states . CONNECTED ;
92102 }
93103 break ;
94104 }
95105} ;
96106
97- TarantoolConnection . prototype . _responseBufferTrack = function ( ) {
98-
107+ TarantoolConnection . prototype . _responseBufferTrack = function ( buffer , length ) {
108+ if ( ! length )
109+ {
110+ if ( buffer . length >= 5 )
111+ {
112+ length = buffer . readUIntBE ( 1 , 4 ) ;
113+ buffer = buffer . slice ( 5 ) ;
114+ }
115+ else
116+ return [ buffer , null ] ;
117+ }
118+ if ( buffer . length >= length )
119+ {
120+ if ( buffer . length == length )
121+ {
122+ this . _processResponse ( buffer ) ;
123+ return [ ] ;
124+ }
125+ else
126+ {
127+ var curBuffer = buffer . slice ( 0 , length ) ;
128+ this . _processResponse ( curBuffer ) ;
129+ return this . _responseBufferTrack ( buffer . slice ( length ) ) ;
130+ }
131+ }
132+ else
133+ return [ buffer , length ] ;
99134} ;
100135
101136TarantoolConnection . prototype . _processResponse = function ( buffer ) {
102137 try {
103138 var success = buffer [ 1 ] == 0x00 ;
104- console . log ( this . buffer ) ;
105- if ( this . success ) {
106- // add fixarraymap with 2 objects before main object
107- var dataBuffer = Buffer . concat ( [ new Buffer ( [ 0x92 ] ) , this . buffer . slice ( 5 ) ] ) ;
108- console . log ( dataBuffer ) ;
109- var obj = msgpack . unpack ( dataBuffer ) ;
110- console . log ( obj ) ;
111- }
112- else {
113- console . log ( 'its error' ) ;
114- }
139+ console . log ( buffer )
140+ // add fixarraymap with 2 objects before main object
141+ var dataBuffer = Buffer . concat ( [ new Buffer ( [ 0x92 ] ) , buffer ] ) ;
142+ console . log ( dataBuffer ) ;
143+ var obj = msgpack . unpack ( dataBuffer ) ;
144+ console . log ( obj ) ;
145+ var reqId = obj [ 0 ] [ 1 ] ;
146+ var task = this . commandsQueue . filter ( function ( t ) {
147+ return t [ 1 ] == reqId ;
148+ } ) [ 0 ] ;
149+ var dfd = task [ 2 ] ;
150+ if ( success )
151+ dfd . resolve ( this . _processResponseBody ( task [ 0 ] , obj [ 1 ] ) ) ;
152+ else
153+ dfd . reject ( obj [ 1 ] ) ;
115154 } catch ( e ) {
116155 console . log ( e , e . stack ) ;
117156 }
118157}
119158
159+ TarantoolConnection . prototype . _processResponseBody = function ( cmd , data ) {
160+ return data ;
161+ } ;
162+
163+ TarantoolConnection . prototype . _dropAll = function ( ) {
164+
165+ } ;
166+
120167TarantoolConnection . prototype . onConnect = function ( ) {
121168 this . state = states . PREHELLO ;
122169} ;
@@ -148,6 +195,34 @@ TarantoolConnection.prototype.ping = function(){
148195 return dfd . promise ( ) ;
149196} ;
150197
198+ TarantoolConnection . prototype . select = function ( spaceId , indexId , limit , offset , iterator , key ) {
199+ var dfd = vow . defer ( ) ;
200+ console . log ( 'start ping' ) ;
201+ var reqId = requestId . getId ( ) ;
202+ var header = this . _header ( tarantoolConstants . RequestCode . rqSelect , reqId ) ;
203+ console . log ( 'header' , header ) ;
204+ var buffered = {
205+ spaceId : msgpack . pack ( spaceId ) ,
206+ indexId : msgpack . pack ( indexId ) ,
207+ limit : msgpack . pack ( limit ) ,
208+ offset : msgpack . pack ( offset ) ,
209+ key : msgpack . pack ( key )
210+ } ;
211+ var body = Buffer . concat ( [ new Buffer ( [ 0x86 , tarantoolConstants . KeysCode . space_id ] ) , buffered . spaceId ,
212+ new Buffer ( [ tarantoolConstants . KeysCode . index_id ] ) , buffered . indexId ,
213+ new Buffer ( [ tarantoolConstants . KeysCode . limit ] ) , buffered . limit ,
214+ new Buffer ( [ tarantoolConstants . KeysCode . offset ] ) , buffered . offset ,
215+ new Buffer ( [ tarantoolConstants . KeysCode . iterator , tarantoolConstants . IteratorsType [ iterator ] ,
216+ tarantoolConstants . KeysCode . key ] ) ,
217+ buffered . key
218+ ] ) ;
219+ console . log ( body , msgpack . unpack ( body ) ) ;
220+ console . log ( buffered ) ;
221+ this . _request ( header , body ) ;
222+ this . commandsQueue . push ( [ tarantoolConstants . RequestCode . rqPing , reqId , dfd ] ) ;
223+ return dfd . promise ( ) ;
224+ } ;
225+
151226
152227TarantoolConnection . prototype . _header = function ( command , reqId ) {
153228 try {
0 commit comments