OSDN Git Service

modified comments
[opengatem/opengatem.git] / mngsrc / queue.c
1 /**************************************************
2 OpengateM - MAC address authentication system 
3  module to control queue 
4
5   As address check by database is time consuming procedure,
6   the recently checked addresses are cached.
7   The cache is implemented with HashTable and Queue.
8   Queue has data odrered by checked time, and
9   controls the remove sequence.
10
11 Copyright (C) 2011 Opengate Project Team
12 Written by Yoshiaki Watanabe
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27
28 Email: watanaby@is.saga-u.ac.jp
29 **************************************************/
30
31 #include "opengatemmng.h"
32
33 /* Queue (linked-list) for storing string-key stirng-data pair */
34 struct queueNode{
35   char keyStr[WORDMAXLN];
36   char dataStr[WORDMAXLN];
37   struct queueNode *next;
38 };
39 static struct queueNode* queueTail=NULL;
40 static struct queueNode* queueHead=NULL;
41
42 /***************************************************/
43 /* initialize Queue (linked-list)                  */
44 /* Queue                                           */
45 /* HeadNode -> DataNode -> DataNode -> TailNode    */
46 /*  (dummy)                             (dummy)    */
47 /*   ^                                   ^         */
48 /*   queueHead                           queueTail */
49 /***************************************************/
50 int initqueue(void){
51
52   char keyStr[WORDMAXLN];
53   char dataStr[WORDMAXLN];
54
55   /* if not exist, prepare head and tail nodes */
56   if(queueHead==NULL){
57     queueHead=(struct queueNode*)malloc(sizeof(struct queueNode));
58     if(queueHead==NULL){
59       err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
60       terminateProg(0);
61     }
62     queueTail=(struct queueNode*)malloc(sizeof(struct queueNode));
63     if(queueTail==NULL){
64       err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
65       terminateProg(0);
66     }
67     queueHead->keyStr[0]='\0';
68     queueHead->dataStr[0]='\0';
69     queueTail->keyStr[0]='\0';
70     queueTail->dataStr[0]='\0';
71     queueHead->next=queueTail;
72     queueTail->next=NULL;
73   }
74   
75   /* if exist, reset all */
76   else{
77     while(Dequeue(keyStr, dataStr))
78       ;
79   }
80   return TRUE;
81 }
82
83 /****************************************/
84 /* Add data to the tail of Queue        */
85 /* keyStr,dataStr:(input)               */
86 /****************************************/
87 int enqueue(char* keyStr, char* dataStr){
88   struct queueNode *newNode;
89
90   /* if not prepared, error */
91   if(queueHead==NULL){
92     err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
93     return FALSE;
94   }
95
96   /* add item after the tail and set it as new tail*/
97   newNode=(struct queueNode*)malloc(sizeof(struct queueNode));
98   if(newNode==NULL){
99     err_msg("ERR at %s#%d: fail to malloc",__FILE__,__LINE__);
100     terminateProg(0);
101   }
102   strlcpy(queueTail->keyStr, keyStr, WORDMAXLN);
103   strlcpy(queueTail->dataStr, dataStr, WORDMAXLN);
104   queueTail->next=newNode;
105   queueTail=newNode;
106   queueTail->keyStr[0]='\0';
107   queueTail->dataStr[0]='\0';
108   queueTail->next=NULL;
109   return TRUE;
110 }
111
112 /**********************************************/
113 /* Get and remove data from the head of Queue */
114 /*  keyStr, dataStr:(output)                  */
115 /**********************************************/
116 int dequeue(char* keyStr, char* dataStr){
117
118   /* set null string as default */
119   keyStr[0]='\0';
120   dataStr[0]='\0';
121
122   /* if not prepared, error */
123   if(queueHead==NULL){
124     err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
125     return FALSE;
126   }
127   else if(queueHead->next==NULL){
128     err_msg("ERR at %s#%d: queue not init",__FILE__,__LINE__);
129     return FALSE;  
130   }
131
132   /* if no data, return false */
133   else if(queueHead->next==queueTail){
134     return FALSE;
135   }
136
137   /* get item from the head */
138   else {
139     struct queueNode *temp;
140     temp=queueHead->next;
141     queueHead->next=temp->next;
142     strlcpy(keyStr, temp->keyStr, sizeof(temp->keyStr));
143     strlcpy(dataStr, temp->dataStr, sizeof(temp->dataStr));
144     free(temp);
145   }
146   return TRUE;
147 }
148
149 /******************************************/
150 /* Listing Queue items (for debugging)    */
151 /******************************************/
152 int listqueue(void){
153
154   struct queueNode *temp;
155
156   if(queueHead==NULL) return FALSE;
157   temp=queueHead->next;
158   while(temp->next!=NULL){
159     printf("[%s][%s]\n", temp->keyStr, temp->dataStr);
160     temp=temp->next;
161   }
162   return TRUE;
163 }
164
165
166 /*****************************/
167 /* free all memory for Queue */
168 /*****************************/
169 void freequeue(void){
170   char keyStr[WORDMAXLN];
171   char dataStr[WORDMAXLN];
172   while(Dequeue(keyStr, dataStr));
173   free(queueHead);
174   free(queueTail);
175   queueHead=queueTail=NULL;
176 }
177
178 /**********************************/
179 /**********************************/
180 int Initqueue(void){
181   int ret;
182   if(debug>1) err_msg("DEBUG:=>initqueue( )");
183   ret = initqueue();
184   if(debug>1) err_msg("DEBUG:(%d)<=initqueue( )",ret);
185   return ret;
186
187 }
188 int Enqueue(char* keyStr, char* dataStr){
189   int ret;
190   if(debug>1) err_msg("DEBUG:=>enqueue(%s,%s)", keyStr, dataStr);
191   ret = enqueue(keyStr, dataStr);
192   if(debug>1) err_msg("DEBUG:(%d)<=enqueue( )",ret);
193   return ret;
194
195 }
196 int Dequeue(char* keyStr, char* dataStr){
197   int ret;
198   if(debug>1) err_msg("DEBUG:=>dequeue( )");
199   ret = dequeue(keyStr, dataStr);
200   if(debug>1) err_msg("DEBUG:(%d)<=dequque(%s,%s)",ret, keyStr, dataStr);
201   return ret;
202
203 }
204 int Listqueue(void){
205   int ret;
206   if(debug>1) err_msg("DEBUG:=>listQueue( )");
207   ret = listqueue();
208   if(debug>1) err_msg("DEBUG:(%d)<=listQueue( )",ret);
209   return ret;
210 }
211
212 void Freequeue(void){
213   if(debug>1) err_msg("DEBUG:=>freeQueue()");
214   freequeue();
215   if(debug>1) err_msg("DEBUG:<=freequeue()");
216 }
217
218