Skip to content
Snippets Groups Projects
Commit e1798f89 authored by jpbl's avatar jpbl
Browse files

compiles

parent 153ca181
No related branches found
No related tags found
No related merge requests found
Showing
with 745 additions and 3 deletions
noinst_PROGRAMS = test
BUILT_SOURCES =
test_SOURCES = \
$(BUILT_SOURCES) \
account.cpp answerreceiver.cpp call.cpp main.cpp request.cpp requesterimpl.cpp session.cpp sessionio.cpp
CLEANFILES = \
$(BUILT_SOURCES)
test_LDFLAGS = $(QT_LDFLAGS) $(X_LDFLAGS) -static
test_LDADD = ../../../utilspp/libutilspp.la -lpthread $(LIBQT)
KDE_CXXFLAGS = $(USE_EXCEPTIONS)
AM_CPPFLAGS = $(QT_INCLUDES) $(X_INCLUDES) -I$(top_srcdir)
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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.
*/
#include <string>
#include "answerreceiver.h"
#include "requesterimpl.h"
#include "sessionio.h"
AnswerReceiver::AnswerReceiver(RequesterImpl *requester,
SessionIO *session)
: mRequester(requester)
, mSession(session)
{}
void
AnswerReceiver::run()
{
std::string answer;
while(mSession->isUp()) {
mSession->receive(answer);
mRequester->receiveAnswer(answer);
}
}
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_ANSWERRECIVER_H
#define SFLPHONEGUI_ANSWERRECIVER_H
#include <qthread.h>
class RequesterImpl;
class SessionIO;
/**
* This class is the thread that will read
* from the SessionIO.
*/
class AnswerReceiver : public QThread
{
public:
AnswerReceiver(RequesterImpl *requester,
SessionIO *session);
/**
* This is the main processing function.
*/
virtual void run();
private:
RequesterImpl *mRequester;
SessionIO *mSession;
};
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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.
*/
#include <iostream>
#include <stdexcept>
#include <unistd.h>
#include "session.h"
int main(int, char **)
{
try {
Session session;
Account account = session.getAccount("patate");
account.registerAccount();
while(1) {
sleep(2);
}
}
catch(std::exception &e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_OBJECTFACTORY_H
#define SFLPHONEGUI_OBJECTFACTORY_H
#include <list>
#include <map>
#include <string>
/**
* This is the base class that we will use to
* create an object from the "create" function.
*/
template< typename Base >
class ObjectCreatorBase
{
public:
virtual Base *create(const std::string &command,
const std::string &sequenceId,
const std::list< std::string > &args) = 0;
virtual ObjectCreatorBase *clone() = 0;
};
/**
* This is the actual class that will create
* the request. It will return a Request
*/
template< typename Base, typename Actual >
class ObjectCreator : public ObjectCreatorBase< Base >
{
public:
virtual Actual *create(const std::string &command,
const std::string &sequenceId,
const std::list< std::string > &args);
virtual ObjectCreatorBase< Base > *clone();
};
/**
* This class is used to create object related to
* a string. However, thoses objects will be created
* with the default constructor.
*/
template< typename Base >
class ObjectFactory
{
public:
/**
* Ask for a new object linked to the string.
*/
Base *create(const std::string &requestname,
const std::string &sequenceId,
const std::list< std::string > &args);
/**
* Register the string to return a Actual type.
*/
template< typename Actual >
void registerObject(const std::string &name);
private:
std::map< std::string, ObjectCreatorBase< Base > * > mObjectCreators;
};
#include "objectfactory.inl"
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_OBJECTFACTORY_H
#define SFLPHONEGUI_OBJECTFACTORY_H
#include <list>
#include <map>
#include <string>
/**
* This is the base class that we will use to
* create an object from the "create" function.
*/
template< typename Base >
class ObjectCreatorBase
{
public:
virtual Base *create(const std::string &command,
const std::string &sequenceId,
const std::list< std::string > &args) = 0;
virtual ObjectCreatorBase *clone() = 0;
};
/**
* This is the actual class that will create
* the request. It will return a Request
*/
template< typename Base, typename Actual >
class ObjectCreator : public ObjectCreatorBase< Base >
{
public:
virtual Actual *create(const std::string &command,
const std::string &sequenceId,
const std::list< std::string > &args);
virtual ObjectCreatorBase< Base > *clone();
};
/**
* This class is used to create object related to
* a string. However, thoses objects will be created
* with the default constructor.
*/
template< typename Base >
class ObjectFactory
{
public:
/**
* Ask for a new object linked to the string.
*/
Base *create(const std::string &requestname,
const std::string &sequenceId,
const std::list< std::string > &args);
/**
* Register the string to return a Actual type.
*/
template< typename Actual >
void registerObject(const std::string &name);
private:
std::map< std::string, ObjectCreatorBase< Base > * > mObjectCreators;
typedef std::map< std::string, ObjectCreatorBase< Base > * >::iterator iterator;
};
#include "objectfactory.inl"
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_OBJECTFACTORY_INL
#define SFLPHONEGUI_OBJECTFACTORY_INL
#include <stdexcept>
template< typename Base, typename Actual >
Actual *
ObjectCreator< Base, Actual >::create(const std::string &command,
const std::string &sequenceId,
const std::list< std::string > &args)
{
return new Actual(command, sequenceId, args);
}
template< typename Base, typename Actual >
ObjectCreatorBase< Base > *
ObjectCreator< Base, Actual >::clone()
{
return new ObjectCreator< Base, Actual >();
}
template< typename Base >
Base *
ObjectFactory< Base >::create(const std::string &command,
const std::string &sequenceId,
const std::list< std::string > &args)
{
typename std::map< std::string, ObjectCreatorBase< Base > * >::iterator pos = mObjectCreators.find(command);
if(pos == mObjectCreators.end()) {
throw std::runtime_error("The command \"" + command + "\" has no creator registered.");
}
return pos->second->create(command, sequenceId, args);
}
template< typename Base >
template< typename Actual >
void
ObjectFactory< Base >::registerObject(const std::string &name)
{
if(mObjectCreators.find(name) != mObjectCreators.end()) {
delete mObjectCreators[name];
}
mObjectCreators[name] = new ObjectCreator< Base, Actual >();
}
#endif
......@@ -47,5 +47,7 @@ class ObjectPool
QWaitCondition mDataAvailable;
};
#include "objectpool.inl"
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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
template< typename T >
void
ObjectPool< T >::push(const T &value)
{
QMutexLocker guard(&mMutex);
mPool.push_back(value);
mDataAvailable.wakeOne();
}
template< typename T >
T
ObjectPool< T >::pop()
{
QMutexLocker guard(&mMutex);
while(mPool.begin() == mPool.end()) {
mDataAvailable.wait(guard.mutex());
}
typename std::list< T >::iterator pos = mPool.begin();
mPool.pop_front();
return (*pos);
}
#endif
......@@ -47,12 +47,13 @@ std::string
Request::toString()
{
std::ostringstream id;
id << mCommand << mSequenceId;
id << mCommand << " " << mSequenceId;
for(std::list< std::string >::const_iterator pos = mArgs.begin();
pos != mArgs.end();
pos++) {
id << " " << (*pos);
}
id << std::endl;
return id.str();
}
......@@ -104,3 +105,49 @@ void
CallRequest::onSuccess(Call, const std::string &, const std::string &)
{}
AccountRequest::AccountRequest(const std::string &sequenceId,
const std::string &command,
const std::list< std::string > &args)
: Request(sequenceId, command, args)
, mAccountId(*args.begin())
{}
void
AccountRequest::onError(const std::string &code, const std::string &message)
{
onError(Account(Requester::instance().getSessionIdFromSequenceId(getSequenceId()),
mAccountId),
code,
message);
}
void
AccountRequest::onError(Account, const std::string &, const std::string &)
{}
void
AccountRequest::onEntry(const std::string &code, const std::string &message)
{
onEntry(Account(Requester::instance().getSessionIdFromSequenceId(getSequenceId()),
mAccountId),
code,
message);
}
void
AccountRequest::onEntry(Account, const std::string &, const std::string &)
{}
void
AccountRequest::onSuccess(const std::string &code, const std::string &message)
{
onSuccess(Account(Requester::instance().getSessionIdFromSequenceId(getSequenceId()),
mAccountId),
code,
message);
}
void
AccountRequest::onSuccess(Account, const std::string &, const std::string &)
{}
......@@ -24,6 +24,8 @@
#include <list>
#include <string>
#include "account.h"
#include "call.h"
class Request
......@@ -151,4 +153,71 @@ class CallRequest : public Request
const std::string mCallId;
};
class AccountRequest : public Request
{
public:
AccountRequest(const std::string &sequenceId,
const std::string &command,
const std::list< std::string > &args);
/**
* This function will be called when the request
* receive its answer, if the request didn't successfully
* ended.
*/
virtual void onError(Account account,
const std::string &code,
const std::string &message);
/**
* This function will be called when the request
* receive an answer, but there's other answers to come.
*/
virtual void onEntry(Account account,
const std::string &code,
const std::string &message);
/**
* This function will be called when the request
* receive its answer, if the request successfully
* ended.
*/
virtual void onSuccess(Account account,
const std::string &code,
const std::string &message);
private:
/**
* This function will be called when the request
* receive its answer, if the request didn't successfully
* ended. This function will call the onError, but with
* the account linked to this request.
*/
virtual void onError(const std::string &code,
const std::string &message);
/**
* This function will be called when the request
* receive its answer, if the there's other answer to
* come. This function will call the onEntry, but with
* the account linked to this request.
*/
virtual void onEntry(const std::string &code,
const std::string &message);
/**
* This function will be called when the request
* receive its answer, if the request successfully
* ended. This function will call the onSuccess function,
* but with the account linked to this request.
*/
virtual void onSuccess(const std::string &code,
const std::string &message);
private:
const std::string mAccountId;
};
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author : Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_REQUESTER_H
#define SFLPHONEGUI_REQUESTER_H
#include "utilspp/Singleton.hpp"
#include "requesterimpl.h"
typedef utilspp::SingletonHolder< RequesterImpl > Requester;
#endif
......@@ -23,12 +23,15 @@
#include "requesterimpl.h"
#include "sessionio.h"
#include "answerreceiver.h"
RequesterImpl::RequesterImpl()
: mCallIdCount(0)
, mSessionIdCount(0)
, mSequenceIdCount(0)
{}
{
registerObject< AccountRequest >(std::string("register"));
}
SessionIO *
RequesterImpl::getSessionIO(const std::string &sessionId)
......@@ -81,8 +84,11 @@ RequesterImpl::registerSession(const std::string &id,
throw std::logic_error("Registering an already know Session ID");
}
AnswerReceiver *a = new AnswerReceiver(this, s);
mAnswerReceivers.insert(std::make_pair(id, a));
mSessions.insert(std::make_pair(id, s));
s->start();
a->start();
}
int
......
......@@ -24,6 +24,7 @@
#include "request.h"
#include "objectfactory.h"
class AnswerReceiver;
class Call;
class SessionIO;
......@@ -97,6 +98,7 @@ class RequesterImpl
private:
ObjectFactory< Request > mRequestFactory;
std::map< std::string, SessionIO * > mSessions;
std::map< std::string, AnswerReceiver * > mAnswerReceivers;
std::map< std::string, Request * > mRequests;
std::map< std::string, std::string > mSequenceToSession;
......
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_REQUESTERIMPL_INL
#define SFLPHONEGUI_REQUESTERIMPL_INL
template< typename Actual >
void
RequesterImpl::registerObject(const std::string &name)
{
mRequestFactory.registerObject< Actual >(name);
}
#endif
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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.
*/
#include <iostream>
#include "session.h"
#include "requester.h"
#include "sessionio.h"
Session::Session(const std::string &id)
: mId(id)
{}
Session::Session()
{
mId = Requester::instance().generateSessionId();
SessionIO *s = new SessionIO(&std::cin, &std::cout);
Requester::instance().registerSession(mId, s);
}
Account
Session::getAccount(const std::string &name)
{
return Account(mId, name);
}
/**
* Copyright (C) 2004-2005 Savoir-Faire Linux inc.
* Author: Jean-Philippe Barrette-LaPierre
* <jean-philippe.barrette-lapierre@savoirfairelinux.com>
*
* 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_SESSION_H
#define SFLPHONEGUI_SESSION_H
#include <string>
#include "account.h"
class Session
{
public:
Session();
Session(const std::string &id);
/**
* retreive the account identified by name.
*/
Account getAccount(const std::string &name);
private:
std::string mId;
};
#endif
......@@ -37,7 +37,6 @@ class InputStreamer : public QThread
{
public:
InputStreamer(SessionIO *sessionIO);
~InputStreamer();
/**
* This is the main processing function.
......
#include <iostream>
#include <sstream>
#include <list>
#include <string>
int main(int, char**)
{
int code;
std::string seq;
std::string message;
std::istringstream s(std::string("100 seq12 Ma réponse"));
s >> code >> seq;
getline(s, message);
std::cout << "Code: " << code << std::endl;
std::cout << "Seq: " << seq << std::endl;
std::cout << "Message: " << message << std::endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment