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-gm.c < prev    next >
C/C++ Source or Header  |  2004-07-28  |  10KB  |  460 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.     int message_size;        /* message size */
  36. };
  37.  
  38.  
  39. /**************************************************************
  40.  * Internal utility functions
  41.  */
  42.  
  43. static struct options *parse_args(
  44.     int argc,
  45.     char *argv[]);
  46.  
  47. #define BIG_SIZE 32000
  48.  
  49. /**************************************************************/
  50.  
  51. int main(
  52.     int argc,
  53.     char **argv)
  54. {
  55.  
  56.     struct options *user_opts = NULL;
  57.     struct server_request *my_req = NULL;
  58.     struct server_ack *my_ack = NULL;
  59.     int ret = -1;
  60.     PVFS_BMI_addr_t server_addr;
  61.     bmi_op_id_t client_ops[2];
  62.     int outcount = 0;
  63.     bmi_error_code_t error_code;
  64.     void *send_buffer = NULL;
  65.     bmi_size_t actual_size;
  66.     bmi_context_id context;
  67.     char big_buffer[BIG_SIZE];
  68.  
  69.     /* grab any command line options */
  70.     user_opts = parse_args(argc, argv);
  71.     if (!user_opts)
  72.     {
  73.     return (-1);
  74.     }
  75.  
  76.     /* set debugging stuff */
  77.     gossip_enable_stderr();
  78.     gossip_set_debug_mask(0, GOSSIP_BMI_DEBUG_ALL);
  79.  
  80.     /* initialize local interface */
  81.     ret = BMI_initialize("bmi_gm", NULL, 0);
  82.     if (ret < 0)
  83.     {
  84.     errno = -ret;
  85.     perror("BMI_initialize");
  86.     return (-1);
  87.     }
  88.  
  89.     ret = BMI_open_context(&context);
  90.     if (ret < 0)
  91.     {
  92.     errno = -ret;
  93.     perror("BMI_open_context()");
  94.     return (-1);
  95.     }
  96.  
  97.     /* get a bmi_addr for the server */
  98.     ret = BMI_addr_lookup(&server_addr, user_opts->hostid);
  99.     if (ret < 0)
  100.     {
  101.     errno = -ret;
  102.     perror("BMI_addr_lookup");
  103.     goto client_exit;
  104.     }
  105.  
  106.     /* allocate a buffer for the initial request and ack */
  107.     my_req = (struct server_request *) BMI_memalloc(server_addr,
  108.                             sizeof(struct
  109.                                server_request),
  110.                             BMI_SEND);
  111.     my_ack =
  112.     (struct server_ack *) BMI_memalloc(server_addr,
  113.                        sizeof(struct server_ack), BMI_RECV);
  114.     if (!my_req || !my_ack)
  115.     {
  116.     fprintf(stderr, "BMI_memalloc failed.\n");
  117.     goto client_exit;
  118.     }
  119.  
  120.     my_req->size = user_opts->message_size;
  121.  
  122.     /* send the initial request on its way */
  123.     ret = BMI_post_sendunexpected(&(client_ops[1]), server_addr, my_req,
  124.                   sizeof(struct server_request), BMI_PRE_ALLOC,
  125.                   0, NULL, context);
  126.     if (ret < 0)
  127.     {
  128.     errno = -ret;
  129.     perror("BMI_post_send");
  130.     goto client_exit;
  131.     }
  132.     if (ret == 0)
  133.     {
  134.     /* turning this into a blocking call for testing :) */
  135.     /* check for completion of request */
  136.     do
  137.     {
  138.         ret = BMI_test(client_ops[1], &outcount, &error_code,
  139.                &actual_size, NULL, 10, context);
  140.     } while (ret == 0 && outcount == 0);
  141.  
  142.     if (ret < 0 || error_code != 0)
  143.     {
  144.         fprintf(stderr, "Request send failed.\n");
  145.         if (ret < 0)
  146.         {
  147.         errno = -ret;
  148.         perror("BMI_test");
  149.         }
  150.         goto client_exit;
  151.     }
  152.     }
  153.  
  154.     /* post a recv for the server acknowledgement */
  155.     ret = BMI_post_recv(&(client_ops[0]), server_addr, my_ack,
  156.             sizeof(struct server_ack), &actual_size, BMI_PRE_ALLOC,
  157.             0, NULL, context);
  158.     if (ret < 0)
  159.     {
  160.     errno = -ret;
  161.     perror("BMI_post_recv");
  162.     return (-1);
  163.     }
  164.     if (ret == 0)
  165.     {
  166.     /* turning this into a blocking call for testing :) */
  167.     /* check for completion of ack recv */
  168.     do
  169.     {
  170.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  171.                &actual_size, NULL, 10, context);
  172.     } while (ret == 0 && outcount == 0);
  173.  
  174.     if (ret < 0 || error_code != 0)
  175.     {
  176.         fprintf(stderr, "Ack recv failed.\n");
  177.         return (-1);
  178.     }
  179.     }
  180.     else
  181.     {
  182.     if (actual_size != sizeof(struct server_ack))
  183.     {
  184.         printf("Short recv.\n");
  185.         return (-1);
  186.     }
  187.     }
  188.  
  189.     /* look at the ack */
  190.     if (my_ack->status != 0)
  191.     {
  192.     fprintf(stderr, "Request denied.\n");
  193.     return (-1);
  194.     }
  195.  
  196.     /* create a buffer to send */
  197.     send_buffer = BMI_memalloc(server_addr, user_opts->message_size, BMI_SEND);
  198.     if (!send_buffer)
  199.     {
  200.     fprintf(stderr, "BMI_memalloc.\n");
  201.     return (-1);
  202.     }
  203.     sprintf((char *) send_buffer, "Hello World.\n");
  204.  
  205.     fprintf(stderr, "Sending eager size message.\n");
  206.     /* send the data payload on its way */
  207.     ret = BMI_post_send(&(client_ops[0]), server_addr, send_buffer,
  208.             user_opts->message_size, BMI_PRE_ALLOC, 0, NULL,
  209.             context);
  210.     if (ret < 0)
  211.     {
  212.     errno = -ret;
  213.     perror("BMI_post_send");
  214.     return (-1);
  215.     }
  216.     if (ret == 0)
  217.     {
  218.     /* turning this into a blocking call for testing :) */
  219.     /* check for completion of data payload send */
  220.     do
  221.     {
  222.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  223.                &actual_size, NULL, 10, context);
  224.     } while (ret == 0 && outcount == 0);
  225.  
  226.     if (ret < 0 || error_code != 0)
  227.     {
  228.         fprintf(stderr, "Data payload send failed.\n");
  229.         return (-1);
  230.     }
  231.     }
  232.     fprintf(stderr, "Done.\n");
  233.  
  234.     fprintf(stderr, "Sending eager size message (SHORT).\n");
  235.     /* send the data payload on its way */
  236.     ret = BMI_post_send(&(client_ops[0]), server_addr, send_buffer,
  237.             user_opts->message_size - 3, BMI_PRE_ALLOC, 0, NULL,
  238.             context);
  239.     if (ret < 0)
  240.     {
  241.     errno = -ret;
  242.     perror("BMI_post_send");
  243.     return (-1);
  244.     }
  245.     if (ret == 0)
  246.     {
  247.     /* turning this into a blocking call for testing :) */
  248.     /* check for completion of data payload send */
  249.     do
  250.     {
  251.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  252.                &actual_size, NULL, 10, context);
  253.     } while (ret == 0 && outcount == 0);
  254.  
  255.     if (ret < 0 || error_code != 0)
  256.     {
  257.         fprintf(stderr, "Data payload send failed.\n");
  258.         return (-1);
  259.     }
  260.     }
  261.     fprintf(stderr, "Done.\n");
  262.  
  263.  
  264.     fprintf(stderr, "Sending rendezvous size message.\n");
  265.     /* send the data payload on its way */
  266.     ret = BMI_post_send(&(client_ops[0]), server_addr, big_buffer,
  267.             BIG_SIZE, BMI_EXT_ALLOC, 0, NULL, context);
  268.     if (ret < 0)
  269.     {
  270.     errno = -ret;
  271.     perror("BMI_post_send");
  272.     return (-1);
  273.     }
  274.     if (ret == 0)
  275.     {
  276.     /* turning this into a blocking call for testing :) */
  277.     /* check for completion of data payload send */
  278.     do
  279.     {
  280.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  281.                &actual_size, NULL, 10, context);
  282.     } while (ret == 0 && outcount == 0);
  283.  
  284.     if (ret < 0 || error_code != 0)
  285.     {
  286.         fprintf(stderr, "Data payload send failed.\n");
  287.         return (-1);
  288.     }
  289.     }
  290.     fprintf(stderr, "Done.\n");
  291.  
  292.     fprintf(stderr, "Sending rendezvous size message (SHORT).\n");
  293.     /* send the data payload on its way */
  294.     ret = BMI_post_send(&(client_ops[0]), server_addr, big_buffer,
  295.             BIG_SIZE - 30, BMI_EXT_ALLOC, 0, NULL, context);
  296.     if (ret < 0)
  297.     {
  298.     errno = -ret;
  299.     perror("BMI_post_send");
  300.     return (-1);
  301.     }
  302.     if (ret == 0)
  303.     {
  304.     /* turning this into a blocking call for testing :) */
  305.     /* check for completion of data payload send */
  306.     do
  307.     {
  308.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  309.                &actual_size, NULL, 10, context);
  310.     } while (ret == 0 && outcount == 0);
  311.  
  312.     if (ret < 0 || error_code != 0)
  313.     {
  314.         fprintf(stderr, "Data payload send failed.\n");
  315.         return (-1);
  316.     }
  317.     }
  318.     fprintf(stderr, "Done.\n");
  319.  
  320.     fprintf(stderr, "Sending rendezvous size message (VERY SHORT).\n");
  321.     /* send the data payload on its way */
  322.     ret = BMI_post_send(&(client_ops[0]), server_addr, big_buffer,
  323.             300, BMI_EXT_ALLOC, 0, NULL, context);
  324.     if (ret < 0)
  325.     {
  326.     errno = -ret;
  327.     perror("BMI_post_send");
  328.     return (-1);
  329.     }
  330.     if (ret == 0)
  331.     {
  332.     /* turning this into a blocking call for testing :) */
  333.     /* check for completion of data payload send */
  334.     do
  335.     {
  336.         ret = BMI_test(client_ops[0], &outcount, &error_code,
  337.                &actual_size, NULL, 10, context);
  338.     } while (ret == 0 && outcount == 0);
  339.  
  340.     if (ret < 0 || error_code != 0)
  341.     {
  342.         fprintf(stderr, "Data payload send failed.\n");
  343.         return (-1);
  344.     }
  345.     }
  346.     fprintf(stderr, "Done.\n");
  347.  
  348.  
  349.  
  350.     /* free up the message buffers */
  351.     BMI_memfree(server_addr, send_buffer, user_opts->message_size, BMI_SEND);
  352.     BMI_memfree(server_addr, my_req, sizeof(struct server_request), BMI_SEND);
  353.     BMI_memfree(server_addr, my_ack, sizeof(struct server_ack), BMI_RECV);
  354.  
  355.   client_exit:
  356.  
  357.     /* shutdown the local interface */
  358.     BMI_close_context(context);
  359.     ret = BMI_finalize();
  360.     if (ret < 0)
  361.     {
  362.     errno = -ret;
  363.     perror("BMI_finalize");
  364.     return (-1);
  365.     }
  366.  
  367.     /* turn off debugging stuff */
  368.     gossip_disable();
  369.  
  370.     return (0);
  371. }
  372.  
  373.  
  374. static struct options *parse_args(
  375.     int argc,
  376.     char *argv[])
  377. {
  378.  
  379.     /* getopt stuff */
  380.     extern char *optarg;
  381.     char flags[] = "h:r:s:c:";
  382.     int one_opt = 0;
  383.  
  384.     struct options *tmp_opts = NULL;
  385.     int len = -1;
  386.     int ret = -1;
  387.  
  388.     /* create storage for the command line options */
  389.     tmp_opts = (struct options *) malloc(sizeof(struct options));
  390.     if (!tmp_opts)
  391.     {
  392.     goto parse_args_error;
  393.     }
  394.  
  395.     /* fill in defaults (except for hostid) */
  396.     tmp_opts->message_size = 100;
  397.  
  398.     /* look at command line arguments */
  399.     while ((one_opt = getopt(argc, argv, flags)) != EOF)
  400.     {
  401.     switch (one_opt)
  402.     {
  403.     case ('h'):
  404.         len = (strlen(optarg)) + 1;
  405.         if ((tmp_opts->hostid = (char *) malloc(len)) == NULL)
  406.         {
  407.         goto parse_args_error;
  408.         }
  409.         memcpy(tmp_opts->hostid, optarg, len);
  410.         break;
  411.     case ('s'):
  412.         ret = sscanf(optarg, "%d", &tmp_opts->message_size);
  413.         if (ret < 1)
  414.         {
  415.         goto parse_args_error;
  416.         }
  417.         if (tmp_opts->message_size < 32)
  418.         {
  419.         fprintf(stderr, "Please pick a larger message size!.\n");
  420.         goto parse_args_error;
  421.         }
  422.         break;
  423.     default:
  424.         break;
  425.     }
  426.     }
  427.  
  428.     /* if we didn't get a host argument, fill in a default: */
  429.     len = (strlen(DEFAULT_HOSTID_GM)) + 1;
  430.     if ((tmp_opts->hostid = (char *) malloc(len)) == NULL)
  431.     {
  432.     goto parse_args_error;
  433.     }
  434.     memcpy(tmp_opts->hostid, DEFAULT_HOSTID_GM, len);
  435.  
  436.     return (tmp_opts);
  437.  
  438.   parse_args_error:
  439.  
  440.     /* if an error occurs, just free everything and return NULL */
  441.     if (tmp_opts)
  442.     {
  443.     if (tmp_opts->hostid)
  444.     {
  445.         free(tmp_opts->hostid);
  446.     }
  447.     free(tmp_opts);
  448.     }
  449.     return (NULL);
  450. }
  451.  
  452. /*
  453.  * Local variables:
  454.  *  c-indent-level: 4
  455.  *  c-basic-offset: 4
  456.  * End:
  457.  *
  458.  * vim: ts=8 sts=4 sw=4 expandtab
  459.  */
  460.