-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_path.html
425 lines (339 loc) · 14.7 KB
/
code_path.html
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<title>Code Path</title>
<!-- link to JQuery and DataTables javascript libraries -->
<script src="js/jquery-1.11.1.min.js"></script>
<script src="js/jquery.dataTables.min.js"></script>
<script src="js/dataTables.jqueryui.js"></script>
<!-- link to JQuery and DataTables CSS definitions -->
<link rel="stylesheet" href="css/jquery-ui.css"/>
<link rel="stylesheet" href="css/dataTables.jqueryui.css"/>
<!-- define an internal style -->
<style type="text/css">
.right {
float:right;
}
.loading {
color:CornflowerBlue;
}
</style>
<script type="text/javascript">
var websocket_port = "6464";
var service_port = "6467";
var service_name = "/cloudi/api/rpc/code_path.erl";
var add_path_service_name = "/cloudi/api/erlang/code_path_add";
var remove_path_service_name = "/cloudi/api/erlang/code_path_remove";
var request_type = "get";
var web_socket = undefined;
var websocket_url = "";
var interval_id = undefined;
function is(type, obj) {
if (obj === undefined)
return type === 'undefined';
else if (obj === null)
return type === 'null';
else
return type === Object.prototype.toString.call(obj).slice(8, - 1);
}
String.prototype.startsWith = function (str) {
return this.slice(0, str.length) == str;
};
// This function uses a web socket to get the current code path settings
function send() {
// if the web socket is undefined then display an error
if (web_socket == undefined) {
$('#connectionStatus').text("Not connected");
open();
}
// if the web socket is in an open status then send the request
if (web_socket.readyState == web_socket.OPEN) {
web_socket.send(request_type);
//console.log("Request (out): " + request_type);
}
}
// This function opens a web socket used to get the current code path settings
function open() {
if (!("WebSocket" in window)) {
$('#connectionStatus').text("Not connected");
alert("This browser does not support WebSockets");
return;
}
// open the websocket
$('#connectionStatus').text("Connecting");
websocket_url = "ws://" + $("#hostname").val() + ":" + websocket_port + service_name;
web_socket = new WebSocket(websocket_url);
console.log("Connecting to " + websocket_url);
// define function that is called when an error occurs with the websocket
web_socket.onerror = function () {
$('#connectionStatus').text("Error");
console.log("Websocket error");
// turn off the timed page refresh
if (interval_id != undefined) {
clearInterval(interval_id);
interval_id = undefined;
}
// hide the loading message
$('#loadingMessage').hide();
alert("Error connecting to websocket " + websocket_url);
};
// define function that is called when the web socket is opened
web_socket.onopen = function () {
$('#connectionStatus').text("Connected");
console.log("Connected");
send();
};
// define function that is called when data is available on the websocket
handle_message = function (data) {
if (data.startsWith("notification:")) {
// client state check to determine this is an incoming
// service request, not an incoming response
console.log("Request (in): " + data);
var response = "ok";
web_socket.send(response);
console.log("Response (in): " + response);
}
else {
//console.log("Response (out): " + data);
if (data != "got connect! yay!") {
// attempt to parse the data response
var start = 0;
var end = 0;
var code_path_entries = [];
var pathName = "";
do {
// parse the service id
start = data.indexOf("\"", end);
if (start > 0) {
end = data.indexOf(",", start);
if (end == - 1) {
pathName = data.substring(start + 1, data.indexOf("\"]", start));
}
else {
pathName = data.substring(start + 1, end - 1);
}
var codePathEntry = new Object([pathName]);
code_path_entries[code_path_entries.length] = codePathEntry;
}
}
while (start > 0 && end > 0);
// display the code path entries in the table
updateTable(code_path_entries);
// setup time interval to refresh page
if (interval_id == undefined) {
interval_id = setInterval("send();", 60000);
}
}
}
};
// define function that is called when a message is received on the web socket
web_socket.onmessage = function (evt) {
var data = evt.data;
if (is("Blob", data)) {
// for the example, treat binary as text
var reader = new FileReader();
reader.readAsText(data, "text/plain");
reader.onload = function (reader_evt) {
data = reader_evt.target.result;
handle_message(data);
};
}
else {
handle_message(data);
}
};
// define function that is called when the websocket is closed
web_socket.onclose = function () {
web_socket = undefined;
console.log("Connection closed");
};
}
// This function closes the web socket used to get the current code path settings
function close() {
$('#connectionStatus').text("Not connected");
if (web_socket == undefined) {
return;
}
web_socket.close();
}
// Toggle buttons visibility when an entry in the table is selected
function toggle_manage_buttons() {
// always show the add button
$('#add_button').show();
var codePath = $('#codePath').text();
if (codePath.length > 0) {
$('#remove_button').show();
}
else {
$('#remove_button').hide();
}
}
function createCORSRequest(method, url) {
console.log("Creating CORS Request " + method + " " + url);
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
}
else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
console.log("XHR not supported by this browser");
}
return xhr;
}
// This function submits an HTTP request to remove a code path
function remove_path(codePath) {
var remove_path_service_url = "http://" + $("#hostname").val() + ":" + service_port + remove_path_service_name;
var xhr = createCORSRequest('POST', remove_path_service_url);
if (!xhr) {
throw new Error('CORS not supported');
}
xhr.onload = function () {
var responseText = xhr.responseText;
console.log(responseText);
// refresh the information on the page
send();
};
xhr.onerror = function (xhr, textStatus, errorThrown) {
console.log('There was an error. Status ' + xhr.status);
console.log('Error response=' + xhr.responseText);
// refresh the information on the page
send();
};
// clear the codePath
$('#codePath').text("");
// hide the manage code path buttons
toggle_manage_buttons();
xhr.send('\"' + codePath + '\"');
}
// This function submits an HTTP request to add a code path
function add_path(codePath) {
var add_path_service_url = "http://" + $("#hostname").val() + ":" + service_port + add_path_service_name;
var xhr = createCORSRequest('POST', add_path_service_url);
if (!xhr) {
throw new Error('CORS not supported');
}
xhr.onload = function () {
var responseText = xhr.responseText;
console.log(responseText);
// refresh the information on the page
send();
};
xhr.onerror = function () {
console.log('There was an error!');
// refresh the information on the page
send();
};
// clear the code path
$('#codePath').text("");
// hide the manage code path buttons
toggle_manage_buttons();
xhr.send('\"' + codePath + '\"');
}
</script>
</head>
<body>
<script type="text/javascript">
// This function is called by the server when data is ready to be processed
function updateTable(codePathEntries) {
// clear table
var oTable = $('#codePathTable').dataTable();
oTable.fnClearTable();
// process each object in the array
for (i = 0;i < codePathEntries.length;i++) {
codePathEntry = codePathEntries[i];
oTable.fnAddData([codePathEntry[0]]);
}
// hide the loading message
$('#loadingMessage').hide();
}
</script>
<div id="logo">
<img class="right" src="powered_by_cloudi.png"></img>
<h1>Code Path</h1>
</div>
<div id="loadingMessage">
<h2 class="loading">Waiting for data...</h2>
</div>
<div>
<input id="hostname" type="text" value="localhost" title="Host name or address to be monitored"></input>
<button class="left" onclick="send();">Refresh Now</button>
<label hidden="true" id="codePath"></label>
<label class="right" id="connectionStatus"></label>
<button id="remove_button" title="Remove selected directory from the CloudI Erlang VM code server's search paths. This doesn't impact any running services, only services that will be started in the future">Remove Path</button>
<button id="add_button" title="Add a directory to the CloudI Erlang VM code server's search paths. The path is always appended to the list of search paths">Add Path</button>
</div>
<table id="codePathTable" class="display">
<thead>
<tr>
<th>Directory Path</th>
</tr>
</thead>
<tbody id="codePathTableBody"></tbody>
</table>
<script type="text/javascript">
$(document).ready(function () {
// assign default hostname
$("#hostname").val(location.host);
// change the table attributes to support jQuery styles
$('#codePathTable').dataTable( {
"bJQueryUI" : true, "sPaginationType" : "full_numbers"
});
// clear the table upon first displaying the page
var oTable = $('#codePathTable').dataTable();
oTable.fnClearTable();
// show the loading message
$('#loadingMessage').show();
// set the connection status
$('#connectionStatus').text("Not connected");
// hide the manage code path buttons
toggle_manage_buttons();
// call the service request function now
send();
// define a function that will highlight a row when selected
$('#codePathTable tbody').on('click', 'tr', function () {
// toggle the row highlighting color
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
}
else {
oTable.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
var table_api = $('#codePathTable').dataTable().api();
// save the code path in a hidden HTML object for use later
var codePathEntry = table_api.row(this).data();
console.log("codePath is " + codePathEntry[0]);
$('#codePath').text(codePathEntry[0]);
// hide the manage code paths buttons
toggle_manage_buttons();
});
});
// define a function to add a new code path
$('#add_button').click(function () {
var codePath = prompt("Enter new code path", "");
if (codePath != null && codePath.length > 0) {
console.log("Code Path is " + codePath);
add_path(codePath);
}
});
// define a function to remove the selected code path
$('#remove_button').click(function () {
var codePath = $('#codePath').text();
if (codePath.length > 0) {
console.log("Code Path is " + codePath);
remove_path(codePath);
}
});
</script>
</body>
</html>