2 * Copyright (c) 1998-2000, Index Data.
3 * See the file LICENSE for details.
5 * $Log: yaz-pdu-assoc.cpp,v $
6 * Revision 1.13 2000-09-08 10:23:42 adam
7 * Added skeleton of yaz-z-server.
9 * Revision 1.12 2000/09/06 14:23:45 adam
12 * Revision 1.11 2000/09/04 08:29:22 adam
13 * Fixed memory leak(s). Added re-use of associations, rather than
14 * re-init, when maximum number of targets are in use.
16 * Revision 1.10 2000/08/10 08:42:42 adam
17 * Fixes for {set,get}_APDU_log.
19 * Revision 1.9 1999/12/06 13:52:45 adam
20 * Modified for new location of YAZ header files. Experimental threaded
23 * Revision 1.8 1999/04/28 13:04:03 adam
24 * Fixed setting of proxy otherInfo so that database(s) are removed.
26 * Revision 1.7 1999/04/21 12:09:01 adam
27 * Many improvements. Modified to proxy server to work with "sessions"
30 * Revision 1.6 1999/04/20 10:30:05 adam
31 * Implemented various stuff for client and proxy. Updated calls
32 * to ODR to reflect new name parameter.
34 * Revision 1.5 1999/04/09 11:46:57 adam
35 * Added object Yaz_Z_Assoc. Much more functional client.
37 * Revision 1.4 1999/03/23 14:17:57 adam
38 * More work on timeout handling. Work on yaz-client.
40 * Revision 1.3 1999/02/02 14:01:20 adam
41 * First WIN32 port of YAZ++.
43 * Revision 1.2 1999/01/28 13:08:44 adam
44 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
45 * yaz-socket-manager.cc.
47 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
48 * First implementation of YAZ++.
54 #include <yaz-pdu-assoc.h>
57 #include <yaz/tcpip.h>
59 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable)
63 m_socketObservable = socketObservable;
76 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
78 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable);
82 Yaz_PDU_Assoc::~Yaz_PDU_Assoc()
87 void Yaz_PDU_Assoc::socketNotify(int event)
89 logf (m_log, "Yaz_PDU_Assoc::socketNotify p=%p event = %d", this, event);
90 if (0 /* m_state == Connected */)
93 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
94 YAZ_SOCKET_OBSERVE_EXCEPT);
95 m_PDU_Observer->connectNotify();
98 else if (m_state == Connecting)
100 if (event & YAZ_SOCKET_OBSERVE_READ)
103 m_PDU_Observer->failNotify();
105 else if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
107 m_PDU_Observer->timeoutNotify();
112 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
113 YAZ_SOCKET_OBSERVE_EXCEPT);
114 m_PDU_Observer->connectNotify();
118 else if (m_state == Listen)
120 if (event & YAZ_SOCKET_OBSERVE_READ)
125 if ((res = cs_listen(m_cs, 0, 0)) == 1)
129 logf(LOG_FATAL|LOG_ERRNO, "cs_listen failed");
132 if (!(new_line = cs_accept(m_cs)))
135 /* 1. create socket-manager
137 3. create top-level object
138 setup observer for child fileid in pdu-assoc
141 int fd = cs_fileno(new_line);
142 cs_fileno(new_line) = -1;
143 cs_close (new_line); /* potential problem ... */
147 Yaz_PDU_Assoc *assoc = new Yaz_PDU_Assoc (m_socketObservable);
148 assoc->m_parent = this;
149 assoc->m_next = m_children;
152 assoc->m_PDU_Observer = m_PDU_Observer->clone(assoc);
157 else if (m_state == Ready)
159 if (event & YAZ_SOCKET_OBSERVE_WRITE)
163 if (event & YAZ_SOCKET_OBSERVE_READ)
167 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
172 logf (m_log, "Connection closed by peer");
174 m_PDU_Observer->failNotify();
177 // lock it, so we know if recv_PDU deletes it.
179 m_destroyed = &destroyed;
181 m_PDU_Observer->recv_PDU(m_input_buf, res);
183 if (destroyed) // it really was destroyed, return now.
185 } while (m_cs && cs_more (m_cs));
187 if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
189 m_PDU_Observer->timeoutNotify();
194 void Yaz_PDU_Assoc::close()
196 m_socketObservable->deleteObserver(this);
200 logf (m_log, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
206 PDU_Queue *q_this = m_queue_out;
207 m_queue_out = m_queue_out->m_next;
215 void Yaz_PDU_Assoc::destroy()
222 // delete from parent's child list (if any)
225 c = &m_parent->m_children;
233 // delete all children ...
237 Yaz_PDU_Assoc *here = *c;
244 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
246 m_buf = (char *) malloc (len);
247 memcpy (m_buf, buf, len);
252 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
257 int Yaz_PDU_Assoc::flush_PDU()
261 logf (m_log, "Yaz_PDU_Assoc::flush_PDU");
262 if (m_state != Ready)
264 logf (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
267 PDU_Queue *q = m_queue_out;
270 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
271 YAZ_SOCKET_OBSERVE_EXCEPT);
274 r = cs_put (m_cs, q->m_buf, q->m_len);
278 m_PDU_Observer->failNotify();
283 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
284 YAZ_SOCKET_OBSERVE_EXCEPT|
285 YAZ_SOCKET_OBSERVE_WRITE);
286 logf (m_log, "Yaz_PDU_Assoc::flush_PDU put %d bytes (incomplete)",
290 logf (m_log, "Yaz_PDU_Assoc::flush_PDU put %d bytes", q->m_len);
291 // whole packet sent... delete this and proceed to next ...
292 m_queue_out = q->m_next;
294 // don't select on write if queue is empty ...
296 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
297 YAZ_SOCKET_OBSERVE_EXCEPT);
301 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
303 logf (m_log, "Yaz_PDU_Assoc::send_PDU");
304 PDU_Queue **pq = &m_queue_out;
305 int is_idle = (*pq ? 0 : 1);
309 logf (m_log, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
314 *pq = new PDU_Queue(buf, len);
318 logf (m_log, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
323 COMSTACK Yaz_PDU_Assoc::comstack()
327 CS_TYPE cs_type = tcpip_type;
328 m_cs = cs_create (cs_type, 0, PROTO_Z3950);
333 void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
338 COMSTACK cs = comstack();
340 logf (m_log, "Yaz_PDU_Assoc::listen %s", addr);
341 m_PDU_Observer = observer;
344 ap = cs_straddr (cs, addr);
347 if (cs_bind(cs, ap, CS_SERVER) < 0)
349 m_socketObservable->addObserver(cs_fileno(cs), this);
350 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
351 YAZ_SOCKET_OBSERVE_EXCEPT);
355 void Yaz_PDU_Assoc::idleTime(int idleTime)
357 m_idleTime = idleTime;
358 logf (m_log, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
359 m_socketObservable->timeoutObserver(this, m_idleTime);
362 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
365 logf (m_log, "Yaz_PDU_Assoc::connect %s", addr);
367 m_PDU_Observer = observer;
368 COMSTACK cs = comstack();
369 void *ap = cs_straddr (cs, addr);
372 logf (m_log, "cs_straddr failed");
375 int res = cs_connect (cs, ap);
376 logf (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(cs), res);
377 m_socketObservable->addObserver(cs_fileno(cs), this);
378 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
379 YAZ_SOCKET_OBSERVE_EXCEPT|
380 YAZ_SOCKET_OBSERVE_WRITE);
381 m_state = Connecting;
384 void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd)
387 m_PDU_Observer = observer;
390 CS_TYPE cs_type = tcpip_type;
391 m_cs = cs_createbysocket(fd, cs_type, 0, PROTO_Z3950);
393 m_socketObservable->addObserver(fd, this);
394 m_socketObservable->maskObserver(this,
395 YAZ_SOCKET_OBSERVE_READ|
396 YAZ_SOCKET_OBSERVE_EXCEPT);
397 m_socketObservable->timeoutObserver(this, m_idleTime);
403 // Single-threaded... Only useful for non-blocking handlers
404 void Yaz_PDU_Assoc::childNotify(int fd)
406 /// Clone PDU Observable (keep socket manager)
407 IYaz_PDU_Observable *new_observable = clone();
409 /// Clone PDU Observer
410 IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable);
412 /// Attach new socket to it
413 new_observable->socket(observer, fd);
417 #include <yaz-socket-manager.h>
432 Yaz_SocketManager *s = (Yaz_SocketManager *) p;
434 logf (LOG_LOG, "thread started");
435 while (s->processEvent() > 0)
437 logf (LOG_LOG, "thread finished");
444 void Yaz_PDU_Assoc::childNotify(int fd)
446 Yaz_SocketManager *socket_observable = new Yaz_SocketManager;
447 Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable);
449 /// Clone PDU Observer
450 IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable);
452 /// Attach new socket to it
453 new_observable->socket(observer, fd);
457 t_id = _beginthread (events, 0, socket_observable);
460 logf (LOG_FATAL|LOG_ERRNO, "_beginthread failed");
466 int id = pthread_create (&type, 0, events, socket_observable);
467 logf (LOG_LOG, "pthread_create returned id=%d", id);