|
1 | 1 | var sys = require('sys');
|
2 | 2 |
|
3 |
| -function Frame() { |
4 |
| - var connected = false, |
5 |
| - sock = '', |
6 |
| - command = '', |
7 |
| - headers = '', |
8 |
| - body = ''; |
9 |
| - |
10 |
| - this.stomp_connect = function (client) { |
11 |
| - connected = true; |
12 |
| - sock = client; |
13 |
| - var args = Array(); |
14 |
| - var headers = Array(); |
15 |
| - |
16 |
| - args['command'] = 'CONNECT'; |
17 |
| - args['headers'] = headers; |
18 |
| - frame_to_send = this.build_frame(args); |
19 |
| - this.send_frame(frame_to_send); |
20 |
| - console.log('connected to stomp'); |
21 |
| - return this; |
22 |
| - }; |
23 |
| - |
24 |
| - this.build_frame = function(args, want_receipt) { |
25 |
| - command = args['command']; |
26 |
| - headers = args['headers']; |
27 |
| - body = args['body']; |
28 |
| - if (want_receipt) { |
29 |
| - receipt_stamp = Math.floor(Math.random()*10000000+1); |
30 |
| - this.headers['receipt'] = "-" |
31 |
| - console.log(want_receipt); |
32 |
| - } |
33 |
| - return this; |
34 |
| - }; |
| 3 | +Frame = module.exports = function(logger) { |
| 4 | + this.connected = false; |
| 5 | + this.sock = null; |
| 6 | + this.command = null; |
| 7 | + this.headers = null; |
| 8 | + this.body = null; |
| 9 | + this.stomp_log = logger; |
| 10 | +}; |
35 | 11 |
|
36 |
| - this.as_string = function() { |
37 |
| - header_strs = Array(); |
| 12 | +Frame.prototype.stomp_connect = function(client) { |
| 13 | + this.connected = true; |
| 14 | + this.sock = client; |
| 15 | + var args = {}; |
| 16 | + var headers = {}; |
| 17 | + |
| 18 | + args['command'] = 'CONNECT'; |
| 19 | + args['headers'] = headers; |
| 20 | + frame_to_send = this.build_frame(args); |
| 21 | + this.send_frame(frame_to_send); |
| 22 | + this.stomp_log.debug('connected to STOMP'); |
| 23 | + return this; |
| 24 | +}; |
38 | 25 |
|
39 |
| - for (var header in headers) { |
40 |
| - header_strs.push(header + ':' + headers[header] + '\n'); |
41 |
| - } |
| 26 | +Frame.prototype.build_frame = function(args, want_receipt) { |
| 27 | + command = args['command']; |
| 28 | + headers = args['headers']; |
| 29 | + body = args['body']; |
| 30 | + if (want_receipt) { |
| 31 | + receipt_stamp = Math.floor(Math.random()*10000000+1); |
| 32 | + this.headers['receipt'] = "-" |
| 33 | + console.log(want_receipt); |
| 34 | + } |
| 35 | + return this; |
| 36 | +}; |
42 | 37 |
|
43 |
| - frame = command + '\n' + header_strs.join() + '\n' + body + '\x00'; |
| 38 | +Frame.prototype.as_string = function() { |
| 39 | + header_strs = Array(); |
44 | 40 |
|
45 |
| - return frame; |
46 |
| - }; |
| 41 | + for (var header in this.headers) { |
| 42 | + header_strs.push(header + ':' + headers[header] + '\n'); |
| 43 | + } |
47 | 44 |
|
48 |
| - this.send_frame = function(frame) { |
49 |
| - console.dir(sock); |
50 |
| - sock.write(frame.as_string()); |
51 |
| - }; |
| 45 | + frame = this.command + '\n' + header_strs.join() + '\n' + this.body + '\x00'; |
52 | 46 |
|
53 |
| - this.parse_frame = function(data) { |
54 |
| - args = Array(); |
55 |
| - headers = Array(); |
56 |
| - headers_str = ''; |
57 |
| - body = ''; |
| 47 | + return frame; |
| 48 | +}; |
58 | 49 |
|
59 |
| - console.dir("Data: " + data); |
60 |
| - command = this.parse_command(data); |
61 |
| - var _data = data.slice(command.length + 1, data.length); |
62 |
| - _data = _data.toString('utf8', start=0, end=_data.length); |
| 50 | +Frame.prototype.send_frame = function(frame) { |
| 51 | + this.sock.write(frame.as_string()); |
| 52 | +}; |
63 | 53 |
|
64 |
| - the_rest = _data.split("\n\n"); |
65 |
| - headers = this.parse_headers(the_rest[0]); |
66 |
| - body = the_rest[1]; |
| 54 | +Frame.prototype.parse_frame = function(data) { |
| 55 | + var args = []; |
| 56 | + var headers_str = null; |
67 | 57 |
|
68 |
| - args['command'] = command; |
69 |
| - args['headers'] = headers; |
70 |
| - args['body'] = body; |
| 58 | + this.command = this.parse_command(data); |
| 59 | + var _data = data.slice(this.command.length + 1, data.length); |
| 60 | + _data = _data.toString('utf8', start=0, end=_data.length); |
71 | 61 |
|
72 |
| - console.dir(args); |
73 |
| - this_frame = new Frame(sock); |
74 |
| - return this_frame.build_frame(args); |
| 62 | + the_rest = _data.split("\n\n"); |
| 63 | + this.headers = this.parse_headers(the_rest[0]); |
| 64 | + this.body = the_rest[1]; |
75 | 65 |
|
76 |
| - }; |
| 66 | + args['command'] = this.command; |
| 67 | + args['headers'] = this.headers; |
| 68 | + args['body'] = this.body; |
77 | 69 |
|
78 |
| - this.parse_headers = function(headers_str) { |
| 70 | + this_frame = new Frame(this.sock); |
| 71 | + return this_frame.build_frame(args); |
79 | 72 |
|
80 |
| - my_headers = Array(); |
81 |
| - headers_split = headers_str.split("\n"); |
82 |
| - for (var i = 0; i < headers_split.length; i++) { |
83 |
| - header = headers_split[i].split(":", 1); |
84 |
| - my_headers[header[0]] = header[1]; |
85 |
| - } |
| 73 | +}; |
| 74 | + |
| 75 | +Frame.prototype.parse_headers = function(headers_str) { |
86 | 76 |
|
87 |
| - return my_headers; |
| 77 | + var these_headers = Array(), |
| 78 | + one_header = Array(); |
| 79 | + var headers_split = headers_str.split("\n"); |
88 | 80 |
|
89 |
| - }; |
| 81 | + for (var i = 0; i < headers_split.length; i++) { |
| 82 | + one_header = headers_split[i].split(":"); |
90 | 83 |
|
91 |
| - this.parse_command = function(data) { |
| 84 | + if (one_header.length > 1) { |
| 85 | + var header_key = one_header.shift(); |
| 86 | + var header_val = one_header.join(':'); |
| 87 | + these_headers[header_key] = header_val; |
| 88 | + } |
| 89 | + else { |
| 90 | + these_headers[one_header[0]] = one_header[1]; |
| 91 | + } |
92 | 92 |
|
93 |
| - this_string = data.toString('ascii', start=0, end=data.length); |
94 |
| - command = this_string.split("\n"); |
95 |
| - console.log("Command: " + command[0]); |
96 |
| - return command[0]; |
| 93 | + } |
97 | 94 |
|
98 |
| - }; |
| 95 | + return these_headers; |
99 | 96 |
|
100 | 97 | };
|
101 | 98 |
|
102 |
| -module.exports = Frame; |
| 99 | +Frame.prototype.parse_command = function(data) { |
| 100 | + |
| 101 | + var command; |
| 102 | + this_string = data.toString('ascii', start=0, end=data.length); |
| 103 | + command = this_string.split("\n"); |
| 104 | + return command[0]; |
| 105 | + |
| 106 | +}; |
0 commit comments