Remove yaz_log call
[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.9 2005-05-17 20:33:57 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         }
164         else
165             off = 1;
166     return no;
167 }
168
169 void Zlint::add_test(Zlint_test *t)
170 {
171     Zlint_t **d = &m_tests;
172     while (*d)
173         d = &(*d)->m_next;
174     *d = new Zlint_t(t);
175     if (!m_cur_test)
176         m_cur_test = m_tests;
177 }
178
179 void Zlint::msg_check_for(const char *fmt, ...)
180 {
181     m_cur_test->m_test_ok = 0;
182     m_cur_test->m_test_number_sequence++;
183     m_cur_test->m_test_reported = 0;
184
185     va_list ap;
186     va_start(ap, fmt);
187     char buf[1024];
188     vsnprintf(buf, sizeof(buf), fmt, ap);
189     printf ("Checking %s .. ", buf);
190     va_end(ap);
191 }
192
193 void Zlint::msg_check_info(const char *fmt, ...)
194 {
195     va_list ap;
196     va_start(ap, fmt);
197     char buf[1024];
198     vsnprintf(buf, sizeof(buf), fmt, ap);
199     printf (" %s\n", buf);
200     va_end(ap);
201 }
202
203 void Zlint::msg_check_ok()
204 {
205     if (!m_cur_test->m_test_reported)
206     {
207         m_cur_test->m_test_ok = 1;
208         m_cur_test->m_test_reported = 1;
209         printf ("OK\n");
210     }
211 }
212
213 void Zlint::msg_check_fail(const char *fmt, ...)
214 {
215     if (!m_cur_test->m_test_reported)
216     {
217         m_cur_test->m_test_ok = 0;
218         m_cur_test->m_test_reported = 1;
219         printf ("Fail\n");
220     }
221     va_list ap;
222     va_start(ap, fmt);
223     char buf[1024];
224     vsnprintf(buf, sizeof(buf), fmt, ap);
225     printf (" %s\n", buf);
226     va_end(ap);
227 }
228
229 void Zlint::msg_check_notapp()
230 {
231     if (!m_cur_test->m_test_reported)
232     {
233         m_cur_test->m_test_ok = 2;
234         m_cur_test->m_test_reported = 1;
235         printf ("Unsupported\n");
236     }
237 }
238
239 void Zlint::getDatabase(char ***db, int *num)
240 {
241     *db = (char**) odr_malloc(odr_encode(), 2*sizeof(char *));
242     (*db)[0] = m_database;
243     (*db)[1] = 0;
244     *num = 1;
245 }
246
247 Zlint_t::Zlint_t(Zlint_test *t)
248 {
249     m_test_number_sequence = 0;
250     m_test_ok = 0;
251     m_test_reported = 0;
252     m_t = t;
253     m_next = 0;
254 }
255
256 Zlint_t::~Zlint_t()
257 {
258     delete m_t;
259 }
260
261 Zlint_code Zlint_test_simple::recv_fail(Zlint *z, int reason)
262 {
263     z->msg_check_fail("target closed connection");
264     return TEST_FINISHED;
265 }