Initial commit.
Support for sleep, wakeup, reboot, get_version, get_free_space.
This commit is contained in:
parent
d4e5973b49
commit
b707a49f6f
2
emulator.bat
Normal file
2
emulator.bat
Normal file
@ -0,0 +1,2 @@
|
||||
@ECHO OFF
|
||||
node .
|
115
handlers.js
Normal file
115
handlers.js
Normal file
@ -0,0 +1,115 @@
|
||||
/*!
|
||||
* OpenKarotz Emulator
|
||||
*
|
||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* http://opensource.org/licenses/MIT
|
||||
*
|
||||
*/
|
||||
|
||||
var url = require('url');
|
||||
var querystring = require('querystring');
|
||||
var fs = require('fs');
|
||||
var log = require('./log');
|
||||
|
||||
function start(res, req) {
|
||||
log.trace('start: begin');
|
||||
|
||||
var body = '<html>'+
|
||||
'<head>'+
|
||||
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'+
|
||||
'<title>OpenKarotz Emulator</title>'+
|
||||
'</head>'+
|
||||
'<body>'+
|
||||
'<h1>OpenKarotz Emulator</h1>'+
|
||||
'<h2>API</h2>'+
|
||||
'<ul>'+
|
||||
'<li><a target="results" href="/cgi-bin/sleep">sleep</a></li>'+
|
||||
'<li><a target="results" href="/cgi-bin/wakeup?silent=0">wakeup</a></li>'+
|
||||
'<li><a target="results" href="/cgi-bin/reboot">reboot</a></li>'+
|
||||
'<li><a target="results" href="/cgi-bin/get_version">get_version</a></li>'+
|
||||
'<li><a target="results" href="/cgi-bin/get_free_space">get_free_space</a></li>'+
|
||||
'</ul>'+
|
||||
'</body>'+
|
||||
'</html>';
|
||||
|
||||
res.writeHead(200, {'Content-Type' : 'text/html'})
|
||||
res.write(body)
|
||||
res.end()
|
||||
log.trace('start: end')
|
||||
}
|
||||
exports.start = start
|
||||
|
||||
function sendResponse(res, data) {
|
||||
log.trace('sendResponse: ' + data);
|
||||
res.writeHead(200, {'Content-Type' : 'text/plain'})
|
||||
res.write(data)
|
||||
res.end()
|
||||
}
|
||||
|
||||
function getParameter(req, param) {
|
||||
var qs = url.parse(req.url).query;
|
||||
return querystring.parse(qs)[param];
|
||||
}
|
||||
|
||||
function sleep(res, req) {
|
||||
log.trace('sleep: begin');
|
||||
var data1 = '{"return":"0"}';
|
||||
var data2 = '{"return":"1","msg":"Unable to perform action, rabbit is already sleeping."}';
|
||||
// TODO: maintain sleep state
|
||||
sendResponse(res, data1);
|
||||
log.trace('sleep: end');
|
||||
}
|
||||
exports.sleep = sleep
|
||||
|
||||
function wakeup(res, req) {
|
||||
log.trace('wakeup: begin');
|
||||
var silent = getParameter(req, "silent");
|
||||
var data = '{"return":"0","silent":"' + silent + '"}';
|
||||
sendResponse(res, data);
|
||||
log.trace('wakeup: end');
|
||||
}
|
||||
exports.wakeup = wakeup
|
||||
|
||||
function reboot(res, req) {
|
||||
log.trace('reboot: begin');
|
||||
var data = '{"return":"0"}';
|
||||
sendResponse(res, data);
|
||||
log.trace('reboot: end');
|
||||
}
|
||||
exports.reboot = reboot
|
||||
|
||||
function get_version(res, req) {
|
||||
log.trace('get_version: begin');
|
||||
var data = '{"version":"200","return":"0"}';
|
||||
sendResponse(res, data);
|
||||
log.trace('get_version: end');
|
||||
}
|
||||
exports.get_version = get_version
|
||||
|
||||
function get_free_space(res, req) {
|
||||
log.trace('get_free_space: begin');
|
||||
var data = '{"karotz_percent_used_space":"45","usb_percent_used_space":"50"}';
|
||||
sendResponse(res, data);
|
||||
log.trace('get_free_space: end');
|
||||
}
|
||||
exports.get_free_space = get_free_space
|
||||
|
43
index.js
Normal file
43
index.js
Normal file
@ -0,0 +1,43 @@
|
||||
/*!
|
||||
* OpenKarotz Emulator
|
||||
*
|
||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* http://opensource.org/licenses/MIT
|
||||
*
|
||||
*/
|
||||
|
||||
var log = require('./log');
|
||||
var server = require('./server');
|
||||
var router = require('./router');
|
||||
var handlers = require('./handlers')
|
||||
|
||||
var handle = {}
|
||||
handle['/'] = handlers.start;
|
||||
handle['/cgi-bin'] = handlers.start;
|
||||
handle['/cgi-bin/sleep'] = handlers.sleep;
|
||||
handle['/cgi-bin/wakeup'] = handlers.wakeup;
|
||||
handle['/cgi-bin/reboot'] = handlers.reboot;
|
||||
handle['/cgi-bin/get_version'] = handlers.get_version;
|
||||
handle['/cgi-bin/get_free_space'] = handlers.get_free_space;
|
||||
|
||||
log.info('OpenKarotz Emulator');
|
||||
server.start(router.route, handle);
|
42
log.js
Normal file
42
log.js
Normal file
@ -0,0 +1,42 @@
|
||||
/*!
|
||||
* OpenKarotz Emulator
|
||||
*
|
||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* http://opensource.org/licenses/MIT
|
||||
*
|
||||
*/
|
||||
|
||||
function trace(message) {
|
||||
console.log(new Date() + ' [trace] ' + message);
|
||||
}
|
||||
|
||||
function info(message) {
|
||||
console.log(new Date() + ' [info] ' + message);
|
||||
}
|
||||
|
||||
function failure(message) {
|
||||
console.error(new Date() + ' [failure] ' + message);
|
||||
}
|
||||
|
||||
exports.trace = trace;
|
||||
exports.info = info;
|
||||
exports.failure = failure;
|
44
router.js
Normal file
44
router.js
Normal file
@ -0,0 +1,44 @@
|
||||
/*!
|
||||
* OpenKarotz Emulator
|
||||
*
|
||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* http://opensource.org/licenses/MIT
|
||||
*
|
||||
*/
|
||||
|
||||
var log = require('./log');
|
||||
|
||||
function route(pathname, handle, res, req) {
|
||||
|
||||
log.trace('Route a request for ' + pathname);
|
||||
|
||||
if (typeof handle[pathname] === 'function') {
|
||||
return handle[pathname](res, req);
|
||||
} else {
|
||||
log.failure('No request handler found for ' + pathname);
|
||||
res.writeHead(404, {'Content-Type' : 'text/plain'});
|
||||
res.write('404 Not Found');
|
||||
res.end();
|
||||
}
|
||||
}
|
||||
|
||||
exports.route = route
|
47
server.js
Normal file
47
server.js
Normal file
@ -0,0 +1,47 @@
|
||||
/*!
|
||||
* OpenKarotz Emulator
|
||||
*
|
||||
* Copyright (c) 2013 Olivier Bagot (http://github.com/hobbe/openkarotz-emulator)
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* http://opensource.org/licenses/MIT
|
||||
*
|
||||
*/
|
||||
|
||||
var http = require('http')
|
||||
var url = require('url');
|
||||
var log = require('./log');
|
||||
var port = 80;
|
||||
|
||||
function start(route, handle) {
|
||||
|
||||
function onRequest(req, res) {
|
||||
var pathname = url.parse(req.url).pathname;
|
||||
|
||||
if (pathname !== '/favicon.ico') {
|
||||
route(pathname, handle, res, req)
|
||||
}
|
||||
}
|
||||
|
||||
http.createServer(onRequest).listen(port)
|
||||
log.info('Server has started on http://localhost:' + port)
|
||||
}
|
||||
|
||||
exports.start = start
|
Loading…
x
Reference in New Issue
Block a user