Skip to content
This repository was archived by the owner on Nov 28, 2018. It is now read-only.

Commit 0ca4814

Browse files
committed
Getting serious. Use a cleaner js OO style. Start setting up utils (like logging).
1 parent eaf4633 commit 0ca4814

File tree

3 files changed

+117
-113
lines changed

3 files changed

+117
-113
lines changed

frame.js

Lines changed: 83 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,106 @@
11
var sys = require('sys');
22

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+
};
3511

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+
};
3825

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+
};
4237

43-
frame = command + '\n' + header_strs.join() + '\n' + body + '\x00';
38+
Frame.prototype.as_string = function() {
39+
header_strs = Array();
4440

45-
return frame;
46-
};
41+
for (var header in this.headers) {
42+
header_strs.push(header + ':' + headers[header] + '\n');
43+
}
4744

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';
5246

53-
this.parse_frame = function(data) {
54-
args = Array();
55-
headers = Array();
56-
headers_str = '';
57-
body = '';
47+
return frame;
48+
};
5849

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+
};
6353

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;
6757

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);
7161

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];
7565

76-
};
66+
args['command'] = this.command;
67+
args['headers'] = this.headers;
68+
args['body'] = this.body;
7769

78-
this.parse_headers = function(headers_str) {
70+
this_frame = new Frame(this.sock);
71+
return this_frame.build_frame(args);
7972

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) {
8676

87-
return my_headers;
77+
var these_headers = Array(),
78+
one_header = Array();
79+
var headers_split = headers_str.split("\n");
8880

89-
};
81+
for (var i = 0; i < headers_split.length; i++) {
82+
one_header = headers_split[i].split(":");
9083

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+
}
9292

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+
}
9794

98-
};
95+
return these_headers;
9996

10097
};
10198

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+
};

stomp.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
var net = require('net');
2-
var Frame = require('./frame');
1+
var net = require('net'),
2+
Frame = require('./frame');
3+
stomp_utils = require('./stomp-utils');
34

4-
function Stomp(port, host) {
5-
var port = port,
6-
host = host,
7-
frame = new Frame();
5+
Stomp = module.exports = function(port, host, debug) {
6+
this.port = port;
7+
this.host = host;
8+
this.socket_connected = false;
9+
this.debug = debug;
10+
this.stomp_log = new StompLogging(debug);
11+
this.frame = new Frame(this.stomp_log);
12+
this.connected_frame = null;
13+
};
814

9-
this.connect = function() {
15+
Stomp.prototype.connect = function() {
1016

11-
console.log('Connecting to ' + host + ':' + port);
12-
client = net.createConnection(port, host);
17+
this.stomp_log.debug('Connecting to ' + this.host + ':' + this.port);
18+
client = net.createConnection(this.port, this.host);
19+
var _stomp = this;
1320

14-
client.addListener('connect', function () {
15-
console.log('connected to socket');
16-
connected_frame = frame.stomp_connect(client);
17-
});
18-
client.addListener('data', function (data) {
19-
console.log("Got: " + data);
20-
connected_frame.parse_frame(data);
21-
});
22-
client.addListener('end', function () {
23-
console.log('goodbye');
24-
});
25-
client.addListener('error', function (error) {
26-
console.log(error);
27-
console.log("error");
28-
});
29-
client.addListener('close', function (error) {
30-
console.log("disconnected");
31-
});
32-
};
33-
}
34-
//stomp = new Stomp(61613, 'localhost');
35-
//stomp.connect();
36-
37-
module.exports = Stomp;
21+
client.addListener('connect', function () {
22+
_stomp.stomp_log.debug('connected to socket');
23+
_stomp.connected_frame = _stomp.frame.stomp_connect(client);
24+
});
25+
client.addListener('data', function (data) {
26+
_stomp.connected_frame.parse_frame(data);
27+
});
28+
client.addListener('end', function () {
29+
_stomp.stomp_log.debug('goodbye');
30+
});
31+
client.addListener('error', function (error) {
32+
console.log("error: " + error);
33+
});
34+
client.addListener('close', function (error) {
35+
_stomp.stomp_log.debug("disconnected");
36+
});
37+
};

stomp_client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var Stomp = require('./stomp');
22

3-
var client = new Stomp(61613, 'localhost');
3+
var client = new Stomp(61613, 'localhost', true);
44

55
client.connect();

0 commit comments

Comments
 (0)