Annotation of cci/usr/src/ucb/talk/table.c, revision 1.1.1.1

1.1       root        1: /* $Header: table.c,v 1.2 85/01/09 13:05:08 rcs Exp $ */
                      2: 
                      3: /* routines to handle insertion, deletion, etc on the table
                      4:    of requests kept by the daemon. Nothing fancy here, linear
                      5:    search on a double-linked list. A time is kept with each 
                      6:    entry so that overly old invitations can be eliminated.
                      7: 
                      8:    Consider this a mis-guided attempt at modularity
                      9:  */
                     10: 
                     11: #include "ctl.h"
                     12: #include <sys/time.h>
                     13: 
                     14: #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */
                     15: 
                     16: #define NIL ( (TABLE_ENTRY *) 0)
                     17: 
                     18: extern int debug;
                     19: struct timeval tp;
                     20: struct timezone *txp;
                     21: 
                     22: typedef struct table_entry TABLE_ENTRY;
                     23: 
                     24: struct table_entry {
                     25:     CTL_MSG request;
                     26:     long time;
                     27:     TABLE_ENTRY *next;
                     28:     TABLE_ENTRY *last;
                     29: };
                     30: 
                     31: TABLE_ENTRY *table = NIL;
                     32: CTL_MSG *find_request();
                     33: CTL_MSG *find_match();
                     34: char *malloc();
                     35: 
                     36:     /*
                     37:      * Look in the table for an invitation that matches the current
                     38:      * request looking for an invitation
                     39:      */
                     40: 
                     41: CTL_MSG *find_match(request)
                     42: CTL_MSG *request;
                     43: {
                     44:     TABLE_ENTRY *ptr;
                     45:     long current_time;
                     46: 
                     47:     gettimeofday(&tp, &txp);
                     48:     current_time = tp.tv_sec;
                     49: 
                     50:     ptr = table;
                     51: 
                     52:     if (debug) {
                     53:        printf("Entering Look-Up with : \n");
                     54:        print_request(request);
                     55:     }
                     56: 
                     57:     while (ptr != NIL) {
                     58: 
                     59:        if ( (ptr->time - current_time) > MAX_LIFE ) {
                     60:                /* the entry is too old */
                     61:            if (debug) printf("Deleting expired entry : \n");
                     62:            if (debug) print_request(&ptr->request);
                     63:            delete(ptr);
                     64:            ptr = ptr->next;
                     65:            continue;
                     66:        }
                     67: 
                     68:        if (debug) {
                     69:                printf("entry: ");
                     70:                print_request(&ptr->request);
                     71:        }
                     72: 
                     73:        if ( strcmp(request->ctlm_l_name, ptr->request.ctlm_r_name) == 0 &&
                     74:             strcmp(request->ctlm_r_name, ptr->request.ctlm_l_name) == 0 &&
                     75:             ptr->request.ctlm_type == LEAVE_INVITE ) {
                     76:            if (debug)
                     77:                printf("found in table\n");
                     78:            return(&ptr->request);
                     79:        }
                     80:        
                     81:        ptr = ptr->next;
                     82:     }
                     83:     
                     84:     if (debug)
                     85:        printf("not found in table\n");
                     86:     return((CTL_MSG *) 0);
                     87: }
                     88: 
                     89:     /*
                     90:      * look for an identical request, as opposed to a complimentary
                     91:      * one as find_match does 
                     92:      */
                     93: 
                     94: CTL_MSG *find_request(request)
                     95: CTL_MSG *request;
                     96: {
                     97:     TABLE_ENTRY *ptr;
                     98:     long current_time;
                     99: 
                    100:     gettimeofday(&tp, &txp);
                    101:     current_time = tp.tv_sec;
                    102: 
                    103:        /* See if this is a repeated message, and check for
                    104:           out of date entries in the table while we are it.
                    105:         */
                    106: 
                    107:     ptr = table;
                    108: 
                    109:     if (debug) {
                    110:        printf("Entering find_request with : \n");
                    111:        print_request(request);
                    112:     }
                    113: 
                    114:     while (ptr != NIL) {
                    115: 
                    116:        if ( (ptr->time - current_time) > MAX_LIFE ) {
                    117:                /* the entry is too old */
                    118:            if (debug) printf("Deleting expired entry : \n");
                    119:            if (debug) print_request(&ptr->request);
                    120:            delete(ptr);
                    121:            ptr = ptr->next;
                    122:            continue;
                    123:        }
                    124: 
                    125:        if (debug) {
                    126:                printf("entry: ");
                    127:                print_request(&ptr->request);
                    128:        }
                    129: 
                    130:        if ( strcmp(request->ctlm_r_name, ptr->request.ctlm_r_name) == 0 &&
                    131:             strcmp(request->ctlm_l_name, ptr->request.ctlm_l_name) == 0 &&
                    132:             request->ctlm_type == ptr->request.ctlm_type &&
                    133:             request->ctlm_pid == ptr->request.ctlm_pid) {
                    134:            
                    135:                /* update the time if we 'touch' it */
                    136:            ptr->time = current_time;
                    137:            if (debug)
                    138:                printf("found in request table\n");
                    139:            return(&ptr->request);
                    140:        }
                    141: 
                    142:        ptr = ptr->next;
                    143:     }
                    144: 
                    145:     if (debug)
                    146:        printf("not found in request table\n");
                    147:     return((CTL_MSG *) 0);
                    148: }
                    149: 
                    150: insert_table(request, response)
                    151: CTL_MSG *request;
                    152: CTL_RESPONSE *response;
                    153: {
                    154:     TABLE_ENTRY *ptr;
                    155:     long current_time;
                    156: 
                    157:     gettimeofday(&tp, &txp);
                    158:     current_time = tp.tv_sec;
                    159: 
                    160:     response->ctlr_id_num = request->ctlm_id_num = new_id();
                    161: 
                    162:        /* insert a new entry into the top of the list */
                    163:     
                    164:     ptr = (TABLE_ENTRY *) malloc(sizeof(TABLE_ENTRY));
                    165: 
                    166:     if (ptr == NIL) {
                    167:        print_error("malloc in insert_table");
                    168:     }
                    169: 
                    170:     ptr->time = current_time;
                    171:     ptr->request = *request;
                    172: 
                    173:     ptr->next = table;
                    174:     if (ptr->next != NIL) {
                    175:        ptr->next->last = ptr;
                    176:     }
                    177:     ptr->last = NIL;
                    178:     table = ptr;
                    179: }
                    180: 
                    181:     /* generate a unique non-zero sequence number */
                    182: 
                    183: new_id()
                    184: {
                    185:     static int current_id = 0;
                    186: 
                    187:     current_id = (current_id + 1) % MAX_ID;
                    188: 
                    189:        /* 0 is reserved, helps to pick up bugs */
                    190: 
                    191:     if (current_id == 0) current_id = 1;
                    192: 
                    193:     return(current_id);
                    194: }
                    195: 
                    196:     /* delete the invitation with id 'id_num' */
                    197: 
                    198: delete_invite(id_num)
                    199: int id_num;
                    200: {
                    201:     TABLE_ENTRY *ptr;
                    202: 
                    203:     ptr = table;
                    204: 
                    205:     if (debug) printf("Entering delete_invite with %d\n", id_num);
                    206: 
                    207:     while (ptr != NIL && ptr->request.ctlm_id_num != id_num) {
                    208:        if (debug) print_request(&ptr->request);
                    209:        ptr = ptr->next;
                    210:     }
                    211:        
                    212:     if (ptr != NIL) {
                    213:        delete(ptr);
                    214:        return(SUCCESS);
                    215:     }
                    216:     
                    217:     return(NOT_HERE);
                    218: }
                    219: 
                    220:     /* classic delete from a double-linked list */
                    221: 
                    222: delete(ptr)
                    223: TABLE_ENTRY *ptr;
                    224: {
                    225:     if (debug) printf("Deleting : ");
                    226:     if (debug) print_request(&ptr->request);
                    227: 
                    228:     if (table == ptr) {
                    229:        table = ptr->next;
                    230:     } else if (ptr->last != NIL) {
                    231:        ptr->last->next = ptr->next;
                    232:     }
                    233: 
                    234:     if (ptr->next != NIL) {
                    235:        ptr->next->last = ptr->last;
                    236:     }
                    237: 
                    238:     free((char *) ptr);
                    239: }

unix.superglobalmegacorp.com

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