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