2 * Copyright (c) 1998-2001, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-pdu-assoc.cpp,v 1.31 2003-10-09 12:11:10 adam Exp $
11 #include <yaz/tcpip.h>
13 #include <yaz++/pdu-assoc.h>
16 void Yaz_PDU_Assoc::init(IYazSocketObservable *socketObservable)
20 m_socketObservable = socketObservable;
33 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable)
35 init (socketObservable);
38 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable,
41 init(socketObservable);
44 if (cs->io_pending & CS_WANT_WRITE)
45 mask |= YAZ_SOCKET_OBSERVE_WRITE;
46 if (cs->io_pending & CS_WANT_READ)
47 mask |= YAZ_SOCKET_OBSERVE_READ;
48 m_socketObservable->addObserver(cs_fileno(cs), this);
51 yaz_log (m_log, "new PDU_Assoc. Ready");
57 yaz_log (m_log, "new PDU_Assoc. Accepting");
58 // assume comstack is accepting...
60 m_socketObservable->addObserver(cs_fileno(cs), this);
61 yaz_log(m_log, "maskObserver 1");
62 m_socketObservable->maskObserver(this,
63 mask |YAZ_SOCKET_OBSERVE_EXCEPT);
68 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
70 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable);
74 void Yaz_PDU_Assoc::socketNotify(int event)
76 yaz_log (m_log, "Yaz_PDU_Assoc::socketNotify p=%p state=%d event = %d",
77 this, m_state, event);
78 if (event & YAZ_SOCKET_OBSERVE_EXCEPT)
81 m_PDU_Observer->failNotify();
84 else if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
86 m_PDU_Observer->timeoutNotify();
92 if (!cs_accept (m_cs))
94 yaz_log (m_log, "Yaz_PDU_Assoc::cs_accept failed");
97 m_PDU_Observer->failNotify();
102 if (m_cs->io_pending & CS_WANT_WRITE)
103 mask |= YAZ_SOCKET_OBSERVE_WRITE;
104 if (m_cs->io_pending & CS_WANT_READ)
105 mask |= YAZ_SOCKET_OBSERVE_READ;
107 { // accept is complete. turn to ready state and write if needed
112 { // accept still incomplete.
113 yaz_log(m_log, "maskObserver 2");
114 m_socketObservable->maskObserver(this,
115 mask|YAZ_SOCKET_OBSERVE_EXCEPT);
120 if (event & YAZ_SOCKET_OBSERVE_READ &&
121 event & YAZ_SOCKET_OBSERVE_WRITE)
123 // For Unix: if both read and write is set, then connect failed.
125 m_PDU_Observer->failNotify();
129 yaz_log (m_log, "cs_rcvconnect");
130 int res = cs_rcvconnect (m_cs);
133 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
134 if (m_cs->io_pending & CS_WANT_WRITE)
135 mask |= YAZ_SOCKET_OBSERVE_WRITE;
136 if (m_cs->io_pending & CS_WANT_READ)
137 mask |= YAZ_SOCKET_OBSERVE_READ;
138 yaz_log(m_log, "maskObserver 3");
139 m_socketObservable->maskObserver(this, mask);
145 m_PDU_Observer->connectNotify();
151 if (event & YAZ_SOCKET_OBSERVE_READ)
156 if ((res = cs_listen(m_cs, 0, 0)) == 1)
160 yaz_log(LOG_FATAL|LOG_ERRNO, "cs_listen failed");
163 if (!(new_line = cs_accept(m_cs)))
165 /* 1. create socket-manager
167 3. create top-level object
168 setup observer for child fileid in pdu-assoc
171 yaz_log (m_log, "new session: parent fd=%d child fd=%d",
172 cs_fileno(m_cs), cs_fileno(new_line));
173 childNotify (new_line);
177 if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_WRITE))
181 if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_WRITE))
185 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
188 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
189 if (m_cs->io_pending & CS_WANT_WRITE)
190 mask |= YAZ_SOCKET_OBSERVE_WRITE;
191 if (m_cs->io_pending & CS_WANT_READ)
192 mask |= YAZ_SOCKET_OBSERVE_READ;
193 yaz_log(m_log, "maskObserver 4");
194 m_socketObservable->maskObserver(this, mask);
199 yaz_log (m_log, "Yaz_PDU_Assoc::Connection closed by peer");
202 m_PDU_Observer->failNotify(); // problem here..
205 // lock it, so we know if recv_PDU deletes it.
207 m_destroyed = &destroyed;
212 m_PDU_Observer->recv_PDU(m_input_buf, res);
214 if (destroyed) // it really was destroyed, return now.
216 } while (m_cs && cs_more (m_cs));
217 if (m_cs && m_state == Ready)
219 yaz_log(m_log, "maskObserver 5");
220 m_socketObservable->maskObserver(this,
221 YAZ_SOCKET_OBSERVE_EXCEPT|
222 YAZ_SOCKET_OBSERVE_READ);
227 yaz_log (m_log, "CLOSING state=%d event was %d", m_state, event);
229 m_PDU_Observer->failNotify();
232 yaz_log (m_log, "Unknown state=%d event was %d", m_state, event);
234 m_PDU_Observer->failNotify();
238 void Yaz_PDU_Assoc::close()
241 for (ch = m_children; ch; ch = ch->m_next)
244 m_socketObservable->deleteObserver(this);
248 yaz_log (m_log, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
254 PDU_Queue *q_this = m_queue_out;
255 m_queue_out = m_queue_out->m_next;
263 void Yaz_PDU_Assoc::destroy()
271 // delete from parent's child list (if any)
274 c = &m_parent->m_children;
282 // delete all children ...
286 Yaz_PDU_Assoc *here = *c;
291 yaz_log (m_log, "Yaz_PDU_Assoc::destroy this=%p", this);
294 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
296 m_buf = (char *) xmalloc (len);
297 memcpy (m_buf, buf, len);
302 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
307 int Yaz_PDU_Assoc::flush_PDU()
311 if (m_state != Ready && m_state != Writing)
313 yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
316 PDU_Queue *q = m_queue_out;
320 yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU queue empty");
321 yaz_log(m_log, "maskObserver 6");
322 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
323 YAZ_SOCKET_OBSERVE_WRITE|
324 YAZ_SOCKET_OBSERVE_EXCEPT);
327 r = cs_put (m_cs, q->m_buf, q->m_len);
330 yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put failed");
332 m_PDU_Observer->failNotify();
337 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
339 if (m_cs->io_pending & CS_WANT_WRITE)
340 mask |= YAZ_SOCKET_OBSERVE_WRITE;
341 if (m_cs->io_pending & CS_WANT_READ)
342 mask |= YAZ_SOCKET_OBSERVE_READ;
344 mask |= YAZ_SOCKET_OBSERVE_WRITE;
345 yaz_log(m_log, "maskObserver 7");
346 m_socketObservable->maskObserver(this, mask);
347 yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes fd=%d (inc)",
348 q->m_len, cs_fileno(m_cs));
351 yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes", q->m_len);
352 // whole packet sent... delete this and proceed to next ...
353 m_queue_out = q->m_next;
355 // don't select on write if queue is empty ...
359 yaz_log(m_log, "maskObserver 8");
360 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
361 YAZ_SOCKET_OBSERVE_EXCEPT);
366 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
368 yaz_log (m_log, "Yaz_PDU_Assoc::send_PDU");
369 PDU_Queue **pq = &m_queue_out;
370 int is_idle = (*pq ? 0 : 1);
374 yaz_log (m_log, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
379 *pq = new PDU_Queue(buf, len);
383 yaz_log (m_log, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
388 COMSTACK Yaz_PDU_Assoc::comstack(const char *type_and_host, void **vp)
390 return cs_create_host(type_and_host, 2, vp);
393 void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
398 yaz_log (LOG_LOG, "Adding listener %s", addr);
400 m_PDU_Observer = observer;
402 m_cs = comstack(addr, &ap);
406 if (cs_bind(m_cs, ap, CS_SERVER) < 0)
408 m_socketObservable->addObserver(cs_fileno(m_cs), this);
409 yaz_log(m_log, "maskObserver 9");
410 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
411 YAZ_SOCKET_OBSERVE_EXCEPT);
412 yaz_log (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs));
416 void Yaz_PDU_Assoc::idleTime(int idleTime)
418 m_idleTime = idleTime;
419 yaz_log (m_log, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
420 m_socketObservable->timeoutObserver(this, m_idleTime);
423 void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
426 yaz_log (m_log, "Yaz_PDU_Assoc::connect %s", addr);
428 m_PDU_Observer = observer;
430 m_cs = comstack(addr, &ap);
431 int res = cs_connect (m_cs, ap);
432 yaz_log (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_cs),
434 m_socketObservable->addObserver(cs_fileno(m_cs), this);
437 { // Connect complete
438 m_state = Connecting;
439 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
440 mask |= YAZ_SOCKET_OBSERVE_WRITE;
441 mask |= YAZ_SOCKET_OBSERVE_READ;
442 yaz_log(m_log, "maskObserver 11");
443 m_socketObservable->maskObserver(this, mask);
447 m_state = Connecting;
448 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
449 if (m_cs->io_pending & CS_WANT_WRITE)
450 mask |= YAZ_SOCKET_OBSERVE_WRITE;
451 if (m_cs->io_pending & CS_WANT_READ)
452 mask |= YAZ_SOCKET_OBSERVE_READ;
453 yaz_log(m_log, "maskObserver 11");
454 m_socketObservable->maskObserver(this, mask);
457 { // Connect failed immediately
458 // Since m_state is Closed we can distinguish this case from
459 // normal connect in socketNotify handler
460 yaz_log(m_log, "maskObserver 12");
461 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_WRITE|
462 YAZ_SOCKET_OBSERVE_EXCEPT);
466 // Single-threaded... Only useful for non-blocking handlers
467 void Yaz_PDU_Assoc::childNotify(COMSTACK cs)
469 Yaz_PDU_Assoc *new_observable =
470 new Yaz_PDU_Assoc (m_socketObservable, cs);
472 new_observable->m_next = m_children;
473 m_children = new_observable;
474 new_observable->m_parent = this;
476 // Clone PDU Observer
477 new_observable->m_PDU_Observer = m_PDU_Observer->sessionNotify
478 (new_observable, cs_fileno(cs));
481 const char*Yaz_PDU_Assoc::getpeername()
483 return cs_addrstr(m_cs);