Model Client and Server Components Using Function Ports
You can model client and server components in Simulink® using function ports. Function ports allow you to facilitate data sharing between component models. This topic shows you how to use Simulink functions, function callers, and function ports to model client and server components.
You can integrate and simulate client and server components and their interfaces in a System Composer™ software architecture model. For more information, see Service Interfaces Overview (System Composer).
Model Server that Provides Services
A server component is one that provides services. Services are modeled as Simulink functions that are scoped to exporting function ports. You can implement the desired algorithm of your service using a Simulink Function block. The function must be scoped to an exporting function port using a Function Element block. Server components must be export-function models. For more information, see Export-Function Models Overview.
In this example, the fetchData
function is defined in a
Simulink Function block and scoped to the
Server
port represented by a Function Element
block.
To associate a function with an exporting function port, you must configure the Trigger block located inside of the Simulink Function block. Follow these steps to configure your Simulink function:
Double-click the Trigger block to open the block parameters.
Select the Treat as Simulink function parameter.
Set the Function name parameter to the name of the Simulink function. In this example, the Function name is set to
fetchData
.Set the Function visibility parameter to
port
.Set the Scope to port parameter to the name of the exporting function port. In this example, the Scope to port is set to
Server
.
The Simulink Function block is scoped to the exporting function port, meaning that the port is not visible elsewhere in the model hierarchy and you can only call the function through the port. The block label of the Function Element block shows the association of the function with the port. The port name is first, followed by the function name.
After you configure the Simulink Function block, you can implement the desired algorithm of your service inside of the Simulink Function. Add any necessary blocks to the function subsystem and ensure these blocks are connected to the argument blocks. ArgIn and ArgOut blocks represent the function input and output arguments, respectively.
Model Client that Requires Services
A client component is one that requires services from other models. Required services are modeled as function callers that are associated with an invoking function port. You can call a required service with a Function-Call Subsystem block and Function Caller block. The function caller must be associated with an invoking function port using a Function Element Call block.
In this example, the fetchData
function is associated with the
invoking function port, Client
. A function connector is shown between
the Function Element Call block labeled
Client.fetchData
and the Function-Call Subsystem
block to represent the association between the function call and the port.
To associate a function caller with an invoking function port, you must configure the Function Caller block located inside of the Function-Call Subsystem block. Follow these steps to configure your Function Caller:
Double-click the Function Caller block to open the block parameters.
Set the Function prototype parameter to the prototype of the Simulink function. In this example, the Function prototype parameter is set to
data = Client.fetchData
.
The Function Caller block is associated with the invoking function port. The block label of the Function Element Call block shows this association of the function with the port. The port name is first, followed by the function name.
After you configure the Function Caller block, you can implement the call to the required service inside the Function-Call Subsystem block. Add any necessary blocks to the function call subsystem to represent any inputs and outputs.
Configure Asynchronous Execution with Function Ports
Asynchronous execution is when the client sends a request to the server, the function executes asynchronously based on the priority order, and the function returns the output arguments to the client.
Asynchronous calls to a service require the use of message payloads to store function output arguments. The behavior of the message payload can change depending on the number of function output arguments.
If the function has one output argument, the output argument becomes the message payload.
If the function has more than one output argument, the Function Caller block bundles the output arguments as a structure that becomes the message payload.
To associate a function with an exporting function port in an asynchronous system, you must configure the Trigger block located inside of the Simulink Function block. The Trigger block parameters are different than those of a synchronous system. Follow these steps to configure your Simulink function for asynchronous execution:
Double-click the Trigger block to open the block parameters.
Set the Trigger type parameter to
function-call
.Select the Treat as a Simulink function parameter.
Set the Function visibility parameter to
port
.Select the Execute function call asynchronously parameter.
To associate a function caller with an invoking function port in an asynchronous system, you must configure the Function Caller block located inside of the Function-Call Subsystem block. The Function Caller block parameters are different than those of a synchronous system. Follow these steps to configure your Function Caller:
Double-click the Function Caller block to open the block parameters.
Select the Execute function call asynchronously parameter.
In an asynchronous system, the Function-Call Subsystem block must connect to a Message Triggered Subsystem block. The Message Triggered Subsystem block acts as a callback for the function.
When implementing asynchronous services, client components must be export-function models.
For more information, see Simulate Asynchronous Services for Vehicle Headlight Management (System Composer).
Composition Model to Simulate Client and Server Components
Once you implement the algorithm of each client and server component in a Simulink model, you can integrate components into a composition model.
Consider utilizing System Composer software architectures as your composition model. Create a software architecture, link software components to your client and server component models, and simulate the behavior of your service from the architecture level. For more information, see Service Interfaces Overview (System Composer).
Note
If using a Simulink model for the composition of synchronous services, the composition model must be an export-function model.
See Also
Simulink Function | Trigger | Function Caller | Function Element | Function Element Call | Function-Call Subsystem | Model | Message Triggered Subsystem
Related Topics
- Export-Function Models Overview
- Service Interfaces Overview (System Composer)
- Model Service-Oriented Communication Between Sensors (System Composer)
- Simulate Asynchronous Services for Vehicle Headlight Management (System Composer)