maxMsg

Two models run in a client-server, remote procedure call (RPC) communication pattern. The client model maxMsgCli sends requests and receives response to/from the server model maxMsgSrv via the deprecated rpcCall method (use call instead). The server model receives requests and send responses. This example demostrates the RPC pattern through the use of the client_of and is_server model parameters and the RPC interface classes/functions.

Mixed Version

Model Code:

 1import sys
 2import random
 3from yggdrasil.interface.YggInterface import YggRpcClient, YGG_MSG_BUF
 4
 5
 6def rand_str(length):
 7    charset = ("0123456789"
 8               + "abcdefghijklmnopqrstuvwxyz"
 9               + "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
10    out = ''
11    while (len(out) < (length - 1)):
12        index = int(random.random() * len(charset))
13        out += charset[index]
14    if sys.version_info[0] == 3:
15        out = out.encode("utf-8")
16    return out
17
18
19msg_size = YGG_MSG_BUF
20
21print("maxMsgCli(P): Hello message size %d." % msg_size)
22
23# Create a max message, send/recv and verify
24rpc = YggRpcClient("maxMsgSrv_maxMsgCli", "%s", "%s")
25
26# Create a max message
27output = rand_str(msg_size)
28
29# Call RPC server
30ret, input = rpc.rpcCall(output)
31if not ret:
32    raise RuntimeError("maxMsgCli(P): RPC ERROR")
33
34# Check to see if response matches
35if (input[0] != output):
36    raise RuntimeError("maxMsgCli(P): ERROR: input/output do not match")
37else:
38    print("maxMsgCli(P): CONFIRM")
39
40# All done, say goodbye
41print("maxMsgCli(P): Goodbye!")
 1disp('maxMsgSrv(M): Hello!');
 2
 3rpc = YggInterface('YggRpcServer', 'maxMsgSrv', '%s', '%s');
 4
 5while (1)
 6  [flag, vars] = rpc.recv();
 7  if (~flag)
 8    break;
 9  end
10  fprintf('maxMsgSrv(M): rpcRecv returned %d, input %.10s...\n', ...
11	  flag, char(vars{1}));
12  flag = rpc.send(vars{1});
13  if (~flag)
14    error('maxMsgSrv(M): Error sending reply.');
15  end
16end
17
18disp('maxMsgSrv(M): Goodbye!');

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: PythonModelDriver
6  args: ./src/maxMsgCli.py
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: MatlabModelDriver
6  args: ./src/maxMsgSrv.m
7  is_server: True

Mixed w/o Matlab Version

Model Code:

 1import sys
 2import random
 3from yggdrasil.interface.YggInterface import YggRpcClient, YGG_MSG_BUF
 4
 5
 6def rand_str(length):
 7    charset = ("0123456789"
 8               + "abcdefghijklmnopqrstuvwxyz"
 9               + "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
10    out = ''
11    while (len(out) < (length - 1)):
12        index = int(random.random() * len(charset))
13        out += charset[index]
14    if sys.version_info[0] == 3:
15        out = out.encode("utf-8")
16    return out
17
18
19msg_size = YGG_MSG_BUF
20
21print("maxMsgCli(P): Hello message size %d." % msg_size)
22
23# Create a max message, send/recv and verify
24rpc = YggRpcClient("maxMsgSrv_maxMsgCli", "%s", "%s")
25
26# Create a max message
27output = rand_str(msg_size)
28
29# Call RPC server
30ret, input = rpc.rpcCall(output)
31if not ret:
32    raise RuntimeError("maxMsgCli(P): RPC ERROR")
33
34# Check to see if response matches
35if (input[0] != output):
36    raise RuntimeError("maxMsgCli(P): ERROR: input/output do not match")
37else:
38    print("maxMsgCli(P): CONFIRM")
39
40# All done, say goodbye
41print("maxMsgCli(P): Goodbye!")
 1#include "YggInterface.h"
 2#include <stdio.h>
 3
 4
 5int main(int argc, char *argv[]) {  
 6
 7    printf("maxMsgSrv(C): Hello!\n");
 8    yggRpc_t rpc = yggRpcServer("maxMsgSrv", "%s", "%s");
 9    size_t input_size = YGG_MSG_BUF;
10    char *input = (char*)malloc(input_size);
11    //char input[YGG_MSG_BUF];
12
13    while (1) {
14      // Reset to size of buffer if not all utilized
15      if (input_size < YGG_MSG_BUF)
16	input_size = YGG_MSG_BUF;
17      
18      int ret = rpcRecvRealloc(rpc, &input, &input_size);
19      if (ret < 0)
20        break;
21      printf("maxMsgSrv(C): rpcRecv returned %d, input (size=%lu) %.10s...\n",
22	     ret, input_size, input);
23      ret = rpcSend(rpc, input, input_size);
24      if (ret < 0) {
25        printf("maxMsgSrv(C): SEND ERROR\n");
26        break;
27      }
28    }
29
30    printf("maxMsgSrv(C): Goodbye!\n");
31    free(input);
32    return 0;
33}
34

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: PythonModelDriver
6  args: ./src/maxMsgCli.py
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: GCCModelDriver
6  args: ./src/maxMsgSrv.c
7  is_server: True

C Version

Model Code:

 1#include "YggInterface.h"
 2#include <stdio.h>
 3
 4
 5void rand_str(char *dest, size_t length) {
 6  char charset[] = "0123456789"
 7    "abcdefghijklmnopqrstuvwxyz"
 8    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 9  while (length-- > 1) {
10    size_t index = (size_t)((double) rand() / RAND_MAX * (sizeof charset - 1));
11    *dest++ = charset[index];
12  }
13  *dest = '\0';
14}
15
16
17int main(int argc, char *argv[]) {
18
19  //char output[YGG_MSG_BUF];
20  //char input[YGG_MSG_BUF];
21  size_t msg_size_output = YGG_MSG_BUF;
22  size_t msg_size_input = YGG_MSG_BUF;
23  char *output = (char*)malloc(msg_size_output);
24  char *input = (char*)malloc(msg_size_input);
25
26  printf("maxMsgCli(C): Hello message size is %d.\n", (int)msg_size_output);
27  
28  // Create a max message, send/recv and verify
29  yggRpc_t rpc = yggRpcClient("maxMsgSrv_maxMsgCli", "%s", "%s");
30    
31  // Create a max message
32  rand_str(output, msg_size_output);
33  printf("maxMsgCli(C): sending %.10s...\n", output);
34    
35  // Call RPC server
36  if (rpcCallRealloc(rpc, output, msg_size_output-1,
37		     &input, &msg_size_input) < 0) {
38    printf("maxMsgCli(C): RPC ERROR\n");
39    free(output);
40    free(input);
41    return -1;
42  }
43  printf("maxMsgCli(C): received %lu bytes: %.10s...\n", msg_size_input, input);
44
45  // Check to see if response matches
46  if (strncmp(output, input, msg_size_output-1)) {
47    printf("maxMsgCli(C): ERROR: input/output do not match\n");
48    free(output);
49    free(input);
50    return -1;
51  } else {
52    printf("maxMsgCli(C): CONFIRM\n");
53  }
54
55  // All done, free and say goodbye
56  printf("maxMsgCli(C): Goodbye!\n");
57  free(output);
58  free(input);
59  return 0;
60}
61
 1#include "YggInterface.h"
 2#include <stdio.h>
 3
 4
 5int main(int argc, char *argv[]) {  
 6
 7    printf("maxMsgSrv(C): Hello!\n");
 8    yggRpc_t rpc = yggRpcServer("maxMsgSrv", "%s", "%s");
 9    size_t input_size = YGG_MSG_BUF;
10    char *input = (char*)malloc(input_size);
11    //char input[YGG_MSG_BUF];
12
13    while (1) {
14      // Reset to size of buffer if not all utilized
15      if (input_size < YGG_MSG_BUF)
16	input_size = YGG_MSG_BUF;
17      
18      int ret = rpcRecvRealloc(rpc, &input, &input_size);
19      if (ret < 0)
20        break;
21      printf("maxMsgSrv(C): rpcRecv returned %d, input (size=%lu) %.10s...\n",
22	     ret, input_size, input);
23      ret = rpcSend(rpc, input, input_size);
24      if (ret < 0) {
25        printf("maxMsgSrv(C): SEND ERROR\n");
26        break;
27      }
28    }
29
30    printf("maxMsgSrv(C): Goodbye!\n");
31    free(input);
32    return 0;
33}
34

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: GCCModelDriver
6  args: ./src/maxMsgCli.c
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: GCCModelDriver
6  args: ./src/maxMsgSrv.c
7  is_server: True

C++ Version

Model Code:

 1#include "YggInterface.hpp"
 2#include <stdio.h>
 3
 4
 5void rand_str(char *dest, size_t length) {
 6  char charset[] = "0123456789"
 7    "abcdefghijklmnopqrstuvwxyz"
 8    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 9  while (length-- > 1) {
10    size_t index = (size_t)((double) rand() / RAND_MAX * (sizeof charset - 1));
11    *dest++ = charset[index];
12  }
13  *dest = '\0';
14}
15
16
17int main(int argc, char *argv[]) {
18
19    //char output[YGG_MSG_BUF];
20    //char input[YGG_MSG_BUF];
21    size_t msg_size_output = YGG_MSG_BUF;
22    size_t msg_size_input = YGG_MSG_BUF;
23    char *output = (char*)malloc(msg_size_output);
24    char *input = (char*)malloc(msg_size_input);
25  
26    printf("maxMsgCli(CPP): Hello message size is %d.\n", (int)msg_size_output);
27    
28    // Create a max message, send/recv and verify
29    YggRpcClient rpc("maxMsgSrv_maxMsgCli", "%s", "%s");
30    
31    // Create a max message
32    rand_str(output, msg_size_output);
33    printf("maxMsgCli(CPP): sending %.10s...\n", output);
34    
35    // Call RPC server
36    if (rpc.callRealloc(4, output, msg_size_output-1,
37			&input, &msg_size_input) < 0) {
38      printf("maxMsgCli(CPP): RPC ERROR\n");
39      free(output);
40      free(input);
41      return -1;
42    }
43    printf("maxMsgCli(CPP): received %lu bytes: %.10s...\n", msg_size_input, input);
44
45    // Check to see if response matches
46    if (strncmp(output, input, msg_size_output-1)) {
47        printf("maxMsgCli(CPP): ERROR: input/output do not match\n");
48        free(output);
49        free(input);
50	    return -1;
51    } else {
52        printf("maxMsgCli(CPP): CONFIRM\n");
53    }
54
55    // All done, say goodbye
56    printf("maxMsgCli(CPP): Goodbye!\n");
57
58    free(output);
59    free(input);
60    return 0;
61}
 1#include "YggInterface.hpp"
 2#include <stdio.h>
 3
 4
 5int main(int argc, char *argv[]) {  
 6
 7    printf("maxMsgSrv(CPP): Hello!\n");
 8    YggRpcServer rpc("maxMsgSrv", "%s", "%s");
 9    size_t input_size = YGG_MSG_BUF;
10    char *input = (char*)malloc(input_size);
11    //char input[YGG_MSG_MAX];
12    
13    while (1) {
14      int ret = rpc.recvRealloc(2, &input, &input_size);
15      if (ret < 0)
16	break;
17      printf("maxMsgSrv(CPP): rpcRecv returned %d, input (size=%lu) %.10s...\n",
18	     ret, input_size, input);
19      ret = rpc.send(2, input, input_size);
20      if (ret < 0) {
21        printf("maxMsgSrv(CPP): SEND ERROR");
22        break;
23      }
24    }
25
26    printf("maxMsgSrv(CPP): Goodbye!\n");
27    return 0;
28}

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: GCCModelDriver
6  args: ./src/maxMsgCli.cpp
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: GCCModelDriver
6  args: ./src/maxMsgSrv.cpp
7  is_server: True

Fortran Version

Model Code:

 1program main
 2  use fygg
 3
 4  integer(kind=c_size_t) :: msg_size_output = YGG_MSG_BUF
 5  integer(kind=c_size_t) :: msg_size_input = YGG_MSG_BUF
 6  character, dimension(YGG_MSG_BUF) :: output
 7  type(yggchar_r) :: input
 8  type(yggcomm) :: rpc
 9  logical :: ret
10
11  print *, "maxMsgCli(F): Hello message size is ", msg_size_output
12
13  ! Create a max message, send/recv and verify
14  rpc = ygg_rpc_client("maxMsgSrv_maxMsgCli", "%s", "%s")
15
16  ! Create a max message
17  call rand_str(output, msg_size_output)
18  print *, "maxMsgCli(F): sending ", output(1:10), "..."
19
20  ! Call RPC server
21  msg_size_input = size(input%x)
22  ret = ygg_rpc_call_realloc(rpc, &
23       [yggarg(output), yggarg(msg_size_output)], &
24       [yggarg(input), yggarg(msg_size_input)])
25  if (.not.ret) then
26     print *, "maxMsgCli(F): RPC ERROR"
27     stop 1
28  end if
29  print *, "maxMsgCli(F): received ", msg_size_input, " bytes: ", &
30       input%x(1:10), "..."
31
32  ! Check to see if response matches
33  if (.not.all(output.eq.(input%x))) then
34     print *, "maxMsgCli(F): ERROR: input/output do not match"
35     stop 1
36  else
37     print *, "maxMsgCli(F): CONFIRM"
38  end if
39
40  ! All done, free and say goodbye
41  print *, "maxMsgCli(F): Goodbye!"
42
43end program main
44
45subroutine rand_str(x, x_siz)
46  use fygg
47  character, dimension(*) :: x
48  real :: x_rand
49  integer(kind=c_size_t) :: x_siz, i, index
50  character(len=62) :: charset
51  charset = "0123456789&
52       &abcdefghijklmnopqrstuvwxyz&
53       &ABCDEFGHIJKLMNOPQRSTUVWXYZ"
54  do i = 1, x_siz
55     call random_number(x_rand)
56     index = int(x_rand * (len(charset) - 1), c_size_t) + 1
57     x(i) = charset(index:index)
58  end do
59  ! do i = (x_siz+1), size(x)
60  !    x(i) = ' '
61  ! end do
62end subroutine rand_str
63
 1program main
 2  use fygg
 3
 4  type(yggcomm) :: rpc
 5  integer(kind=c_size_t) :: input_size
 6  type(yggchar_r) :: input
 7  logical :: ret
 8
 9  print *, "maxMsgSrv(F): Hello!"
10  rpc = ygg_rpc_server("maxMsgSrv", "%s", "%s")
11
12  do while (.true.)
13     ! Reset to size of buffer if not all utilized
14     input_size = size(input%x)
15
16     ret = ygg_recv_var_realloc(rpc, [yggarg(input), yggarg(input_size)])
17     if (.not.ret) exit
18     print *, "maxMsgSrv(F): rpcRecv returned ", ret, ", input (size=", &
19          input_size, ") ", input%x(1:10)
20     ret = ygg_send_var(rpc, [yggarg(input), yggarg(input_size)])
21     if (.not.ret) then
22        print *, "maxMsgSrv(F): SEND ERROR"
23        exit
24     end if
25
26  end do
27
28  print *, "maxMsgSrv(F): Goodbye!"
29
30end program main

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: FortranModelDriver
6  args: ./src/maxMsgCli.f90
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: FortranModelDriver
6  args: ./src/maxMsgSrv.f90
7  is_server: True

Julia Version

Model Code:

 1using Yggdrasil
 2using Printf
 3using Random
 4
 5
 6msg_size = Yggdrasil.YggInterface("YGG_MSG_BUF")
 7@printf("maxMsgCli(Julia): Hello message size %d.\n", msg_size)
 8
 9# Create a max message, send/recv and verify
10rpc = Yggdrasil.YggInterface("YggRpcClient", "maxMsgSrv_maxMsgCli", "%s", "%s")
11
12# Create a max message
13output = Yggdrasil.Bytes(Random.randstring(msg_size))
14
15# Call RPC server
16ret, input = rpc.call(output)
17if (!ret)
18    error("maxMsgCli(Julia): RPC ERROR")
19end
20
21# Check to see if response matches
22if (input[1] != output)
23    error("maxMsgCli(Julia): ERROR: input/output do not match")
24else
25    println("maxMsgCli(Julia): CONFIRM")
26end
27
28# All done, say goodbye
29println("maxMsgCli(Julia): Goodbye!")
 1# Import library for input/output channels
 2using Yggdrasil
 3using Printf
 4
 5
 6println("maxMsgSrv(Julia): Hello!")
 7rpc = Yggdrasil.YggInterface("YggRpcServer", "maxMsgSrv", "%s", "%s")
 8
 9while true
10    ret, input = rpc.recv()
11    if (!ret)
12	break
13    end
14    @printf("maxMsgSrv(Julia): rpcRecv returned %s, input %.10s...\n", ret, input[1])
15    flag = rpc.send(input[1])
16    if (!flag)
17        error("maxMsgSrv(Julia): Error sending reply.")
18    end
19end  # while
20
21println("maxMsgSrv(Julia): Goodbye!")

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: JuliaModelDriver
6  args: ./src/maxMsgCli.jl
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: JuliaModelDriver
6  args: ./src/maxMsgSrv.jl
7  is_server: True

Matlab Version

Model Code:

 1charset = ['0123456789', ...
 2	   'abcdefghijklmnopqrstuvwxyz', ...
 3	   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
 4
 5
 6msg_size = YggInterface('YGG_MSG_BUF');
 7fprintf('maxMsgCli(M): Hello message size is %d.\n', msg_size);
 8
 9% Create a max message, send/recv and verify
10rpc = YggInterface('YggRpcClient', 'maxMsgSrv_maxMsgCli', '%s', '%s');
11
12% Create a max message
13output = string(randsample(charset, msg_size-1, true));
14
15% Call RPC server
16[flag, vars] = rpc.call(output);
17if (~flag)
18  error('maxMsgCli(M): RPC ERROR');
19end;
20
21% Check to see if response matches
22if (vars{1} ~= output)
23  error('maxMsgCli(M): ERROR: input/output do not match');
24else
25  disp('maxMsgCli(M): CONFIRM');
26end;
27
28% All done, say goodbye
29disp('maxMsgCli(M): Goodbye!');
 1disp('maxMsgSrv(M): Hello!');
 2
 3rpc = YggInterface('YggRpcServer', 'maxMsgSrv', '%s', '%s');
 4
 5while (1)
 6  [flag, vars] = rpc.recv();
 7  if (~flag)
 8    break;
 9  end
10  fprintf('maxMsgSrv(M): rpcRecv returned %d, input %.10s...\n', ...
11	  flag, char(vars{1}));
12  flag = rpc.send(vars{1});
13  if (~flag)
14    error('maxMsgSrv(M): Error sending reply.');
15  end
16end
17
18disp('maxMsgSrv(M): Goodbye!');

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: MatlabModelDriver
6  args: ./src/maxMsgCli.m
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: MatlabModelDriver
6  args: ./src/maxMsgSrv.m
7  is_server: True

Python Version

Model Code:

 1import sys
 2import random
 3from yggdrasil.interface.YggInterface import YggRpcClient, YGG_MSG_BUF
 4
 5
 6def rand_str(length):
 7    charset = ("0123456789"
 8               + "abcdefghijklmnopqrstuvwxyz"
 9               + "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
10    out = ''
11    while (len(out) < (length - 1)):
12        index = int(random.random() * len(charset))
13        out += charset[index]
14    if sys.version_info[0] == 3:
15        out = out.encode("utf-8")
16    return out
17
18
19msg_size = YGG_MSG_BUF
20
21print("maxMsgCli(P): Hello message size %d." % msg_size)
22
23# Create a max message, send/recv and verify
24rpc = YggRpcClient("maxMsgSrv_maxMsgCli", "%s", "%s")
25
26# Create a max message
27output = rand_str(msg_size)
28
29# Call RPC server
30ret, input = rpc.rpcCall(output)
31if not ret:
32    raise RuntimeError("maxMsgCli(P): RPC ERROR")
33
34# Check to see if response matches
35if (input[0] != output):
36    raise RuntimeError("maxMsgCli(P): ERROR: input/output do not match")
37else:
38    print("maxMsgCli(P): CONFIRM")
39
40# All done, say goodbye
41print("maxMsgCli(P): Goodbye!")
 1from yggdrasil.interface.YggInterface import YggRpcServer
 2
 3
 4print("maxMsgSrv(P): Hello!")
 5rpc = YggRpcServer("maxMsgSrv", "%s", "%s")
 6
 7while True:
 8    ret, input = rpc.rpcRecv()
 9    if not ret:
10        break
11    print("maxMsgSrv(P): rpcRecv returned %s, input %.10s..." % (ret, input[0]))
12    flag = rpc.rpcSend(input[0])
13    if not flag:
14        raise RuntimeError('maxMsgSrv(P): Error sending reply.')
15
16print("maxMsgSrv(P): Goodbye!")

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: PythonModelDriver
6  args: ./src/maxMsgCli.py
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: PythonModelDriver
6  args: ./src/maxMsgSrv.py
7  is_server: True

R Version

Model Code:

 1library(yggdrasil)
 2
 3
 4charset = strsplit("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", '')[[1]]
 5
 6msg_size = YggInterface('YGG_MSG_BUF')
 7fprintf("maxMsgCli(R): Hello message size %d.", msg_size)
 8
 9# Create a max message, send/recv and verify
10rpc <- YggInterface('YggRpcClient', "maxMsgSrv_maxMsgCli", "%s", "%s")
11
12# Create a max message
13output <- ygg_bytes(paste(sample(charset, msg_size, replace=TRUE), collapse=''))
14
15# Call RPC server
16c(ret, input) %<-% rpc$call(output)
17if (!ret) {
18  stop("maxMsgCli(R): RPC ERROR")
19}
20
21# Check to see if response matches
22if (input[[1]] != output) {
23  stop("maxMsgCli(R): ERROR: input/output do not match")
24} else {
25  print("maxMsgCli(R): CONFIRM")
26}
27
28# All done, say goodbye
29print("maxMsgCli(R): Goodbye!")
 1library(yggdrasil)
 2
 3
 4print("maxMsgSrv(R): Hello!")
 5rpc <- YggInterface('YggRpcServer', "maxMsgSrv", "%s", "%s")
 6
 7while (TRUE) {
 8  c(ret, input) %<-% rpc$recv()
 9  if (!ret) {
10    break
11  }
12  fprintf("maxMsgSrv(R): rpcRecv returned %s, input %.10s...", ret, input[[1]])
13  flag <- rpc$send(input[[1]])
14  if (!flag) {
15    stop('maxMsgSrv(R): Error sending reply.')
16  }
17}
18
19print("maxMsgSrv(R): Goodbye!")

Model YAML:

1---
2
3model:
4  name: maxMsgCli
5  driver: RModelDriver
6  args: ./src/maxMsgCli.R
7  client_of: maxMsgSrv
1---
2
3model:
4  name: maxMsgSrv
5  driver: RModelDriver
6  args: ./src/maxMsgSrv.R
7  is_server: True