2 * Copyright (c) 1998-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Log: yaz-pdu-assoc.cpp,v $
7 * Revision 1.8 1999-04-28 13:04:03 adam
8 * Fixed setting of proxy otherInfo so that database(s) are removed.
10 * Revision 1.7 1999/04/21 12:09:01 adam
11 * Many improvements. Modified to proxy server to work with "sessions"
14 * Revision 1.6 1999/04/20 10:30:05 adam
15 * Implemented various stuff for client and proxy. Updated calls
16 * to ODR to reflect new name parameter.
18 * Revision 1.5 1999/04/09 11:46:57 adam
19 * Added object Yaz_Z_Assoc. Much more functional client.
21 * Revision 1.4 1999/03/23 14:17:57 adam
22 * More work on timeout handling. Work on yaz-client.
24 * Revision 1.3 1999/02/02 14:01:20 adam
25 * First WIN32 port of YAZ++.
27 * Revision 1.2 1999/01/28 13:08:44 adam
28 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
29 * yaz-socket-manager.cc.
31 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
32 * First implementation of YAZ++.
38 #include <yaz-pdu-assoc.h>
43 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable,
48 m_socketObservable = socketObservable;
59 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
61 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable, 0);
65 Yaz_PDU_Assoc::~Yaz_PDU_Assoc()
70 void Yaz_PDU_Assoc::socketNotify(int event)
72 logf (LOG_LOG, "Yaz_PDU_Assoc::socketNotify p=%p event = %d", this, event);
73 if (m_state == Connected)
76 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
77 YAZ_SOCKET_OBSERVE_EXCEPT);
78 m_PDU_Observer->connectNotify();
81 else if (m_state == Connecting)
83 if (event & YAZ_SOCKET_OBSERVE_READ)
86 m_PDU_Observer->failNotify();
91 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
92 YAZ_SOCKET_OBSERVE_EXCEPT);
93 m_PDU_Observer->connectNotify();
97 else if (m_state == Listen)
99 if (event & YAZ_SOCKET_OBSERVE_READ)
104 if ((res = cs_listen(m_cs, 0, 0)) == 1)
108 logf(LOG_FATAL, "cs_listen failed");
111 if (!(new_line = cs_accept(m_cs)))
114 Yaz_PDU_Assoc *assoc = new Yaz_PDU_Assoc (m_socketObservable,
116 assoc->m_parent = this;
117 assoc->m_next = m_children;
120 assoc->m_PDU_Observer = m_PDU_Observer->clone(assoc);
121 assoc->m_state = Ready;
122 assoc->m_socketObservable->addObserver(cs_fileno(new_line), assoc);
123 assoc->m_socketObservable->maskObserver(assoc,
124 YAZ_SOCKET_OBSERVE_READ|
125 YAZ_SOCKET_OBSERVE_EXCEPT);
126 assoc->m_socketObservable->timeoutObserver(assoc,
130 else if (m_state == Ready)
132 if (event & YAZ_SOCKET_OBSERVE_WRITE)
136 if (event & YAZ_SOCKET_OBSERVE_READ)
140 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
145 logf (LOG_LOG, "Connection closed by client");
147 m_PDU_Observer->failNotify();
150 // lock it, so we know if recv_PDU deletes it.
152 m_destroyed = &destroyed;
154 m_PDU_Observer->recv_PDU(m_input_buf, res);
155 if (destroyed) // it really was destroyed, return now.
157 } while (m_cs && cs_more (m_cs));
159 if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
161 m_PDU_Observer->timeoutNotify();
166 void Yaz_PDU_Assoc::close()
168 m_socketObservable->deleteObserver(this);
172 logf (LOG_LOG, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
178 PDU_Queue *q_this = m_queue_out;
179 m_queue_out = m_queue_out->m_next;
182 // free (m_input_buf);
187 void Yaz_PDU_Assoc::destroy()
194 // delete from parent's child list (if any)
197 c = &m_parent->m_children;
205 // delete all children ...
209 Yaz_PDU_Assoc *here = *c;
216 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
218 m_buf = (char *) malloc (len);
219 memcpy (m_buf, buf, len);
224 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
229 int Yaz_PDU_Assoc::flush_PDU()
233 logf (LOG_LOG, "Yaz_PDU_Assoc::flush_PDU");
234 if (m_state != Ready)
238 PDU_Queue *q = m_queue_out;
241 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
242 YAZ_SOCKET_OBSERVE_EXCEPT);
245 r = cs_put (m_cs, q->m_buf, q->m_len);
249 m_PDU_Observer->failNotify();
254 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
255 YAZ_SOCKET_OBSERVE_EXCEPT|
256 YAZ_SOCKET_OBSERVE_WRITE);
257 logf (LOG_LOG, "Yaz_PDU_Assoc::flush_PDU put %d bytes (incomplete)",
261 logf (LOG_LOG, "Yaz_PDU_Assoc::flush_PDU put %d bytes", q->m_len);
262 // whole packet sent... delete this and proceed to next ...
263 m_queue_out = q->m_next;
265 // don't select on write if queue is empty ...
267 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
268 YAZ_SOCKET_OBSERVE_EXCEPT);
272 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
274 logf (LOG_LOG, "Yaz_PDU_Assoc::send_PDU");
275 PDU_Queue **pq = &m_queue_out;
276 int is_idle = (*pq ? 0 : 1);
280 logf (LOG_LOG, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
285 *pq = new PDU_Queue(buf, len);
289 logf (LOG_LOG, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
294 COMSTACK Yaz_PDU_Assoc::comstack()
298 CS_TYPE cs_type = tcpip_type;
299 int protocol = PROTO_Z3950;
300 m_cs = cs_create (cs_type, 0, protocol);
305 void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
310 COMSTACK cs = comstack();
312 logf (LOG_LOG, "Yaz_PDU_Assoc::listen %s", addr);
313 m_PDU_Observer = observer;
316 ap = cs_straddr (cs, addr);
319 if (cs_bind(cs, ap, CS_SERVER) < 0)
321 m_socketObservable->addObserver(cs_fileno(cs), this);
322 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
323 YAZ_SOCKET_OBSERVE_EXCEPT);
327 void Yaz_PDU_Assoc::idleTime(int idleTime)
329 m_idleTime = idleTime;
330 logf (LOG_LOG, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
331 m_socketObservable->timeoutObserver(this, m_idleTime);
334 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
337 logf (LOG_LOG, "Yaz_PDU_Assoc::connect %s", addr);
339 m_PDU_Observer = observer;
340 COMSTACK cs = comstack();
341 void *ap = cs_straddr (cs, addr);
344 logf (LOG_LOG, "cs_straddr failed");
347 int res = cs_connect (cs, ap);
350 logf (LOG_LOG|LOG_ERRNO, "Yaz_PDU_Assoc::connect failed");
352 logf (LOG_LOG, "Yaz_PDU_Assoc::connect fd=%d", cs_fileno(cs));
353 m_socketObservable->addObserver(cs_fileno(cs), this);
354 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
355 YAZ_SOCKET_OBSERVE_EXCEPT|
356 YAZ_SOCKET_OBSERVE_WRITE);
357 m_state = Connecting;
364 logf (LOG_LOG, "Yaz_PDU_Assoc::connect fd=%d", cs_fileno(cs));
365 m_socketObservable->addObserver(cs_fileno(cs), this);
366 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
367 YAZ_SOCKET_OBSERVE_EXCEPT|
368 YAZ_SOCKET_OBSERVE_WRITE);
371 logf (LOG_LOG, "Yaz_PDU_Assoc::connect pending");
372 m_state = Connecting;
376 logf (LOG_LOG, "Yaz_PDU_Assoc::Connect complete");