Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.7.0 #106

Merged
merged 5 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions examples/C/src/ReflexGame/ReflexGame.lf
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ reactor RandomSource(min_time: time = 2 sec, max_time: time = 8 sec) {
=}
}

/**
* Upon receiving a prompt, record the time of the prompt, then listen for user input. When the user
* hits return, then schedule a physical action that records the time of this event and then report
* the response time.
*/
reactor GetUserInput {
/** Listen for keyboard input and produce it on the output when it arrives. */
reactor User {
output out: char
physical action user_response: char

preamble {=
// Thread to read input characters until an EOF is received.
// Each time a newline is received, schedule a user_response action.
Expand All @@ -82,20 +81,31 @@ reactor GetUserInput {
}
=}

physical action user_response: char
reaction(startup) -> user_response {=
// Start the thread that listens for Enter or Return.
lf_thread_t thread_id;
lf_thread_create(&thread_id, &read_input, user_response);
=}

reaction(user_response) -> out {=
lf_set(out, user_response->value);
=}
}

/**
* Upon receiving a prompt, record the time of the prompt. When user input arrives, schedule a
* physical action that records the time of this event and then report the response time. Also,
* check for cheating.
*/
reactor GetUserInput {
state prompt_time: time = 0
state total_time_in_ms: int = 0
state count: int = 0

input prompt: int
input user_response: char
output another: int

reaction(startup) -> user_response {=
// Start the thread that listens for Enter or Return.
lf_thread_t thread_id;
lf_thread_create(&thread_id, &read_input, user_response);
=}

reaction(prompt) {=
self->prompt_time = lf_time_logical();
=}
Expand Down Expand Up @@ -134,6 +144,8 @@ reactor GetUserInput {
main reactor ReflexGame {
p = new RandomSource()
g = new GetUserInput()
u = new User()
p.out -> g.prompt
g.another -> p.another
u.out -> g.user_response
}
18 changes: 9 additions & 9 deletions examples/C/src/browser-ui/WebSocket.lf
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/**
* Demo of a use of WebSocketServer enabling a user interface realized in the browser. Compile and
* run this program, then open WebSocket.html in your favorite browser. This example program sends
* to the web page a counting sequence. It also accepts text messages from the web page and prints
* them on standard output.
* to all connected web pages a counting sequence. The count starts when the first web page
* connects. This also accepts text messages from the web page and prints them on standard output.
*
* This example also shows how to limit the number of connections to just two. If you try to open
* the WebSocket.html web page more than twice, only the first two attempts will succeed in
* connecting. By default, WebSocketServer imposes no such limit.
*
* In this example, if you connect two clients, one will receive even numbered counts, and the other
* will receive odd numbered counts. This is because the count state variable is shared across all
* clients.
*
* This uses the <a href="https://libwebsockets.org">libwebsockets</a> (see <a
* href="https://libwebsockets.org/lws-api-doc-main/html/index.html">API documentation</a> and <a
* href="https://libwebsockets.org/lws-api-doc-main/html/md_READMEs_README_build.html">installation
Expand All @@ -31,6 +27,7 @@ import WebSocketServer from "../lib/WebSocketServer.lf"

main reactor {
state count: int = 0
state running: bool = false
logical action send_action: web_socket_instance_t*

s = new WebSocketServer(
Expand All @@ -49,7 +46,7 @@ main reactor {
web_socket_message_t* container = (web_socket_message_t*)malloc(sizeof(web_socket_message_t));
container->message = message;
container->length = strlen(message) + 1;
container->wsi = send_action->value->wsi;
container->wsi = NULL; // Broadcast to all connected clients.
container->binary = false; // Sending text.

lf_set(s.send, container);
Expand All @@ -61,8 +58,11 @@ main reactor {
if (s.connected->value.connected) {
lf_print("======== Connected a new client");

// Start sending.
lf_schedule_copy(send_action, 0, &s.connected->value, 1);
// Start sending if not already started.
if (!self->running) {
lf_schedule_copy(send_action, 0, &s.connected->value, 1);
self->running = true;
}
} else {
lf_print("======== Disconnected client");
}
Expand Down
Loading
Loading