@@ -3,56 +3,67 @@ var sys = require('sys'),
3
3
exceptions = require ( './stomp-exceptions' ) ;
4
4
5
5
Frame = module . exports = function ( logger ) {
6
+ this . me = Math . floor ( Math . random ( ) * 10000000 + 1 ) ;
6
7
this . sock = null ;
7
8
this . command = null ;
8
9
this . headers = null ;
9
10
this . body = null ;
11
+ this . reply = null ;
10
12
this . session = null ;
11
13
this . stomp_log = logger ;
12
14
this . rqueue = new StompQueue ( ) ;
13
15
this . iqueue = new IntermediateQueue ( ) ;
16
+ this . utils = new StompUtils ( ) ;
14
17
} ;
15
18
16
19
Frame . prototype . stomp_connect = function ( client ) {
17
- this . sock = client ;
18
- var args = { } ;
19
- var headers = { } ;
20
- var next_frame = null ;
20
+ var self = this ,
21
+ args = { } ,
22
+ headers = { }
23
+ next_frame = null ;
21
24
25
+ this . sock = client ;
22
26
args [ 'command' ] = 'CONNECT' ;
23
27
args [ 'headers' ] = headers ;
24
- frame_to_send = this . build_frame ( args ) ;
25
- this . send_frame ( frame_to_send ) ;
28
+ frame_to_send = this . build_frame ( args , true ) ;
29
+ parsed_frame = this . send_frame ( frame_to_send ) ;
26
30
this . stomp_log . debug ( 'Connected to STOMP' ) ;
27
- next_frame = this . get_reply ( ) ;
28
- this . session = next_frame . headers ;
31
+ this . stomp_log . debug ( 'headers: ' + sys . inspect ( this . headers ) ) ;
32
+ if ( 'session' in this . headers )
33
+ this . session = this . headers [ 'session' ] ;
34
+
29
35
return this ;
30
36
} ;
31
37
32
38
Frame . prototype . build_frame = function ( args , want_receipt ) {
39
+ var self = this ,
40
+ receipt_stamp = null ;
41
+
33
42
this . command = args [ 'command' ] ;
34
43
this . headers = args [ 'headers' ] ;
35
44
this . body = args [ 'body' ] ;
36
- var receipt_stamp = null ;
37
45
38
46
if ( want_receipt ) {
39
47
receipt_stamp = Math . floor ( Math . random ( ) * 10000000 + 1 ) ;
40
- this . headers [ 'receipt' ] = this . session [ 'session' ] + '-' + receipt_stamp ;
41
- this . stomp_log . debug ( want_receipt ) ;
48
+ if ( this . session != null ) {
49
+ this . headers [ 'receipt' ] = receipt_stamp + "-" + this . session ;
50
+ }
51
+ else {
52
+ this . headers [ 'receipt' ] = receipt_stamp + "-" ;
53
+ }
42
54
}
43
-
44
55
return this ;
45
56
} ;
46
57
47
58
Frame . prototype . as_string = function ( ) {
48
- var header_strs = Array ( ) ,
59
+ var self = this ,
60
+ header_strs = Array ( ) ,
49
61
frame = null ,
50
62
command = this . command ,
51
63
headers = this . headers ,
52
64
body = this . body ;
53
65
54
66
for ( var header in headers ) {
55
- this . stomp_log . debug ( header ) ;
56
67
header_strs . push ( header + ':' + headers [ header ] + '\n' ) ;
57
68
}
58
69
@@ -62,6 +73,7 @@ Frame.prototype.as_string = function() {
62
73
} ;
63
74
64
75
Frame . prototype . send_frame = function ( frame ) {
76
+ self = this ;
65
77
this . sock . write ( frame . as_string ( ) ) ;
66
78
67
79
if ( 'receipt' in frame . headers )
@@ -70,11 +82,12 @@ Frame.prototype.send_frame = function(frame) {
70
82
} ;
71
83
72
84
Frame . prototype . parse_frame = function ( data ) {
73
- var args = [ ] ,
85
+ var self = this ,
86
+ args = [ ] ,
74
87
headers_str = null ;
75
88
76
- if ( data == null || data == undefined )
77
- return ;
89
+ if ( ! this . utils . really_defined ( data ) )
90
+ return null ;
78
91
79
92
this . command = this . parse_command ( data ) ;
80
93
var _data = data . slice ( this . command . length + 1 , data . length ) ;
@@ -95,20 +108,23 @@ Frame.prototype.parse_frame = function(data) {
95
108
args [ 'body' ] = this . body ;
96
109
97
110
this_frame = new Frame ( this . sock ) ;
98
- return this_frame . build_frame ( args ) ;
111
+ return_frame = this_frame . build_frame ( args ) ;
112
+ return return_frame ;
99
113
} ;
100
114
101
115
Frame . prototype . parse_headers = function ( headers_str ) {
102
- var these_headers = Array ( ) ,
103
- one_header = Array ( ) ,
116
+ var these_headers = new Array ( ) ,
117
+ one_header = new Array ( ) ,
118
+ header_key = null ,
119
+ header_val = null ,
104
120
headers_split = headers_str . split ( '\n' ) ;
105
121
122
+
106
123
for ( var i = 0 ; i < headers_split . length ; i ++ ) {
107
124
one_header = headers_split [ i ] . split ( ':' ) ;
108
-
109
125
if ( one_header . length > 1 ) {
110
- var header_key = one_header . shift ( ) ;
111
- var header_val = one_header . join ( ':' ) ;
126
+ header_key = one_header . shift ( ) ;
127
+ header_val = one_header . join ( ':' ) ;
112
128
these_headers [ header_key ] = header_val ;
113
129
}
114
130
else {
@@ -126,32 +142,36 @@ Frame.prototype.parse_command = function(data) {
126
142
} ;
127
143
128
144
Frame . prototype . get_reply = function ( ) {
145
+ var self = this ;
146
+
129
147
while ( true ) {
130
148
try {
131
- return this . rqueue . get ( )
149
+ return self . rqueue . get ( )
132
150
}
133
151
catch ( error ) {
134
152
if ( error . name == "QueueEmpty" ) {
135
- frame = this . parse_frame ( ) ;
136
- if ( frame == null || frame == undefined )
153
+ this_frame = self . reply ;
154
+ if ( ! self . utils . really_defined ( this_frame ) )
137
155
return null ;
138
- if ( frame . command == "MESSAGE" )
139
- this . iqueue . put ( frame ) ;
156
+ if ( this_frame . command == "MESSAGE" )
157
+ return self . iqueue . put ( this_frame ) ;
140
158
else
141
- this . rqueue . put ( frame ) ;
159
+ return self . rqueue . put ( this_frame ) ;
142
160
}
143
161
}
144
162
}
145
163
} ;
146
164
147
165
Frame . prototype . get_message = function ( ) {
166
+ var self = this ;
167
+
148
168
while ( true ) {
149
- frame = this . rqueue . get ( )
150
- if ( frame == null || frame == undefined )
169
+ this_frame = this . rqueue . get ( )
170
+ if ( ! this . utils . really_defined ( this_frame ) )
151
171
return null ;
152
- if ( frame . command == "MESSAGE" )
172
+ if ( this_frame . command == "MESSAGE" )
153
173
return frame ;
154
174
else
155
- this . rqueue . put ( frame ) ;
175
+ this . rqueue . put ( this_frame ) ;
156
176
}
157
177
} ;
0 commit comments