Annotation of 43BSDReno/sys/nfs/TEST/billboard/src/bb_board.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  ******************************************************************************
                      3:  *
                      4:  * Module: bb_board.c
                      5:  *
                      6:  * Description: 
                      7:  *
                      8:  * Functions: 
                      9:  *         bb_read_board()     - Read or create the board data file.
                     10:  *         bb_board_tag()      - Change an entry in the board.
                     11:  *         bb_get_clients()    - Return a list of clients.
                     12:  *         bb_get_servers()    - Return a list of servers.
                     13:  *         bb_board_update()   - Update the board file on disk.
                     14:  *
                     15:  *
                     16:  ******************************************************************************
                     17:  */
                     18: 
                     19: /*
                     20:  ******************************************************************************
                     21:  * Include Files
                     22:  ******************************************************************************
                     23:  */
                     24: #include <stdio.h>
                     25: #include <rpc/rpc.h>
                     26: #include "common.h"
                     27: #include "protocol.h"
                     28: #include "server.h"
                     29: 
                     30: 
                     31: 
                     32: /*************************************************************************
                     33: **                                                                     **
                     34: **  bb_read_board() - If the board data file exists then read it and   **
                     35: **  store it in the board array.  Otherwise create the file with zeroed        **
                     36: **  entries.                                                           **
                     37: **                                                                     **
                     38: *************************************************************************/
                     39: BB_board       board;                  /* The board of tested imps.    */
                     40: 
                     41: int
                     42: bb_read_board()
                     43: {
                     44:     FILE       *board_file;            /* Pointer to the board file.   */
                     45:     int                i;                      /* Index into board array.      */
                     46:     int                j;                      /* Index into board array.      */
                     47:     int                num_imps;               /* The number of implementations*/
                     48: 
                     49:     num_imps = bb_get_imp_cnt();
                     50: 
                     51:     /*
                     52:     **  If the file exists read it into the board array.
                     53:     */
                     54:     if ( (board_file = fopen( BB_BOARD_FILE, "r")) != NULL )
                     55:     {
                     56:        for ( i = 0; i < num_imps; i++ )
                     57:        {
                     58:            for ( j = 0; j < num_imps; j++ )
                     59:            {
                     60:                if ( (board[i][j] = getw( board_file)) == (u_char)EOF )
                     61:                {
                     62:                    fprintf( stderr, "ERROR: Board data file incomplete.\n");
                     63:                    return BB_FAILURE;
                     64:                }
                     65:            }
                     66:        }
                     67: 
                     68:        fclose( board_file);
                     69:        return BB_SUCCESS;
                     70:     }
                     71: 
                     72:     /*
                     73:     **  The file did not exist so create it.
                     74:     */
                     75:     if ( (board_file = fopen( BB_BOARD_FILE, "w")) == NULL )
                     76:     {
                     77:        fprintf( stderr, "FAILED opening the board data file '%s'.\n",
                     78:                 BB_BOARD_FILE);
                     79:        fclose( board_file);
                     80:        return BB_FAILURE;
                     81:     }
                     82: 
                     83:     /*
                     84:     **  Fill the file with zeroed entries.
                     85:     */
                     86:     for ( i = 0; i < num_imps; i++ )
                     87:     {
                     88:        for ( j = 0; j < num_imps; j++ )
                     89:        {
                     90:            if ( putw( (int)BB_BOARD_UNSET, board_file) == 1 )
                     91:            {
                     92:                fprintf( stderr, "FAILED writting new board file.\n");
                     93:                fclose( board_file);
                     94:                return BB_FAILURE;
                     95:            }
                     96:        }
                     97:     }
                     98: 
                     99:     fclose( board_file);
                    100:     return BB_SUCCESS;
                    101: }
                    102: 
                    103: 
                    104: 
                    105: 
                    106: /*************************************************************************
                    107: **                                                                     **
                    108: ** bb_board_tag() - Change the value of the board slot to either set it        **
                    109: ** or unset it.  Struct p_in contains a BB_id the client and the server.**
                    110: ** The p_out struct contains most of the company data.                 **
                    111: **                                                                     **
                    112: *************************************************************************/
                    113: void
                    114: bb_board_tag( p_in, p_out, set)
                    115:   BB_set_in    *p_in;          /* Set input, client id, server id.     */
                    116:   BB_set_out   *p_out;         /* Set output, most company data.       */
                    117:   int          set;
                    118: {
                    119:     int                client_id;              /* The client identifier.       */
                    120:     int                server_id;              /* The server identifier.       */
                    121:     BB_co_data codata;                 /* The company data.            */
                    122:     
                    123:     /*
                    124:     **  Get the client and server id's.
                    125:     */
                    126:     if ( (client_id = bb_get_hash( p_in->client)) == BB_HASH_ID_NOT_FOUND )
                    127:     {
                    128:        p_out->status = BB_BAD_CLIENT;
                    129:        return;
                    130:     }
                    131: 
                    132:     if ( (server_id = bb_get_hash( p_in->server)) == BB_HASH_ID_NOT_FOUND )
                    133:     {
                    134:        p_out->status = BB_BAD_SERVER;
                    135:        return;
                    136:     }
                    137: 
                    138:     if ( set == BB_SET )
                    139:     {
                    140:        if ( board[server_id][client_id] == BB_BOARD_SET )
                    141:        {
                    142:            p_out->status = BB_ALREADY_SET;
                    143:        }
                    144:        board[server_id][client_id] = BB_BOARD_SET;
                    145:     }
                    146:     else if ( set == BB_UNSET )
                    147:     {
                    148:        if ( board[server_id][client_id] == BB_BOARD_UNSET )
                    149:        {
                    150:            p_out->status = BB_ALREADY_UNSET;
                    151:        }
                    152:        board[server_id][client_id] = BB_BOARD_UNSET;
                    153:     }
                    154:     else
                    155:     {
                    156:        fprintf( stderr, "Unknown set operation.\n");
                    157:        p_out->status = BB_FAILURE;
                    158:        return;
                    159:     }
                    160: 
                    161:     /*
                    162:     **  Fill in the company data parts of the output structure.
                    163:     */
                    164:     if ( bb_get_codata( client_id, &codata) != BB_SUCCESS )
                    165:     {
                    166:        /* OOOPS...this wasn't supposed to happen! */
                    167:        p_out->status = BB_FAILURE;
                    168:        return;
                    169:     }
                    170:     p_out->client.booth = codata.booth;
                    171:     strncpy( p_out->client.company, codata.company, BB_COMPANY_NAME_LEN);
                    172:     strncpy( p_out->client.imp, codata.imp, BB_IMP_NAME_LEN);
                    173:     strncpy( p_out->client.id, codata.id, BB_ID_NAME_LEN);
                    174:     if ( bb_get_codata( server_id, &codata) != BB_SUCCESS )
                    175:     {
                    176:        /* OOOPS...this wasn't supposed to happen! */
                    177:        p_out->status = BB_FAILURE;
                    178:        return;
                    179:     }
                    180:     p_out->server.booth = codata.booth;
                    181:     strncpy( p_out->server.company, codata.company, BB_COMPANY_NAME_LEN);
                    182:     strncpy( p_out->server.imp, codata.imp, BB_IMP_NAME_LEN);
                    183:     strncpy( p_out->server.id, codata.id, BB_ID_NAME_LEN);
                    184: 
                    185:     /*
                    186:     **  Set the status to indicate whether or not the write to disk
                    187:     **  succeeded otherwise use the previously set status.
                    188:     */
                    189:     if ( bb_board_update( client_id, server_id) != BB_SUCCESS )
                    190:     {
                    191:        p_out->status = BB_FAILURE;
                    192:     }
                    193: }
                    194: 
                    195: 
                    196: 
                    197: /*************************************************************************
                    198: **                                                                     **
                    199: **  bb_get_clients() - Get the list of all the clients and their test  **
                    200: **  status for a particular server.                                    **
                    201: **                                                                     **
                    202: *************************************************************************/
                    203: int
                    204: bb_get_clients( id, list)
                    205:   BB_id                id;             /* Id of the client.            */
                    206:   BB_row       list;           /* List of all the servers.     */
                    207: {
                    208:     int                index;          /* Index of the client.         */
                    209: 
                    210:     if ( (index = bb_get_hash( id)) == BB_HASH_ID_NOT_FOUND )
                    211:     {
                    212:         return BB_BAD_CLIENT;
                    213:     }
                    214: 
                    215:     memcpy( list, board[index], sizeof( list));
                    216:     return BB_SUCCESS;
                    217: }
                    218: 
                    219: 
                    220: 
                    221: /*************************************************************************
                    222: **                                                                     **
                    223: **  bb_get_servers() - Get the list of all the servers and their test  **
                    224: **  status for a particular client.                                    **
                    225: **                                                                     **
                    226: *************************************************************************/
                    227: int
                    228: bb_get_servers( id, list)
                    229:   BB_id                id;             /* Id of the client.            */
                    230:   BB_row       list;           /* List of all the servers.     */
                    231: {
                    232:     int                index;          /* Index of the client.         */
                    233:     int                i;              /* Nice loop variable name.     */
                    234: 
                    235:     if ( (index = bb_get_hash( id)) == BB_HASH_ID_NOT_FOUND )
                    236:     {
                    237:         return BB_BAD_CLIENT;
                    238:     }
                    239: 
                    240:     for( i = 0; i < BB_MAX_IMP; i++ )
                    241:     {
                    242:        list[i] = board[i][index];
                    243:     }
                    244: 
                    245:     return BB_SUCCESS;
                    246: }
                    247: 
                    248: 
                    249: /*************************************************************************
                    250: **                                                                     **
                    251: **  bb_board_update() - Update the billboard on the disk.  Since these **
                    252: **  only happen one at a time, update is for client,server pair.       **
                    253: **                                                                     **
                    254: *************************************************************************/
                    255: int
                    256: bb_board_update( client, server)
                    257:   int          client;         /* The index of the client to update.   */
                    258:   int          server;         /* The index of the server to update.   */
                    259: {
                    260:     int                num_imps;       /* The number of implementations.       */
                    261:     FILE       *board_file;    /* Pointer to the board file.           */
                    262:     long       seek_to;        /* The position to seek to in the file. */
                    263: 
                    264:     num_imps = bb_get_imp_cnt();
                    265: 
                    266:     if ( (client >= num_imps) || (server >= num_imps) )
                    267:     {
                    268:        return BB_FAILURE;
                    269:     }
                    270: 
                    271:     /*
                    272:     **  The word that we want to write is at a location server rows
                    273:     **  down and client rows across.  Since the output is written in
                    274:     **  integers, multiply this number by the sizeof an integer and
                    275:     **  that is where in the file to write the number.
                    276:     */
                    277:     seek_to = ((server * num_imps) + client) * sizeof(int);
                    278: 
                    279:     /*
                    280:     **  Open the board file.
                    281:     */
                    282:     if ( (board_file = fopen( BB_BOARD_FILE, "r+")) == NULL )
                    283:     {
                    284:        fprintf( stderr, "ERROR: Could not open board file for update.\n");
                    285:        return BB_FAILURE;
                    286:     }
                    287: 
                    288:     if ( fseek( board_file, seek_to, 0) != 0 )
                    289:     {
                    290:        fprintf( stderr, "ERROR: Could not seek in board file.\n");
                    291:        fclose( board_file);
                    292:        return BB_FAILURE;
                    293:     }
                    294: 
                    295:     if ( putw( (int)board[server][client], board_file) == 1 )
                    296:     {
                    297:        fprintf( stderr, "ERROR: Could not update board file.\n");
                    298:        fclose( board_file);
                    299:        return BB_FAILURE;
                    300:     }
                    301: 
                    302:     fclose( board_file);
                    303:     return BB_SUCCESS;
                    304: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.