2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-pdu-assoc.cpp,v $
6 * Revision 1.22 2001-01-29 11:18:24 adam
7 * Server sets OPTIONS search and present.
9 * Revision 1.21 2000/11/20 14:17:36 adam
10 * Yet another WIN32 fix for connect notify.
12 * Revision 1.20 2000/11/20 11:27:33 adam
13 * Fixes for connect operation (timeout and notify fix).
15 * Revision 1.19 2000/11/01 14:22:59 adam
16 * Added fd parameter for method IYaz_PDU_Observer::clone.
18 * Revision 1.18 2000/10/24 12:29:57 adam
19 * Fixed bug in proxy where a Yaz_ProxyClient could be owned by
20 * two Yaz_Proxy's (fatal).
22 * Revision 1.17 2000/10/11 11:58:16 adam
23 * Moved header files to include/yaz++. Switched to libtool and automake.
24 * Configure script creates yaz++-config script.
26 * Revision 1.16 2000/09/22 09:54:11 heikki
29 * Revision 1.15 2000/09/21 21:43:20 adam
30 * Better high-level server API.
32 * Revision 1.14 2000/09/12 12:09:53 adam
33 * More work on high-level server.
35 * Revision 1.13 2000/09/08 10:23:42 adam
36 * Added skeleton of yaz-z-server.
38 * Revision 1.12 2000/09/06 14:23:45 adam
41 * Revision 1.11 2000/09/04 08:29:22 adam
42 * Fixed memory leak(s). Added re-use of associations, rather than
43 * re-init, when maximum number of targets are in use.
45 * Revision 1.10 2000/08/10 08:42:42 adam
46 * Fixes for {set,get}_APDU_log.
48 * Revision 1.9 1999/12/06 13:52:45 adam
49 * Modified for new location of YAZ header files. Experimental threaded
52 * Revision 1.8 1999/04/28 13:04:03 adam
53 * Fixed setting of proxy otherInfo so that database(s) are removed.
55 * Revision 1.7 1999/04/21 12:09:01 adam
56 * Many improvements. Modified to proxy server to work with "sessions"
59 * Revision 1.6 1999/04/20 10:30:05 adam
60 * Implemented various stuff for client and proxy. Updated calls
61 * to ODR to reflect new name parameter.
63 * Revision 1.5 1999/04/09 11:46:57 adam
64 * Added object Yaz_Z_Assoc. Much more functional client.
66 * Revision 1.4 1999/03/23 14:17:57 adam
67 * More work on timeout handling. Work on yaz-client.
69 * Revision 1.3 1999/02/02 14:01:20 adam
70 * First WIN32 port of YAZ++.
72 * Revision 1.2 1999/01/28 13:08:44 adam
73 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
74 * yaz-socket-manager.cc.
76 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
77 * First implementation of YAZ++.
83 #include <yaz++/yaz-pdu-assoc.h>
86 #include <yaz/tcpip.h>
88 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable)
92 m_socketObservable = socketObservable;
105 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
107 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable);
111 void Yaz_PDU_Assoc::socketNotify(int event)
113 logf (m_log, "Yaz_PDU_Assoc::socketNotify p=%p event = %d", this, event);
117 if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_EXCEPT))
120 m_PDU_Observer->failNotify();
122 else if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
124 m_PDU_Observer->timeoutNotify();
129 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
130 YAZ_SOCKET_OBSERVE_EXCEPT);
131 m_PDU_Observer->connectNotify();
136 if (event & YAZ_SOCKET_OBSERVE_READ)
141 if ((res = cs_listen(m_cs, 0, 0)) == 1)
145 logf(LOG_FATAL|LOG_ERRNO, "cs_listen failed");
148 if (!(new_line = cs_accept(m_cs)))
150 /* 1. create socket-manager
152 3. create top-level object
153 setup observer for child fileid in pdu-assoc
156 int fd = cs_fileno(new_line);
157 logf (m_log, "accept ok fd = %d", fd);
158 cs_fileno(new_line) = -1;
164 if (event & YAZ_SOCKET_OBSERVE_WRITE)
168 if (event & YAZ_SOCKET_OBSERVE_READ)
172 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
177 logf (m_log, "Connection closed by peer");
179 m_PDU_Observer->failNotify(); // problem here..
182 // lock it, so we know if recv_PDU deletes it.
184 m_destroyed = &destroyed;
186 m_PDU_Observer->recv_PDU(m_input_buf, res);
188 if (destroyed) // it really was destroyed, return now.
190 } while (m_cs && cs_more (m_cs));
192 if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
194 m_PDU_Observer->timeoutNotify();
198 logf (m_log, "CLOSING state=%d event was %d", m_state, event);
200 m_PDU_Observer->failNotify();
203 logf (m_log, "Unknown state=%d event was %d", m_state, event);
205 m_PDU_Observer->failNotify();
209 void Yaz_PDU_Assoc::close()
211 m_socketObservable->deleteObserver(this);
215 logf (m_log, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
221 PDU_Queue *q_this = m_queue_out;
222 m_queue_out = m_queue_out->m_next;
230 void Yaz_PDU_Assoc::destroy()
237 // delete from parent's child list (if any)
240 c = &m_parent->m_children;
248 // delete all children ...
252 Yaz_PDU_Assoc *here = *c;
259 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
261 m_buf = (char *) malloc (len);
262 memcpy (m_buf, buf, len);
267 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
272 int Yaz_PDU_Assoc::flush_PDU()
276 logf (m_log, "Yaz_PDU_Assoc::flush_PDU");
277 if (m_state != Ready)
279 logf (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
282 PDU_Queue *q = m_queue_out;
285 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
286 YAZ_SOCKET_OBSERVE_EXCEPT);
289 r = cs_put (m_cs, q->m_buf, q->m_len);
293 m_PDU_Observer->failNotify();
298 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
299 YAZ_SOCKET_OBSERVE_EXCEPT|
300 YAZ_SOCKET_OBSERVE_WRITE);
301 logf (m_log, "Yaz_PDU_Assoc::flush_PDU put %d bytes (incomplete)",
305 logf (m_log, "Yaz_PDU_Assoc::flush_PDU put %d bytes", q->m_len);
306 // whole packet sent... delete this and proceed to next ...
307 m_queue_out = q->m_next;
309 // don't select on write if queue is empty ...
311 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
312 YAZ_SOCKET_OBSERVE_EXCEPT);
316 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
318 logf (m_log, "Yaz_PDU_Assoc::send_PDU");
319 PDU_Queue **pq = &m_queue_out;
320 int is_idle = (*pq ? 0 : 1);
324 logf (m_log, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
329 *pq = new PDU_Queue(buf, len);
333 logf (m_log, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
338 COMSTACK Yaz_PDU_Assoc::comstack()
342 CS_TYPE cs_type = tcpip_type;
343 m_cs = cs_create (cs_type, 0, PROTO_Z3950);
348 void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
353 COMSTACK cs = comstack();
355 logf (m_log, "Yaz_PDU_Assoc::listen %s", addr);
356 m_PDU_Observer = observer;
359 ap = cs_straddr (cs, addr);
362 if (cs_bind(cs, ap, CS_SERVER) < 0)
364 m_socketObservable->addObserver(cs_fileno(cs), this);
365 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
366 YAZ_SOCKET_OBSERVE_EXCEPT);
367 logf (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(cs));
371 void Yaz_PDU_Assoc::idleTime(int idleTime)
373 m_idleTime = idleTime;
374 logf (m_log, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
375 m_socketObservable->timeoutObserver(this, m_idleTime);
378 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
381 logf (m_log, "Yaz_PDU_Assoc::connect %s", addr);
383 m_PDU_Observer = observer;
384 COMSTACK cs = comstack();
385 void *ap = cs_straddr (cs, addr);
388 logf (m_log, "cs_straddr failed");
391 int res = cs_connect (cs, ap);
392 logf (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(cs), res);
393 m_socketObservable->addObserver(cs_fileno(cs), this);
394 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
395 YAZ_SOCKET_OBSERVE_EXCEPT|
396 YAZ_SOCKET_OBSERVE_WRITE);
398 m_state = Connecting;
399 // if res < 0, then cs_connect failed immediately -> m_state is Closed..
402 void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd)
405 m_PDU_Observer = observer;
408 CS_TYPE cs_type = tcpip_type;
409 m_cs = cs_createbysocket(fd, cs_type, 0, PROTO_Z3950);
411 m_socketObservable->addObserver(fd, this);
412 m_socketObservable->maskObserver(this,
413 YAZ_SOCKET_OBSERVE_READ|
414 YAZ_SOCKET_OBSERVE_EXCEPT);
415 m_socketObservable->timeoutObserver(this, m_idleTime);
420 // 1 = single-threaded
421 // 0 = multi-threaded
423 // Single-threaded... Only useful for non-blocking handlers
424 void Yaz_PDU_Assoc::childNotify(int fd)
426 // Clone PDU Observable (keep socket manager)
427 IYaz_PDU_Observable *new_observable = clone();
429 // Clone PDU Observer
430 IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable, fd);
432 // Attach new socket to it
433 new_observable->socket(observer, fd);
437 #include <yaz++/yaz-socket-manager.h>
452 Yaz_SocketManager *s = (Yaz_SocketManager *) p;
454 logf (LOG_LOG, "thread started");
455 while (s->processEvent() > 0)
457 logf (LOG_LOG, "thread finished");
464 void Yaz_PDU_Assoc::childNotify(int fd)
466 Yaz_SocketManager *socket_observable = new Yaz_SocketManager;
467 Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable);
469 /// Clone PDU Observer
470 IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable, fd);
472 /// Attach new socket to it
473 new_observable->socket(observer, fd);
477 t_id = _beginthread (events, 0, socket_observable);
480 logf (LOG_FATAL|LOG_ERRNO, "_beginthread failed");
486 int id = pthread_create (&type, 0, events, socket_observable);
487 logf (LOG_LOG, "pthread_create returned id=%d", id);