Skip to content

Commit

Permalink
Merge pull request #21 from ogavb/dynamicOptionsAlignment
Browse files Browse the repository at this point in the history
Dynamic options alignment part 1: Support for different solver executors
  • Loading branch information
stefanogermano authored Nov 17, 2019
2 parents 59d4451 + 8ee416a commit 67a87ff
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ We use [Semantic Versioning](http://semver.org) for versioning. For the versions
- Stefano Germano
- Eliana Palermiti
- Rocco Palermiti
- Alexander Karaulshchikov
- Francesco Calimeri

From the [Department of Mathematics and Computer Science](https://www.mat.unical.it) of the [University of Calabria](http://unical.it)
Expand Down
69 changes: 53 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ var http = require('http');
var forceSSL = require('express-force-ssl');
var webSocket = require('websocket').w3cwebsocket;
var fs = require('fs');
var propertiesReader = require('properties-reader');

var properties = propertiesReader('config/properties');
// System config loading
var properties = require('./config/app-config.json');
var httpPort = properties.port.http;
var httpsPort = properties.port.https;
var key = properties.path.key;
var cert = properties.path.cert;
var maxAge = properties.max_age;

// Services configuration file
var servicesConfig = require('./config/services.json');

var app = express();

var key = properties.get("path.key");
var cert = properties.get("path.cert");
var enableHTTPS = false;

if (key.length !== 0 && cert.length !== 0) {
Expand All @@ -22,7 +28,7 @@ if (key.length !== 0 && cert.length !== 0) {
};

// Enable redirect from HTTP to HTTPS
var securePort = properties.get('port.https');
var securePort = httpsPort;
app.use(forceSSL);
app.set('forceSSLOptions', {
httpsPort: securePort,
Expand All @@ -34,17 +40,14 @@ if (key.length !== 0 && cert.length !== 0) {

// Sets "Strict-Transport-Security, by default maxAge is set 1 year in seconds
app.use(helmet.hsts({
maxAge: properties.get("max.age")
maxAge: maxAge
}));

var server = http.createServer(app);

var io = require('socket.io').listen(enableHTTPS ? secureServer : server);
var ws_server = properties.get('ws.server');
var pckg = require('./package.json');

var port = properties.get('port.http');

app.use(express.static('resources'));

app.post('/version', function (req, res) { // send the version (and take it in package.json) of the application
Expand All @@ -56,16 +59,33 @@ io.sockets.on('connection', function (socket) { // Wait for the incoming connect
print_log('Opened connection')

socket.on('run', function (data) { // Wait for the incoming data with the 'run' event and send data

print_log('Executed "run"')

var client = new webSocket(ws_server); // connect to the EmbASPServerExecutor
print_log('Connecting to "' + ws_server + '"')
// FIXME: This replace is needed because the client use 'solver' but the executor use 'engine'
data = data.replace('engine', 'solver');

// The function return the host path of one of the executors for a particular language and solver, if know
var host = getExcecutorURL( data );

// Check if the choosen host is configured
if( host == undefined )
{
socket.emit( 'problem', {
reason: 'No Executor available for this solver!'
});
return;
}

// Connect to the executor
var client = new webSocket( host );

print_log('Connecting to "' + host + '"')

client.onopen = function () { // Opens the connection and send data
print_log('Sending to EmbASPServerExecutor:\n' + JSON.stringify(JSON.parse(data), null, '\t'))
client.send(data);
};

client.onerror = function (error) {
print_log('WebSocket problem:\n' + JSON.stringify(error, null, '\t'));
socket.emit('problem', {
Expand All @@ -75,13 +95,12 @@ io.sockets.on('connection', function (socket) { // Wait for the incoming connect
reason: 'Execution error, please try again later!'
});
};

client.onmessage = function (output) { // Wait for the incoming data from the EmbASPServerExecutor
var model = JSON.parse(output.data);
print_log('From EmbASPServerExecutor:\nModel "' + model.model + '"\nError "' + model.error + '"'); // debug string
socket.emit('output', model); // Socket.io calls emit() to send data to the browser.

};

});
});

Expand All @@ -91,11 +110,29 @@ if (enableHTTPS) {
print_log('Version: ' + pckg.version);
});
}
server.listen(port, function () {
print_log('App listening on port ' + port);

server.listen(httpPort, function () {
print_log('App listening on port ' + httpPort);
print_log('Version: ' + pckg.version);
});

function print_log(statement) {
console.log('%s: %s', (new Date()).toLocaleString(), statement); // debug string
}

function getExcecutorURL(data) {
data = JSON.parse(data);
for(var i in servicesConfig.languages) {
if(servicesConfig.languages[i].value === data.language) {
var solvers = servicesConfig.languages[i].solvers;
for(var j in solvers) {
if(solvers[j].value === data.solver) {
// TODO let the user choose the executor. atm this is a missing data
// by default the first executor will be chosen
var executor = solvers[j].executors[0];
return executor.protocol + '://' + executor.url + ':' + executor.port + executor.path;
}
}
}
}
}
13 changes: 13 additions & 0 deletions config/app-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"port" :
{
"http" : 8084,
"https" : 8085
},
"path" :
{
"key" : "",
"cert" : ""
},
"max_age" : 31536000
}
6 changes: 0 additions & 6 deletions config/properties

This file was deleted.

89 changes: 89 additions & 0 deletions config/services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"languages" :
[{
"name" : "ASP",
"value" : "asp",
"solvers" :
[{
"name" : "Clingo",
"value" : "clingo",
"executors" :
[{
"protocol" : "ws",
"url" : "localhost",
"name" : "EmbASPServerExecutor",
"path" : "/EmbASPServerExecutor/home",
"port" : 8080
}],
"options" :
[{
"name" : "Free choise",
"value" : "free choise",
"word_argument" : true,
"description" : "Missing description"
}]
},
{
"name" : "DLV",
"value" : "dlv",
"executors" :
[{
"protocol" : "ws",
"url" : "localhost",
"name" : "EmbASPServerExecutor",
"path" : "/EmbASPServerExecutor/home",
"port" : 8080
}],
"options" :
[{
"name" : "Free choise",
"value" : "",
"word_argument" : true,
"description" : "Missing description"
},
{
"name" : "Filter",
"value" : "-filter=",
"word_argument" : true,
"description" : "Missing description"
},
{
"name" : "No Facts",
"value" : "-nofacts",
"word_argument" : false,
"description" : "Missing description"
},
{
"name" : "Silent",
"value" : "-silent",
"word_argument" : false,
"description" : "Missing description"
},
{
"name" : "Query",
"value" : "-FC",
"word_argument" : false,
"description" : "Missing description"
}]
},
{
"name" : "DLV2",
"value" : "dlv2",
"executors" :
[{
"protocol" : "ws",
"url" : "localhost",
"name" : "EmbASPServerExecutor",
"path" : "/EmbASPServerExecutor/home",
"port" : 8080
}],
"options" :
[{
"name" : "Free choise",
"value" : "",
"word_argument" : true,
"description" : "Missing description"
}]
}]
}]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LoIDE",
"version": "1.11.2",
"version": "2.0.0",
"description": "Web-based IDE for Logic Programming",
"main": "app.js",
"scripts": {
Expand All @@ -24,6 +24,10 @@
"name": "Rocco Palermiti",
"email": "@"
},
{
"name": "Alexander Karaulshchikov",
"email": "alexander.karaulshchikov@gmail.com"
},
{
"name": "Francesco Calimeri",
"email": "calimeri@mat.unical.it"
Expand All @@ -37,7 +41,6 @@
"express": "^4.17.1",
"express-force-ssl": "^0.3.2",
"helmet": "^3.21.1",
"properties-reader": "0.3.1",
"socket.io": "^2.3.0",
"websocket": "^1.0.30"
},
Expand Down

0 comments on commit 67a87ff

Please sign in to comment.