forked from cj/ibug
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
161 lines (127 loc) · 4.75 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Node documentation: http://nodejs.org/api.html
// Based on node_chat: http://github.com/ry/node_chat
var queue = require("./queue");
var fu = require("./fu");
var sys = require("sys");
var url = require("url");
var qs = require("querystring");
var HOST = undefined; // localhost
var PORT = 8001;
if (process.ARGV.length > 2) {
var _url;
// if its just an init then port#
//if (process.ARGV[2].parseInt())
var _url = url.parse("http://" + process.ARGV[2]);
HOST = _url.hostname;
PORT = _url.port;
}
var escapeJS = function (s) {
return s.replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "")
}
var channel = new function () {
var client = { queue: {}, callback: null, queue: new queue.queue() };
var console = { queue: {}, callback: null, queue: new queue.queue() };
this.getQueue = function (queueName) {
return (queueName == "client" && client) || console;
}
// Message are broken into bits, so queue them until we recieve all the bits.
this.queue = function (queueName, encodedBit, messageNumber, bitLength, bitNumber) {
var queue = this.getQueue(queueName);
// [bit1, bit2, bit3, ..., bitx, number_of_bits_recieved]
if (!queue[messageNumber]) {
queue[messageNumber] = [];
queue[messageNumber][bitLength] = 1;
} else {
queue[messageNumber][bitLength]++;
}
queue[messageNumber][bitNumber] = encodedBit
// If all bits in a message are here, send it.
if (queue[messageNumber][bitLength] >= bitLength)
{
var message;
// Don't send # of bits recieved.
queue[messageNumber].pop();
var message = queue[messageNumber].join("");
try {
message = decodeURIComponent(message);
} catch (err)
{
message += '... Error decoding URI Component';
}
delete queue[messageNumber];
if (queue.callback)
{
var callback = queue.callback;
queue.callback = null;
callback(message);
}
else
{
queue.queue.Enqueue(message);
}
}
// else
// {
// if (!queue.callback)
// {
// sys.puts('No callback set');
// }
// else
// {
// sys.puts('Waiting for more bits, queueBitlength: ' + queue[messageNumber][bitLength] + ' requested: ' + bitLength);
// }
// }
};
this.listen = function (queueName, callback) {
// sys.puts('All bits are here, sending message');
var queue = this.getQueue(queueName);
if (queue.queue.IsEmpty())
{
queue.callback = callback;
}
else
{
callback(queue.queue.Dequeue());
}
};
};
fu.listen(PORT, HOST);
fu.get("/", fu.staticHandler("firebug.html"));
fu.get("/firebug.css", fu.staticHandler("firebug.css"));
fu.get("/firebug.js", fu.staticHandler("firebug.js"));
fu.get("/ibug.js", fu.staticHandler("ibug.js"));
fu.get("/warningIcon.png", fu.staticHandler("warningIcon.png"));
fu.get("/errorIcon.png", fu.staticHandler("errorIcon.png"));
fu.get("/infoIcon.png", fu.staticHandler("infoIcon.png"));
fu.get("/response", function (req, res) {
// HACK: The message has been split, so don't decode it until it's all here.
var m = req.url.match(/\?n=(.*?)&l=(.*?)&b=(.*?)&m=(.*)/);
var messageNumber = m[1];
var bitLength = m[2]
var bitNumber = m[3];
var encodedBit = m[4];
sys.puts("Queing Message " + messageNumber + ", encodedBit: " + encodedBit + " bitLength: " + bitLength + " bitNumber: " + bitNumber + "...");
channel.queue("console", encodedBit, messageNumber, bitLength, bitNumber);
res.simpleText(200, 'OK');
});
fu.get("/command", function (req, res) {
// HACK: The message has been split, so don't decode it until it's all here.
var m = req.url.match(/\?n=(.*?)&l=(.*?)&b=(.*?)&m=(.*)/);
var messageNumber = m[1];
var bitLength = m[2]
var bitNumber = m[3];
var encodedBit = m[4];
channel.queue("client", encodedBit, messageNumber, bitLength, bitNumber);
res.simpleJSON(200, {});
});
fu.get("/client", function (req, res) {
channel.listen("client", function (message) {
var s = qs.parse(url.parse(req.url).query).s;
res.simpleScript(200, "parent.console.command('" + escapeJS(message) + "'); parent.console.lastScriptLoaded = " + s + ";");
});
});
fu.get("/console", function (req, res) {
channel.listen("console", function (message) {
res.simpleScript(200, "parent.command('" + escapeJS(message) + "');");
});
});