diff --git a/src/gui/official/requesterimpl.cpp b/src/gui/official/requesterimpl.cpp index aaab0f920ce19608ae79022cf77dfd883f6fb819..85dee2c5b80572fcd48c9ef36ee11cc7acdfc746 100644 --- a/src/gui/official/requesterimpl.cpp +++ b/src/gui/official/requesterimpl.cpp @@ -21,6 +21,7 @@ #include <stdexcept> #include "requesterimpl.h" +#include "sessionio.h" RequesterImpl::RequesterImpl() : mCallIdCount(0) @@ -45,7 +46,7 @@ RequesterImpl::sendCallCommand(const std::string &sessionId, const std::string &command) { // We retreive the internal of a session. - SessionSender *session = getSessionSender(sessionId); + SessionIO *session = getSessionIO(sessionId); // We ask the factory to create the request. Request *request = mCallRequestFactory.create(command, sequenceId, callId) @@ -58,7 +59,68 @@ RequesterImpl::sendCallCommand(const std::string &sessionId, } void -RequesterImpl::receiveCallCommand +RequesterImpl::registerRequest(const std::string &sessionId, + const std::string &sequenceId, + Request *request) +{ + if(mRequests.find(sequenceId) != mRequests.end()) { + throw std::logic_error("Registering an already know sequence ID"); + } + + mRequests.insert(std::make_pair(sequenceId, request)); +} + +void +RequesterImpl::receiveAnswer(const std::string &answer) +{ + std::string code; + std::string seq; + std::string message; + std::istream s(answer); + s >> code >> seq; + getline(s, message); + receiveAnswer(code, seq, message); +} + +int +RequesterImpl::getCodeCategory(const std::string &code) +{ + int c; + std::istream s(code); + s >> c; + return c / 100; +} + +void +RequesterImpl::receiveAnswer(const std::string &code, + const std::string &sequence, + const std::string &message) +{ + c = getCodeCategory(code); + + std::map< std::string, Request * >::iterator pos; + pos = mRequests.find(sequence); + if(pos == mRequests.end()) { + std::cerr << "We received an answer with an unknown sequence" << std::endl; + return; + } + + if(c <= 1) { + //Other answers will come for this request. + (*pos)->onEntry(code, message); + } + else{ + //This is the final answer of this request. + if(c == 2) { + (*pos)->onSuccess(code, message); + } + else { + (*pos)->onError(code, message); + } + delete(*pos); + mRequests.erase(pos); + } +} std::string RequesterImpl::generateCallId() diff --git a/src/gui/official/requesterimpl.h b/src/gui/official/requesterimpl.h index bb452ab4a1e2b8ae1fe690764b3cf250837787cd..e7c7ffc93262c8b9f8cddfef1ded233755463e8a 100644 --- a/src/gui/official/requesterimpl.h +++ b/src/gui/official/requesterimpl.h @@ -41,7 +41,7 @@ class RequesterImpl const std::string &callId, char *command); - + static int getCodeCategory(const std::string &code); private: /** @@ -64,9 +64,15 @@ class RequesterImpl */ std::string generateSequenceId(); + /** + * Return the SessionIO instance related to + * the session ID. + */ + SessionIO *getSessionIO(const std::string &sessionId); private: std::map< std::string, SessionIO * > mSessions; + std::map< std::string, Request * > mRequests; /**