X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fyaz-pdu-assoc.cpp;h=566f9b0bf251efea1e679f1f333e8e06b272be0a;hb=7a9cf0a84f1169b52469f736be7e98e7440eeff4;hp=c0b70c37b121795f78d352d7abd3a2d5b018ae74;hpb=26b3f7f15abcb69aaf00fb6bdaf410ff3e7de14c;p=yazpp-moved-to-github.git diff --git a/src/yaz-pdu-assoc.cpp b/src/yaz-pdu-assoc.cpp index c0b70c3..566f9b0 100644 --- a/src/yaz-pdu-assoc.cpp +++ b/src/yaz-pdu-assoc.cpp @@ -1,10 +1,54 @@ /* - * Copyright (c) 1998-1999, Index Data. + * Copyright (c) 1998-2001, Index Data. * See the file LICENSE for details. - * Sebastian Hammer, Adam Dickmeiss * * $Log: yaz-pdu-assoc.cpp,v $ - * Revision 1.9 1999-12-06 13:52:45 adam + * Revision 1.23 2001-03-26 14:43:49 adam + * New threaded PDU association. + * + * Revision 1.22 2001/01/29 11:18:24 adam + * Server sets OPTIONS search and present. + * + * Revision 1.21 2000/11/20 14:17:36 adam + * Yet another WIN32 fix for connect notify. + * + * Revision 1.20 2000/11/20 11:27:33 adam + * Fixes for connect operation (timeout and notify fix). + * + * Revision 1.19 2000/11/01 14:22:59 adam + * Added fd parameter for method IYaz_PDU_Observer::clone. + * + * Revision 1.18 2000/10/24 12:29:57 adam + * Fixed bug in proxy where a Yaz_ProxyClient could be owned by + * two Yaz_Proxy's (fatal). + * + * Revision 1.17 2000/10/11 11:58:16 adam + * Moved header files to include/yaz++. Switched to libtool and automake. + * Configure script creates yaz++-config script. + * + * Revision 1.16 2000/09/22 09:54:11 heikki + * minor + * + * Revision 1.15 2000/09/21 21:43:20 adam + * Better high-level server API. + * + * Revision 1.14 2000/09/12 12:09:53 adam + * More work on high-level server. + * + * Revision 1.13 2000/09/08 10:23:42 adam + * Added skeleton of yaz-z-server. + * + * Revision 1.12 2000/09/06 14:23:45 adam + * WIN32 updates. + * + * Revision 1.11 2000/09/04 08:29:22 adam + * Fixed memory leak(s). Added re-use of associations, rather than + * re-init, when maximum number of targets are in use. + * + * Revision 1.10 2000/08/10 08:42:42 adam + * Fixes for {set,get}_APDU_log. + * + * Revision 1.9 1999/12/06 13:52:45 adam * Modified for new location of YAZ header files. Experimental threaded * operation. * @@ -18,7 +62,7 @@ * Revision 1.6 1999/04/20 10:30:05 adam * Implemented various stuff for client and proxy. Updated calls * to ODR to reflect new name parameter. - * + *g * Revision 1.5 1999/04/09 11:46:57 adam * Added object Yaz_Z_Assoc. Much more functional client. * @@ -38,13 +82,14 @@ */ #include - -#include - +#include #include #include -Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable) +#include + + +void Yaz_PDU_Assoc::init(IYazSocketObservable *socketObservable) { m_state = Closed; m_cs = 0; @@ -61,50 +106,120 @@ Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable) m_log = LOG_DEBUG; } -IYaz_PDU_Observable *Yaz_PDU_Assoc::clone() +Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable) { - Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable); - return copy; + init (socketObservable); +} + +Yaz_PDU_Assoc::Yaz_PDU_Assoc(IYazSocketObservable *socketObservable, + COMSTACK cs) +{ + init(socketObservable); + m_cs = cs; + unsigned mask = 0; + if (cs->io_pending & CS_WANT_WRITE) + mask |= YAZ_SOCKET_OBSERVE_WRITE; + if (cs->io_pending & CS_WANT_READ) + mask |= YAZ_SOCKET_OBSERVE_READ; + m_socketObservable->addObserver(cs_fileno(cs), this); + if (!mask) + { + yaz_log (m_log, "new PDU_Assoc. Ready"); + m_state = Ready; + flush_PDU(); + } + else + { + yaz_log (m_log, "new PDU_Assoc. Accepting"); + // assume comstack is accepting... + m_state = Accepting; + m_socketObservable->addObserver(cs_fileno(cs), this); + m_socketObservable->maskObserver(this, + mask |YAZ_SOCKET_OBSERVE_EXCEPT); + } } -Yaz_PDU_Assoc::~Yaz_PDU_Assoc() + +IYaz_PDU_Observable *Yaz_PDU_Assoc::clone() { - destroy(); + Yaz_PDU_Assoc *copy = new Yaz_PDU_Assoc(m_socketObservable); + return copy; } void Yaz_PDU_Assoc::socketNotify(int event) { - logf (m_log, "Yaz_PDU_Assoc::socketNotify p=%p event = %d", this, event); - if (0 /* m_state == Connected */) + yaz_log (m_log, "Yaz_PDU_Assoc::socketNotify p=%p state=%d event = %d", + this, m_state, event); + if (event & YAZ_SOCKET_OBSERVE_EXCEPT) { - m_state = Ready; - m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| - YAZ_SOCKET_OBSERVE_EXCEPT); - m_PDU_Observer->connectNotify(); - flush_PDU(); + close(); + m_PDU_Observer->failNotify(); + return; } - else if (m_state == Connecting) + else if (event & YAZ_SOCKET_OBSERVE_TIMEOUT) { - if (event & YAZ_SOCKET_OBSERVE_READ) + m_PDU_Observer->timeoutNotify(); + return; + } + switch (m_state) + { + case Accepting: + if (!cs_accept (m_cs)) { + yaz_log (m_log, "Yaz_PDU_Assoc::cs_accept failed"); + m_cs = 0; close(); m_PDU_Observer->failNotify(); } - else if (event & YAZ_SOCKET_OBSERVE_TIMEOUT) - { - m_PDU_Observer->timeoutNotify(); - } - else + else { - m_state = Ready; - m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| - YAZ_SOCKET_OBSERVE_EXCEPT); - m_PDU_Observer->connectNotify(); - flush_PDU(); + unsigned mask = 0; + if (m_cs->io_pending & CS_WANT_WRITE) + mask |= YAZ_SOCKET_OBSERVE_WRITE; + if (m_cs->io_pending & CS_WANT_READ) + mask |= YAZ_SOCKET_OBSERVE_READ; + if (!mask) + { // accept is complete. turn to ready state and write if needed + m_state = Ready; + flush_PDU(); + } + else + { // accept still incomplete. + m_socketObservable->maskObserver(this, + mask|YAZ_SOCKET_OBSERVE_EXCEPT); + } } - } - else if (m_state == Listen) - { + break; + case Connecting: + if (event & YAZ_SOCKET_OBSERVE_READ && + event & YAZ_SOCKET_OBSERVE_WRITE) + { + // For Unix: if both read and write is set, then connect failed. + close(); + m_PDU_Observer->failNotify(); + } + else + { + yaz_log (m_log, "cs_connect again"); + int res = cs_connect (m_cs, 0); + if (res == 1) + { + unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT; + if (m_cs->io_pending & CS_WANT_WRITE) + mask |= YAZ_SOCKET_OBSERVE_WRITE; + if (m_cs->io_pending & CS_WANT_READ) + mask |= YAZ_SOCKET_OBSERVE_READ; + m_socketObservable->maskObserver(this, mask); + } + else + { + m_state = Ready; + m_PDU_Observer->connectNotify(); + flush_PDU(); + } + } + break; + case Listen: if (event & YAZ_SOCKET_OBSERVE_READ) { int res; @@ -114,52 +229,45 @@ void Yaz_PDU_Assoc::socketNotify(int event) return; if (res < 0) { - logf(LOG_FATAL, "cs_listen failed"); + yaz_log(LOG_FATAL|LOG_ERRNO, "cs_listen failed"); return; } if (!(new_line = cs_accept(m_cs))) return; - /* 1. create socket-manager 2. create pdu-assoc 3. create top-level object setup observer for child fileid in pdu-assoc 4. start thread */ - int fd = cs_fileno(new_line); - cs_fileno(new_line) = -1; - cs_close (new_line); -#if 1 - childNotify(fd); -#else - Yaz_PDU_Assoc *assoc = new Yaz_PDU_Assoc (m_socketObservable); - assoc->m_parent = this; - assoc->m_next = m_children; - m_children = assoc; - - assoc->m_PDU_Observer = m_PDU_Observer->clone(assoc); - socket(fd); -#endif + childNotify (new_line); } - } - else if (m_state == Ready) - { - if (event & YAZ_SOCKET_OBSERVE_WRITE) - { - flush_PDU(); - } - if (event & YAZ_SOCKET_OBSERVE_READ) + break; + case Writing: + if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_WRITE)) + flush_PDU(); + break; + case Ready: + if (event & (YAZ_SOCKET_OBSERVE_READ|YAZ_SOCKET_OBSERVE_WRITE)) { do { int res = cs_get (m_cs, &m_input_buf, &m_input_len); if (res == 1) + { + unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT; + if (m_cs->io_pending & CS_WANT_WRITE) + mask |= YAZ_SOCKET_OBSERVE_WRITE; + if (m_cs->io_pending & CS_WANT_READ) + mask |= YAZ_SOCKET_OBSERVE_READ; + m_socketObservable->maskObserver(this, mask); return; + } else if (res <= 0) { - logf (m_log, "Connection closed by peer"); + yaz_log (m_log, "Yaz_PDU_Assoc::Connection closed by peer"); close(); - m_PDU_Observer->failNotify(); + m_PDU_Observer->failNotify(); // problem here.. return; } // lock it, so we know if recv_PDU deletes it. @@ -167,14 +275,25 @@ void Yaz_PDU_Assoc::socketNotify(int event) m_destroyed = &destroyed; m_PDU_Observer->recv_PDU(m_input_buf, res); + m_destroyed = 0; if (destroyed) // it really was destroyed, return now. return; } while (m_cs && cs_more (m_cs)); + if (m_cs) + m_socketObservable->maskObserver(this, + YAZ_SOCKET_OBSERVE_EXCEPT| + YAZ_SOCKET_OBSERVE_READ); } - if (event & YAZ_SOCKET_OBSERVE_TIMEOUT) - { - m_PDU_Observer->timeoutNotify(); - } + break; + case Closed: + logf (m_log, "CLOSING state=%d event was %d", m_state, event); + close(); + m_PDU_Observer->failNotify(); + break; + default: + logf (m_log, "Unknown state=%d event was %d", m_state, event); + close(); + m_PDU_Observer->failNotify(); } } @@ -194,7 +313,7 @@ void Yaz_PDU_Assoc::close() m_queue_out = m_queue_out->m_next; delete q_this; } -// free (m_input_buf); + xfree (m_input_buf); m_input_buf = 0; m_input_len = 0; } @@ -245,8 +364,7 @@ int Yaz_PDU_Assoc::flush_PDU() { int r; - logf (m_log, "Yaz_PDU_Assoc::flush_PDU"); - if (m_state != Ready) + if (m_state != Ready && m_state != Writing) { logf (m_log, "YAZ_PDU_Assoc::flush_PDU, not ready"); return 1; @@ -254,27 +372,37 @@ int Yaz_PDU_Assoc::flush_PDU() PDU_Queue *q = m_queue_out; if (!q) { + m_state = Ready; + logf (m_log, "YAZ_PDU_Assoc::flush_PDU queue empty"); m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| + YAZ_SOCKET_OBSERVE_WRITE| YAZ_SOCKET_OBSERVE_EXCEPT); return 0; } r = cs_put (m_cs, q->m_buf, q->m_len); if (r < 0) { + logf (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put failed"); close(); m_PDU_Observer->failNotify(); return r; } if (r == 1) { - m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| - YAZ_SOCKET_OBSERVE_EXCEPT| - YAZ_SOCKET_OBSERVE_WRITE); - logf (m_log, "Yaz_PDU_Assoc::flush_PDU put %d bytes (incomplete)", + unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT; + m_state = Writing; + if (m_cs->io_pending & CS_WANT_WRITE) + mask |= YAZ_SOCKET_OBSERVE_WRITE; + if (m_cs->io_pending & CS_WANT_READ) + mask |= YAZ_SOCKET_OBSERVE_READ; + + m_socketObservable->maskObserver(this, mask); + logf (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes (incomplete)", q->m_len); return r; - } - logf (m_log, "Yaz_PDU_Assoc::flush_PDU put %d bytes", q->m_len); + } + m_state = Ready; + logf (m_log, "Yaz_PDU_Assoc::flush_PDU cs_put %d bytes", q->m_len); // whole packet sent... delete this and proceed to next ... m_queue_out = q->m_next; delete q; @@ -307,35 +435,30 @@ int Yaz_PDU_Assoc::send_PDU(const char *buf, int len) return 0; } -COMSTACK Yaz_PDU_Assoc::comstack() +COMSTACK Yaz_PDU_Assoc::comstack(const char *type_and_host, void **vp) { - if (!m_cs) - { - CS_TYPE cs_type = tcpip_type; - m_cs = cs_create (cs_type, 0, PROTO_Z3950); - } - return m_cs; + return cs_create_host(type_and_host, 0, vp); } void Yaz_PDU_Assoc::listen(IYaz_PDU_Observer *observer, const char *addr) { close(); - void *ap; - COMSTACK cs = comstack(); - logf (m_log, "Yaz_PDU_Assoc::listen %s", addr); + logf (LOG_LOG, "Adding listener %s", addr); + m_PDU_Observer = observer; - if (!cs) - return; - ap = cs_straddr (cs, addr); - if (!ap) + void *ap; + m_cs = comstack(addr, &ap); + + if (!m_cs) return; - if (cs_bind(cs, ap, CS_SERVER) < 0) + if (cs_bind(m_cs, ap, CS_SERVER) < 0) return; - m_socketObservable->addObserver(cs_fileno(cs), this); + m_socketObservable->addObserver(cs_fileno(m_cs), this); m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| YAZ_SOCKET_OBSERVE_EXCEPT); + logf (m_log, "Yaz_PDU_Assoc::listen ok fd=%d", cs_fileno(m_cs)); m_state = Listen; } @@ -352,88 +475,38 @@ void Yaz_PDU_Assoc::connect(IYaz_PDU_Observer *observer, logf (m_log, "Yaz_PDU_Assoc::connect %s", addr); close(); m_PDU_Observer = observer; - COMSTACK cs = comstack(); - void *ap = cs_straddr (cs, addr); - if (!ap) - { - logf (m_log, "cs_straddr failed"); - return; + void *ap; + m_cs = comstack(addr, &ap); + int res = cs_connect (m_cs, ap); + logf (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_cs), res); + m_socketObservable->addObserver(cs_fileno(m_cs), this); + + if (res >= 0) + { // Connect pending or complet + m_state = Connecting; + unsigned mask = YAZ_SOCKET_OBSERVE_EXCEPT; + if (m_cs->io_pending & CS_WANT_WRITE) + mask |= YAZ_SOCKET_OBSERVE_WRITE; + if (m_cs->io_pending & CS_WANT_READ) + mask |= YAZ_SOCKET_OBSERVE_READ; + m_socketObservable->maskObserver(this, mask); } - int res = cs_connect (cs, ap); - logf (m_log, "Yaz_PDU_Assoc::connect fd=%d res=%d", cs_fileno(cs), res); - m_socketObservable->addObserver(cs_fileno(cs), this); - m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_READ| - YAZ_SOCKET_OBSERVE_EXCEPT| - YAZ_SOCKET_OBSERVE_WRITE); - m_state = Connecting; -} - -void Yaz_PDU_Assoc::socket(IYaz_PDU_Observer *observer, int fd) -{ - close(); - m_PDU_Observer = observer; - if (fd >= 0) - { - CS_TYPE cs_type = tcpip_type; - m_cs = cs_createbysocket(fd, cs_type, 0, PROTO_Z3950); - m_state = Ready; - m_socketObservable->addObserver(fd, this); - m_socketObservable->maskObserver(this, - YAZ_SOCKET_OBSERVE_READ| + else + { // Connect failed immediately + // Since m_state is Closed we can distinguish this case from + // normal connect in socketNotify handler + m_socketObservable->maskObserver(this, YAZ_SOCKET_OBSERVE_WRITE| YAZ_SOCKET_OBSERVE_EXCEPT); - m_socketObservable->timeoutObserver(this, m_idleTime); } } -#if 1 -void Yaz_PDU_Assoc::childNotify(int fd) -{ - /// Clone PDU Observable (keep socket manager) - IYaz_PDU_Observable *new_observable = clone(); - - /// Clone PDU Observer - IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable); - - /// Attach new socket to it - new_observable->socket(observer, fd); -} -#else - -#include -#include - -class thread_info { - Yaz_SocketManager *socketManager; - IYaz_PDU_Observable * - -}; - -static void *events(void *p) +// Single-threaded... Only useful for non-blocking handlers +void Yaz_PDU_Assoc::childNotify(COMSTACK cs) { - Yaz_SocketManager *s = (Yaz_SocketManager *) p; + Yaz_PDU_Assoc *new_observable = + new Yaz_PDU_Assoc (m_socketObservable, cs); - while (s->processEvent() > 0) - ; - return 0; + // Clone PDU Observer + new_observable->m_PDU_Observer = m_PDU_Observer->sessionNotify + (new_observable, cs_fileno(cs)); } - -void Yaz_PDU_Assoc::childNotify(int fd) -{ - Yaz_SocketManager *socket_observable = new Yaz_SocketManager; - IYaz_PDU_Observable *new_observable = clone(); - - m_socketObservable = socket_observable; - - /// Clone PDU Observer - IYaz_PDU_Observer *observer = m_PDU_Observer->clone(new_observable); - - /// Attach new socket to it - new_observable->socket(observer, fd); - - pthread_t type; - - int id = pthread_create (&type, 0, events, socket_observable); - logf (LOG_LOG, "pthread_create returned id=%d", id); -} -#endif -