Skip to content

Commit

Permalink
updated to work with new frame datamodel
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwohl committed May 10, 2015
1 parent f7575c8 commit 9065bbe
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
bower_components
bower_components
conf.json
166 changes: 116 additions & 50 deletions frame.js
Original file line number Diff line number Diff line change
@@ -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>', 'Username to which this frame will be linked.')
.option('-d, --dom <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>', 'Username to which this frame will be linked.')
.option('-f, --framename <framename>', 'Name for the frame.')
.option('-d, --dom <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");
}
}
}



3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
21 changes: 14 additions & 7 deletions static/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -72,6 +78,7 @@
});
}
};

ws.onclose = function(evt) {
console.log('connection closed');
connected = false;
Expand Down

0 comments on commit 9065bbe

Please sign in to comment.