summaryrefslogtreecommitdiff
path: root/zhm/queue.c
blob: 70665788a732c0fa48f58ced4c451bd4dd66fd83 (plain)
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
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the hostmanager queueing routines.
 *
 *      Created by:     David C. Jedlinsky
 *
 *      $Source$
 *      $Author$
 *
 *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h". 
 */

#include <zephyr/mit-copyright.h>
#include <zephyr/zephyr.h>

#ifndef lint
#ifndef SABER
static char rcsid_queue_c[] = "$Header$";
#endif SABER
#endif lint

#ifdef DEBUG
#define DPR(a) fprintf(stderr, a)
#define DPR2(a,b) fprintf(stderr, a, b)
#else
#define DPR(a)
#define DPR2(a,b)
#endif

#define TIMEOUT 10

typedef struct _Queue {
      int timeout;
      int retries;
      ZNotice_t z_notice;
      caddr_t z_packet;
      struct sockaddr_in reply;
} Queue;

struct _qelem {
      struct _qelem *q_forw;
      struct _qelem *q_back;
      Queue *q_data;
};

typedef struct _qelem Qelem;

extern char *malloc();

Qelem hm_queue, *is_in_queue();

Code_t init_queue()
{
      hm_queue.q_forw = hm_queue.q_back = &hm_queue;
      hm_queue.q_data = NULL;
      DPR ("Queue initialized and flushed.\n");
}

Code_t add_notice_to_queue(notice, packet, repl)
     ZNotice_t *notice;
     caddr_t packet;
     struct sockaddr_in *repl;
{
      Qelem *elem;
      Queue *entry;

      DPR ("Adding notice to queue...\n");
      if (!is_in_queue(*notice)) {
	    elem = (Qelem *)malloc(sizeof(Qelem));
	    entry = (Queue *)malloc(sizeof(Queue));
	    entry->timeout = TIMEOUT;
	    entry->retries = 0;
	    entry->z_notice = *notice;
	    entry->z_packet = packet;
	    entry->reply = *repl;
	    elem->q_data = entry;
	    elem->q_forw = elem;
	    elem->q_back = elem;
	    insque(elem, hm_queue.q_back);
      }
#ifdef DEBUG
      if (!is_in_queue(*notice))
	return(ZERR_NONOTICE);
      else
#endif DEBUG
	return(ZERR_NONE);
}

Code_t remove_notice_from_queue(notice, packet, repl)
     ZNotice_t *notice;
     caddr_t *packet;
     struct sockaddr_in *repl;
{
      Qelem *elem;

      DPR ("Removing notice from queue...\n");
      /* Set notice & packet to the one removed, so we can acknowledge */
      if ((elem = is_in_queue(*notice)) == NULL)
	return(ZERR_NONOTICE);
      else {
	    *notice = elem->q_data->z_notice;
	    *packet = elem->q_data->z_packet;
	    *repl = elem->q_data->reply;
	    remque(elem);
	    dump_queue();
	    return(ZERR_NONE);
      }
}

Code_t retransmit_queue()
{
      DPR ("Retransmitting queue to new server...\n");
}

Code_t dump_queue()
{
      Qelem *srch;
      caddr_t mp;
      int ml;

      DPR ("Dumping queue...\n");
      if ((srch = hm_queue.q_forw) == &hm_queue)
	printf("Queue is empty.\n");
      else do {
	    printf("notice:\n");
	    printf("\tz_kind: %d\n", srch->q_data->z_notice.z_kind);
	    printf("\tz_port: %u\n", srch->q_data->z_notice.z_port);
	    printf("\tz_class: %s\n", srch->q_data->z_notice.z_class);
	    printf("\tz_clss_inst: %s\n", srch->q_data->z_notice.z_class_inst);
	    printf("\tz_opcode: %s\n", srch->q_data->z_notice.z_opcode);
	    printf("\tz_sender: %s\n", srch->q_data->z_notice.z_sender);
	    printf("\tz_recip: %s\n", srch->q_data->z_notice.z_recipient);
	    printf("\tMessage:\n");
	    mp = srch->q_data->z_notice.z_message;
	    for (ml = strlen(mp)+1;
		 ml <= srch->q_data->z_notice.z_message_len; ml++) {
		  printf("\t%s\n", mp);
		  mp += strlen(mp)+1;
		  ml += strlen(mp);
	    }
	    srch = srch->q_forw;
      } while (srch != &hm_queue);
}

Qelem *is_in_queue(notice)
     ZNotice_t notice;
{
      Qelem *srch;

      srch = hm_queue.q_forw;
      if (srch == &hm_queue)
	return(NULL);
      do {
	    if (ZCompareUID(&(srch->q_data->z_notice.z_uid), &(notice.z_uid)))
	      return(srch);
	    srch = srch->q_forw;
      } while (srch != &hm_queue);
      return(NULL);
}