lf-lang / lingua-franca

Intuitive concurrent programming in any language
https://www.lf-lang.org
Other
239 stars 63 forks source link

Federated Execution in TypeScript #451

Open lhstrh opened 3 years ago

lhstrh commented 3 years ago

Currently, the TypeScript target is behind in its capability to support federated execution. The code generator needs to be updated. Aspects that needs work:

hokeun commented 3 years ago

To enable federated tests for the TypeScript target, I think we need to generate a shell script to run the RTI and federates as part of the TypeScript code generation process and use the shell script for testing.

This will require significant changes to:

  1. the TypeScript code generation (to generate the shell script as well as .ts and .js files), and
  2. testing (to run the shell script instead of running by changing the switch case in the "private void execute(LFTest test)" method in org.lflang.tests/src/org/lflang/tests/runtime/TestBase.java).

Here is an example shell script that runs the RTI and federates for the C target.

#!/bin/bash
# Launcher for federated HelloDistributed.lf Lingua Franca program.
# Uncomment to specify to behave as close as possible to the POSIX standard.
# set -o posix

# Enable job control
set -m
shopt -s huponexit

# Set a trap to kill all background jobs on error or control-C
# Use two distinct traps so we can see which signal causes this.
cleanup() {
    printf "Killing federate %s.\n" ${pids[*]}
    # The || true clause means this is not an error if kill fails.
    kill ${pids[@]} || true
    printf "#### Killing RTI %s.\n" ${RTI}
    kill ${RTI} || true
    exit 1
}
cleanup_err() {
    echo "#### Received ERR signal on line $1. Invoking cleanup()."
    cleanup
}
cleanup_sigint() {
    echo "#### Received SIGINT signal on line $1. Invoking cleanup()."
    cleanup
}

trap 'cleanup_err $LINENO' ERR
trap 'cleanup_sigint $LINENO' SIGINT

# Create a random 48-byte text ID for this federation.
# The likelihood of two federations having the same ID is 1/16,777,216 (1/2^24).
FEDERATION_ID=`openssl rand -hex 24`
echo "Federate HelloDistributed in Federation ID '$FEDERATION_ID'"
# Launch the federates:
echo "#### Launching the runtime infrastructure (RTI)."
# First, check if the RTI is on the PATH
if ! command -v RTI &> /dev/null
then
    echo "RTI could not be found."
    echo "The source code can be found in org.lflang/src/lib/core/federated/RTI"
    exit
fi                
# The RTI is started first to allow proper boot-up
# before federates will try to connect.
# The RTI will be brought back to foreground
# to be responsive to user inputs after all federates
# are launched.
RTI -i ${FEDERATION_ID} \
                 -n 2 \
                 -c initial  \
                  exchanges-per-interval 10 \
                  &
# Store the PID of the RTI
RTI=$!
# Wait for the RTI to boot up before
# starting federates (this could be done by waiting for a specific output
# from the RTI, but here we use sleep)
sleep 1
echo "#### Launching the federate s."
/Users/hokeunkim/Development/lingua-franca-intellij/test/C/bin/federated/HelloDistributed_s -i $FEDERATION_ID &
pids[0]=$!
echo "#### Launching the federate d."
/Users/hokeunkim/Development/lingua-franca-intellij/test/C/bin/federated/HelloDistributed_d -i $FEDERATION_ID &
pids[1]=$!
echo "#### Bringing the RTI back to foreground so it can receive Control-C."
fg %1
echo "RTI has exited. Wait for federates to exit."
# Wait for launched processes to finish.
# The errors are handled separately via trap.
for pid in "${pids[@]}"
do
    wait $pid
done
echo "All done."
Soroosh129 commented 3 years ago

Have you looked at FedCLauncher.java as an example?

In theory, only a handful of methods need to be overridden to adapt the launch script generated by FedLauncher.xtend to the TS target. If there is a showstopper there, we might be able to fix it in the base FedLauncher.xtend class.

hokeun commented 3 years ago

Thanks for the pointer! No, I wasn't aware of the file you pointed out. This is a great starting point and I'll certainly leverage this. :)

hokeun commented 3 years ago

Adding a note from the discussion with Marten and Soroush today: