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