From d0b833f8f2b657f19622ee62a012fffed9370c07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Antakli?= Date: Mon, 12 Aug 2024 12:45:56 +0200 Subject: [PATCH 1/2] implemented reaction to asynchronous action calls --- app/components/services/test-service.js | 38 +++++- .../components/services/test-service.css | 16 ++- .../components/services/test-service.hbs | 30 +++-- testActionService.js | 122 ++++++------------ 4 files changed, 113 insertions(+), 93 deletions(-) diff --git a/app/components/services/test-service.js b/app/components/services/test-service.js index 61cd3abc..cdd9ae81 100644 --- a/app/components/services/test-service.js +++ b/app/components/services/test-service.js @@ -28,6 +28,7 @@ export default Ember.Component.extend({ wssConnection: false, socketRef: null, response: "", + asyncResponse: "", wssMessage: {}, connectionError: false, @@ -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(); }, @@ -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() { @@ -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); }); @@ -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", @@ -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); + }); +} diff --git a/app/styles/components/services/test-service.css b/app/styles/components/services/test-service.css index e2950db2..4362cc58 100644 --- a/app/styles/components/services/test-service.css +++ b/app/styles/components/services/test-service.css @@ -2,6 +2,9 @@ width: 100%; padding-top: 40px; padding-bottom: 40px; + overflow: auto; + position: absolute; + height: 100%; } :global(#test-service-wrapper) { @@ -10,6 +13,7 @@ margin-right: auto; padding: 30px; box-shadow: 0px 5px 10px #d1d1e0; + overflow: auto; } :global(#test-service-wrapper h1) { @@ -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 { diff --git a/app/templates/components/services/test-service.hbs b/app/templates/components/services/test-service.hbs index f100f6a2..69e89ebb 100644 --- a/app/templates/components/services/test-service.hbs +++ b/app/templates/components/services/test-service.hbs @@ -34,7 +34,7 @@

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.

-

Received message

+

Received message

Date: {{wssMessage.date}}
Headers: @@ -49,22 +49,36 @@ {{wssMessage.body}}
-

{{ui/message/send-message}}
-

Response message

+

Response message

- Specify content (text/turtle) that the Test Service will send after calling its /post endpoint. + Specify content (text/turtle) that the Test Service will send directly after receiving a message on its /post endpoint.

-
- {{textarea value=response cols="93" rows="6"}} - +
+
+
+ +
+

Asynchronous Response message

+

+ Content (text/turtle) that the Test Service will send after 2 seconds, if an asynchronous action (payload contains actn:asyncRequestURI) was received on /post. +

+
+
+ {{textarea value=asyncResponse rows="6"}} +
diff --git a/testActionService.js b/testActionService.js index fde355b9..5f5c7392 100644 --- a/testActionService.js +++ b/testActionService.js @@ -4,6 +4,10 @@ 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; @@ -11,15 +15,10 @@ 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 = " ."; +let requestURI = ""; +let response = " ."; +let asyncResponse = " ."; const wss = new WebSocket.Server({ server }); app.use(bodyParser.text({ type: 'text/plain', limit: '50mb' })); @@ -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(""); }); @@ -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) { @@ -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) { @@ -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); + } + }); +} From d3f861a2f44807ae641aa4a88346ebd4c33d5f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Antakli?= Date: Mon, 12 Aug 2024 12:55:18 +0200 Subject: [PATCH 2/2] changed asynchronous icon --- app/templates/components/services/test-service.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/templates/components/services/test-service.hbs b/app/templates/components/services/test-service.hbs index 69e89ebb..6f89eebb 100644 --- a/app/templates/components/services/test-service.hbs +++ b/app/templates/components/services/test-service.hbs @@ -70,7 +70,7 @@
-

Asynchronous Response message

+

Asynchronous Response message

Content (text/turtle) that the Test Service will send after 2 seconds, if an asynchronous action (payload contains actn:asyncRequestURI) was received on /post.