From 9065bbedfafff3cd108076561e826dc0d1af2f1f Mon Sep 17 00:00:00 2001 From: Jonathan Wohl Date: Sun, 10 May 2015 00:09:14 -0400 Subject: [PATCH] updated to work with new frame datamodel --- .gitignore | 3 +- frame.js | 166 ++++++++++++++++++++++++++++++++-------------- package.json | 3 + static/frame.html | 21 ++++-- 4 files changed, 135 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index bb93d68..aab4b68 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -bower_components \ No newline at end of file +bower_components +conf.json \ No newline at end of file diff --git a/frame.js b/frame.js index 857834b..b24b6ab 100755 --- a/frame.js +++ b/frame.js @@ -1,72 +1,138 @@ #! /usr/local/bin/node -var program = require('commander') - +var program = require('commander'); + program - .version('0.0.1') - .option('-u, --username ', 'Username to which this frame will be linked.') - .option('-d, --dom ', 'The root domain (including port) at which the frame server is accessible. Defaults to localhost:8888.') - .option('-c, --chromium', 'If this flag is present, force the use chromium.') - .parse(process.argv) + .version('0.0.1') + .option('-u, --username ', 'Username to which this frame will be linked.') + .option('-f, --framename ', 'Name for the frame.') + .option('-d, --dom ', 'The root domain (including port) at which the frame server is accessible. Defaults to localhost:8888.') + .option('-r, --reset', 'If present, reset the configuration, i.e. treat this as an entirely new frame.') + .option('-c, --chromium', 'If this flag is present, force the use chromium.') + .parse(process.argv); if (!program.username) { - console.log('Username is required.') - program.outputHelp() - process.exit() + console.log('Username is required.'); + program.outputHelp(); + process.exit(); } else { - console.log('Starting as ' + program.username); + console.log('Starting as ' + program.username); } var username = program.username, - root_domain = program.dom || "localhost:8888", - chromium = program.chromium; + root_domain = program.dom || "localhost:8888", + chromium = program.chromium, + reset = program.reset, + framename = program.framename || 'New Frame'; + +var connect = require('connect'); +var http = require('http'); +var serveStatic = require('serve-static'); +var open = require('open'); +var uuid = require('node-uuid'); +var exec = require('child_process').exec; +var fs = require('fs'); +var request = require('request-json'); +var client = request.createClient('http://'+root_domain); +var conf; + + +// If reset flag is present, remove the conf +if (reset) fs.unlinkSync('./conf.json'); -var connect = require('connect') -var http = require('http') -var serveStatic = require('serve-static') -var open = require('open') -var exec = require('child_process').exec - -var app = connect() +var app = connect(); app.use('/config', function(req, res, next) { - var resp = { - root_domain: root_domain, - username: username - } - res.end(JSON.stringify(resp)); + res.end(JSON.stringify(conf)); }); -app.use(serveStatic('./static', {'index': ['frame.html']})) +app.use(serveStatic('./static', { + 'index': ['frame.html'] +})); + + +// try to get frame conf from local file: +try { + conf = require('./conf.json'); + if (conf.owner !== username || conf.name !== framename) { + // TODO: if the supplied user or framename don't match the conf, update the frame on the server? + var frame = { + owner: username, + users: [username], + name: framename + }; + client.put('/frames/'+conf._id.$oid, frame, function(err, res, body) { + if (err) console.log(err); + console.log("body", body); + if (res.statusCode === 200) { + createConfigFile(body); + conf = body; + conf.root_domain = root_domain; + startServer(); + } + }); + } else { + conf.root_domain = root_domain; + if (conf) startServer(); + } +} catch (e) { + // no conf file, create new frame and save resulting conf + var frame = { + owner: username, + users: [username], + name: framename, + active: false + }; + + client.post('/frames', frame, function(err, res, body) { + if (err) console.log(err); + console.log("body", body); + if (res.statusCode === 200) { + createConfigFile(body); + conf = body; + conf.root_domain = root_domain; + startServer(); + } + }); +} -function getUser() { - return username; +function createConfigFile(conf) { + fs.writeFile('conf.json', JSON.stringify(conf), function(err) { + if (err) { + console.log(err); + } + }); } -// respond to all requests -// app.use(function(req, res){ -// res.end('Hello from Connect!\n'); -// }) - + + //create node.js http server and listen on port -http.createServer(app).listen(7000) - -// if we're on linux, let's try to open the thing with chromium in kiosk mode -if(/^linux/.test(process.platform)) { - console.log('linux', process.platform); - var xinitrc_path = __dirname + '/bin/.xinitrc'; - exec('xinit ' + xinitrc_path, function (error, stdout, stderr) { - console.log(error, stdout, stderr); - }); -} else { - console.log('not linux', process.platform); - if(chromium) { - exec('/Applications/Chromium.app/Contents/MacOS/Chromium --kiosk http://localhost:7000', function (error, stdout, stderr) { - console.log(error, stdout, stderr); - }); +function startServer() { + http.createServer(app).listen(7000); + launchFrame(); +} + + +function launchFrame() { + // if we're on linux, let's try to open the thing with chromium in kiosk mode + if (/^linux/.test(process.platform)) { + console.log('linux', process.platform); + var xinitrc_path = __dirname + '/bin/.xinitrc'; + exec('xinit ' + xinitrc_path, function(error, stdout, stderr) { + console.log(error, stdout, stderr); + }); } else { - open("http://localhost:7000"); - } + console.log('not linux', process.platform); + if (chromium) { + exec('/Applications/Chromium.app/Contents/MacOS/Chromium --kiosk http://localhost:7000', function(error, stdout, stderr) { + console.log(error, stdout, stderr); + }); + } else { + open("http://localhost:7000"); + } + } } + + diff --git a/package.json b/package.json index 34d3030..2c60121 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,10 @@ "dependencies": { "commander": "^2.7.1", "connect": "^3.3.5", + "node-uuid": "^1.4.3", "open": "0.0.5", + "request": "^2.55.0", + "request-json": "^0.5.3", "serve-static": "^1.9.2" } } diff --git a/static/frame.html b/static/frame.html index 29d52f6..7e6b79d 100644 --- a/static/frame.html +++ b/static/frame.html @@ -31,31 +31,37 @@ var $body = $('body'), connected = false, domain, - user; + username, + framename, + frame_id; $.getJSON('http://localhost:7000/config', function(resp) { // resp should contain the username and root_domain domain = resp.root_domain; - user = resp.username; + username = resp.owner; + framename = resp.name; + frame_id = resp._id.$oid; setInterval(check_connection, 5000); check_connection(); }); function check_connection() { if (!connected) { - connect_ws(domain, user); + connect_ws(domain, frame_id); } } // initializes WebSocket connection - function connect_ws(root_domain, username) { - console.log('init WebSocket connection as ' + username); - var ws = new WebSocket("ws://" + root_domain + "/ws/" + username); + function connect_ws(root_domain, frame_id) { + console.log('init WebSocket connection for this frame ' + frame_id); + var ws = new WebSocket("ws://" + root_domain + "/ws/" + frame_id); + ws.onopen = function() { console.log('connection open'); - ws.send("connecting as " + username); + ws.send("connecting as " + frame_id); connected = true; }; + ws.onmessage = function(evt) { var data = JSON.parse(evt.data); console.log(data); @@ -72,6 +78,7 @@ }); } }; + ws.onclose = function(evt) { console.log('connection closed'); connected = false;