2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-pdu-assoc.cpp,v 1.37 2003-10-23 11:45:08 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;
34 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable)
36 init (socketObservable);
39 Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable,
42 init(socketObservable);
45 if (cs->io_pending & CS_WANT_WRITE)
46 mask |= YAZ_SOCKET_OBSERVE_WRITE;
47 if (cs->io_pending & CS_WANT_READ)
48 mask |= YAZ_SOCKET_OBSERVE_READ;
49 m_socketObservable->addObserver(cs_fileno(cs), this);
52 yaz_log (m_log, "new PDU_Assoc. Ready");
58 yaz_log (m_log, "new PDU_Assoc. Accepting");
59 // assume comstack is accepting...
61 m_socketObservable->addObserver(cs_fileno(cs), this);
62 yaz_log(m_log, "maskObserver 1");
63 m_socketObservable->maskObserver(this,
64 mask |YAZ_SOCKET_OBSERVE_EXCEPT);
69 IYaz_PDU_Observable *Yaz_PDU_Assoc::clone()
71 Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable);
75 void Yaz_PDU_Assoc::socketNotify(int event)
77 yaz_log (m_log, "Yaz_PDU_Assoc::socketNotify p=%p state=%d event = %d",
78 this, m_state, event);
79 if (event & YAZ_SOCKET_OBSERVE_EXCEPT)
82 m_PDU_Observer->failNotify();
85 else if (event & YAZ_SOCKET_OBSERVE_TIMEOUT)
87 m_PDU_Observer->timeoutNotify();
93 if (!cs_accept (m_cs))
95 yaz_log (m_log, "Yaz_PDU_Assoc::cs_accept failed");
98 m_PDU_Observer->failNotify();
103 if (m_cs->io_pending & CS_WANT_WRITE)
104 mask |= YAZ_SOCKET_OBSERVE_WRITE;
105 if (m_cs->io_pending & CS_WANT_READ)
106 mask |= YAZ_SOCKET_OBSERVE_READ;
108 { // accept is complete. turn to ready state and write if needed
113 { // accept still incomplete.
114 yaz_log(m_log, "maskObserver 2");
115 m_socketObservable->maskObserver(this,
116 mask|YAZ_SOCKET_OBSERVE_EXCEPT);
121 if (event & YAZ_SOCKET_OBSERVE_READ &&
122 event & YAZ_SOCKET_OBSERVE_WRITE)
124 // For Unix: if both read and write is set, then connect failed.
126 m_PDU_Observer->failNotify();
130 yaz_log (m_log, "cs_rcvconnect");
131 int res = cs_rcvconnect (m_cs);
134 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
135 if (m_cs->io_pending & CS_WANT_WRITE)
136 mask |= YAZ_SOCKET_OBSERVE_WRITE;
137 if (m_cs->io_pending & CS_WANT_READ)
138 mask |= YAZ_SOCKET_OBSERVE_READ;
139 yaz_log(m_log, "maskObserver 3");
140 m_socketObservable->maskObserver(this, mask);
146 m_PDU_Observer->connectNotify();
152 if (event & YAZ_SOCKET_OBSERVE_READ)
157 if ((res = cs_listen(m_cs, 0, 0)) == 1)
161 yaz_log(LOG_FATAL|LOG_ERRNO, "cs_listen failed");
164 if (!(new_line = cs_accept(m_cs)))
166 /* 1. create socket-manager
168 3. create top-level object
169 setup observer for child fileid in pdu-assoc
172 yaz_log (m_log, "new session: parent fd=%d child fd=%d",
173 cs_fileno(m_cs), cs_fileno(new_line));
174 childNotify (new_line);
178 if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_WRITE))
182 if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_WRITE))
186 int res = cs_get (m_cs, &m_input_buf, &m_input_len);
189 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
190 if (m_cs->io_pending & CS_WANT_WRITE)
191 mask |= YAZ_SOCKET_OBSERVE_WRITE;
192 if (m_cs->io_pending & CS_WANT_READ)
193 mask |= YAZ_SOCKET_OBSERVE_READ;
194 yaz_log(m_log, "maskObserver 4");
195 m_socketObservable->maskObserver(this, mask);
200 yaz_log (m_log, "Yaz_PDU_Assoc::Connection closed by peer");
203 m_PDU_Observer->failNotify(); // problem here..
206 // lock it, so we know if recv_PDU deletes it.
208 m_destroyed = &destroyed;
213 PDU_Queue **pq = &m_queue_in;
217 *pq = new PDU_Queue(m_input_buf, res);
219 m_PDU_Observer->recv_PDU(m_input_buf, res);
221 if (destroyed) // it really was destroyed, return now.
224 } while (m_cs && cs_more (m_cs));
225 if (m_cs && m_state == Ready)
227 yaz_log(m_log, "maskObserver 5");
228 m_socketObservable->maskObserver(this,
229 YAZ_SOCKET_OBSERVE_EXCEPT|
230 YAZ_SOCKET_OBSERVE_READ);
235 yaz_log (m_log, "CLOSING state=%d event was %d", m_state, event);
237 m_PDU_Observer->failNotify();
240 yaz_log (m_log, "Unknown state=%d event was %d", m_state, event);
242 m_PDU_Observer->failNotify();
246 void Yaz_PDU_Assoc::close()
249 for (ch = m_children; ch; ch = ch->m_next)
252 m_socketObservable->deleteObserver(this);
256 yaz_log (m_log, "Yaz_PDU_Assoc::close fd=%d", cs_fileno(m_cs));
262 PDU_Queue *q_this = m_queue_out;
263 m_queue_out = m_queue_out->m_next;
271 void Yaz_PDU_Assoc::destroy()
279 // delete from parent's child list (if any)
282 c = &m_parent->m_children;
290 // delete all children ...
294 Yaz_PDU_Assoc *here = *c;
299 yaz_log (m_log, "Yaz_PDU_Assoc::destroy this=%p", this);
302 Yaz_PDU_Assoc::PDU_Queue::PDU_Queue(const char *buf, int len)
304 m_buf = (char *) xmalloc (len);
305 memcpy (m_buf, buf, len);
310 Yaz_PDU_Assoc::PDU_Queue::~PDU_Queue()
315 int Yaz_PDU_Assoc::flush_PDU()
319 if (m_state != Ready && m_state != Writing)
321 yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready");
324 PDU_Queue *q = m_queue_out;
328 yaz_log (m_log, "YAZ_PDU_Assoc::flush_PDU queue empty");
329 yaz_log(m_log, "maskObserver 6");
330 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
331 YAZ_SOCKET_OBSERVE_WRITE|
332 YAZ_SOCKET_OBSERVE_EXCEPT);
335 r = cs_put (m_cs, q->m_buf, q->m_len);
338 yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put failed");
340 m_PDU_Observer->failNotify();
345 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
347 if (m_cs->io_pending & CS_WANT_WRITE)
348 mask |= YAZ_SOCKET_OBSERVE_WRITE;
349 if (m_cs->io_pending & CS_WANT_READ)
350 mask |= YAZ_SOCKET_OBSERVE_READ;
352 mask |= YAZ_SOCKET_OBSERVE_WRITE;
353 yaz_log(m_log, "maskObserver 7");
354 m_socketObservable->maskObserver(this, mask);
355 yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes fd=%d (inc)",
356 q->m_len, cs_fileno(m_cs));
359 yaz_log (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes", q->m_len);
360 // whole packet sent... delete this and proceed to next ...
361 m_queue_out = q->m_next;
363 // don't select on write if queue is empty ...
367 yaz_log(m_log, "maskObserver 8");
368 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ|
369 YAZ_SOCKET_OBSERVE_EXCEPT);
374 int Yaz_PDU_Assoc::send_PDU(const char *buf, int len)
376 yaz_log (m_log, "Yaz_PDU_Assoc::send_PDU");
377 PDU_Queue **pq = &m_queue_out;
378 int is_idle = (*pq ? 0 : 1);
382 yaz_log (m_log, "Yaz_PDU_Assoc::send_PDU failed, m_cs == 0");
387 *pq = new PDU_Queue(buf, len);
391 yaz_log (m_log, "Yaz_PDU_Assoc::cannot send_PDU fd=%d",
396 COMSTACK Yaz_PDU_Assoc::comstack(const char *type_and_host, void **vp)
398 return cs_create_host(type_and_host, 2, vp);
401 int Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer,
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, YAZ_SOCKET_OBSERVE_READ|
417 YAZ_SOCKET_OBSERVE_EXCEPT);
418 yaz_log (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs));
423 void Yaz_PDU_Assoc::idleTime(int idleTime)
425 m_idleTime = idleTime;
426 yaz_log (m_log, "Yaz_PDU_Assoc::idleTime(%d)", idleTime);
427 m_socketObservable->timeoutObserver(this, m_idleTime);
430 int Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer,
433 yaz_log (m_log, "Yaz_PDU_Assoc::connect %s", addr);
435 m_PDU_Observer = observer;
437 m_cs = comstack(addr, &ap);
440 int res = cs_connect (m_cs, ap);
441 yaz_log (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_cs),
443 m_socketObservable->addObserver(cs_fileno(m_cs), this);
446 { // Connect complete
447 m_state = Connecting;
448 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
449 mask |= YAZ_SOCKET_OBSERVE_WRITE;
450 mask |= YAZ_SOCKET_OBSERVE_READ;
451 yaz_log(m_log, "maskObserver 11");
452 m_socketObservable->maskObserver(this, mask);
456 m_state = Connecting;
457 unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT;
458 if (m_cs->io_pending & CS_WANT_WRITE)
459 mask |= YAZ_SOCKET_OBSERVE_WRITE;
460 if (m_cs->io_pending & CS_WANT_READ)
461 mask |= YAZ_SOCKET_OBSERVE_READ;
462 yaz_log(m_log, "maskObserver 11");
463 m_socketObservable->maskObserver(this, mask);
466 { // Connect failed immediately
467 // Since m_state is Closed we can distinguish this case from
468 // normal connect in socketNotify handler
469 yaz_log(m_log, "maskObserver 12");
470 m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_WRITE|
471 YAZ_SOCKET_OBSERVE_EXCEPT);
476 // Single-threaded... Only useful for non-blocking handlers
477 void Yaz_PDU_Assoc::childNotify(COMSTACK cs)
479 Yaz_PDU_Assoc *new_observable =
480 new Yaz_PDU_Assoc (m_socketObservable, cs);
482 new_observable->m_next = m_children;
483 m_children = new_observable;
484 new_observable->m_parent = this;
486 // Clone PDU Observer
487 new_observable->m_PDU_Observer = m_PDU_Observer->sessionNotify
488 (new_observable, cs_fileno(cs));
491 const char*Yaz_PDU_Assoc::getpeername()
493 return cs_addrstr(m_cs);