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.7 1999-04-21 12:09:01 adam
8 * Many improvements. Modified to proxy server to work with "sessions"
11 * Revision 1.6 1999/04/20 10:30:05 adam
12 * Implemented various stuff for client and proxy. Updated calls
13 * to ODR to reflect new name parameter.
15 * Revision 1.5 1999/04/09 11:46:57 adam
16 * Added object Yaz_Z_Assoc. Much more functional client.
18 * Revision 1.4 1999/03/23 14:17:57 adam
19 * More work on timeout handling. Work on yaz-client.
21 * Revision 1.3 1999/02/02 14:01:20 adam
22 * First WIN32 port of YAZ++.
24 * Revision 1.2 1999/01/28 13:08:44 adam
25 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
26 * yaz-socket-manager.cc.
28 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
29 * First implementation of YAZ++.
35 #include <yaz-pdu-assoc.h>
40 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable,
45 m_socketObservable = socketObservable;
56 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
58 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable, 0);
62 Yaz_PDU_Assoc::~Yaz_PDU_Assoc()
67 void Yaz_PDU_Assoc::socketNotify(int event)
69 logf (LOG_LOG, "Yaz_PDU_Assoc::socketNotify p=%p event = %d", this, event);
70 if (m_state == Connected)
73 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
74 YAZ_SOCKET_OBSERVE_EXCEPT);
75 m_PDU_Observer->connectNotify();
78 else if (m_state == Connecting)
80 if (event & YAZ_SOCKET_OBSERVE_READ)
83 m_PDU_Observer->failNotify();
88 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
89 YAZ_SOCKET_OBSERVE_EXCEPT);
90 m_PDU_Observer->connectNotify();
94 else if (m_state == Listen)
96 if (event & YAZ_SOCKET_OBSERVE_READ)
101 if ((res = cs_listen(m_cs, 0, 0)) == 1)
105 logf(LOG_FATAL, "cs_listen failed");
108 if (!(new_line = cs_accept(m_cs)))
111 Yaz_PDU_Assoc *assoc = new Yaz_PDU_Assoc (m_socketObservable,
113 assoc->m_parent = this;
114 assoc->m_next = m_children;
117 assoc->m_PDU_Observer = m_PDU_Observer->clone(assoc);
118 assoc->m_state = Ready;
119 assoc->m_socketObservable->addObserver(cs_fileno(new_line), assoc);
120 assoc->m_socketObservable->maskObserver(assoc,
121 YAZ_SOCKET_OBSERVE_READ|
122 YAZ_SOCKET_OBSERVE_EXCEPT);
123 assoc->m_socketObservable->timeoutObserver(assoc,
127 else if (m_state == Ready)
129 if (event & YAZ_SOCKET_OBSERVE_WRITE)
133 if (event & YAZ_SOCKET_OBSERVE_READ)
137 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
142 logf (LOG_LOG, "Connection closed by client");
144 m_PDU_Observer->failNotify();
147 // lock it, so we know if recv_PDU deletes it.
149 m_destroyed = &destroyed;
151 m_PDU_Observer->recv_PDU(m_input_buf, res);
152 if (destroyed) // it really was destroyed, return now.
154 } while (m_cs && cs_more (m_cs));
156 if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
158 m_PDU_Observer->timeoutNotify();
163 void Yaz_PDU_Assoc::close()
165 m_socketObservable->deleteObserver(this);
169 logf (LOG_LOG, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
175 PDU_Queue *q_this = m_queue_out;
176 m_queue_out = m_queue_out->m_next;
179 // free (m_input_buf);
184 void Yaz_PDU_Assoc::destroy()
191 // delete from parent's child list (if any)
194 c = &m_parent->m_children;
202 // delete all children ...
206 Yaz_PDU_Assoc *here = *c;
213 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
215 m_buf = (char *) malloc (len);
216 memcpy (m_buf, buf, len);
221 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
226 int Yaz_PDU_Assoc::flush_PDU()
230 logf (LOG_LOG, "Yaz_PDU_Assoc::flush_PDU");
231 if (m_state != Ready)
235 PDU_Queue *q = m_queue_out;
238 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
239 YAZ_SOCKET_OBSERVE_EXCEPT);
242 r = cs_put (m_cs, q->m_buf, q->m_len);
246 m_PDU_Observer->failNotify();
251 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
252 YAZ_SOCKET_OBSERVE_EXCEPT|
253 YAZ_SOCKET_OBSERVE_WRITE);
254 logf (LOG_LOG, "Yaz_PDU_Assoc::flush_PDU put %d bytes (incomplete)",
258 logf (LOG_LOG, "Yaz_PDU_Assoc::flush_PDU put %d bytes", q->m_len);
259 // whole packet sent... delete this and proceed to next ...
260 m_queue_out = q->m_next;
262 // don't select on write if queue is empty ...
264 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
265 YAZ_SOCKET_OBSERVE_EXCEPT);
269 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
271 logf (LOG_LOG, "Yaz_PDU_Assoc::send_PDU");
272 PDU_Queue **pq = &m_queue_out;
273 int is_idle = (*pq ? 0 : 1);
277 logf (LOG_LOG, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
282 *pq = new PDU_Queue(buf, len);
286 logf (LOG_LOG, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
291 COMSTACK Yaz_PDU_Assoc::comstack()
295 CS_TYPE cs_type = tcpip_type;
296 int protocol = PROTO_Z3950;
297 m_cs = cs_create (cs_type, 0, protocol);
302 void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
307 COMSTACK cs = comstack();
309 logf (LOG_LOG, "Yaz_PDU_Assoc::listen %s", addr);
310 m_PDU_Observer = observer;
313 ap = cs_straddr (cs, addr);
316 if (cs_bind(cs, ap, CS_SERVER) < 0)
318 m_socketObservable->addObserver(cs_fileno(cs), this);
319 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
320 YAZ_SOCKET_OBSERVE_EXCEPT);
324 void Yaz_PDU_Assoc::idleTime(int idleTime)
326 m_idleTime = idleTime;
327 logf (LOG_LOG, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
328 m_socketObservable->timeoutObserver(this, m_idleTime);
331 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
334 logf (LOG_LOG, "Yaz_PDU_Assoc::connect %s", addr);
336 m_PDU_Observer = observer;
337 COMSTACK cs = comstack();
338 void *ap = cs_straddr (cs, addr);
341 logf (LOG_LOG, "cs_straddr failed");
344 int res = cs_connect (cs, ap);
347 logf (LOG_LOG, "Yaz_PDU_Assoc::connect failed");
349 logf (LOG_LOG, "Yaz_PDU_Assoc::connect fd=%d", cs_fileno(cs));
350 m_socketObservable->addObserver(cs_fileno(cs), this);
351 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
352 YAZ_SOCKET_OBSERVE_EXCEPT|
353 YAZ_SOCKET_OBSERVE_WRITE);
354 m_state = Connecting;
361 logf (LOG_LOG, "Yaz_PDU_Assoc::connect fd=%d", cs_fileno(cs));
362 m_socketObservable->addObserver(cs_fileno(cs), this);
363 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
364 YAZ_SOCKET_OBSERVE_EXCEPT|
365 YAZ_SOCKET_OBSERVE_WRITE);
368 logf (LOG_LOG, "Yaz_PDU_Assoc::connect pending");
369 m_state = Connecting;
373 logf (LOG_LOG, "Yaz_PDU_Assoc::Connect complete");