1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/* This file is part of the Project Athena Zephyr Notification System.
* It contains the hostmanager queueing routines.
*
* Created by: David C. Jedlinsky
*
* $Source: /mit/zephyr/repository/zephyr/zhm/queue.c,v $
* $Author: ghudson $
*
* Copyright (c) 1987 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#include "zhm.h"
#ifndef lint
#ifndef SABER
static char rcsid_queue_c[] = "$Header: /mit/zephyr/repository/zephyr/zhm/queue.c,v 1.16 1995/07/02 05:51:53 ghudson Exp $";
#endif /* SABER */
#endif /* lint */
typedef struct _Queue {
long timeout;
int retries;
ZNotice_t z_notice;
caddr_t z_packet;
struct sockaddr_in reply;
struct _Queue *next, **prev_p;
} Queue;
static Queue *hm_queue;
static Queue *is_in_queue __P((ZNotice_t *notice));
static Code_t dump_queue __P((void));
int rexmit_times[] = { 2, 2, 4, 4, 8, -1 };
extern int timeout_type;
#ifdef DEBUG
Code_t dump_queue();
#endif
void init_queue()
{
Queue *q;
while (hm_queue) {
q = hm_queue;
free(q->z_packet);
hm_queue = q->next;
free(q);
}
DPR ("Queue initialized and flushed.\n");
}
Code_t add_notice_to_queue(notice, packet, repl, len)
ZNotice_t *notice;
char * packet;
struct sockaddr_in *repl;
int len;
{
Queue *entry;
DPR ("Adding notice to queue...\n");
if (!is_in_queue(notice)) {
entry = (Queue *) malloc(sizeof(Queue));
if (entry == NULL)
return(ZERR_NONOTICE);
entry->timeout = time((time_t *)0) + rexmit_times[0];
entry->retries = 0;
entry->z_packet = (char *) malloc(Z_MAXPKTLEN);
if (entry->z_packet == NULL) {
free(entry);
return(ZERR_NONOTICE);
}
memcpy(entry->z_packet, packet, Z_MAXPKTLEN);
if (ZParseNotice(entry->z_packet, len, &entry->z_notice)
!= ZERR_NONE) {
syslog(LOG_ERR, "ZParseNotice failed, but succeeded before");
free(entry->z_packet);
} else {
entry->reply = *repl;
if (hm_queue)
hm_queue->prev_p = &entry->next;
entry->next = hm_queue;
entry->prev_p = &hm_queue;
hm_queue = entry;
}
}
#ifdef DEBUG
if (!is_in_queue(notice))
return(ZERR_NONOTICE);
else
#endif /* DEBUG */
return(ZERR_NONE);
}
Code_t remove_notice_from_queue(notice, kind, repl)
ZNotice_t *notice;
ZNotice_Kind_t *kind;
struct sockaddr_in *repl;
{
Queue *entry;
DPR ("Removing notice from queue...\n");
entry = is_in_queue(notice);
if (entry == NULL)
return(ZERR_NONOTICE);
*kind = entry->z_notice.z_kind;
*repl = entry->reply;
free(entry->z_packet);
if (entry->next)
entry->next->prev_p = entry->prev_p;
*entry->prev_p = entry->next;
free(entry);
if (!hm_queue)
alarm(0);
#ifdef DEBUG
dump_queue();
#endif /* DEBUG */
return(ZERR_NONE);
}
void retransmit_queue(sin)
struct sockaddr_in *sin;
{
Queue *entry;
Code_t ret;
DPR ("Retransmitting queue to new server...\n");
ret = ZSetDestAddr(sin);
if (ret != ZERR_NONE) {
Zperr (ret);
com_err("queue", ret, "setting destination");
}
for (entry = hm_queue; entry; entry = entry->next) {
DPR("notice:\n");
DPR2("\tz_kind: %d\n", entry->z_notice.z_kind);
DPR2("\tz_port: %u\n", ntohs(entry->z_notice.z_port));
DPR2("\tz_class: %s\n", entry->z_notice.z_class);
DPR2("\tz_clss_inst: %s\n", entry->z_notice.z_class_inst);
DPR2("\tz_opcode: %s\n", entry->z_notice.z_opcode);
DPR2("\tz_sender: %s\n", entry->z_notice.z_sender);
DPR2("\tz_recip: %s\n", entry->z_notice.z_recipient);
ret = send_outgoing(&entry->z_notice);
if (ret != ZERR_NONE) {
Zperr(ret);
com_err("queue", ret, "sending raw notice");
}
entry->timeout = rexmit_times[0];
entry->retries = 0;
}
timeout_type = NOTICES;
alarm(rexmit_times[0]);
}
#ifdef DEBUG
static Code_t dump_queue()
{
Queue *entry;
caddr_t mp;
int ml;
DPR ("Dumping queue...\n");
if (!hm_queue) {
printf("Queue is empty.\n");
return;
}
for (entry = hm_queue; entry; entry = entry->next) {
printf("notice:\n");
printf("\tz_kind: %d\n", entry->z_notice.z_kind);
printf("\tz_port: %u\n", ntohs(entry->z_notice.z_port));
printf("\tz_class: %s\n", entry->z_notice.z_class);
printf("\tz_clss_inst: %s\n", entry->z_notice.z_class_inst);
printf("\tz_opcode: %s\n", entry->z_notice.z_opcode);
printf("\tz_sender: %s\n", entry->z_notice.z_sender);
printf("\tz_recip: %s\n", entry->z_notice.z_recipient);
printf("\tMessage:\n");
mp = entry->z_notice.z_message;
for (ml = strlen(mp) + 1; ml <= entry->z_notice.z_message_len; ml++) {
printf("\t%s\n", mp);
mp += strlen(mp)+1;
ml += strlen(mp);
}
}
}
#endif /* DEBUG */
int queue_len()
{
int length = 0;
Queue *entry;
for (entry = hm_queue; entry; entry = entry->next)
length++;
return length;
}
static Queue *is_in_queue(notice)
ZNotice_t *notice;
{
Queue *entry;
for (entry = hm_queue; entry; entry = entry->next) {
if (ZCompareUID(&entry->z_notice.z_uid, ¬ice->z_uid))
return entry;
}
return NULL;
}
void resend_notices(sin)
struct sockaddr_in *sin;
{
Queue *entry;
Code_t ret;
DPR ("Resending notices...\n");
ret = ZSetDestAddr(sin);
if (ret != ZERR_NONE) {
Zperr(ret);
com_err("queue", ret, "setting destination");
}
for (entry = hm_queue; entry; entry = entry->next) {
if (entry->timeout <= time((time_t *)0)) {
entry->retries++;
if (rexmit_times[entry->retries] == -1) {
new_server(NULL);
break;
} else {
DPR("notice:\n");
DPR2("\tz_kind: %d\n", entry->z_notice.z_kind);
DPR2("\tz_port: %u\n", ntohs(entry->z_notice.z_port));
DPR2("\tz_class: %s\n", entry->z_notice.z_class);
DPR2("\tz_clss_inst: %s\n", entry->z_notice.z_class_inst);
DPR2("\tz_opcode: %s\n", entry->z_notice.z_opcode);
DPR2("\tz_sender: %s\n", entry->z_notice.z_sender);
DPR2("\tz_recip: %s\n", entry->z_notice.z_recipient);
ret = send_outgoing(&entry->z_notice);
if (ret != ZERR_NONE) {
Zperr(ret);
com_err("queue", ret, "sending raw notice");
}
entry->timeout = time(NULL) + rexmit_times[entry->retries];
}
}
}
timeout_type = NOTICES;
alarm(rexmit_times[0]);
}
|