home *** CD-ROM | disk | FTP | other *** search
/ ftp.parl.clemson.edu / 2015-02-07.ftp.parl.clemson.edu.tar / ftp.parl.clemson.edu / pub / pvfs2 / orangefs-2.8.3-20110323.tar.gz / orangefs-2.8.3-20110323.tar / orangefs / test / io / bmi / test-bmi-client-list-gm.c < prev    next >
C/C++ Source or Header  |  2004-07-28  |  9KB  |  402 lines

  1. /*
  2.  * (C) 2001 Clemson University and The University of Chicago
  3.  *
  4.  * See COPYING in top-level directory.
  5.  */
  6.  
  7.  
  8.  
  9.  
  10. /*
  11.  * This is an example of a client program that uses the BMI library
  12.  * for communications.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. #include "bmi.h"
  22. #include "test-bmi.h"
  23. #include "gossip.h"
  24.  
  25. /**************************************************************
  26.  * Data structures 
  27.  */
  28.  
  29. /* A little structure to hold program options, either defaults or
  30.  * specified on the command line 
  31.  */
  32. struct options
  33. {
  34.     char *hostid;        /* host identifier */
  35. };
  36.  
  37. #if 0
  38. /* for testing eager mode */
  39. #define MSG1_SIZE 1024
  40. #define MSG2_SIZE 2048
  41. #define MSG3_SIZE 3072
  42. #else
  43. /* for testing rendezvous mode */
  44. #define MSG1_SIZE (1024*8)
  45. #define MSG2_SIZE (2048*8)
  46. #define MSG3_SIZE (3072*8)
  47. #endif
  48. /**************************************************************
  49.  * Internal utility functions
  50.  */
  51.  
  52. static struct options *parse_args(
  53.     int argc,
  54.     char *argv[]);
  55.  
  56.  
  57. /**************************************************************/
  58.  
  59. int main(
  60.     int argc,
  61.     char **argv)
  62. {
  63.  
  64.     struct options *user_opts = NULL;
  65.     struct server_request *my_req = NULL;
  66.     struct server_ack *my_ack = NULL;
  67.     int ret = -1;
  68.     PVFS_BMI_addr_t server_addr;
  69.     void *send_buffer1 = NULL;
  70.     void *send_buffer2 = NULL;
  71.     void *send_buffer3 = NULL;
  72.     bmi_op_id_t client_ops[2];
  73.     int outcount = 0;
  74.     bmi_error_code_t error_code;
  75.     void *in_test_user_ptr = &server_addr;
  76.     void *out_test_user_ptr = NULL;
  77.     bmi_size_t actual_size;
  78.     void *buffer_list[3];
  79.     bmi_size_t size_list[3];
  80.     int i = 0;
  81.     int last = 0;
  82.     bmi_context_id context;
  83.  
  84.     /* grab any command line options */
  85.     user_opts = parse_args(argc, argv);
  86.     if (!user_opts)
  87.     {
  88.     return (-1);
  89.     }
  90.  
  91.     /* set debugging stuff */
  92.     gossip_enable_stderr();
  93.     gossip_set_debug_mask(1, GOSSIP_BMI_DEBUG_ALL);
  94.  
  95.     /* initialize local interface */
  96.     ret = BMI_initialize("bmi_gm", NULL, 0);
  97.     if (ret < 0)
  98.     {
  99.     errno = -ret;
  100.     perror("BMI_initialize");
  101.     return (-1);
  102.     }
  103.  
  104.     ret = BMI_open_context(&context);
  105.     if (ret < 0)
  106.     {
  107.     errno = -ret;
  108.     perror("BMI_open_context()");
  109.     return (-1);
  110.     }
  111.  
  112.     /* get a bmi_addr for the server */
  113.     ret = BMI_addr_lookup(&server_addr, user_opts->hostid);
  114.     if (ret < 0)
  115.     {
  116.     errno = -ret;
  117.     perror("BMI_addr_lookup");
  118.     return (-1);
  119.     }
  120.  
  121.     /* allocate a buffer for the initial request and ack */
  122.     my_req = (struct server_request *) BMI_memalloc(server_addr,
  123.                             sizeof(struct
  124.                                server_request),
  125.                             BMI_SEND);
  126.     my_ack =
  127.     (struct server_ack *) BMI_memalloc(server_addr,
  128.                        sizeof(struct server_ack), BMI_RECV);
  129.     if (!my_req || !my_ack)
  130.     {
  131.     fprintf(stderr, "BMI_memalloc failed.\n");
  132.     return (-1);
  133.     }
  134.  
  135.     my_req->size = MSG1_SIZE + MSG2_SIZE + MSG3_SIZE;
  136.  
  137.     /* send the initial request on its way */
  138.     buffer_list[0] = my_req;
  139.     size_list[0] = sizeof(struct server_request);
  140.     ret = BMI_post_sendunexpected_list(&(client_ops[1]), server_addr,
  141.                        (const void **) buffer_list, size_list,
  142.                        1, size_list[0], BMI_PRE_ALLOC, 0,
  143.                        in_test_user_ptr, context);
  144.     if (ret < 0)
  145.     {
  146.     errno = -ret;
  147.     perror("BMI_post_send");
  148.     return (-1);
  149.     }
  150.     if (ret == 0)
  151.     {
  152.     /* turning this into a blocking call for testing :) */
  153.     /* check for completion of request */
  154.     do
  155.     {
  156.         ret = BMI_test(client_ops[1], &outcount, &error_code, &actual_size,
  157.                &out_test_user_ptr, 10, context);
  158.     } while (ret == 0 && outcount == 0);
  159.  
  160.     if (ret < 0 || error_code != 0)
  161.     {
  162.         fprintf(stderr, "Request send failed.\n");
  163.         if (ret < 0)
  164.         {
  165.         errno = -ret;
  166.         perror("BMI_test");
  167.         }
  168.         return (-1);
  169.     }
  170.  
  171.     if (in_test_user_ptr != out_test_user_ptr)
  172.     {
  173.         fprintf(stderr, "1st ptr failure.\n");
  174.     }
  175.     else
  176.     {
  177.         fprintf(stderr, "1st ptr success.\n");
  178.     }
  179.     out_test_user_ptr = NULL;
  180.     }
  181.  
  182.     /* post a recv for the server acknowledgement */
  183.     buffer_list[0] = my_ack;
  184.     size_list[0] = sizeof(struct server_ack);
  185.  
  186.     ret = BMI_post_recv_list(&(client_ops[0]), server_addr, buffer_list,
  187.                  size_list, 1, sizeof(struct server_ack),
  188.                  &actual_size, BMI_PRE_ALLOC, 0, in_test_user_ptr,
  189.                  context);
  190.     if (ret < 0)
  191.     {
  192.     errno = -ret;
  193.     perror("BMI_post_recv");
  194.     return (-1);
  195.     }
  196.     if (ret == 0)
  197.     {
  198.     /* turning this into a blocking call for testing :) */
  199.     /* check for completion of ack recv */
  200.     do
  201.     {
  202.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  203.                &actual_size, &out_test_user_ptr, 10, context);
  204.     } while (ret == 0 && outcount == 0);
  205.  
  206.     if (ret < 0 || error_code != 0)
  207.     {
  208.         fprintf(stderr, "Ack recv failed.\n");
  209.         return (-1);
  210.     }
  211.     if (in_test_user_ptr != out_test_user_ptr)
  212.     {
  213.         fprintf(stderr, "2nd ptr failure.\n");
  214.     }
  215.     else
  216.     {
  217.         fprintf(stderr, "2nd ptr success.\n");
  218.     }
  219.     out_test_user_ptr = NULL;
  220.     }
  221.     else
  222.     {
  223.     if (actual_size != sizeof(struct server_ack))
  224.     {
  225.         printf("Short recv.\n");
  226.         return (-1);
  227.     }
  228.     }
  229.  
  230.     /* look at the ack */
  231.     if (my_ack->status != 0)
  232.     {
  233.     fprintf(stderr, "Request denied.\n");
  234.     return (-1);
  235.     }
  236.  
  237.     /* create 3 buffers to send */
  238.     send_buffer1 = BMI_memalloc(server_addr, MSG1_SIZE, BMI_SEND);
  239.     send_buffer2 = BMI_memalloc(server_addr, MSG2_SIZE, BMI_SEND);
  240.     send_buffer3 = BMI_memalloc(server_addr, MSG3_SIZE, BMI_SEND);
  241.     if (!send_buffer1 || !send_buffer2 || !send_buffer3)
  242.     {
  243.     fprintf(stderr, "BMI_memalloc.\n");
  244.     return (-1);
  245.     }
  246.     buffer_list[0] = send_buffer1;
  247.     buffer_list[1] = send_buffer2;
  248.     buffer_list[2] = send_buffer3;
  249.     size_list[0] = MSG1_SIZE;
  250.     size_list[1] = MSG2_SIZE;
  251.     size_list[2] = MSG3_SIZE;
  252.  
  253.     for (i = 0; i < (MSG1_SIZE / sizeof(int)); i++)
  254.     {
  255.     ((int *) send_buffer1)[i] = i;
  256.     }
  257.     last = i;
  258.     for (i = last; i < (last + (MSG2_SIZE / sizeof(int))); i++)
  259.     {
  260.     ((int *) send_buffer2)[i - last] = i;
  261.     }
  262.     last = i;
  263.     for (i = last; i < (last + (MSG3_SIZE / sizeof(int))); i++)
  264.     {
  265.     ((int *) send_buffer3)[i - last] = i;
  266.     }
  267.  
  268.     /* send the data payload on its way */
  269.     ret = BMI_post_send_list(&(client_ops[0]), server_addr,
  270.                  (const void **) buffer_list, size_list, 3,
  271.                  (MSG1_SIZE + MSG2_SIZE + MSG3_SIZE),
  272.                  BMI_PRE_ALLOC, 0, in_test_user_ptr, context);
  273.     if (ret < 0)
  274.     {
  275.     errno = -ret;
  276.     perror("BMI_post_send");
  277.     return (-1);
  278.     }
  279.     if (ret == 0)
  280.     {
  281.     /* turning this into a blocking call for testing :) */
  282.     /* check for completion of data payload send */
  283.     do
  284.     {
  285.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  286.                &actual_size, &out_test_user_ptr, 10, context);
  287.     } while (ret == 0 && outcount == 0);
  288.  
  289.     if (ret < 0 || error_code != 0)
  290.     {
  291.         fprintf(stderr, "Data payload send failed.\n");
  292.         return (-1);
  293.     }
  294.     if (in_test_user_ptr != out_test_user_ptr)
  295.     {
  296.         fprintf(stderr, "3rd ptr failure.\n");
  297.     }
  298.     else
  299.     {
  300.         fprintf(stderr, "3rd ptr success.\n");
  301.     }
  302.     out_test_user_ptr = NULL;
  303.     }
  304.  
  305.     /* free up the message buffers */
  306.     BMI_memfree(server_addr, send_buffer1, MSG1_SIZE, BMI_SEND);
  307.     BMI_memfree(server_addr, send_buffer2, MSG2_SIZE, BMI_SEND);
  308.     BMI_memfree(server_addr, send_buffer3, MSG3_SIZE, BMI_SEND);
  309.     BMI_memfree(server_addr, my_req, sizeof(struct server_request), BMI_SEND);
  310.     BMI_memfree(server_addr, my_ack, sizeof(struct server_ack), BMI_RECV);
  311.  
  312.     /* shutdown the local interface */
  313.     BMI_close_context(context);
  314.     ret = BMI_finalize();
  315.     if (ret < 0)
  316.     {
  317.     errno = -ret;
  318.     perror("BMI_finalize");
  319.     return (-1);
  320.     }
  321.  
  322.     /* turn off debugging stuff */
  323.     gossip_disable();
  324.  
  325.     free(user_opts->hostid);
  326.     free(user_opts);
  327.  
  328.     return (0);
  329. }
  330.  
  331.  
  332. static struct options *parse_args(
  333.     int argc,
  334.     char *argv[])
  335. {
  336.  
  337.     /* getopt stuff */
  338.     extern char *optarg;
  339.     char flags[] = "h:r:s:c:";
  340.     int one_opt = 0;
  341.  
  342.     struct options *tmp_opts = NULL;
  343.     int len = -1;
  344.  
  345.     /* create storage for the command line options */
  346.     tmp_opts = (struct options *) malloc(sizeof(struct options));
  347.     if (!tmp_opts)
  348.     {
  349.     goto parse_args_error;
  350.     }
  351.  
  352.     /* look at command line arguments */
  353.     while ((one_opt = getopt(argc, argv, flags)) != EOF)
  354.     {
  355.     switch (one_opt)
  356.     {
  357.     case ('h'):
  358.         len = (strlen(optarg)) + 1;
  359.         if ((tmp_opts->hostid = (char *) malloc(len)) == NULL)
  360.         {
  361.         goto parse_args_error;
  362.         }
  363.         memcpy(tmp_opts->hostid, optarg, len);
  364.         break;
  365.     default:
  366.         break;
  367.     }
  368.     }
  369.  
  370.     /* if we didn't get a host argument, fill in a default: */
  371.     len = (strlen(DEFAULT_HOSTID_GM)) + 1;
  372.     if ((tmp_opts->hostid = (char *) malloc(len)) == NULL)
  373.     {
  374.     goto parse_args_error;
  375.     }
  376.     memcpy(tmp_opts->hostid, DEFAULT_HOSTID_GM, len);
  377.  
  378.     return (tmp_opts);
  379.  
  380.   parse_args_error:
  381.  
  382.     /* if an error occurs, just free everything and return NULL */
  383.     if (tmp_opts)
  384.     {
  385.     if (tmp_opts->hostid)
  386.     {
  387.         free(tmp_opts->hostid);
  388.     }
  389.     free(tmp_opts);
  390.     }
  391.     return (NULL);
  392. }
  393.  
  394. /*
  395.  * Local variables:
  396.  *  c-indent-level: 4
  397.  *  c-basic-offset: 4
  398.  * End:
  399.  *
  400.  * vim: ts=8 sts=4 sw=4 expandtab
  401.  */
  402.