diff --git a/src/gui/server/ObjectPool.hpp b/src/gui/server/ObjectPool.hpp index 0606f9c7d082fa5988a2cec62dcbda93ea65a10c..d686f9bd668cea2d28feaa49ce76d8d58842ed52 100644 --- a/src/gui/server/ObjectPool.hpp +++ b/src/gui/server/ObjectPool.hpp @@ -40,9 +40,6 @@ class ObjectPool */ 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; diff --git a/src/gui/server/ObjectPool.inl b/src/gui/server/ObjectPool.inl index 4c234043febe592e184aeb2e5b7d5fd58fe4e3c5..9f96681e1eec7d044cc3384e0bfb25eaa6e1e5c2 100644 --- a/src/gui/server/ObjectPool.inl +++ b/src/gui/server/ObjectPool.inl @@ -31,50 +31,25 @@ 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; + //ost::MutexLock guard(mMutex); + if (mSemaphore.wait(time)) { + + if(mPool.begin() == mPool.end()) { + return false; + } else { + typename std::list< T >::iterator pos = mPool.begin(); + value = (*pos); + mPool.pop_front(); + return true; + } } + return false; } -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 bc1f41954372af4b00a77ccc71b02808220231df..2597c44e261a96a4cf891fad7f497351c47d1030 100644 --- a/src/gui/server/guiserver.cpp +++ b/src/gui/server/guiserver.cpp @@ -27,11 +27,15 @@ void TCPSessionIO::run() { + std::string response; while(!testCancel() && good()) { - if (isPending(ost::TCPSocket::pendingInput)) { - std::string output; - std::getline(*this, output); - _gui->pushRequestMessage(output); + if (isPending(ost::TCPSocket::pendingInput, 10)) { + std::string input; + std::getline(*this, input); + _gui->pushRequestMessage(input); + } + if (_outputPool.pop(response, 10)) { + *this << response << std::endl; } } } @@ -45,6 +49,14 @@ GUIServer::GUIServer() // destructor GUIServer::~GUIServer() { + // Waiting Requests cleanup + std::map<std::string, Request*>::iterator iter = _waitingRequests.begin(); + while (iter != _waitingRequests.end()) { + _waitingRequests.erase(iter); + delete (iter->second); + iter++; + } + } int @@ -75,8 +87,7 @@ GUIServer::exec() { while(_sessionIO->good()) { if ( _requests.pop(request, 1000)) { output = request->execute(*this); - pushResponseMessage(output); - delete request; + handleExecutedRequest(request, output); } } } @@ -93,7 +104,6 @@ GUIServer::pushRequestMessage(const std::string &request) { Request *tempRequest = _factory.create(request); std::cout << "pushRequestMessage" << std::endl; - //ost::MutexLock lock(_mutex); _requests.push(tempRequest); } @@ -101,28 +111,36 @@ void GUIServer::pushResponseMessage(const ResponseMessage &response) { std::cout << "pushResponseMessage" << std::endl; - //ost::MutexLock lock(_mutex); - *_sessionIO << response.toString() << std::endl; + _sessionIO->push(response.toString()); // remove the request from the list - //if (response.isFinal()) { - // removeRequest(response.sequenceId()); - //} + if (response.isFinal()) { + std::map<std::string, Request*>::iterator iter = _waitingRequests.find(response.sequenceId()); + if (iter != _waitingRequests.end()) { + _waitingRequests.erase(iter); + delete (iter->second); + } + } } /** - * Remove a request with its sequence id + * Delete the request from the list of request + * or send it into the waitingRequest map */ void -GUIServer::removeRequest(const std::string& sequenceId) +GUIServer::handleExecutedRequest(Request * const request, const ResponseMessage& response) { - ost::MutexLock lock(_mutex); - std::list<Request*>::iterator iter; - for(iter=_requests.begin(); iter!=_requests.end(); iter++) { - if ( (*iter)->sequenceId() == sequenceId ) { - delete (*iter); + if (response.isFinal()) { + delete request; + } else { + if (_waitingRequests.find(request->sequenceId()) == _waitingRequests.end()) { + _waitingRequests[response.sequenceId()] = request; + } else { + // we don't deal with requests with a sequenceId already send... + delete request; } } + _sessionIO->push(response.toString()); } /** diff --git a/src/gui/server/guiserver.h b/src/gui/server/guiserver.h index e3cd16957ee8e07c9204602b7a6678599e782e2d..81930c83f96f72700b4b91da847578ad710dca20 100644 --- a/src/gui/server/guiserver.h +++ b/src/gui/server/guiserver.h @@ -40,9 +40,13 @@ public: _gui(gui) {} void run(); - + void push(const std::string &response) { + _outputPool.push(response); + } + private: GUIServer *_gui; + ObjectPool<std::string> _outputPool; }; typedef std::map<short, SubCall> CallMap; @@ -56,10 +60,11 @@ public: // exec loop int exec(void); + //void handleExecutedRequest(Request* request, const void pushRequestMessage(const std::string& request); Request *popRequest(void); void pushResponseMessage(const ResponseMessage& response); - void removeRequest(const std::string& sequenceId); + void handleExecutedRequest(Request * const request, const ResponseMessage& response); void insertSubCall(short id, SubCall& subCall); void removeSubCall(short id); @@ -87,7 +92,7 @@ public: void hangup(const std::string& callId); private: - ost::TCPSession* _sessionIO; + TCPSessionIO* _sessionIO; /** * This callMap is necessary because @@ -96,7 +101,11 @@ private: * and also a sequence number */ CallMap _callMap; + // Incoming requests not executed ObjectPool<Request*> _requests; + // Requests executed but waiting for a final response + std::map<std::string, Request*> _waitingRequests; + RequestFactory _factory; ost::Mutex _mutex; };