From 11f134b7705f8dcc0291978832f3f6905a5ab521 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Sun, 25 Feb 2024 08:18:53 -0800 Subject: [PATCH] Added Uptime example combining WebSocket and BrowserUI --- examples/C/src/browser-ui/Uptime.lf | 53 +++++++++++++++++++++++++++ examples/C/src/browser-ui/uptime.html | 42 +++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/C/src/browser-ui/Uptime.lf create mode 100644 examples/C/src/browser-ui/uptime.html diff --git a/examples/C/src/browser-ui/Uptime.lf b/examples/C/src/browser-ui/Uptime.lf new file mode 100644 index 00000000..6e23beae --- /dev/null +++ b/examples/C/src/browser-ui/Uptime.lf @@ -0,0 +1,53 @@ +/** + * FIXME. + * http://localhost:8080 + * + * @author Edward A. Lee + */ +target C { + build-type: debug, + keepalive: true +} + +import ServerUI from "../lib/ServerUI.lf" +import WebSocketServer from "../lib/WebSocketServer.lf" + +main reactor { + timer seconds(0, 1s) + + state count: int = 0 + + s = new ServerUI(hostport = 8080, initial_file = "Uptime.html") + w = new WebSocketServer() + + reaction(startup) {= + lf_print("Point your browser to http://localhost:8080"); + =} + + reaction(s.initialized) {= + self->count++; // Count the number of connections. + =} + + reaction(seconds) -> w.send {= + instant_t uptime = lf_time_logical_elapsed(); + // Truncate to the nearest second. + uptime = (uptime / SEC(1)) * SEC(1); + char* message = (char*)malloc(LF_TIME_BUFFER_LENGTH * sizeof(char)); + size_t length = lf_readable_time(message, uptime); + + // Broadcast to all connected sockets. This is accomplished by providing a NULL wsi. + web_socket_message_t* to_send = (web_socket_message_t*)malloc(sizeof(web_socket_message_t)); + to_send->wsi = NULL; + to_send->length = length; + to_send->message = message; + lf_set(w.send, to_send); + =} + + reaction(w.connected) {= + if (w.connected->value.connected) { + lf_print("======== Connected a new client: %p", w.connected->value.wsi); + } else { + lf_print("======== Disconnected client: %p", w.connected->value.wsi); + } + =} +} diff --git a/examples/C/src/browser-ui/uptime.html b/examples/C/src/browser-ui/uptime.html new file mode 100644 index 00000000..1b628911 --- /dev/null +++ b/examples/C/src/browser-ui/uptime.html @@ -0,0 +1,42 @@ + + + Uptime Lingua Franca + +

The Uptime Lingua Franca program has been running for:

+

No data yet

+ This page connects to a Lingua Franca program that feeds it time data through a web socket. +

Not connected

+ + +