Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
m-schuetz committed May 8, 2017
1 parent 0743eeb commit ad2847c
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 64 deletions.
20 changes: 19 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ let paths = {
"src/workers/PotreeElevationProfileWorker.js",
"src/workers/PotreeExtractRegionWorker.js",
"src/potree_server.js",
],
settings: [
"resources/settings.json"
],
resources: [
"resources/**/*"
],
www: [
"www/**/*"
]
};

Expand All @@ -30,6 +39,15 @@ gulp.task("build", [], function(){
.pipe(concat('potree_server.js'))
.pipe(size({showFiles: true}))
.pipe(gulp.dest('build/potree_server'));

gulp.src(paths.settings)
.pipe(gulp.dest('build/potree_server'));

gulp.src(paths.resources)
.pipe(gulp.dest('build/potree_server/resources'));

gulp.src(paths.www)
.pipe(gulp.dest('build/potree_server/www'));

return;
});
Expand All @@ -51,7 +69,7 @@ gulp.task('watch', function() {
gulp.run("build");
gulp.run("server");

gulp.watch(['src/**/*.js'], function(){
gulp.watch(['src/**/*.js', 'resources/**/*', 'www/**/*'], function(){
gulp.run("build");
gulp.run("server");
});
Expand Down
9 changes: 9 additions & 0 deletions resources/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"port": 3000,
"wwwroot": "D:/dev/workspaces/PotreeServer/master/build/potree_server/www",
"serverWorkingDirectory": "D:/",
"outputDirectory": "D:/dev/temp",
"elevationProfileExe": "D:/dev/workspaces/CPotree/master/bin/Release_x64/PotreeElevationProfile.exe",
"extractRegionExe": "D:/dev/workspaces/CPotree/master/bin/Release_x64/PotreeExtractRegion.exe",
"maxPointsProcessedThreshold": 10000000
}
124 changes: 67 additions & 57 deletions src/potree_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,24 @@ const spawn = require('child_process').spawn;
const uuid = require('uuid');
const url = require('url');
const http = require('http');

const fs = require('fs');
const path = require('path');

const port = 3000;
const serverWorkingDirectory = "D:/";
const outputDirectory = "D:/dev/temp";
const elevationProfileExe = "D:/dev/workspaces/CPotree/master/bin/Release_x64/PotreeElevationProfile.exe";
const extractRegionExe = "D:/dev/workspaces/CPotree/master/bin/Release_x64/PotreeExtractRegion.exe";
let maxPointsProcessedThreshold = 10*1000*1000;
//console.log(__filename);
//console.log(__dirname);

const css = `
let settingsPath = `${__dirname}/settings.json`;
let settings = null;

.centering{
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
console.log(`Using settings from: '${settingsPath}'`);

.panel{
border: 1px solid black;
position: absolute;
if(fs.existsSync(settingsPath)){
settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
}else{
console.err(`No settings found at: '${settingsPath}'`);
process.exit()
}

.titlebar{
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
margin: 5px;
}
.workerdata{
display: flex;
margin: 5px;
}
.content{
display: block;
margin: 5px;
}
td{
padding: 2px 15px 2px 5px;
}
`;

const workers = {
active: new Map(),
finished: new Map()
Expand All @@ -62,7 +30,7 @@ const workers = {
function potreeElevationProfile(pointcloud, coordinates, width, minLevel, maxLevel, estimate){

let purl = url.parse(pointcloud);
let realPointcloudPath = serverWorkingDirectory + purl.pathname.substr(1);
let realPointcloudPath = settings.serverWorkingDirectory + purl.pathname.substr(1);

console.log("realPointcloudPath", realPointcloudPath);

Expand All @@ -79,15 +47,15 @@ function potreeElevationProfile(pointcloud, coordinates, width, minLevel, maxLev
args.push("--estimate");
}

let result = spawnSync(elevationProfileExe, args, {shell: false});
let result = spawnSync(settings.elevationProfileExe, args, {shell: false});

return result;
}

function potreeExtractRegion(pointcloud, box, minLevel, maxLevel, estimate){

let purl = url.parse(pointcloud);
let realPointcloudPath = serverWorkingDirectory + purl.pathname.substr(1);
let realPointcloudPath = settings.serverWorkingDirectory + purl.pathname.substr(1);

console.log("realPointcloudPath", realPointcloudPath);

Expand All @@ -103,7 +71,7 @@ function potreeExtractRegion(pointcloud, box, minLevel, maxLevel, estimate){
args.push("--estimate");
}

let result = spawnSync(extractRegionExe, args, {shell: false});
let result = spawnSync(settings.extractRegionExe, args, {shell: false});

return result;
}
Expand Down Expand Up @@ -168,7 +136,7 @@ let handlers = {
};
}

if(result.pointsProcessed > maxPointsProcessedThreshold){
if(result.pointsProcessed > settings.maxPointsProcessedThreshold){
let res = {
status: "ERROR_POINT_PROCESSED_ESTIMATE_TOO_LARGE",
estimate: result.pointsProcessed,
Expand Down Expand Up @@ -214,7 +182,7 @@ let handlers = {
};
}

if(result.pointsProcessed > maxPointsProcessedThreshold){
if(result.pointsProcessed > settings.maxPointsProcessedThreshold){
let res = {
status: "ERROR_POINT_PROCESSED_ESTIMATE_TOO_LARGE",
estimate: result.pointsProcessed,
Expand Down Expand Up @@ -245,7 +213,7 @@ let handlers = {

if(worker){

let filePath = `${outputDirectory}/${worker.uuid}/result.las`;
let filePath = `${settings.outputDirectory}/${worker.uuid}/result.las`;
let stat = fs.statSync(filePath);

response.writeHead(200, {
Expand Down Expand Up @@ -281,10 +249,8 @@ let handlers = {

if(!workerID){



let res = `<html>
<style>${css}</style>
<link rel="stylesheet" type="text/css" href="http://${request.headers.host}/server.css">
<body>`;

{ // ACTIVE WORKERS
Expand Down Expand Up @@ -367,7 +333,45 @@ let handlers = {
};


function handleRequestFile(request, response){
//http://localhost:3000/resources/server.css

let purl = url.parse(request.url, true);
let query = purl.query;


let file = `${settings.wwwroot}${purl.pathname}`;

if(fs.existsSync(file)){
// TODO proper mime type handling, e.g.https://www.npmjs.com/package/mime-types
if(file.toLowerCase().endsWith(".css")){
response.writeHead(200, {
'Content-Type': 'text/css'
});
}else{
response.writeHead(200, {
'Content-Type': 'application/octet-stream'
});
}


let readStream = fs.createReadStream(file);
readStream.pipe(response);

readStream.on('close', () => {
response.end();
});

readStream.on('error', () => {
response.end();
});

}else{
response.statusCode = 404;
response.end("");
}

}



Expand All @@ -381,16 +385,22 @@ function startServer(){
let basename = purl.pathname.substr(purl.pathname.lastIndexOf("/") + 1);
let query = purl.query;

console.log("from: ", request.headers.origin);
console.log("from: ", request.headers.host);
console.log("request: ", request.url);

if(request.headers.origin){
response.setHeader('Access-Control-Allow-Origin', request.headers.origin);
}
response.setHeader('Access-Control-Allow-Headers', 'authorization, content-type');

let handler = handlers[basename] || handlers["get_status"];

let handler = null;
if(request.url === "/"){
handler = handlers["get_status"];
}else if(handlers[basename]){
handler = handlers[basename];
}else{
handler = handleRequestFile;
}

if(handler){
handler(request, response);
Expand All @@ -405,12 +415,12 @@ function startServer(){

let server = http.createServer(requestHandler);

server.listen(port, (err) => {
server.listen(settings.port, (err) => {
if (err) {
return console.log('something bad happened', err)
}

console.log(`server is listening on ${port}`)
console.log(`server is listening on ${settings.port}`)
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/workers/PotreeElevationProfileWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class PotreeElevationProfileWorker extends Worker{
super.start();

let purl = url.parse(this.pointcloud);
let realPointcloudPath = serverWorkingDirectory + purl.pathname.substr(1);
let outPath = `${outputDirectory}/${this.uuid}/result.las`;
let realPointcloudPath = settings.serverWorkingDirectory + purl.pathname;
let outPath = `${settings.outputDirectory}/${this.uuid}/result.las`;

console.log("realPointcloudPath", realPointcloudPath);

Expand All @@ -32,7 +32,7 @@ class PotreeElevationProfileWorker extends Worker{
console.log("spawing elevation profile task with arguments: ");
console.log(args);

let process = spawn(elevationProfileExe, args, {shell: false});
let process = spawn(settings.elevationProfileExe, args, {shell: false});
process.on('close', (code) => {
this.done();
});
Expand Down
6 changes: 3 additions & 3 deletions src/workers/PotreeExtractRegionWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class PotreeExtractRegionWorker extends Worker{
super.start();

let purl = url.parse(this.pointcloud);
let realPointcloudPath = serverWorkingDirectory + purl.pathname.substr(1);
let outPath = `${outputDirectory}/${this.uuid}/result.las`;
let realPointcloudPath = settings.serverWorkingDirectory + purl.pathname;
let outPath = `${settings.outputDirectory}/${this.uuid}/result.las`;

console.log("realPointcloudPath", realPointcloudPath);

Expand All @@ -36,7 +36,7 @@ class PotreeExtractRegionWorker extends Worker{
console.log("spawing region extraction task with arguments: ");
console.log(args);

let process = spawn(extractRegionExe, args, {shell: false});
let process = spawn(settings.extractRegionExe, args, {shell: false});
process.on('close', (code) => {
this.done();
});
Expand Down
35 changes: 35 additions & 0 deletions www/server.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

.centering{
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}

.panel{
border: 1px solid black;
position: absolute;
}

.titlebar{
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
margin: 5px;
}

.workerdata{
display: flex;
margin: 5px;
}

.content{
display: block;
margin: 5px;
}

td th{
padding: 2px 15px 2px 5px;
}

0 comments on commit ad2847c

Please sign in to comment.