A Example

The example consists of a pair of C programs called master and slave. They should be run in the background by:

  % slave &
  % master &

The code for master is:

  /* amsmaster
   * A test of ams - run in conjunction with amsslave
   *   % amsslave &
   *   % amsmaster
  */
  #include <stdio.h>
  #include <sys/types.h>
  #include <sys/time.h>
  #include <string.h>
  #include <signal.h>
  #include <errno.h>
  #include <unistd.h>
  
  #include "sae_par.h"
  #include "adam_defns.h"
  #include "messys_len.h"
  #include "messys_par.h"
  
  #include "ams.h"
  
  int main()
  {
     int outmsg_status;
     int outmsg_function;
     int outmsg_context;
     int outmsg_length;
     char outmsg_name[32];
     char outmsg_value[MSG_VAL_LEN];
     int inmsg_status;
     int inmsg_context;
     int inmsg_length;
     char inmsg_name[32];
     char inmsg_value[MSG_VAL_LEN];
     int status;
     int path;
     int messid;
     int j;
  
     status = 0;
  
  /* Set up components of a GSOC OBEY message. The slave does not care about
   * the name component of the message */
     outmsg_status = SAI__OK;
     outmsg_function = MESSYS__MESSAGE;
     outmsg_context = OBEY;
     outmsg_length = 16;
  
     strcpy ( outmsg_name, "junk" );
     strcpy ( outmsg_value, "master calling" );
  
  /* Register as "master" with the message system */
     ams_init ( "master", &status );
     if ( status != SAI__OK )
     {
        printf ( "master - bad status after ams_init\n" );
     }
  
  /* Get a path to "slave" and report */
     ams_path ( "slave", &path, &status );
     if ( status != SAI__OK )
     {
        printf ( "master - bad status after ams_path\n" );
     }
     else
     {
        printf ( "master - path set up ok\n" );
     }
  
  /* Perform 1000 identical transactions - send a GSOC obey message and
   * await an initial acknowledgement (message_status = DTASK__ACTSTART)
   * and a completion message (message_status = SAI__OK) */
     for ( j=0; j<1000; j++ )
     {
  /* Send the OBEY command */
        ams_send ( path, outmsg_function, outmsg_status, outmsg_context,
          outmsg_name, outmsg_length, outmsg_value, &messid, &status );
  /* Get the acknowledement reply - content not checked */
        ams_getreply ( MESSYS__INFINITE, path, messid, 32, MSG_VAL_LEN,
          &inmsg_status, &inmsg_context, inmsg_name, &inmsg_length,
          inmsg_value, &status );
  /* Get the completion reply - content not checked.
   * AMS will terminate the transaction if it is the expected message status
   * SAI__OK */
        ams_getreply ( MESSYS__INFINITE, path, messid, 32, MSG_VAL_LEN,
          &inmsg_status, &inmsg_context, inmsg_name, &inmsg_length,
          inmsg_value, &status );
     }
  
  /* If all OK, display the last received message value;
   * otherwise display the error status */
     if ( status != 0 )
     {
        printf ( "master: bad status = %d\n", status );
     }
     else
     {
        printf ( "master: received - %s\n", inmsg_value );
     }
  
     return 0;
  }

The code for slave is:

  /* amsslave
   * A test of ams - run in conjunction with amsmaster
   *   % amsslave &
   *   % amsmaster
  */
  #include <stdio.h>
  #include <time.h>
  #include <sys/types.h>
  #include <sys/time.h>
  #include <string.h>
  #include <signal.h>
  #include <errno.h>
  #include <unistd.h>
  
  #include "sae_par.h"
  #include "adam_defns.h"
  #include "dtask_err.h"             /* dtask error codes */
  
  #include "messys_len.h"
  #include "messys_par.h"
  
  #include "ams.h"
  
  int main()
  {
     int outmsg_status;
     int outmsg_function;
     int outmsg_context;
     int outmsg_length;
     char outmsg_name[32];
     char outmsg_value[MSG_VAL_LEN];
     int inmsg_status;
     int inmsg_context;
     int inmsg_length;
     char inmsg_name[32];
     char inmsg_value[MSG_VAL_LEN];
  
     int status;
     int path;
     int messid;
     int j;
  
  /* Set up components of a reply to a GSOC OBEY message. The master does not
   * care about the name component of the message */
     status = 0;
     outmsg_status = SAI__OK;
     outmsg_function = MESSYS__MESSAGE;
     outmsg_context = OBEY;
     outmsg_length = 16;
  
     strcpy ( outmsg_name, "junk" );
     strcpy ( outmsg_value, "slave replying" );
  
  /* Register as "slave" with the message system */
     ams_init ( "slave", &status );
     if ( status != 0 )
     {
        printf ( "slave: failed init\n" );
     }
  
  /* Receive 1000 commands, sending an initial acknowledgement and a completion
   * message in reply to each */
     for ( j=0; j<1000; j++ )
     {
  /* Await a command message */
        ams_receive ( MESSYS__INFINITE, 32, MSG_VAL_LEN, &inmsg_status,
          &inmsg_context, inmsg_name, &inmsg_length, inmsg_value, &path,
          &messid, &status );
  
  /* Send an initial acknowledgement (message_status = DTASK_ACTSTART). */
        outmsg_status = DTASK__ACTSTART;
        ams_reply ( path, messid, outmsg_function, outmsg_status,
          outmsg_context, outmsg_name, outmsg_length, outmsg_value,
          &status );
  
  /* Send a completion message (message_status = SAI__OK) - this will terminate
     the transaction at both ends */
        outmsg_status = SAI__OK;
        ams_reply ( path, messid, outmsg_function, outmsg_status,
          outmsg_context, outmsg_name, outmsg_length, outmsg_value,
          &status );
  
  /* If there was a failure, exit the loop */
        if ( status != SAI__OK ) break;
  
     }
  
  /* If all OK, display the last received message value;
   * otherwise display the error status */
     if ( status != 0 )
     {
        printf ( "slave: bad status = %d\n", status );
     }
     else
     {
        printf ( "slave: received - %s\n", inmsg_value );
     }
  
     return 0;
  }