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