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 | import sys
from cis_interface.interface.CisInterface import (
CisRpcClient, CisOutput)
def main(iterations):
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.
Returns:
int: Exit code. Negative if an error occurred.
"""
exit_code = 0
print('Hello from Python client: iterations = %d ' % iterations)
# Set up connections matching yaml
# RPC client-side connection will be $(server_name)_$(client_name)
rpc = CisRpcClient("server_client", "%d", "%d")
log = CisOutput("output_log", 'fib(%-2d) = %-2d\n')
# Iterate over Fibonacci sequence
for i in range(1, iterations + 1):
# Call the server and receive response
print('client(Python): Calling fib(%d)' % i)
ret, result = rpc.call(i)
if not ret:
print('client(Python): RPC CALL ERROR')
exit_code = -1
break
fib = result[0]
print('client(Python): Response fib(%d) = %d' % (i, fib))
# Log result by sending it to the log connection
ret = log.send(i, fib)
if not ret:
print('client(Python): SEND ERROR')
exit_code = -1
break
print('Goodbye from Python client')
return exit_code
if __name__ == '__main__':
# Take number of iterations from the first argument
exit_code = main(int(sys.argv[1]))
sys.exit(exit_code)
|