diff --git a/src/gui/server/ObjectPool.hpp b/src/gui/server/ObjectPool.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0606f9c7d082fa5988a2cec62dcbda93ea65a10c --- /dev/null +++ b/src/gui/server/ObjectPool.hpp @@ -0,0 +1,56 @@ +/** + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * Yan Morin <yan.morin@savoirfairelinux.com> (cc++ mutex) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef SFLPHONEGUI_OBJECTPOOL_H +#define SFLPHONEGUI_OBJECTPOOL_H + +#include <list> +#include <string> +#include <cc++/thread.h> + +template< typename T > +class ObjectPool +{ + public: + /** + * This function will push a line in the pool. + */ + void push(const T &line); + + /** + * This function will wait for an available line. + */ + bool pop(T &value, unsigned long time = ULONG_MAX); + + typename std::list< T >::iterator begin(); + typename std::list< T >::iterator end(); + + private: + std::list< T > mPool; + + ost::Mutex mMutex; + ost::Semaphore mSemaphore; +}; + +#include "ObjectPool.inl" + +#endif + diff --git a/src/gui/server/ObjectPool.inl b/src/gui/server/ObjectPool.inl new file mode 100644 index 0000000000000000000000000000000000000000..4c234043febe592e184aeb2e5b7d5fd58fe4e3c5 --- /dev/null +++ b/src/gui/server/ObjectPool.inl @@ -0,0 +1,80 @@ +/** + * Copyright (C) 2004-2005 Savoir-Faire Linux inc. + * Author: Jean-Philippe Barrette-LaPierre + * <jean-philippe.barrette-lapierre@savoirfairelinux.com> + * Yan Morin <yan.morin@savoirfairelinux.com> (cc++ mutex) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef SFLPHONEGUI_OBJECTPOOL_INL +#define SFLPHONEGUI_OBJECTPOOL_INL + +#include <iostream> + +template< typename T > +void +ObjectPool< T >::push(const T &value) +{ + ost::MutexLock guard(mMutex); + mPool.push_back(value); + mSemaphore.post(); + std::cerr << "push value..." << std::endl; +} + +template< typename T > +bool +ObjectPool< T >::pop(T &value, unsigned long time) +{ + ost::MutexLock guard(mMutex); + mSemaphore.wait(time); + + if(mPool.begin() == mPool.end()) { + std::cerr << "empty list" << std::endl; + return false; + } else { + std::cerr << "pop value..." << std::endl; + typename std::list< T >::iterator pos = mPool.begin(); + value = (*pos); + mPool.pop_front(); + return true; + } +} + +template< typename T > +typename std::list< T >::iterator +ObjectPool< T >::begin() +{ + + ost::MutexLock guard(mMutex); + std::cerr << mPool.size(); + typename std::list< T >::iterator iter = mPool.begin(); + mSemaphore.post(); + return iter; +} + +template< typename T > +typename std::list< T >::iterator +ObjectPool< T >::end() +{ + ost::MutexLock guard(mMutex); + std::cerr << mPool.size(); + typename std::list< T >::iterator iter = mPool.end(); + mSemaphore.post(); + return iter; +} + + +#endif diff --git a/src/gui/server/guiserver.cpp b/src/gui/server/guiserver.cpp index f6722e8165ff9082190a4f65fd20586a327417eb..bc1f41954372af4b00a77ccc71b02808220231df 100644 --- a/src/gui/server/guiserver.cpp +++ b/src/gui/server/guiserver.cpp @@ -64,18 +64,20 @@ GUIServer::exec() { // waiting for a new connection std::cout << "waiting for a new connection..." << std::endl; - + //I'm accepting an incomming connection - _sessionIO = new TCPSessionIO(aServer, this); + _sessionIO = new TCPSessionIO(aServer, this); _sessionIO->start(); - + // wait for the first message std::cout << "accepting connection..." << std::endl; - + while(_sessionIO->good()) { - request = popRequest(); - output = request->execute(*this); - pushResponseMessage(output); + if ( _requests.pop(request, 1000)) { + output = request->execute(*this); + pushResponseMessage(output); + delete request; + } } } } @@ -87,41 +89,25 @@ GUIServer::exec() { } void -GUIServer::pushRequestMessage(const std::string &request) +GUIServer::pushRequestMessage(const std::string &request) { + Request *tempRequest = _factory.create(request); std::cout << "pushRequestMessage" << std::endl; - _mutex.enterMutex(); - _requests.push_back(_factory.create(request)); - _mutex.leaveMutex(); -} - -Request * -GUIServer::popRequest() -{ - Request *request = 0; - while(!request) { - _mutex.enterMutex(); - if ( _requests.begin() != _requests.end() ) { - request = _requests.front(); - _requests.pop_front(); - } - _mutex.leaveMutex(); - } - return request; + //ost::MutexLock lock(_mutex); + _requests.push(tempRequest); } void GUIServer::pushResponseMessage(const ResponseMessage &response) { std::cout << "pushResponseMessage" << std::endl; - _mutex.enterMutex(); + //ost::MutexLock lock(_mutex); *_sessionIO << response.toString() << std::endl; - _mutex.leaveMutex(); // remove the request from the list - if (response.isFinal()) { - removeRequest(response.sequenceId()); - } + //if (response.isFinal()) { + // removeRequest(response.sequenceId()); + //} } /** @@ -130,6 +116,7 @@ GUIServer::pushResponseMessage(const ResponseMessage &response) void GUIServer::removeRequest(const std::string& sequenceId) { + ost::MutexLock lock(_mutex); std::list<Request*>::iterator iter; for(iter=_requests.begin(); iter!=_requests.end(); iter++) { if ( (*iter)->sequenceId() == sequenceId ) { diff --git a/src/gui/server/guiserver.h b/src/gui/server/guiserver.h index 40b0a52abbc2ec6e690be0e7c2600bfd40796a56..e3cd16957ee8e07c9204602b7a6678599e782e2d 100644 --- a/src/gui/server/guiserver.h +++ b/src/gui/server/guiserver.h @@ -29,6 +29,7 @@ #include "subcall.h" #include "requestfactory.h" +#include "ObjectPool.hpp" class GUIServer; class TCPSessionIO : public ost::TCPSession @@ -95,7 +96,7 @@ private: * and also a sequence number */ CallMap _callMap; - std::list<Request*> _requests; + ObjectPool<Request*> _requests; RequestFactory _factory; ost::Mutex _mutex; };