rpc_lesson1 Example

Python Version

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
import numpy as np
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."""

    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(np.int32(result))
        if not flag:
            raise RuntimeError('server: ERROR sending')

    print('Goodbye from Python server')

    
if __name__ == '__main__':
    main()
 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
import sys
import numpy as np
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.

    """

    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(np.int32(i))
        if not ret:
            raise RuntimeError('client(Python): RPC CALL ERROR')
        fib = result[0]
        print('client(Python): Response fib(%d) = %d' % (i, fib))

        # Log result by sending it to the log connection
        ret = log.send(np.int32(i), fib)
        if not ret:
            raise RuntimeError('client(Python): SEND ERROR')

    print('Goodbye from Python client')

    
if __name__ == '__main__':
    # Take number of iterations from the first argument
    main(int(sys.argv[1]))

Model YAML:

1
2
3
4
5
6
7
---

model:
  name: server
  language: python
  args: ./src/server.py
  is_server: True  # Creates a RPC server queue called "server"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
---

model:
  name: client
  language: python
  args:
    - ./src/client.py
    - 3  # Pass the number of iterations that should be performed
  client_of: server  # Creates an RPC client queue "server_client"
  outputs: output_log

connections:
  input: output_log
  output: client_output.txt
  in_temp: True

Matlab Version

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
import numpy as np
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."""

    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(np.int32(result))
        if not flag:
            raise RuntimeError('server: ERROR sending')

    print('Goodbye from Python server')

    
if __name__ == '__main__':
    main()
 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
function client(iterations)
  
  iterations = str2num(iterations);
  fprintf('Hello from Matlab client: iterations = %d\n', iterations);

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

  % Iterate over Fibonacci sequence
  for i = 1:iterations
    
    % Call the server and receive response
    fprintf('client(Matlab): Calling fib(%d)\n', i);
    [ret, result] = rpc.call(int32(i));
    if (~ret);
      error('client(Matlab): RPC CALL ERROR');
    end;
    fib = result{1};
    fprintf('client(Matlab): Response fib(%d) = %d\n', i, fib);

    % Log result by sending it to the log connection
    ret = log.send(int32(i), fib);
    if (~ret);
      error('client(Matlab): SEND ERROR');
    end;
  end;

  disp('Goodbye from Matlab client');
  
end

Model YAML:

1
2
3
4
5
6
7
---

model:
  name: server
  language: python
  args: ./src/server.py
  is_server: True  # Creates a RPC server queue called "server"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
---

model:
  name: client
  language: matlab
  args:
    - ./src/client.m
    - 3  # Pass the number of iterations that should be performed
  client_of: server  # Creates an RPC client queue "server_client"
  outputs: output_log

connections:
  input: output_log
  output: client_output.txt
  in_temp: True

C Version

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
import numpy as np
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."""

    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(np.int32(result))
        if not flag:
            raise RuntimeError('server: ERROR sending')

    print('Goodbye from Python server')

    
if __name__ == '__main__':
    main()
 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
#include "CisInterface.h"
#include <stdio.h>


int main(int argc, char *argv[]) {
   
  int iterations = atoi(argv[1]);
  int exit_code = 0;
  printf("Hello from C client: iterations %d\n", iterations);
  
  // Set up connections matching yaml
  // RPC client-side connection will be $(server_name)_$(client_name)
  cisRpc_t rpc = cisRpcClient("server_client", "%d", "%d");
  cisOutput_t log = cisOutputFmt("output_log", "fib(%-2d) = %-2d\n");

  // Initialize variables
  int ret = 0;
  int fib = -1;
  char *logmsg = (char*)malloc(CIS_MSG_MAX*sizeof(char));
  int i;

  // Iterate over Fibonacci sequence
  for (i = 1; i <= iterations; i++) {
    
    // Call the server and receive response
    printf("client(C): Calling fib(%d)\n", i);
    ret = rpcCall(rpc, i, &fib);
    if (ret < 0) {
      printf("client(C): RPC CALL ERROR\n");
      exit_code = -1;
      break;
    }
    printf("client(C): Response fib(%d) = %d\n", i, fib);

    // Log result by sending it to the log connection
    ret = cisSend(log, i, fib);
    if (ret < 0) {
      printf("client(C): SEND ERROR\n");
      exit_code = -1;
      break;
    }
  }

  free(logmsg);
  printf("Goodbye from C client\n");
  return exit_code;
    
}

Model YAML:

1
2
3
4
5
6
7
---

model:
  name: server
  language: python
  args: ./src/server.py
  is_server: True  # Creates a RPC server queue called "server"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
---

model:
  name: client
  language: c
  args:
    - ./src/client.c
    - 3  # Pass the number of iterations that should be performed
  client_of: server  # Creates an RPC client queue "server_client"
  outputs: output_log

connections:
  input: output_log
  output: client_output.txt
  in_temp: True

C++ Version

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
import numpy as np
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."""

    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(np.int32(result))
        if not flag:
            raise RuntimeError('server: ERROR sending')

    print('Goodbye from Python server')

    
if __name__ == '__main__':
    main()
 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
#include "CisInterface.hpp"
#include <stdio.h>


int main(int argc, char *argv[]) {
   
  int iterations = atoi(argv[1]);
  int exit_code = 0;
  printf("Hello from C++ client: iterations %d\n", iterations);
  
  // Set up connections matching yaml
  // RPC client-side connection will be $(server_name)_$(client_name)
  CisRpcClient rpc("server_client", "%d", "%d");
  CisOutput log("output_log", "fib(%-2d) = %-2d\n");
  
  // Initialize variables
  int ret = 0;
  int fib = -1;
  char *logmsg = (char*)malloc(CIS_MSG_MAX*sizeof(char));
  int i;

  // Iterate over Fibonacci sequence
  for (i = 1; i <= iterations; i++) {
    
    // Call the server and receive response
    printf("client(C++): Calling fib(%d)\n", i);
    ret = rpc.call(2, i, &fib);
    if (ret < 0) {
      printf("client(C++): RPC CALL ERROR\n");
      exit_code = -1;
      break;
    }
    printf("client(C++): Response fib(%d) = %d\n", i, fib);

    // Log result by sending it to the log connection
    ret = log.send(2, i, fib);
    if (ret < 0) {
      printf("client(C++): SEND ERROR\n");
      exit_code = -1;
      break;
    }
  }

  free(logmsg);
  printf("Goodbye from C++ client\n");
  return exit_code;
}

Model YAML:

1
2
3
4
5
6
7
---

model:
  name: server
  language: python
  args: ./src/server.py
  is_server: True  # Creates a RPC server queue called "server"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
---

model:
  name: client
  language: c++
  args:
    - ./src/client.cpp
    - 3  # Pass the number of iterations that should be performed
  client_of: server  # Creates an RPC client queue "server_client"
  outputs: output_log

connections:
  input: output_log
  output: client_output.txt
  in_temp: True