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.4 1999-03-23 14:17:57 adam
8 * More work on timeout handling. Work on yaz-client.
10 * Revision 1.3 1999/02/02 14:01:20 adam
11 * First WIN32 port of YAZ++.
13 * Revision 1.2 1999/01/28 13:08:44 adam
14 * Yaz_PDU_Assoc better encapsulated. Memory leak fix in
15 * yaz-socket-manager.cc.
17 * Revision 1.1.1.1 1999/01/28 09:41:07 adam
18 * First implementation of YAZ++.
24 #include <yaz-pdu-assoc.h>
29 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable,
34 m_socketObservable = socketObservable;
45 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
47 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable, 0);
51 Yaz_PDU_Assoc::~Yaz_PDU_Assoc()
56 void Yaz_PDU_Assoc::socketNotify(int event)
58 logf (LOG_LOG, "socketNotify p=%p event = %d", this, event);
59 if (m_state == Connected)
62 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
63 YAZ_SOCKET_OBSERVE_EXCEPT);
64 m_PDU_Observer->connectNotify();
67 else if (m_state == Connecting)
69 if (event & YAZ_SOCKET_OBSERVE_READ)
72 m_PDU_Observer->failNotify();
77 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
78 YAZ_SOCKET_OBSERVE_EXCEPT);
79 m_PDU_Observer->connectNotify();
83 else if (m_state == Listen)
85 if (event & YAZ_SOCKET_OBSERVE_READ)
90 if ((res = cs_listen(m_cs, 0, 0)) == 1)
94 logf(LOG_FATAL, "cs_listen failed");
97 if (!(new_line = cs_accept(m_cs)))
100 Yaz_PDU_Assoc *assoc = new Yaz_PDU_Assoc (m_socketObservable,
102 assoc->m_parent = this;
103 assoc->m_next = m_children;
106 assoc->m_PDU_Observer = m_PDU_Observer->clone(assoc);
107 assoc->m_state = Ready;
108 assoc->m_socketObservable->addObserver(cs_fileno(new_line), assoc);
109 assoc->m_socketObservable->maskObserver(assoc,
110 YAZ_SOCKET_OBSERVE_READ|
111 YAZ_SOCKET_OBSERVE_EXCEPT);
113 assoc->m_socketObservable->timeoutObserver(assoc, m_idleTime);
116 else if (m_state == Ready)
118 if (event & YAZ_SOCKET_OBSERVE_WRITE)
122 if (event & YAZ_SOCKET_OBSERVE_READ)
126 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
131 logf (LOG_LOG, "Connection closed by client");
133 m_PDU_Observer->failNotify();
136 // lock it, so we know if recv_PDU deletes it.
138 m_destroyed = &destroyed;
140 m_PDU_Observer->recv_PDU(m_input_buf, res);
141 if (destroyed) // it really was destroyed, return now.
143 } while (m_cs && cs_more (m_cs));
145 if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
147 m_PDU_Observer->timeoutNotify();
152 void Yaz_PDU_Assoc::close()
154 m_socketObservable->deleteObserver(this);
158 logf (LOG_LOG, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
164 PDU_Queue *q_this = m_queue_out;
165 m_queue_out = m_queue_out->m_next;
168 // free (m_input_buf);
173 void Yaz_PDU_Assoc::destroy()
180 // delete from parent's child list (if any)
183 c = &m_parent->m_children;
191 // delete all children ...
195 Yaz_PDU_Assoc *here = *c;
202 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
204 m_buf = (char *) malloc (len);
205 memcpy (m_buf, buf, len);
210 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
215 int Yaz_PDU_Assoc::flush_PDU()
219 if (m_state != Ready)
221 PDU_Queue *q = m_queue_out;
224 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
225 YAZ_SOCKET_OBSERVE_EXCEPT);
228 r = cs_put (m_cs, q->m_buf, q->m_len);
232 m_PDU_Observer->failNotify();
237 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
238 YAZ_SOCKET_OBSERVE_EXCEPT|
239 YAZ_SOCKET_OBSERVE_WRITE);
240 logf (LOG_LOG, "put %d bytes (incomplete write)", q->m_len);
243 logf (LOG_LOG, "put %d bytes fd=%d", q->m_len, cs_fileno(m_cs));
244 // whole packet sent... delete this and proceed to next ...
245 m_queue_out = q->m_next;
246 logf (LOG_LOG, "m_queue_out = %p", m_queue_out);
248 // don't select on write if queue is empty ...
250 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
251 YAZ_SOCKET_OBSERVE_EXCEPT);
255 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
257 logf (LOG_LOG, "send_PDU");
258 PDU_Queue **pq = &m_queue_out;
259 int is_idle = (*pq ? 0 : 1);
263 logf (LOG_LOG, "send_PDU failed, m_cs == 0");
268 *pq = new PDU_Queue(buf, len);
275 logf (LOG_LOG, "cannot send_PDU fd=%d", cs_fileno(m_cs));
280 COMSTACK Yaz_PDU_Assoc::comstack()
284 CS_TYPE cs_type = tcpip_type;
285 int protocol = PROTO_Z3950;
286 m_cs = cs_create (cs_type, 0, protocol);
291 void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
296 COMSTACK cs = comstack();
298 logf (LOG_LOG, "Yaz_PDU_Assoc::listen %s", addr);
299 m_PDU_Observer = observer;
302 ap = cs_straddr (cs, addr);
305 if (cs_bind(cs, ap, CS_SERVER) < 0)
307 m_socketObservable->addObserver(cs_fileno(cs), this);
308 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
309 YAZ_SOCKET_OBSERVE_EXCEPT);
313 void Yaz_PDU_Assoc::idleTime(int idleTime)
315 m_idleTime = idleTime;
318 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
321 logf (LOG_LOG, "Yaz_PDU_Assoc::connect %s", addr);
323 m_PDU_Observer = observer;
324 COMSTACK cs = comstack();
325 void *ap = cs_straddr (cs, addr);
328 logf (LOG_LOG, "cs_straddr failed");
331 int res = cs_connect (cs, ap);
334 logf (LOG_DEBUG, "Yaz_PDU_Assoc::connect failed");
339 logf (LOG_LOG, "Yaz_PDU_Assoc::connect fd=%d", cs_fileno(cs));
340 m_socketObservable->addObserver(cs_fileno(cs), this);
341 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
342 YAZ_SOCKET_OBSERVE_EXCEPT|
343 YAZ_SOCKET_OBSERVE_WRITE);
345 m_state = Connecting;