Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Python UDP modules



Python UDP modules are Python socket modules where the associated Python script use UDP sockets to communicate with the framework.

Overview


These modules allow to execute Python code in any Python 2 or Python 3 environment:
  • A Python module creates a Python process using the Python executable path defined in the framework properties. By default the framework will look for a Python 2.x executable among the applications installed on the System
  • services reception from the Python module will be deferred to the associated Python script using an UDP communication
  • services invocation from the Python script will be deferred as a Python module Service invocation using an UDP communication

pythonarchi
Two generic additional Python scripts are necessary to handle the communication between the Python module and the Python script:
  • The pythonModule.py script is responsible for the Services subscription in the Python environment
  • The pythonUtils.py script is an utility script usable by the user Python script


Note that these two modules are valid for both a Python 2 or Python 3 environment for UDP communication.

Initialization and runtime sequence


Using lengthy methods for the Python scripts


Declaration

These modules are declared by the top-level pythonModule element. For example:
      <pythonModule name="FlightManagementSystem">

Types limitations


Depending on the Python executable version, there are two ways of exchanging data between Java and Python:
  • For Python version up to 2.5, there is not JSON library included in the Python implementation, so the exchange of Data is performed using a simple raw text format. With this format, there are some limitations on the types which can be exchanged with Python modules. Only the following types are handled:
  • For Python 2.6 and later, there is a JSON library included in the Python implementation, so the exchange of Data is performed using the JSON format. With this format, there are no limitations on the types which can be exchanged with Python modules, including types complex types[1]
    For example, it is possible to exchange arrays of structures, or maps whose values are structures

Python UDP module implementation


The pythonImplementation element declares the Python script file which implements the script, and specifies the associated properties.

The following list specifies the property which is specific to UDP modules:
  • separator: the optional separator between values when sending or receiving services between Java and the Python script. The default value for the separator is "^"[2]
    This is only useful for Python version up to 2.5. Later Python versions use JSON. later Python versions use JSON for the Java / Python communication and don't need a specific separator
    . See also values separator

Examples

For example:
      <pythonModule name="FlightManagementSystem" >
         <pythonImplementation path="pythonAppli" inputPort="6000" outputPort="6005" inputSize="1024" outputSize="5000"/>
      </pythonModule>
Another example, where available free local ports are used for inputPort and outputPort, and 1024 is used for both inputSize and outputSize:
      <pythonModule name="FlightManagementSystem" >
         <pythonImplementation path="pythonAppli" />
      </pythonModule>

Values separator

The content of Services sent or received by the Python script is a String containing the service name followed by the values separated by the separator[3]
The order of the values is the order of the Service declaration
.

Note that this is only useful for Python version up to 2.5. later Python versions use JSON. later Python versions use JSON for the Java / Python communication and don't need a specific separator.

For example for the following Service:
      <services>
         <publish name="position" >
            <data name="latitude" type="float" />
            <data name="longitude" type="float" />
            <data name="altitude" type="float" />
         </publish>
      </services>
We may have the following content exchanged between Java and Python:
      position=latitude:1.23^longitude:0.45^altitude:1200
It is possible to change the default value of the separator to be any character (or even a string having more than one character), including unicode character. This may be useful if string data sent or received by the Python script contain the default separator character.

For example the following declarations are valid:
  • <pythonImplementation ... separator="!" >
  • <pythonImplementation ... separator="!!" >
  • <pythonImplementation ... separator="&#x00B0;" >

Python compatibility

Main Article: Python modules version

The pythonModule.py and pythonUtils.py scripts must change when the communication between Java and Python is upgraded.

The Python modules version is provided by the Framework as a launch argument when starting the Python process. The pythonModule.py script check if its own version is compatible with the version proviced by Java. This means that you normally should replace the pythonModule.py and pythonUtils.py scripts if the Python modules version changes.

Python library


In order for the Python script to work properly, you must copy two additional Python scripts which will be used as a library in the same directory as the script itself:
  • The pythonModule.py script
  • The pythonUtils.py script

Python script


A Python script may have methods declared for:
  • The initialization of the module
  • The start of the module
  • The cyclic call for a cyclic service
  • The subscription of a service

Example

In the following example, the FlightManagementSystem module:
  • subscribe to the position publish service
  • subscribe to the directTo event service
  • provides the computeFlightPlan request-response service
      <pythonModule name="FlightManagementSystem" >
         <pythonImplementation path="pythonAppli"/>
         <interfaces>
            <subscribe service="position" />
            <eventReceived service="directTo"/>
            <requestReceived service="computeFlightPlan"/>
         </interfaces>
      </pythonModule>
Another way to specify the Python module is to explictly specify the ports and port sizes for the Java / Python communication:
      <pythonModule name="FlightManagementSystem" >
         <pythonImplementation path="pythonAppli" inputPort="6000" outputPort="6005" inputSize="1024" outputSize="1024"/>
         <interfaces>
            <subscribe service="position" />
            <eventReceived service="directTo"/>
            <requestReceived service="computeFlightPlan"/>
         </interfaces>
      </pythonModule>


We could have the following code for the script:
      from pythonUtils import PythonUtils

      class PythonAppli:
        def subscribe(self, pythonUtils, serviceName):
        service = pythonUtils.getService(serviceName)
        if serviceName == "position":
        // get position
        ...
        elif serviceName == "directTo":
        // compute directTo
        ...
        elif serviceName == "computeFlightPlan":
        // compute FlightPlan and sends the response
        ...

Notes

  1. ^ For example, it is possible to exchange arrays of structures, or maps whose values are structures
  2. ^ This is only useful for Python version up to 2.5. Later Python versions use JSON. later Python versions use JSON for the Java / Python communication and don't need a specific separator
  3. ^ The order of the values is the order of the Service declaration

See also


Categories: concepts | python

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence