<no title> — cis_interface 0.3.0 documentation

Model Code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    import sys
    from cis_interface.interface.CisInterface import CisRpcServer

    def get_fibonacci(n):
        r"""Compute the nth number of the Fibonacci sequence.

        Args:
            n (int): Index of the Fibonacci number in the Fibonacci sequence that
                should be returned.

        Returns:
            int: The nth Fibonacci number.

        """
        pprev = 0
        prev = 1
        result = 1
        fib_no = 1
        while fib_no < n:
            result = prev + pprev
            pprev = prev
            prev = result
            fib_no = fib_no + 1
        return result

    def main():
        r"""Function to execute server communication and computation in a loop.

        Returns:
            int: Exit code. Negative if an error occurred.

        """

        exit_code = 0
        print('Hello from Python server!')

        # Create server-side rpc conneciton using model name
        rpc = CisRpcServer("server", "%d", "%d")

        # Continue receiving requests until the connection is closed when all
        # clients have disconnected.
        while True:
            print('server: receiving...')
            retval, rpc_in = rpc.recv()
            if not retval:
                print('server: end of input')
                break

            # Compute fibonacci number
            n = rpc_in[0]
            print('server: Received request for Fibonacci number %d' % n)
            result = get_fibonacci(n)
            print('server: Sending response for Fibonacci number %d: %d' % (n, result))

            # Send response back
            flag = rpc.send(result)
            if not flag:
                print('server: ERROR sending')
                exit_code = -1
                break

        print('Goodbye from Python server')
        return exit_code

    if __name__ == '__main__':
        exit_code = main()
        sys.exit(exit_code)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    import sys
    from cis_interface.interface.CisInterface import (
        CisRpcClient, CisOutput)

    def main(iterations, client_index):
        r"""Function to execute client communication with server that computes
        numbers in the Fibonacci sequence.

        Args:
            iterations (int): The number of Fibonacci numbers to log.
            client_index (int): Index of the client in total list of clients.

        Returns:
            int: Exit code. Negative if an error occurred.

        """

        exit_code = 0
        print('Hello from Python client%d: iterations = %d ' % (client_index,
                                                                iterations))

        # Set up connections matching yaml
        # RPC client-side connection will be $(server_name)_$(client_name)
        rpc = CisRpcClient("server_client%d" % client_index, "%d", "%d")
        log = CisOutput("output_log%d" % client_index, 'fib(%-2d) = %-2d\n')

        # Iterate over Fibonacci sequence
        for i in range(1, iterations + 1):

            # Call the server and receive response
            print('client%d(Python): Calling fib(%d)' % (client_index, i))
            ret, result = rpc.call(i)
            if not ret:
                print('client%d(Python): RPC CALL ERROR' % client_index)
                exit_code = -1
                break
            fib = result[0]
            print('client%d(Python): Response fib(%d) = %d' % (client_index, i, fib))

            # Log result by sending it to the log connection
            ret = log.send(i, fib)
            if not ret:
                print('client%d(Python): SEND ERROR' % client_index)
                exit_code = -1
                break

        print('Goodbye from Python client%d' % client_index)
        return exit_code

    if __name__ == '__main__':
        # Take number of iterations from the first argument and the
        # client index from the second
        exit_code = main(int(sys.argv[1]), int(sys.argv[2]))
        sys.exit(exit_code)

(Example in other languages)