From eb28345e243fa72fa9a6e4b890ae4ee2cc2b24d9 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 25 Feb 2014 10:56:09 +0100 Subject: [PATCH] Change ISocketObservable interface fd is passed to maskOserver rather than to addObserver --- include/yazpp/socket-manager.h | 4 ++-- include/yazpp/socket-observer.h | 5 +++-- src/yaz-pdu-assoc.cpp | 45 ++++++++++++++++++++++----------------- src/yaz-socket-manager.cpp | 17 +++++++++------ 4 files changed, 41 insertions(+), 30 deletions(-) diff --git a/include/yazpp/socket-manager.h b/include/yazpp/socket-manager.h index 4d25e83..86e8c4c 100644 --- a/include/yazpp/socket-manager.h +++ b/include/yazpp/socket-manager.h @@ -46,13 +46,13 @@ class YAZ_EXPORT SocketManager : public ISocketObservable { Rep *m_p; public: /// Add an observer - virtual void addObserver(int fd, ISocketObserver *observer); + virtual void addObserver(ISocketObserver *observer); /// Delete an observer virtual void deleteObserver(ISocketObserver *observer); /// Delete all observers virtual void deleteObservers(); /// Set event mask for observer - virtual void maskObserver(ISocketObserver *observer, int mask); + virtual void maskObserver(ISocketObserver *observer, int mask, int fd); /// Set timeout virtual void timeoutObserver(ISocketObserver *observer, int timeout); /// Process one event. return > 0 if event could be processed; diff --git a/include/yazpp/socket-observer.h b/include/yazpp/socket-observer.h index 38aa20b..cf1d105 100644 --- a/include/yazpp/socket-observer.h +++ b/include/yazpp/socket-observer.h @@ -64,13 +64,14 @@ namespace yazpp_1 { class YAZ_EXPORT ISocketObservable { public: /// Add an observer interested in socket fd - virtual void addObserver(int fd, ISocketObserver *observer) = 0; + virtual void addObserver(ISocketObserver *observer) = 0; /// Delete an observer virtual void deleteObserver(ISocketObserver *observer) = 0; /// Delete all observers virtual void deleteObservers() = 0; /// Specify the events that the observer is intersted in. - virtual void maskObserver(ISocketObserver *observer, int mask) = 0; + virtual void maskObserver(ISocketObserver *observer, + int mask, int fd) = 0; /// Specify timeout virtual void timeoutObserver(ISocketObserver *observer, int timeout)=0; diff --git a/src/yaz-pdu-assoc.cpp b/src/yaz-pdu-assoc.cpp index 369dfcf..50d4a79 100644 --- a/src/yaz-pdu-assoc.cpp +++ b/src/yaz-pdu-assoc.cpp @@ -102,7 +102,7 @@ PDU_Assoc::PDU_Assoc(ISocketObservable *socketObservable, mask |= SOCKET_OBSERVE_WRITE; if (cs->io_pending & CS_WANT_READ) mask |= SOCKET_OBSERVE_READ; - m_p->m_socketObservable->addObserver(cs_fileno(cs), this); + m_p->m_socketObservable->addObserver(this); if (!mask) { yaz_log(m_p->log, "new PDU_Assoc. Ready"); @@ -114,10 +114,11 @@ PDU_Assoc::PDU_Assoc(ISocketObservable *socketObservable, yaz_log(m_p->log, "new PDU_Assoc. Accepting"); // assume comstack is accepting... m_p->state = PDU_Assoc_priv::Accepting; - m_p->m_socketObservable->addObserver(cs_fileno(cs), this); + m_p->m_socketObservable->addObserver(this); yaz_log(m_p->log, "maskObserver 1"); m_p->m_socketObservable->maskObserver(this, - mask|SOCKET_OBSERVE_EXCEPT); + mask|SOCKET_OBSERVE_EXCEPT, + cs_fileno(cs)); } } @@ -172,8 +173,9 @@ void PDU_Assoc::socketNotify(int event) else { // accept still incomplete. yaz_log(m_p->log, "maskObserver 2"); - m_p->m_socketObservable->maskObserver(this, - mask|SOCKET_OBSERVE_EXCEPT); + m_p->m_socketObservable->maskObserver( + this, + mask|SOCKET_OBSERVE_EXCEPT, cs_fileno(m_p->cs)); } } break; @@ -188,8 +190,8 @@ void PDU_Assoc::socketNotify(int event) if (m_p->cs->io_pending & CS_WANT_READ) mask |= SOCKET_OBSERVE_READ; yaz_log(m_p->log, "maskObserver 3"); - m_p->m_socketObservable->addObserver(cs_fileno(m_p->cs), this); - m_p->m_socketObservable->maskObserver(this, mask); + m_p->m_socketObservable->maskObserver(this, mask, + cs_fileno(m_p->cs)); } else { @@ -244,7 +246,8 @@ void PDU_Assoc::socketNotify(int event) if (m_p->cs->io_pending & CS_WANT_READ) mask |= SOCKET_OBSERVE_READ; yaz_log(m_p->log, "maskObserver 4"); - m_p->m_socketObservable->maskObserver(this, mask); + m_p->m_socketObservable->maskObserver(this, mask, + cs_fileno(m_p->cs)); return; } else if (res <= 0) @@ -279,7 +282,8 @@ void PDU_Assoc::socketNotify(int event) yaz_log(m_p->log, "maskObserver 5"); m_p->m_socketObservable->maskObserver(this, SOCKET_OBSERVE_EXCEPT| - SOCKET_OBSERVE_READ); + SOCKET_OBSERVE_READ, + cs_fileno(m_p->cs)); } } break; @@ -393,7 +397,8 @@ int PDU_Assoc::flush_PDU() yaz_log(m_p->log, "maskObserver 6"); m_p->m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ| SOCKET_OBSERVE_WRITE| - SOCKET_OBSERVE_EXCEPT); + SOCKET_OBSERVE_EXCEPT, + cs_fileno(m_p->cs)); if (m_p->m_session_is_dead) { shutdown(); @@ -409,7 +414,6 @@ int PDU_Assoc::flush_PDU() m_PDU_Observer->failNotify(); return r; } - m_p->m_socketObservable->addObserver(cs_fileno(m_p->cs), this); if (r == 1) { unsigned mask = SOCKET_OBSERVE_EXCEPT; @@ -421,7 +425,7 @@ int PDU_Assoc::flush_PDU() mask |= SOCKET_OBSERVE_WRITE; yaz_log(m_p->log, "maskObserver 7"); - m_p->m_socketObservable->maskObserver(this, mask); + m_p->m_socketObservable->maskObserver(this, mask, cs_fileno(m_p->cs)); yaz_log(m_p->log, "PDU_Assoc::flush_PDU cs_put %d bytes fd=%d (inc)", q->m_len, cs_fileno(m_p->cs)); return r; @@ -436,7 +440,8 @@ int PDU_Assoc::flush_PDU() m_p->state = PDU_Assoc_priv::Ready; yaz_log(m_p->log, "maskObserver 8"); m_p->m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ| - SOCKET_OBSERVE_EXCEPT); + SOCKET_OBSERVE_EXCEPT, + cs_fileno(m_p->cs)); if (m_p->m_session_is_dead) shutdown(); } @@ -519,10 +524,11 @@ int PDU_Assoc::listen(IPDU_Observer *observer, const char *addr) fcntl(fd, F_SETFD, oldflags); } #endif - m_p->m_socketObservable->addObserver(fd, this); + m_p->m_socketObservable->addObserver(this); yaz_log(m_p->log, "maskObserver 9"); m_p->m_socketObservable->maskObserver(this, SOCKET_OBSERVE_READ| - SOCKET_OBSERVE_EXCEPT); + SOCKET_OBSERVE_EXCEPT, + cs_fileno(m_p->cs)); yaz_log(m_p->log, "PDU_Assoc::listen ok fd=%d", fd); m_p->state = PDU_Assoc_priv::Listen; return 0; @@ -552,7 +558,7 @@ int PDU_Assoc::connect(IPDU_Observer *observer, const char *addr) int res = cs_connect(m_p->cs, ap); yaz_log(m_p->log, "PDU_Assoc::connect fd=%d res=%d", cs_fileno(m_p->cs), res); - m_p->m_socketObservable->addObserver(cs_fileno(m_p->cs), this); + m_p->m_socketObservable->addObserver(this); if (res == 0) { // Connect complete @@ -561,7 +567,7 @@ int PDU_Assoc::connect(IPDU_Observer *observer, const char *addr) mask |= SOCKET_OBSERVE_WRITE; mask |= SOCKET_OBSERVE_READ; yaz_log(m_p->log, "maskObserver 11"); - m_p->m_socketObservable->maskObserver(this, mask); + m_p->m_socketObservable->maskObserver(this, mask, cs_fileno(m_p->cs)); } else if (res > 0) { // Connect pending @@ -572,7 +578,7 @@ int PDU_Assoc::connect(IPDU_Observer *observer, const char *addr) if (m_p->cs->io_pending & CS_WANT_READ) mask |= SOCKET_OBSERVE_READ; yaz_log(m_p->log, "maskObserver 11"); - m_p->m_socketObservable->maskObserver(this, mask); + m_p->m_socketObservable->maskObserver(this, mask, cs_fileno(m_p->cs)); } else { // Connect failed immediately @@ -580,7 +586,8 @@ int PDU_Assoc::connect(IPDU_Observer *observer, const char *addr) // normal connect in socketNotify handler yaz_log(m_p->log, "maskObserver 12"); m_p->m_socketObservable->maskObserver(this, SOCKET_OBSERVE_WRITE| - SOCKET_OBSERVE_EXCEPT); + SOCKET_OBSERVE_EXCEPT, + cs_fileno(m_p->cs)); } return 0; } diff --git a/src/yaz-socket-manager.cpp b/src/yaz-socket-manager.cpp index e119735..b5ccd9b 100644 --- a/src/yaz-socket-manager.cpp +++ b/src/yaz-socket-manager.cpp @@ -79,7 +79,7 @@ int SocketManager::getNumberOfObservers() return i; } -void SocketManager::addObserver(int fd, ISocketObserver *observer) +void SocketManager::addObserver(ISocketObserver *observer) { SocketEntry *se; @@ -91,7 +91,7 @@ void SocketManager::addObserver(int fd, ISocketObserver *observer) m_p->observers = se; se->observer = observer; } - se->fd = fd; + se->fd = -1; se->mask = 0; se->last_activity = 0; se->timeout = -1; @@ -122,18 +122,21 @@ void SocketManager::deleteObservers() m_p->observers = 0; } -void SocketManager::maskObserver(ISocketObserver *observer, int mask) +void SocketManager::maskObserver(ISocketObserver *observer, int mask, int fd) { SocketEntry *se; - yaz_log(m_p->log, "obs=%p read=%d write=%d except=%d", observer, - mask & SOCKET_OBSERVE_READ, - mask & SOCKET_OBSERVE_WRITE, - mask & SOCKET_OBSERVE_EXCEPT); + yaz_log(m_p->log, "obs=%p read=%d write=%d except=%d fd=%d", observer, + mask & SOCKET_OBSERVE_READ, + mask & SOCKET_OBSERVE_WRITE, + mask & SOCKET_OBSERVE_EXCEPT, fd); se = *m_p->lookupObserver(observer); if (se) + { se->mask = mask; + se->fd = fd; + } } void SocketManager::timeoutObserver(ISocketObserver *observer, -- 1.7.10.4