Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophBodenstein authored and ChristophBodenstein committed Nov 13, 2016
2 parents 1760e12 + 34836e8 commit b9d90a8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 54 deletions.
1 change: 0 additions & 1 deletion TOEDistributionServer/src/node/tns/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ app.use(function(req,res, next){
app.use('/', routes);
app.use('/timenetws-server',routes);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
Expand Down
1 change: 1 addition & 0 deletions TOEDistributionServer/src/node/tns/data/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### This dir is used for mongodb if you use the given mongoconfig.conf.
111 changes: 58 additions & 53 deletions TOEDistributionServer/src/node/tns/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var mkdirp = require('mkdirp');
var path = require('path');
var glob = require("glob");
var disk = require('diskusage');
var util = require("util");
const os = require('os');

var masterpw = "gulli";
Expand Down Expand Up @@ -167,14 +168,19 @@ router.post('/rest/file/upload', function (req, res) {
var db = req.db;
var simlist = db.collection('simlist');
var serversecrets = db.collection('serversecrets');
console.log("Client tries to upload file.");

console.log("will try to parse it...");
form.parse(req, function (err, fields, files) {
console.log("parsed form...");
if (err) {
console.log("Error parsing incoming form: " + err);
res.status(500);
res.json({'success': false});
return false;
} else {
var old_path = files.attachment.path,
console.log("Will try to save received file.");
var old_path = files.attachment.path,
file_size = files.attachment.size,
file_name = files.attachment.name,
simid = fields.simid,
Expand Down Expand Up @@ -307,64 +313,61 @@ router.get('/rest/api/downloads/ND', function (req, res) {
var clientID=req.param('ID');
var clientSkills=req.param('SKILLS');

simlist.findOne({distributed: false}, function(err, result){

if(err || !result){
//console.log("error reading or no result.");
//console.log("Will answer with res-code 500. No Simfiles available. Will check for timedoutsims.");
res.status(500);
res.json({'success': false});
//Mark Request in DB for Statistics, count the clients waiting for tasks.

activeclients.remove({ip: req.connection.remoteAddress, id:clientID}, function (err) {
if (err) {
console.log("Error removing client from list.");
}
});

simlist.findAndModify(
{distributed: false},
[],
{$set: {distributed: true, timestamp: Date.now()}},
false,
true,
function (err, result) {
activeclients.insert({ip: req.connection.remoteAddress,id: clientID,skills: clientSkills,timestamp: Date.now()},
function (err, result) {
if (err) {
console.log("Error updating client-collection.");
} else {
}
});

if (err) {
console.log("Error updating a non-distributed simulation-entry.");
} else {
if (result) {
//console.log("Delivering file " + result.path);

var options = {
headers: {
'filename': result.name,
'simid': result.simid
}
};

var fileName = path.resolve(result.path);
} else{
//console.log("Delivering file: " + result.path);
//console.log("Name: " + result.name);
//console.log("SIMID: " + result.simid);
var options = {
headers: {
'filename': result.name,
'simid': result.simid
}
};

var fileName = path.resolve(result.path);
res.sendFile(fileName, options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
//console.log('Sent:', fileName);
}
else {
//console.log('Sent:', fileName);
//Set to distributed in db
simlist.update({_id:result._id}, {$set: {distributed:true, timestamp: Date.now()}});
}
});
} else {
//console.log("Will answer with res-code 500. No Simfiles available. Will check for timedoutsims.");
res.status(500);
res.json({'success': false});

//Mark Request in DB for Statistics, count the clients waiting for tasks.
activeclients.remove({ip: req.connection.remoteAddress, id:clientID}, function (err) {
if (err) {
console.log("Error removing client from list.");
}
});
activeclients.insert({
ip: req.connection.remoteAddress,
id: clientID,
skills: clientSkills,
timestamp: Date.now()
}, function (err, result) {
if (err) {
console.log("Error updating client-collection.");
} else {
}
});
}
}
}
//console.log("Result-Name:"+ util.inspect(result));
});

});
//{$set: {distributed: true, timestamp: Date.now()}},


});

Expand Down Expand Up @@ -508,21 +511,23 @@ router.get('/rest/api/downloads/log/:simid', function (req, res) {
var db = req.db;
var simlist = db.collection('simlist');
var simid = req.params.simid;
//console.log("Asking for logfiles for: "+simid);
console.log("Asking for logfiles for: "+simid);

//Set timestamp, when master was last seen
arrayOfMasterLastSeen[simid]=Date.now();

console.log("Check db for one logfile.");

simlist.findOne({simid: simid, simulated: true, logdownloaded: false}, function (err, result) {

if (err) {
console.log("Error searching logfiles for: " + req.params.simid);
}
if (result) {
//console.log("findOne was sucessful.");
//console.log(result.name);
console.log("findOne was sucessful.");
console.log(result.name);
var logfilepath = (result.path.split(result.name))[0] + result.logname;//.split(result.name)[0]+"/"+result.logfilename;
//console.log("Delivering logfile " + logfilepath);
console.log("Delivering logfile " + logfilepath);

var options = {
headers: {
Expand All @@ -539,7 +544,7 @@ router.get('/rest/api/downloads/log/:simid', function (req, res) {
res.status(err.status).end();
}
else {
//console.log('Sent:', fileName + ". Try to update db.");
console.log('Sent:', fileName + ". Try to update db.");
simlist.updateById(result._id, {$set: {logdownloaded: true}}, function (err, res) {
if (err) {
console.log("Error updating" + result.name);
Expand All @@ -557,7 +562,7 @@ router.get('/rest/api/downloads/log/:simid', function (req, res) {
}
});
} else {
//console.log("Will answer with res-code 500. No Logfile available.");
console.log("Will answer with res-code 500. No Logfile available.");
res.status(500);
res.json({'success': false});
}
Expand Down
2 changes: 2 additions & 0 deletions TOEDistributionServer/src/node/tns/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
mongod --config mongoconfig.conf &
set PORT=80
export PORT=80
while true; do
npm start
done
1 change: 1 addition & 0 deletions TOEDistributionServer/src/node/tns/uploads/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### This dir is used for uploaded simulation files.

0 comments on commit b9d90a8

Please sign in to comment.