remote_pool.c
changeset 9 fb6632e6c1bb
child 13 ee426f453cf5
equal deleted inserted replaced
8:4d38ccbeb93e 9:fb6632e6c1bb
       
     1 #include <string.h>
       
     2 #include <stdio.h>
       
     3 #include <ctype.h>
       
     4 
       
     5 #include "remote_pool.h"
       
     6 #include "common.h"
       
     7 
       
     8 void remote_pool_init (struct remote_pool *pool_info) {
       
     9     memset(pool_info, 0, sizeof(*pool_info));
       
    10 }
       
    11 
       
    12 int remote_pool_add (struct remote_pool *pool_info, const char *hostname, const char *portname) {
       
    13     int i;
       
    14     struct remote_node *node_info = NULL;
       
    15     
       
    16     // find a free remote_node entry and store its pointer in node_info
       
    17     for (i = 0; i < REMOTE_POOL_MAX; i++) {
       
    18         if (!pool_info->nodes[i].valid) {
       
    19             node_info = &pool_info->nodes[i];
       
    20             break;
       
    21         }
       
    22     }
       
    23 
       
    24     if (!node_info) {
       
    25         error("remote_pool_add: pool full, consider increasing REMOTE_POOL_MAX");
       
    26         return -1;
       
    27     }
       
    28 
       
    29     return remote_node_init(node_info, hostname, portname);
       
    30 }
       
    31 
       
    32 int remote_pool_load (struct remote_pool *pool_info, const char *filename) {
       
    33     // open the file
       
    34     FILE *fh = fopen(filename, "r");
       
    35 
       
    36     if (!fh) {
       
    37         perr("remote_pool_load: fopen(%s)", filename);
       
    38         return -1;
       
    39     }
       
    40     
       
    41     // read it in line-by-line
       
    42     char line_buf[POOL_FILE_LINE_LENGTH], *hostname, *portname, *c;
       
    43     
       
    44     // non-ferror error indicator
       
    45     int error_flag = 0;
       
    46 
       
    47     while (1) {
       
    48         // read in a line
       
    49         if (!fgets(line_buf, POOL_FILE_LINE_LENGTH, fh)) {
       
    50             perr("remote_pool_load: fgets");
       
    51             break;
       
    52         }
       
    53 
       
    54         // strip comments
       
    55         c = strchr(line_buf, '#');
       
    56 
       
    57         if (c)
       
    58             *c = '\0';
       
    59         
       
    60         // skip empty lines
       
    61         for (c = line_buf; *c != '\0'; c++)
       
    62             if (!isspace(*c))
       
    63                 break;
       
    64 
       
    65         if (*c == '\0')
       
    66             continue;
       
    67         
       
    68         // parse
       
    69         if (!parse_hostport(line_buf, &hostname, &portname)) {
       
    70             error_flag = 1;
       
    71             break;
       
    72         }
       
    73 
       
    74         // add it to the pool
       
    75         if (remote_pool_add(pool_info, hostname, portname)) {
       
    76             error_flag = 1;
       
    77             break;
       
    78         }
       
    79     }
       
    80     
       
    81     // pick up ferrors if otherwise fine
       
    82     error_flag = error_flag || ferror(fh);
       
    83     
       
    84     // close the fd
       
    85     fclose(fh);
       
    86     
       
    87     // error return?
       
    88     if (error_flag)
       
    89         return -1;
       
    90     
       
    91     return 0;
       
    92 }
       
    93 
       
    94 struct remote_node *remote_pool_get (struct remote_pool *pool_info) {
       
    95     int i;
       
    96     struct remote_node *node_info = NULL;
       
    97 
       
    98     // find a the appropriate remote_node entry and store its pointer in node_info
       
    99     for (i = 0; i < REMOTE_POOL_MAX; i++) {
       
   100         if (pool_info->nodes[i].valid && (node_info == NULL || pool_info->nodes[i].current_load < node_info->current_load)) {
       
   101             node_info = &pool_info->nodes[i];
       
   102         }
       
   103     }
       
   104 
       
   105     // either NULL or the right remote_info
       
   106     return node_info;
       
   107 }
       
   108 
       
   109 int remote_pool_size (struct remote_pool *pool_info) {
       
   110     int i, size = 0;
       
   111 
       
   112     for (i = 0; i < REMOTE_POOL_MAX; i++) {
       
   113         if (pool_info->nodes[i].valid) {
       
   114             size++;
       
   115         }
       
   116     }
       
   117     
       
   118     return size;
       
   119 }