Skip to content

Commit

Permalink
Merge branch 'feature/asnc_test_action-service'
Browse files Browse the repository at this point in the history
  • Loading branch information
André Antakli committed Aug 12, 2024
2 parents b35d24b + d3f861a commit 9c7a906
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 93 deletions.
38 changes: 36 additions & 2 deletions app/components/services/test-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default Ember.Component.extend({
wssConnection: false,
socketRef: null,
response: "",
asyncResponse: "",
wssMessage: {},
connectionError: false,

Expand All @@ -36,7 +37,6 @@ export default Ember.Component.extend({
that = this;
setTriplestoreField();
this.set("wssMessage.body", "Here you can see the output of the TestService (testService.js) that it received via an HTTP/POST (Content-Type: text/turtle; Request-URI: http://localhost:4201/post) message.");
getResponseMessage();
this.actions.connect();
},

Expand All @@ -52,6 +52,7 @@ export default Ember.Component.extend({
socket.on('message', myMessageHandler, that);
socket.on('close', myCloseHandler, that);
that.set('socketRef', socket);
getResponseMessage();
},

disconnect() {
Expand All @@ -77,8 +78,9 @@ export default Ember.Component.extend({
.then(function () {
$("#send-message").trigger("showToast");
that.set("messageError", "");
});
})
})

.catch(function (error) {
that.set("messageError", uri);
});
Expand Down Expand Up @@ -116,11 +118,29 @@ function getResponseMessage() {
}).then(function (data) {
console.log(data);
that.set("response", data);
})
.then(function () {
getAsyncResponseMessage();
})
.catch(function (error) {
alert("No TestServiceAction Service is running on http://" + document.location.hostname + ":4201");
});
}

function getAsyncResponseMessage() {
return $.ajax({
url: "http://localhost:4201/getAsyncResponse",
type: "GET",
headers: { Accept: "text/plain" }
}).then(function (data) {
console.log(data);
that.set("asyncResponse", data);
}).catch(function (error) {
alert("No TestServiceAction Service is running on http://" + document.location.hostname + ":4201");
});
}


function sendResponseMessage(content) {
return $.ajax({
url: "http://" + document.location.hostname + ":4201/response",
Expand All @@ -134,3 +154,17 @@ function sendResponseMessage(content) {
console.log(error);
});
}

function sendResponseMessage(content) {
return $.ajax({
url: "http://" + document.location.hostname + ":4201/asyncResponse",
type: "POST",
contentType: "text/plain",
data: content,
}).then(function (data) {
$("#send-message").trigger("showToast");
getResponseMessage();
}).catch(function (error) {
console.log(error);
});
}
16 changes: 12 additions & 4 deletions app/styles/components/services/test-service.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
width: 100%;
padding-top: 40px;
padding-bottom: 40px;
overflow: auto;
position: absolute;
height: 100%;
}

:global(#test-service-wrapper) {
Expand All @@ -10,6 +13,7 @@
margin-right: auto;
padding: 30px;
box-shadow: 0px 5px 10px #d1d1e0;
overflow: auto;
}

:global(#test-service-wrapper h1) {
Expand Down Expand Up @@ -56,13 +60,17 @@
padding: 0px !important;
}

:global(#test-service-response) {
position: relative;
overflow: auto;
:global(#test-service-response),
:global(#test-service-async-response) {
width: 47%;
float: left;
margin-right: 3%;
}

:global(#test-service-response textarea) {
:global(#test-service-response textarea),
:global(#test-service-async-response textarea) {
float: left;
width: 90%;
}

.test-service-ser-response {
Expand Down
30 changes: 22 additions & 8 deletions app/templates/components/services/test-service.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<p>
To test the input for ServiceActions (generated by the Payload Query), you can start a TestService. To do this, execute the startTestService.bat in the ajan-editor root folder. Afterwards you can connect to this service via connect. To see the received input of the TestService, create a new ServiceAction, change the Request-Uri to "http://localhost:4201/post" and execute it with an behavior action.
</p>
<h3>Received message</h3>
<h3><i class="arrow alternate circle blue down icon"></i> Received message</h3>
<div><b style="padding-right:10px;">Date:</b> {{wssMessage.date}}</div>
<div class="test-service-headers">
<b>Headers:</b>
Expand All @@ -49,22 +49,36 @@
{{wssMessage.body}}
</div>
</div>
<div local-class="clearfix"></div>

<hr>

{{ui/message/send-message}}

<div id="test-service-response">
<h3>Response message</h3>
<h3><i class="arrow alternate circle green up icon"></i> Response message</h3>
<p>
Specify content (text/turtle) that the <b>Test Service</b> will send after calling its /post endpoint.
Specify content (text/turtle) that the <b>Test Service</b> will send directly after receiving a message on its /post endpoint.
</p>
<div id="test-service-response-message">
<div id="test-service-response" local-class="test-service-row-value">
{{textarea value=response cols="93" rows="6"}}
<button data-tooltip="send Message" class="ui right icon yellow button" local-class="test-service-ser-response" {{action "setResponse" response}}>
<i class="icon send no-side-margins"></i>
<div local-class="test-service-row-value">
{{textarea value=response rows="6"}}
<button data-tooltip="send Message" class="ui right icon blue button" local-class="test-service-ser-response" {{action "setResponse" response}}>
<i class="icon save no-side-margins"></i>
</button>
</div>
</div>
</div>

<div id="test-service-async-response">
<h3><i class="clock outline icon green"></i><i class="arrow alternate circle green up icon"></i> Asynchronous Response message</h3>
<p>
Content (text/turtle) that the <b>Test Service</b> will send after <b>2 seconds</b>, if an asynchronous action (payload contains <b>actn:asyncRequestURI</b>) was received on /post.
</p>
<div id="test-service-async-response-message">
<div local-class="test-service-row-value">
{{textarea value=asyncResponse rows="6"}}
<button data-tooltip="send Message" class="ui right icon blue button" local-class="test-service-ser-response" {{action "setAsyncResponse" asyncResponse}}>
<i class="icon save no-side-margins"></i>
</button>
</div>
</div>
Expand Down
122 changes: 43 additions & 79 deletions testActionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ const http = require('http');
const WebSocket = require('ws');
const cors = require('cors');
const bodyParser = require('body-parser');
const rdf = require('rdf-ext');
const n3 = require('rdf-parser-n3');
const stringToStream = require('string-to-stream');
const XMLHttpRequest = require('xmlhttprequest-ssl');

const app = express();
const port = 4201;

//initialize a simple http server
const server = http.createServer(app);

let start;
let startAll;
let endAll;
let performance = "";
let iterations = 1000;
let iteration = 0;
let data = "";
let body = "";
let response = "<http://localhost:4201/post> <http://localhost:4201/test-service-ns#message> <http://localhost:4201/test-service-ns#Received> .";
let requestURI = "";
let response = "<http://localhost:4201/post> <http://localhost:4201/test-service-ns#message> <http://localhost:4201/test-service-ns#Received> .";
let asyncResponse = "<http://localhost:4201/post> <http://localhost:4201/test-service-ns#message> <http://localhost:4201/test-service-ns#AsynchronousResponse> .";
const wss = new WebSocket.Server({ server });

app.use(bodyParser.text({ type: 'text/plain', limit: '50mb' }));
Expand All @@ -39,83 +38,23 @@ app.get('/', (req, res) => {
res.send('Hello World!');
});

app.get('/performance', (req, res) => {
let total = endAll - startAll - (1000 * iterations) ;
let average = total / iterations;
res.send(performance + " --> " + total + "ms" + ", 1 cycle = " + average + "ms");
});

app.get('/start', (req, res) => {
res.send('start');
iteration = iterations;
performance = "";
startAll = Date.now();
/*try {
data = fs.readFileSync('/Projects/AJAN/github/AJAN-editor/test_rdf.txt', 'utf8');
console.log(data);
} catch (err) {
console.error(err);
}*/
sendRequest();
app.get('/getResponse', (req, res) => {
res.send(response);
});

function sendRequest() {
const options = {
hostname: 'localhost',
port: 8080,
path: '/ajan/agents/test?capability=execute',
method: 'POST',
headers: {
'Content-Type': 'text/turtle',
'Content-Length': data.length
}
}

const reqTest = http.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
});
});

reqTest.on('error', error => {
console.error(error)
});
start = Date.now();
reqTest.write(data);
reqTest.end();
}

app.post('/end', (req, res) => {
console.log("\r-------------------------------");
console.log(req.body);
let end = Date.now() - start;
console.log("\r-------------------------------");
console.log("performance time:" + end);
console.log("-------------------------------");
app.post('/response', (req, res) => {
console.log(req.body);
console.log("-------------------------------");
response = req.body;
res.send("");
iteration -= 1;
performance += (iterations - iteration) + ": " + end + "ms; ";
console.log(iteration);
if (iteration > 0) {
var waitTill = new Date(new Date().getTime() + 1 * 1000);
while(waitTill > new Date()){}
sendRequest();
console.log("sent!");
} else {
endAll = Date.now();
}
});

app.get('/getResponse', (req, res) => {
res.send(response);
app.get('/getAsyncResponse', (req, res) => {
res.send(asyncResponse);
});

app.post('/response', (req, res) => {
app.post('/asycResponse', (req, res) => {
console.log(req.body);
response = req.body;
asyncResponse = req.body;
res.send("");
});

Expand All @@ -129,8 +68,7 @@ app.post('/post', (req, res) => {
body = wssMessage;
client.send(JSON.stringify(wssMessage));
});
res.set('Content-Type', 'text/turtle');
res.send(response);
sendResponse(res, req.body);
});

wss.on('connection', function connection(ws) {
Expand All @@ -148,7 +86,7 @@ wss.on('connection', function connection(ws) {
});

server.listen(port, () => {
console.log(`Server started on port ${server.address().port} :)`);
console.log(`Server started on port ${server.address().port} :)`);
});

function getHeaders(headers) {
Expand Down Expand Up @@ -176,3 +114,29 @@ function sendConnectMessage(ws) {
ws.send(JSON.stringify(body));
console.log("send!");
}

function sendResponse(res, msg) {
res.set('Content-Type', 'text/turtle');
res.send(response);

requestURI = "";
const parser = new n3();
const quadStream = parser.import(stringToStream(msg));
const graph = Promise.resolve(rdf.dataset().import(quadStream));
graph.then((value) => {
value.forEach((quad) => {
if (quad.predicate.value == "http://www.ajan.de/actn#asyncRequestURI") {
requestURI = quad.object.value;
console.log(requestURI);
}
});
if (requestURI != "") {
setTimeout(function () {
let xhr = new XMLHttpRequest();
xhr.open("POST", requestURI, true);
xhr.setRequestHeader("Content-Type", "text/turtle");
xhr.send(asyncResponse);
}, 2000);
}
});
}

0 comments on commit 9c7a906

Please sign in to comment.