From b707a49f6f34fff0392facb6f6ff4b944f100e9b Mon Sep 17 00:00:00 2001 From: Olivier Bagot Date: Mon, 18 Nov 2013 20:32:46 +0100 Subject: [PATCH] Initial commit. Support for sleep, wakeup, reboot, get_version, get_free_space. --- emulator.bat | 2 + handlers.js | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 43 +++++++++++++++++++ log.js | 42 +++++++++++++++++++ router.js | 44 ++++++++++++++++++++ server.js | 47 +++++++++++++++++++++ 6 files changed, 293 insertions(+) create mode 100644 emulator.bat create mode 100644 handlers.js create mode 100644 index.js create mode 100644 log.js create mode 100644 router.js create mode 100644 server.js diff --git a/emulator.bat b/emulator.bat new file mode 100644 index 0000000..c8f8ae1 --- /dev/null +++ b/emulator.bat @@ -0,0 +1,2 @@ +@ECHO OFF +node . diff --git a/handlers.js b/handlers.js new file mode 100644 index 0000000..369c275 --- /dev/null +++ b/handlers.js @@ -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 = ''+ + ''+ + ''+ + 'OpenKarotz Emulator'+ + ''+ + ''+ + '

OpenKarotz Emulator

'+ + '

API

'+ + ''+ + ''+ + ''; + + 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 + diff --git a/index.js b/index.js new file mode 100644 index 0000000..591ed1b --- /dev/null +++ b/index.js @@ -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); diff --git a/log.js b/log.js new file mode 100644 index 0000000..ec1ca43 --- /dev/null +++ b/log.js @@ -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; diff --git a/router.js b/router.js new file mode 100644 index 0000000..8a486af --- /dev/null +++ b/router.js @@ -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 diff --git a/server.js b/server.js new file mode 100644 index 0000000..aa6b225 --- /dev/null +++ b/server.js @@ -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