Skip to content
Snippets Groups Projects
Commit d2de28ad authored by yanmorin's avatar yanmorin
Browse files

ObjectPool Integration Testing phase
Note that request* are temporary delete right now...
parent 7e32c6c3
No related branches found
No related tags found
No related merge requests found
/**
* 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
/**
* 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
......@@ -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 ) {
......
......@@ -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;
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment