Skip to content

Commit

Permalink
added loading of client file list
Browse files Browse the repository at this point in the history
still needs to be debugged.  For #22
  • Loading branch information
DepthDeluxe committed Jul 28, 2014
1 parent ff3400e commit 3496a30
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
17 changes: 10 additions & 7 deletions server/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ module.exports = function(grunt) {
});
grunt.loadNpmTasks('grunt-nodemon');

grunt.registerTask('prepare', function() {
child_process.spawn('mkdir', ['./data']);
});

grunt.registerTask('clean', function() {
try {
fs.rmdirSync('./data');
} catch(err) {
}
fs.mkdirSync('./data');
child_process.spawn('rm', ['-rf', ['./data']]);

// prepare the space again
grunt.task.run(['prepare']);
});

grunt.registerTask('run', function() {
grunt.task.run(['clean']);
grunt.task.run(['prepare']);

// start up the server
var Server = require('./app');
Expand All @@ -60,7 +63,7 @@ module.exports = function(grunt) {
PythonControl.app = "my_app.py";
}

grunt.task.run(['clean', 'nodemon']);
grunt.task.run(['prepare', 'nodemon']);
});
};

Expand Down
28 changes: 26 additions & 2 deletions server/src/model/BeamServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var ex = require('../exceptions');

function BeamServer() {
this.clients = {};

// attempt a load from memory
this.load();
}

BeamServer.prototype.connect = function(name, url) {
Expand Down Expand Up @@ -54,8 +57,10 @@ BeamServer.prototype.eventify = function(client) {
BeamServer.prototype.save = function() {
var clientList = [];
_.forEach(this.clients, function(client) {
logger.info(client.name);
logger.info(client.url);
if (client.name === undefined || client.url === undefined) {
return;
}

clientList.push({
name: client.name,
url: client.url
Expand All @@ -66,6 +71,25 @@ BeamServer.prototype.save = function() {
return fs.writeFileAsync('./data/clients.json', data);
};

BeamServer.prototype.load = function() {
var self = this;

// connect to every file found in clients.json
return fs.readFileAsync('./data/clients.json')
.then(function(data) {
data = JSON.stringify(data);

_.forEach(data, function(client) {
self.connect(client.name, client.url);
});
})
.catch(function(err) {
logger.warn(err);
logger.warn('Failed to find a clients.json');
logger.warn('This is OK if the app is being run for the first time');
});
};

function waitForConnect(client) {
return Promise.delay(500)
.then(function() {
Expand Down

0 comments on commit 3496a30

Please sign in to comment.