Ignore more files
[yazpp-moved-to-github.git] / zlint / zlint.cpp
1 /*
2  * Copyright (c) 2004, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: zlint.cpp,v 1.12 2005-09-22 12:40:45 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <stdarg.h>
10
11 #include <yaz/comstack.h>
12 #include <yaz/options.h>
13 #include <yaz/otherinfo.h>
14 #include <yaz/charneg.h>
15 #include <yaz/log.h>
16
17 #include <zlint.h>
18
19 Zlint_test::~Zlint_test()
20 {
21
22 }
23
24 class Zlint_t {
25 public:
26     friend class Zlint;
27     Zlint_t(Zlint_test *t);
28     ~Zlint_t();
29 private:
30     Zlint_test *m_t;
31     Zlint_t *m_next;
32     int m_test_number_sequence;
33     int m_test_ok;
34     int m_test_reported;
35 };
36
37 Zlint::Zlint(IPDU_Observable *the_PDU_Observable) : 
38     Z_Assoc(the_PDU_Observable)
39     
40 {
41     m_PDU_Observable = the_PDU_Observable;
42     m_host = 0;
43     m_tests = 0;
44     m_cur_test = 0;
45     m_database = 0;
46 }
47
48 Zlint::~Zlint()
49 {
50     while (m_tests)
51     {
52         Zlint_t *t = m_tests;
53         m_tests = t->m_next;
54         delete t;
55     }
56     xfree(m_host);
57     xfree(m_database);
58 }
59
60 void Zlint::set_host(const char *cp)
61 {
62     xfree(m_host);
63     m_host = xstrdup(cp);
64     client(m_host);
65     timeout(30);
66
67     const char *basep;
68     cs_get_host_args(m_host, &basep);
69     if (!basep || !*basep)
70         basep = "Default";
71     xfree(m_database);
72     m_database = xstrdup(basep);
73 }
74
75 void Zlint::timeoutNotify()
76 {
77     if (m_cur_test)
78     {
79         if (m_cur_test->m_t->recv_fail(this, 2) != TEST_FINISHED)
80         {
81             close();
82             client(m_host);
83             timeout(30);
84             return;
85         }
86     }
87     close_goto_next();
88 }
89
90 void Zlint::failNotify()
91 {
92     if (m_cur_test)
93     {
94         if (m_cur_test->m_t->recv_fail(this, 1) != TEST_FINISHED)
95         {
96             close();
97             client(m_host);
98             timeout(30);
99             return;
100         }
101     }
102     close_goto_next();
103 }
104
105 void Zlint::connectNotify()
106 {
107     if (m_cur_test)
108     {
109         if (m_cur_test->m_t->init(this) != TEST_FINISHED)
110             return;
111     }
112     close_goto_next();
113 }
114
115 void Zlint::recv_GDU(Z_GDU *gdu, int len)
116 {
117     if (m_cur_test)
118     {
119         int r = m_cur_test->m_t->recv_gdu(this, gdu);
120         if (r == TEST_CONTINUE)
121             return;
122         if (r == TEST_REOPEN)
123         {
124             close();
125             client(m_host);
126             timeout(30);
127             return;
128         }
129     }
130     close_goto_next();
131 }
132
133 void Zlint::close_goto_next()
134 {
135     close();
136     if (m_cur_test)
137         m_cur_test = m_cur_test->m_next;
138     if (m_cur_test)
139         client(m_host);
140     timeout(30);
141 }
142
143 IPDU_Observer *Zlint::sessionNotify(
144     IPDU_Observable *the_PDU_Observable, int fd)
145 {
146     return 0;
147 }
148
149 Z_ReferenceId *Zlint::mk_refid(const char *buf, int len)
150 {
151     Z_ReferenceId *id = 
152         (Z_ReferenceId *) odr_malloc(odr_encode(), sizeof(*id));
153     id->size = id->len = len;
154     id->buf = (unsigned char*) odr_malloc(odr_encode(), len);
155     memcpy(id->buf, buf, len);
156     return id;
157 }
158
159 int Zlint::initResponseGetVersion(Z_InitResponse *init)
160 {
161     int no = 0;
162     int off = 0;
163     int i;
164     for (i = 0; i<12; i++)
165         if (ODR_MASK_GET(init->protocolVersion, no))
166         {
167             no = i+1;
168         }
169         else
170             off = 1;
171     return no;
172 }
173
174 void Zlint::add_test(Zlint_test *t)
175 {
176     Zlint_t **d = &m_tests;
177     while (*d)
178         d = &(*d)->m_next;
179     *d = new Zlint_t(t);
180     if (!m_cur_test)
181         m_cur_test = m_tests;
182 }
183
184 void Zlint::msg_check_for(const char *fmt, ...)
185 {
186     m_cur_test->m_test_ok = 0;
187     m_cur_test->m_test_number_sequence++;
188     m_cur_test->m_test_reported = 0;
189
190     va_list ap;
191     va_start(ap, fmt);
192     char buf[1024];
193     vsnprintf(buf, sizeof(buf), fmt, ap);
194     printf ("Checking %s .. ", buf);
195     va_end(ap);
196 }
197
198 void Zlint::msg_check_info(const char *fmt, ...)
199 {
200     va_list ap;
201     va_start(ap, fmt);
202     char buf[1024];
203     vsnprintf(buf, sizeof(buf), fmt, ap);
204     printf (" %s\n", buf);
205     va_end(ap);
206 }
207
208 void Zlint::msg_check_ok()
209 {
210     if (!m_cur_test->m_test_reported)
211     {
212         m_cur_test->m_test_ok = 1;
213         m_cur_test->m_test_reported = 1;
214         printf ("OK\n");
215     }
216 }
217
218 void Zlint::msg_check_fail(const char *fmt, ...)
219 {
220     if (!m_cur_test->m_test_reported)
221     {
222         m_cur_test->m_test_ok = 0;
223         m_cur_test->m_test_reported = 1;
224         printf ("Fail\n");
225     }
226     va_list ap;
227     va_start(ap, fmt);
228     char buf[1024];
229     vsnprintf(buf, sizeof(buf), fmt, ap);
230     printf (" %s\n", buf);
231     va_end(ap);
232 }
233
234 void Zlint::msg_check_notapp()
235 {
236     if (!m_cur_test->m_test_reported)
237     {
238         m_cur_test->m_test_ok = 2;
239         m_cur_test->m_test_reported = 1;
240         printf ("Unsupported\n");
241     }
242 }
243
244 void Zlint::getDatabase(char ***db, int *num)
245 {
246     *db = (char**) odr_malloc(odr_encode(), 2*sizeof(char *));
247     (*db)[0] = m_database;
248     (*db)[1] = 0;
249     *num = 1;
250 }
251
252 Zlint_t::Zlint_t(Zlint_test *t)
253 {
254     m_test_number_sequence = 0;
255     m_test_ok = 0;
256     m_test_reported = 0;
257     m_t = t;
258     m_next = 0;
259 }
260
261 Zlint_t::~Zlint_t()
262 {
263     delete m_t;
264 }
265
266 Zlint_code Zlint_test_simple::recv_fail(Zlint *z, int reason)
267 {
268     z->msg_check_fail("target closed connection");
269     return TEST_FINISHED;
270 }
271 /*
272  * Local variables:
273  * c-basic-offset: 4
274  * indent-tabs-mode: nil
275  * End:
276  * vim: shiftwidth=4 tabstop=8 expandtab
277  */
278