Samples
The Code RealTime Git repository contains several sample applications. You can use them for learning about various features of the Art language and the APIs provided by the TargetRTS. You can also use them as a starting point for creating your own applications with Code RealTime.
The samples are listed here in an order that may be appropriate when learning to use Code RealTime, starting with the simplest examples and proceeding with more advanced ones. For each sample, Art language constructs and TargetRTS APIs that it uses, are listed, so you can find the sample that shows what you currently want to learn more about. To keep these lists short, we only list what is new in the sample compared to previously mentioned samples.
HelloWorld
The most classical of samples for learning a new programming language. This version prints "Hello World!" on the console after a short delay, implemented by means of a timer. Watch this video to see how the sample was created.
Art: capsule, state machine, initial transition, state, timer port, triggered transition, embedded C++ code
TargetRTS: RTTiming::informIn(), RTController::abort()
PingPong
Two capsules, Pinger
and Ponger
, send the events ping
and pong
back and forth. The ping
event has an integer parameter which gets incremented by one for each ping-pong round. The sample prints messages to stdout both using standard C++ code and with the Log service of the TargetRTS.
Art: fixed part, connector, service and behavior port, protocol and event, log port
TargetRTS: RTOutSignal::send(), Log::log(), Log::show(), Log::commit()
PingPongTimeDeltaTracker
While the standard PingPong sample runs forever, this version lets the user enter the number of ping-pong rounds at start-up. When the rounds are completed, information about how long it took is printed.
Art: choice, entry action, transition guard
TargetRTS: RTTimespec::getclock()
PiComputer
This application computes a value of Pi using Madhava's formula, which consists of a series of additions and multiplications. The result is obtained as a collaboration of three capsules, each with a simple state machine, which coordinate their work by sending events with parameters.
Art: composite state (hierarchical state machine)
TrafficLight
This sample implements a classical traffic light that switches between red, yellow and green based on a periodic timer. The actual traffic light is implemented by means of a class Light
with a state machine. An instance of that class is managed by the TrafficLight
capsule. The Light
class uses an interface class ILogger
, which the TrafficLight
capsules implements, for logging messages when the traffic light switches state. It delegates the work of logging to a separate Logger capsule. Note that only the ILogger
interface is provided to the Light
object, instead of the whole TrafficLight
capsule which has an unnecessary big API for the needs of the Light
.
TargetRTS: RTTiming::informEvery()
DiningPhilosophers
Two different implementations of the classical Dining Philosophers problem. The applications implement 3 different strategies (represented by an enum) for how to let all philosophers eat and think without causing a deadlock.
Version 1 of the sample uses fixed parts which are incarnated statically. The strategy to use is hard-coded into the application.
Version 2 of the sample uses optional parts which are incarnated dynamically, and with initialization data passed to the initial transition of the capsule state machine. The strategy to use is read as input from the command-line.
Art: initialization data passed to the initial transition, automatically generated type descriptor
TargetRTS: RTActor::getName(), RTMessage::sap(), Frame::incarnate()
MoreOrLess
A "guess-the-secret-number" game which illustrates the use of plugin capsule parts.
Art: plugin part
TargetRTS: Frame::import(), Frame::deport()
DependencyInjection
This sample shows how to use dependency injection to customize capsule incarnation. Build variants are used for configuring how to customize the dependency injection, which will influence on how the application behaves.
Art: capsule inheritance, capsule factory, dependency injection, build variants
TargetRTS: RTInjector, RTActorFactoryInterface
QtTrafficLight
This application has a user interface developed with Qt which controls a realtime application that implements the logic of a traffic light that works together with a pedestrian light. Note that you need to have Qt installed to build and run this sample.
Art: composite state with entry- and exitpoint, external port, notifying port, internal transition
TargetRTS: RTTiming::cancelTimer()
TrafficLightWeb
This sample is similar to QtTrafficLight but the user interface is instead implemented as a Node.js web application. The application uses the TCPServer library and the Node.js application therefore can use the rt-tcp-utils library for communicating with it. Communication between the Node.js application and the web page uses socket.io.
Art: junction point
TargetRTS: Extracting command-line arguments by means of RTMain::argCount() and RTMain::argStrings()
TcpRangeCounter
This sample uses the TCPServer library which makes it possible to communicate with the application from the "outside" by means of making TCP requests. Two sample client applications for making such requests are included:
The sample also shows how to run a capsule instance in its own thread.
Art: capsule constructor
TargetRTS: Frame::incarnateCustom()
DistributedPingPong
This sample uses the TCPServer library for implementing a distributed application. Two instances of the PingPong application are launched and the TCPServer library ensures that the events that are sent out by one application, are serialized to JSON, sent over TCP and received by the other application. The applications are connected by means of command-line arguments that specify the application's own port, and the port of the other application.
To start the communication, a special command-line argument -injectFirstPing
is supported. The implementation of this argument uses APIs of RTMessage for programmatically injecting an event on a port.
Art: timer data, symmetric events, calling code from an inherited transition
TargetRTS: RTMain::argCount(), RTMain::argStrings(), RTMemoryUtil, RTMessage, RTController::receive()
TokenRing
This sample is similar to DistributedPingPong, and also uses the TCPServer library. But instead of just connecting two applications, here multiple applications can be connected in a ring structure. The applications exchange a token
event which has a string (RTString
) data parameter.
TargetRTS: RTString
gRPC_MazeRunner
This sample uses the gRPCServer library which allows a realtime application to communicate using gRPC with other applications. The sample also shows how to mix Art files with hand-written C++ source files and build them into a single application.
TargetRTS: RTOutSignal::invoke()