remote_pool.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 Aug 2008 21:30:32 +0300
changeset 41 540737bf6bac
parent 26 6d615203d963
permissions -rw-r--r--
sending requests, and partial support for receiving -- incomplete, not tested
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include <string.h>
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#include <stdio.h>
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <ctype.h>
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
#include "remote_pool.h"
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include "common.h"
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
void remote_pool_init (struct remote_pool *pool_info) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
    memset(pool_info, 0, sizeof(*pool_info));
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
}
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
26
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    12
int remote_pool_add (struct remote_pool *pool_info, const char *addr_spec) {
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
    int i;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    struct remote_node *node_info = NULL;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    // find a free remote_node entry and store its pointer in node_info
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    for (i = 0; i < REMOTE_POOL_MAX; i++) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
        if (!pool_info->nodes[i].valid) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
            node_info = &pool_info->nodes[i];
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
            break;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
        }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
26
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    24
    if (!node_info)
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    25
        ERROR("remote_pool_add: pool full, consider increasing REMOTE_POOL_MAX");
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
26
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    27
    return remote_node_init(node_info, addr_spec);
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    28
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    29
error:
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    30
    return -1;
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
}
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
int remote_pool_load (struct remote_pool *pool_info, const char *filename) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    // open the file
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
    FILE *fh = fopen(filename, "r");
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    if (!fh) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        perr("remote_pool_load: fopen(%s)", filename);
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        return -1;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
    // read it in line-by-line
26
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    43
    char line_buf[POOL_FILE_LINE_LENGTH], *c;
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    // non-ferror error indicator
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    int error_flag = 0;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    while (1) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        // read in a line
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
        if (!fgets(line_buf, POOL_FILE_LINE_LENGTH, fh)) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
            perr("remote_pool_load: fgets");
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
            break;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
        // strip comments
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
        c = strchr(line_buf, '#');
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
        if (c)
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
            *c = '\0';
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
        // skip empty lines
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
        for (c = line_buf; *c != '\0'; c++)
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
            if (!isspace(*c))
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
                break;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
        if (*c == '\0')
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
            continue;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
        // add it to the pool
26
6d615203d963 support for PF_LOCAL, it works, but needs some more testing/cleanup old code
Tero Marttila <terom@fixme.fi>
parents: 13
diff changeset
    70
        if (remote_pool_add(pool_info, line_buf)) {
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
            error_flag = 1;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
            break;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
        }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    // pick up ferrors if otherwise fine
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    error_flag = error_flag || ferror(fh);
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
    // close the fd
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    fclose(fh);
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
    // error return?
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    if (error_flag)
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
        return -1;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    return 0;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
}
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
struct remote_node *remote_pool_get (struct remote_pool *pool_info) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    int i;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    struct remote_node *node_info = NULL;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    // find a the appropriate remote_node entry and store its pointer in node_info
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    for (i = 0; i < REMOTE_POOL_MAX; i++) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
        if (pool_info->nodes[i].valid && (node_info == NULL || pool_info->nodes[i].current_load < node_info->current_load)) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
            node_info = &pool_info->nodes[i];
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    97
        }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    98
    }
13
ee426f453cf5 * fix some (of the) stupid things in Makefile
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
    99
    
ee426f453cf5 * fix some (of the) stupid things in Makefile
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   100
    if (node_info) {
ee426f453cf5 * fix some (of the) stupid things in Makefile
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   101
        // add one to its load, because that's probably correct, and works if we pick multiple nodes from the pool at the same time
ee426f453cf5 * fix some (of the) stupid things in Makefile
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   102
        node_info->current_load++;
ee426f453cf5 * fix some (of the) stupid things in Makefile
Tero Marttila <terom@fixme.fi>
parents: 9
diff changeset
   103
    }
9
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   105
    // either NULL or the right remote_info
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
    return node_info;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
}
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   109
int remote_pool_size (struct remote_pool *pool_info) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   110
    int i, size = 0;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   111
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    for (i = 0; i < REMOTE_POOL_MAX; i++) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
        if (pool_info->nodes[i].valid) {
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
            size++;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
        }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    }
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    return size;
fb6632e6c1bb two new modules, remote_node and remote_pool
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
}