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

Integration of the ArgTokenizer Class
The argument are now inside a string list
And Request Classes throw exception when their arguments are wrong
parent 28988916
Branches
Tags
No related merge requests found
...@@ -6,8 +6,7 @@ SUBDIRS = $(officialdir) qt server ...@@ -6,8 +6,7 @@ SUBDIRS = $(officialdir) qt server
noinst_LTLIBRARIES = libguiframework.la noinst_LTLIBRARIES = libguiframework.la
libguiframework_la_SOURCES = \ libguiframework_la_SOURCES = guiframework.cpp guiframework.h
guiframework.cpp guiframework.h
libguiframework_la_CPPFLAGS = -I$(top_srcdir) libguiframework_la_CPPFLAGS = -I$(top_srcdir)
libguiframework_la_CXXFLAGS = $(libccext2_CFLAGS) libguiframework_la_CXXFLAGS = $(libccext2_CFLAGS)
......
...@@ -8,4 +8,5 @@ libsflphoneguiserver_la_LIBADD = ...@@ -8,4 +8,5 @@ libsflphoneguiserver_la_LIBADD =
AM_CPPFLAGS = -I$(top_srcdir) $(libccext2_CFLAGS) AM_CPPFLAGS = -I$(top_srcdir) $(libccext2_CFLAGS)
noinst_HEADERS = responsemessage.h request.h requestfactory.h subcall.h noinst_HEADERS = responsemessage.h request.h requestfactory.h subcall.h \
argtokenizer.h
...@@ -21,8 +21,11 @@ ...@@ -21,8 +21,11 @@
#define __REQUEST_H__ #define __REQUEST_H__
#include <string> #include <string>
#include <list>
#include "responsemessage.h" #include "responsemessage.h"
typedef std::list<std::string> TokenList;
/** /**
Request are received from the client Request are received from the client
and execute on the server and execute on the server
...@@ -30,10 +33,14 @@ Request execution always return a ResponseMessage ...@@ -30,10 +33,14 @@ Request execution always return a ResponseMessage
@author Yan Morin @author Yan Morin
*/ */
class GUIServer; class GUIServer;
class RequestConstructorException {
};
class Request class Request
{ {
public: public:
Request(const std::string &sequenceId, const std::string &arg) : _sequenceId(sequenceId), _arg(arg) {} Request(const std::string &sequenceId, const TokenList& argList) : _sequenceId(sequenceId), _argList(argList) {}
virtual ~Request() {} virtual ~Request() {}
virtual ResponseMessage execute(GUIServer& gui) = 0; virtual ResponseMessage execute(GUIServer& gui) = 0;
ResponseMessage message(const std::string &code, const std::string &message) { ResponseMessage message(const std::string &code, const std::string &message) {
...@@ -44,99 +51,114 @@ public: ...@@ -44,99 +51,114 @@ public:
protected: protected:
std::string _sequenceId; std::string _sequenceId;
std::string _arg; TokenList _argList;
}; };
class RequestGlobalCall : public Request class RequestCall : public Request {
{
public: public:
RequestGlobalCall(const std::string &sequenceId, const std::string &arg) : Request(sequenceId,arg) { RequestCall(const std::string &sequenceId, const TokenList& argList) : Request(sequenceId,argList) {
unsigned int spacePos = _arg.find(' '); TokenList::iterator iter = _argList.begin();
if (spacePos == std::string::npos) { // check for the callid
// only one argument, so it's must be the callId bool argsAreValid = false;
_callId = _arg; // Args are: account callid destination
} else { // acc1000 c10345 sip:test@test.com
_callId = _arg.substr(0, spacePos); if (iter != _argList.end() && iter->find("acc")==0) {
_arg = _arg.substr(spacePos+1, _arg.size()-spacePos+1); _account = *iter;
_argList.pop_front();
iter = _argList.begin();
// a call method can only begin by 'c' since it's only the client that
// call with method
if (iter != _argList.end() && (*iter)[0]=='c') {
_callId = *iter;
iter++;
// last arg is the destination
if (iter != _argList.end()) {
_destination = *iter;
argsAreValid = true;
} }
} }
virtual ~RequestGlobalCall() {} }
virtual ResponseMessage execute(GUIServer& gui) { return message("200","OK"); } if (!argsAreValid) {
throw RequestConstructorException();
}
}
virtual ResponseMessage execute(GUIServer& gui);
protected: private:
std::string _callId; std::string _callId;
std::string _destination;
std::string _account;
}; };
class RequestCall : public RequestGlobalCall {
class RequestGlobalCall : public Request
{
public: public:
RequestCall(const std::string &sequenceId, const std::string &arg) : RequestGlobalCall(sequenceId,arg) { RequestGlobalCall(const std::string &sequenceId, const TokenList& argList) : Request(sequenceId, argList) {
// only one argument, so it's must be the account (that switch is JP fault) TokenList::iterator iter = _argList.begin();
_account = _callId; if (iter != _argList.end() && ((*iter)[0]=='c' || (*iter)[0]=='s') ) {
_callId = ""; _callId = *iter;
unsigned int spacePos = _arg.find(' '); _argList.pop_front();
if (spacePos == std::string::npos) {
_callId = _arg;
} else { } else {
_callId = _arg.substr(0, spacePos); throw RequestConstructorException();
_destination = _arg.substr(spacePos+1, _arg.size()-spacePos+1);
} }
} }
virtual ResponseMessage execute(GUIServer& gui); virtual ~RequestGlobalCall() {}
virtual ResponseMessage execute(GUIServer& gui) { return message("200","OK"); }
private: protected:
std::string _destination; std::string _callId;
std::string _account;
}; };
class RequestAnswer : public RequestGlobalCall { class RequestAnswer : public RequestGlobalCall {
public: public:
RequestAnswer(const std::string &sequenceId, const std::string &arg) : RequestGlobalCall(sequenceId,arg) {} RequestAnswer(const std::string &sequenceId, const TokenList& argList) : RequestGlobalCall(sequenceId,argList) {}
}; };
class RequestRefuse : public RequestGlobalCall { class RequestRefuse : public RequestGlobalCall {
public: public:
RequestRefuse(const std::string &sequenceId, const std::string &arg) : RequestGlobalCall(sequenceId,arg) {} RequestRefuse(const std::string &sequenceId, const TokenList& argList) : RequestGlobalCall(sequenceId,argList) {}
}; };
class RequestHold : public RequestGlobalCall { class RequestHold : public RequestGlobalCall {
public: public:
RequestHold(const std::string &sequenceId, const std::string &arg) : RequestGlobalCall(sequenceId,arg) {} RequestHold(const std::string &sequenceId, const TokenList& argList) : RequestGlobalCall(sequenceId,argList) {}
}; };
class RequestUnhold : public RequestGlobalCall { class RequestUnhold : public RequestGlobalCall {
public: public:
RequestUnhold(const std::string &sequenceId, const std::string &arg) : RequestGlobalCall(sequenceId,arg) {} RequestUnhold(const std::string &sequenceId, const TokenList& argList) : RequestGlobalCall(sequenceId,argList) {}
}; };
class RequestTransfer : public RequestGlobalCall { class RequestTransfer : public RequestGlobalCall {
public: public:
RequestTransfer(const std::string &sequenceId, const std::string &arg) : RequestGlobalCall(sequenceId,arg) {} RequestTransfer(const std::string &sequenceId, const TokenList& argList) : RequestGlobalCall(sequenceId,argList) {}
}; };
class RequestGlobal : public Request class RequestGlobal : public Request
{ {
public: public:
RequestGlobal(const std::string &sequenceId, const std::string &arg) : Request(sequenceId,arg) {} RequestGlobal(const std::string &sequenceId, const TokenList& argList) : Request(sequenceId,argList) {}
virtual ~RequestGlobal() {} virtual ~RequestGlobal() {}
virtual ResponseMessage execute(GUIServer& gui) { return message("200","OK"); } virtual ResponseMessage execute(GUIServer& gui) { return message("200","OK"); }
}; };
class RequestMute : public RequestGlobal { class RequestMute : public RequestGlobal {
public: public:
RequestMute(const std::string &sequenceId, const std::string &arg) : RequestGlobal(sequenceId,arg) {} RequestMute(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList) {}
}; };
class RequestUnmute : public RequestGlobal { class RequestUnmute : public RequestGlobal {
public: public:
RequestUnmute(const std::string &sequenceId, const std::string &arg) : RequestGlobal(sequenceId,arg) {} RequestUnmute(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList) {}
}; };
class RequestQuit : public RequestGlobal { class RequestQuit : public RequestGlobal {
public: public:
RequestQuit(const std::string &sequenceId, const std::string &arg) : RequestGlobal(sequenceId,arg) {} RequestQuit(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList) {}
}; };
class RequestSyntaxError : public Request class RequestSyntaxError : public Request
{ {
public: public:
RequestSyntaxError(const std::string &sequenceId, const std::string &arg) : Request(sequenceId, arg) {} RequestSyntaxError(const std::string &sequenceId, const TokenList& argList) : Request(sequenceId, argList) {}
~RequestSyntaxError() {} ~RequestSyntaxError() {}
ResponseMessage execute(GUIServer& gui) { ResponseMessage execute(GUIServer& gui) {
return message("501", "Syntax Error"); return message("501", "Syntax Error");
......
...@@ -25,46 +25,37 @@ ...@@ -25,46 +25,37 @@
Request * Request *
RequestFactory::create(const std::string& requestLine) RequestFactory::create(const std::string& requestLine)
{ {
std::string requestName;
std::string sequenceId="seq0";
std::string arguments;
unsigned int spacePos = requestLine.find(' '); TokenList tList = _tokenizer.tokenize(requestLine);
// we find a spacePos TokenList::iterator iter = tList.begin();
if (spacePos != std::string::npos) { // there is atleast one token (the command)
/* if (iter != tList.end()) {
012345678901234 std::string requestName = *iter;
call seq1 cdddd tList.pop_front();
spacePos = 4 iter = tList.begin();
spacePos2 = 9 // there is atleast a second token (the sequenceId)
0 for 4 = 0 for spacePos if (iter != tList.end() && iter->find("seq") == 0 ) {
5 for 4 = (spacePos+1 for spacePos2-spacePos-1) std::string sequenceId = *iter;
10 for 5 = (spacePos2+1 for size - spacePos2+1) tList.pop_front();
*/ try {
requestName = requestLine.substr(0, spacePos); Request *r = create(requestName, sequenceId, tList);
return r;
unsigned int spacePos2 = requestLine.find(' ', spacePos+1); } catch (...) {
if (spacePos2 == std::string::npos) { // if the create return an exception
// command that end with a sequence number // we create a syntaxerror
sequenceId = requestLine.substr(spacePos+1, requestLine.size()-spacePos+1);
} else {
sequenceId = requestLine.substr(spacePos+1, spacePos2-spacePos-1);
arguments = requestLine.substr(spacePos2+1, requestLine.size()-spacePos2+1);
} }
} else {
requestName = "syntaxerror";
} }
}
return create(requestName, sequenceId, arguments); return create("syntaxerror", "seq0", tList);
} }
Request * Request *
RequestFactory::create( RequestFactory::create(
const std::string &requestname, const std::string& requestName,
const std::string& sequenceId, const std::string& sequenceId,
const std::string &arg) const TokenList& argList)
{ {
std::map< std::string, RequestCreatorBase * >::iterator pos = mRequests.find(requestname); std::map< std::string, RequestCreatorBase * >::iterator pos = mRequests.find(requestName);
if(pos == mRequests.end()) { if(pos == mRequests.end()) {
pos = mRequests.find("syntaxerror"); pos = mRequests.find("syntaxerror");
if(pos == mRequests.end()) { if(pos == mRequests.end()) {
...@@ -72,7 +63,7 @@ RequestFactory::create( ...@@ -72,7 +63,7 @@ RequestFactory::create(
} }
} }
return pos->second->create(sequenceId, arg); return pos->second->create(sequenceId, argList);
} }
template< typename T > template< typename T >
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <map> #include <map>
#include "request.h" #include "request.h"
#include "argtokenizer.h"
class Request; class Request;
/** /**
...@@ -30,7 +31,7 @@ class Request; ...@@ -30,7 +31,7 @@ class Request;
class RequestCreatorBase class RequestCreatorBase
{ {
public: public:
virtual Request *create(const std::string &sequenceId, const std::string &arg) = 0; virtual Request *create(const std::string &sequenceId, const TokenList& argList) = 0;
virtual RequestCreatorBase *clone() = 0; virtual RequestCreatorBase *clone() = 0;
}; };
...@@ -38,9 +39,9 @@ template< typename T > ...@@ -38,9 +39,9 @@ template< typename T >
class RequestCreator : public RequestCreatorBase class RequestCreator : public RequestCreatorBase
{ {
public: public:
virtual Request *create(const std::string &sequenceId, const std::string &arg) virtual Request *create(const std::string &sequenceId, const TokenList& argList)
{ {
return new T(sequenceId, arg); return new T(sequenceId, argList);
} }
virtual RequestCreatorBase *clone() virtual RequestCreatorBase *clone()
...@@ -57,13 +58,14 @@ public: ...@@ -57,13 +58,14 @@ public:
Request *create( Request *create(
const std::string& requestname, const std::string& requestname,
const std::string& sequenceId, const std::string& sequenceId,
const std::string &arg); const TokenList& argList);
template< typename T > template< typename T >
void registerRequest(const std::string& requestname); void registerRequest(const std::string& requestname);
void registerAll(); void registerAll();
private: private:
std::map< std::string, RequestCreatorBase * > mRequests; std::map< std::string, RequestCreatorBase * > mRequests;
ArgTokenizer _tokenizer;
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment