2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-pdu-assoc.cpp,v 1.46 2006-03-30 11:58:00 adam Exp $
11 #include <yaz/tcpip.h>
13 #include <yazpp/pdu-assoc.h>
15 using namespace yazpp_1;
17 void PDU_Assoc::init(ISocketObservable *socketObservable)
21 m_socketObservable = socketObservable;
35 PDU_Assoc::PDU_Assoc(ISocketObservable *socketObservable)
37 init (socketObservable);
40 PDU_Assoc::PDU_Assoc(ISocketObservable *socketObservable,
43 init(socketObservable);
46 if (cs->io_pending & CS_WANT_WRITE)
47 mask |= SOCKET_OBSERVE_WRITE;
48 if (cs->io_pending & CS_WANT_READ)
49 mask |= SOCKET_OBSERVE_READ;
50 m_socketObservable->addObserver(cs_fileno(cs), this);
53 yaz_log (m_log, "new PDU_Assoc. Ready");
59 yaz_log (m_log, "new PDU_Assoc. Accepting");
60 // assume comstack is accepting...
62 m_socketObservable->addObserver(cs_fileno(cs), this);
63 yaz_log(m_log, "maskObserver 1");
64 m_socketObservable->maskObserver(this,
65 mask |SOCKET_OBSERVE_EXCEPT);
70 IPDU_Observable *PDU_Assoc::clone()
72 PDU_Assoc *copy = new PDU_Assoc(m_socketObservable);
76 void PDU_Assoc::socketNotify(int event)
78 yaz_log (m_log, "PDU_Assoc::socketNotify p=%p state=%d event = %d",
79 this, m_state, event);
80 if (event & SOCKET_OBSERVE_EXCEPT)
83 m_PDU_Observer->failNotify();
86 else if (event & SOCKET_OBSERVE_TIMEOUT)
88 m_PDU_Observer->timeoutNotify();
94 if (!cs_accept (m_cs))
96 yaz_log (m_log, "PDU_Assoc::cs_accept failed");
99 m_PDU_Observer->failNotify();
104 if (m_cs->io_pending & CS_WANT_WRITE)
105 mask |= SOCKET_OBSERVE_WRITE;
106 if (m_cs->io_pending & CS_WANT_READ)
107 mask |= SOCKET_OBSERVE_READ;
109 { // accept is complete. turn to ready state and write if needed
114 { // accept still incomplete.
115 yaz_log(m_log, "maskObserver 2");
116 m_socketObservable->maskObserver(this,
117 mask|SOCKET_OBSERVE_EXCEPT);
122 if (event & SOCKET_OBSERVE_READ &&
123 event & SOCKET_OBSERVE_WRITE)
125 // For Unix: if both read and write is set, then connect failed.
127 m_PDU_Observer->failNotify();
131 yaz_log (m_log, "cs_rcvconnect");
132 int res = cs_rcvconnect (m_cs);
135 unsigned mask = SOCKET_OBSERVE_EXCEPT;
136 if (m_cs->io_pending & CS_WANT_WRITE)
137 mask |= SOCKET_OBSERVE_WRITE;
138 if (m_cs->io_pending & CS_WANT_READ)
139 mask |= SOCKET_OBSERVE_READ;
140 yaz_log(m_log, "maskObserver 3");
141 m_socketObservable->maskObserver(this, mask);
147 m_PDU_Observer->connectNotify();
153 if (event & SOCKET_OBSERVE_READ)
158 if ((res = cs_listen(m_cs, 0, 0)) == 1)
162 yaz_log(YLOG_FATAL|YLOG_ERRNO, "cs_listen failed");
165 if (!(new_line = cs_accept(m_cs)))
167 /* 1. create socket-manager
169 3. create top-level object
170 setup observer for child fileid in pdu-assoc
173 yaz_log (m_log, "new session: parent fd=%d child fd=%d",
174 cs_fileno(m_cs), cs_fileno(new_line));
175 childNotify (new_line);
179 if (event & (SOCKET_OBSERVE_READ|SOCKET_OBSERVE_WRITE))
183 if (event & (SOCKET_OBSERVE_READ|SOCKET_OBSERVE_WRITE))
187 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
190 unsigned mask = SOCKET_OBSERVE_EXCEPT;
191 if (m_cs->io_pending & CS_WANT_WRITE)
192 mask |= SOCKET_OBSERVE_WRITE;
193 if (m_cs->io_pending & CS_WANT_READ)
194 mask |= SOCKET_OBSERVE_READ;
195 yaz_log(m_log, "maskObserver 4");
196 m_socketObservable->maskObserver(this, mask);
201 yaz_log (m_log, "PDU_Assoc::Connection closed by peer");
204 m_PDU_Observer->failNotify(); // problem here..
207 // lock it, so we know if recv_PDU deletes it.
209 m_destroyed = &destroyed;
214 PDU_Queue **pq = &m_queue_in;
218 *pq = new PDU_Queue(m_input_buf, res);
220 m_PDU_Observer->recv_PDU(m_input_buf, res);
222 if (destroyed) // it really was destroyed, return now.
225 } while (m_cs && cs_more (m_cs));
226 if (m_cs && m_state == Ready)
228 yaz_log(m_log, "maskObserver 5");
229 m_socketObservable->maskObserver(this,
230 SOCKET_OBSERVE_EXCEPT|
231 SOCKET_OBSERVE_READ);
236 yaz_log (m_log, "CLOSING state=%d event was %d", m_state, event);
238 m_PDU_Observer->failNotify();
241 yaz_log (m_log, "Unknown state=%d event was %d", m_state, event);
243 m_PDU_Observer->failNotify();
247 void PDU_Assoc::close()
250 for (ch = m_children; ch; ch = ch->m_next)
253 m_socketObservable->deleteObserver(this);
257 yaz_log (m_log, "PDU_Assoc::close fd=%d", cs_fileno(m_cs));
263 PDU_Queue *q_this = m_queue_out;
264 m_queue_out = m_queue_out->m_next;
272 void PDU_Assoc::destroy()
280 // delete from parent's child list (if any)
283 c = &m_parent->m_children;
291 // delete all children ...
295 PDU_Assoc *here = *c;
300 yaz_log (m_log, "PDU_Assoc::destroy this=%p", this);
303 PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
305 m_buf = (char *) xmalloc (len);
306 memcpy (m_buf, buf, len);
311 PDU_Assoc::PDU_Queue::~PDU_Queue()
316 int PDU_Assoc::flush_PDU()
320 if (m_state != Ready && m_state != Writing)
322 yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
325 PDU_Queue *q = m_queue_out;
329 yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU queue empty");
330 yaz_log(m_log, "maskObserver 6");
331 m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ|
332 SOCKET_OBSERVE_WRITE|
333 SOCKET_OBSERVE_EXCEPT);
336 r = cs_put (m_cs, q->m_buf, q->m_len);
339 yaz_log (m_log, "PDU_Assoc::flush_PDU cs_put failed");
341 m_PDU_Observer->failNotify();
346 unsigned mask = SOCKET_OBSERVE_EXCEPT;
348 if (m_cs->io_pending & CS_WANT_WRITE)
349 mask |= SOCKET_OBSERVE_WRITE;
350 if (m_cs->io_pending & CS_WANT_READ)
351 mask |= SOCKET_OBSERVE_READ;
353 mask |= SOCKET_OBSERVE_WRITE;
354 yaz_log(m_log, "maskObserver 7");
355 m_socketObservable->maskObserver(this, mask);
356 yaz_log (m_log, "PDU_Assoc::flush_PDU cs_put %d bytes fd=%d (inc)",
357 q->m_len, cs_fileno(m_cs));
360 yaz_log (m_log, "PDU_Assoc::flush_PDU cs_put %d bytes", q->m_len);
361 // whole packet sent... delete this and proceed to next ...
362 m_queue_out = q->m_next;
364 // don't select on write if queue is empty ...
368 yaz_log(m_log, "maskObserver 8");
369 m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ|
370 SOCKET_OBSERVE_EXCEPT);
375 int PDU_Assoc::send_PDU(const char *buf, int len)
377 yaz_log (m_log, "PDU_Assoc::send_PDU");
378 PDU_Queue **pq = &m_queue_out;
379 int is_idle = (*pq ? 0 : 1);
383 yaz_log (m_log, "PDU_Assoc::send_PDU failed, m_cs == 0");
388 *pq = new PDU_Queue(buf, len);
392 yaz_log (m_log, "PDU_Assoc::cannot send_PDU fd=%d",
397 COMSTACK PDU_Assoc::comstack(const char *type_and_host, void **vp)
399 return cs_create_host(type_and_host, 2, vp);
402 int PDU_Assoc::listen(IPDU_Observer *observer, const char *addr)
406 m_PDU_Observer = observer;
408 m_cs = comstack(addr, &ap);
412 if (cs_bind(m_cs, ap, CS_SERVER) < 0)
414 m_socketObservable->addObserver(cs_fileno(m_cs), this);
415 yaz_log(m_log, "maskObserver 9");
416 m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ|
417 SOCKET_OBSERVE_EXCEPT);
418 yaz_log (m_log, "PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs));
423 void PDU_Assoc::idleTime(int idleTime)
425 m_idleTime = idleTime;
426 yaz_log (m_log, "PDU_Assoc::idleTime(%d)", idleTime);
427 m_socketObservable->timeoutObserver(this, m_idleTime);
430 int PDU_Assoc::connect(IPDU_Observer *observer, const char *addr)
432 yaz_log (m_log, "PDU_Assoc::connect %s", addr);
434 m_PDU_Observer = observer;
436 m_cs = comstack(addr, &ap);
439 int res = cs_connect (m_cs, ap);
440 yaz_log (m_log, "PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_cs),
442 m_socketObservable->addObserver(cs_fileno(m_cs), this);
445 { // Connect complete
446 m_state = Connecting;
447 unsigned mask = SOCKET_OBSERVE_EXCEPT;
448 mask |= SOCKET_OBSERVE_WRITE;
449 mask |= SOCKET_OBSERVE_READ;
450 yaz_log(m_log, "maskObserver 11");
451 m_socketObservable->maskObserver(this, mask);
455 m_state = Connecting;
456 unsigned mask = SOCKET_OBSERVE_EXCEPT;
457 if (m_cs->io_pending & CS_WANT_WRITE)
458 mask |= SOCKET_OBSERVE_WRITE;
459 if (m_cs->io_pending & CS_WANT_READ)
460 mask |= SOCKET_OBSERVE_READ;
461 yaz_log(m_log, "maskObserver 11");
462 m_socketObservable->maskObserver(this, mask);
465 { // Connect failed immediately
466 // Since m_state is Closed we can distinguish this case from
467 // normal connect in socketNotify handler
468 yaz_log(m_log, "maskObserver 12");
469 m_socketObservable->maskObserver(this, SOCKET_OBSERVE_WRITE|
470 SOCKET_OBSERVE_EXCEPT);
475 // Single-threaded... Only useful for non-blocking handlers
476 void PDU_Assoc::childNotify(COMSTACK cs)
478 PDU_Assoc *new_observable =
479 new PDU_Assoc (m_socketObservable, cs);
481 // Clone PDU Observer
482 new_observable->m_PDU_Observer = m_PDU_Observer->sessionNotify
483 (new_observable, cs_fileno(cs));
485 if (!new_observable->m_PDU_Observer)
487 new_observable->close();
488 delete new_observable;
491 new_observable->m_next = m_children;
492 m_children = new_observable;
493 new_observable->m_parent = this;
496 const char*PDU_Assoc::getpeername()
500 return cs_addrstr(m_cs);
505 * indent-tabs-mode: nil
507 * vim: shiftwidth=4 tabstop=8 expandtab