diff --git a/src/FileManager.cpp b/src/FileWrapper.cpp similarity index 65% rename from src/FileManager.cpp rename to src/FileWrapper.cpp index e10e0ff068fda8853fa0462417ee92746a791582..e2b49f89a6b67e0adc2cf8f82aa25df4072e5fea 100644 --- a/src/FileManager.cpp +++ b/src/FileWrapper.cpp @@ -1,16 +1,15 @@ -#include <FileManager.h> -#include <stdexcept> +#include <FileWrapper.h> namespace DataExporter_ns { //============================================================================== -// FileManager::FileManager() +// FileWrapper::FileWrapper() //============================================================================== -FileManager::FileManager(Tango::DeviceImpl* deviceImpl_p, +FileWrapper::FileWrapper(Tango::DeviceImpl* deviceImpl_p, boost::filesystem::path& path) : Tango::LogAdapter(deviceImpl_p) { - DEBUG_STREAM << "FileManager::FileManager()" << endl; + DEBUG_STREAM << "FileWrapper::FileWrapper()" << endl; m_inputFileStream.open(path.string(), std::ios::binary | std::ios::ate); @@ -20,51 +19,56 @@ FileManager::FileManager(Tango::DeviceImpl* deviceImpl_p, } //============================================================================== -// FileManager::~FileManager() +// FileWrapper::~FileWrapper() //============================================================================== -FileManager::~FileManager() +FileWrapper::~FileWrapper() { - DEBUG_STREAM << "FileManager::~FileManager()" << endl; + DEBUG_STREAM << "FileWrapper::~FileWrapper()" << endl; m_inputFileStream.close(); } //============================================================================== -// FileManager::create() +// FileWrapper::create() //============================================================================== -FileManager::SP FileManager::create(Tango::DeviceImpl* deviceImpl_p, +FileWrapper::SP FileWrapper::create(Tango::DeviceImpl* deviceImpl_p, boost::filesystem::path& path) { - FileManager::SP d_sp(new FileManager(deviceImpl_p, path), - FileManager::Deleter()); + FileWrapper::SP d_sp(new FileWrapper(deviceImpl_p, path), + FileWrapper::Deleter()); return d_sp; } //============================================================================== -// FileManager::isOpen() +// FileWrapper::isOpen() //============================================================================== -bool FileManager::isOpen() +bool FileWrapper::isOpen() { return m_inputFileStream.is_open(); } //============================================================================== -// FileManager::isCompleted() +// FileWrapper::isBad() //============================================================================== -bool FileManager::isCompleted() +bool FileWrapper::isBad() { - return m_inputFileStream.tellg() >= m_inputFileSize; + return m_inputFileStream.bad(); } //============================================================================== -// FileManager::read() +// FileWrapper::isCompleted() //============================================================================== -void FileManager::read(std::vector<char>& writeBuff) throw(std::runtime_error) +bool FileWrapper::isCompleted() { - if(m_inputFileStream.tellg() >= m_inputFileSize) - throw std::runtime_error("Already completed"); + return m_inputFileStream.tellg() >= m_inputFileSize; +} +//============================================================================== +// FileWrapper::read() +//============================================================================== +void FileWrapper::read(std::vector<char>& writeBuff) throw(std::runtime_error) +{ int leftToRead = m_inputFileSize - m_inputFileStream.tellg(); int bufferSize = 0; diff --git a/src/FileManager.h b/src/FileWrapper.h similarity index 79% rename from src/FileManager.h rename to src/FileWrapper.h index 58aff7dc50c5e7b8fac940929e80f45b5e2cc60f..46c711d52c579a9d3be5b8d636461fa1e0e01fa8 100644 --- a/src/FileManager.h +++ b/src/FileWrapper.h @@ -1,5 +1,5 @@ -#ifndef FILEMANAGER_H -#define FILEMANAGER_H +#ifndef FILEWRAPPER_H +#define FILEWRAPPER_H #include <Configuration.h> @@ -10,41 +10,43 @@ namespace DataExporter_ns { -class FileManager : public Tango::LogAdapter +class FileWrapper : public Tango::LogAdapter { public: //------------------------------------------------------------------------------ // [Public] Shared pointer typedef //------------------------------------------------------------------------------ - typedef boost::shared_ptr<FileManager> SP; + typedef boost::shared_ptr<FileWrapper> SP; protected: //------------------------------------------------------------------------------ // [Protected] Constructor destructor deleter //------------------------------------------------------------------------------ - FileManager(Tango::DeviceImpl*, boost::filesystem::path&); + FileWrapper(Tango::DeviceImpl*, boost::filesystem::path&); - virtual ~FileManager(); + virtual ~FileWrapper(); class Deleter; friend Deleter; class Deleter { public: - void operator()(FileManager* d) { delete d; } + void operator()(FileWrapper* d) { delete d; } }; public: //------------------------------------------------------------------------------ // [Public] Class creation method //------------------------------------------------------------------------------ - static FileManager::SP create(Tango::DeviceImpl*, boost::filesystem::path&); + static FileWrapper::SP create(Tango::DeviceImpl*, boost::filesystem::path&); //------------------------------------------------------------------------------ // [Public] Input stream methods //------------------------------------------------------------------------------ virtual bool isOpen(); + virtual bool isBad(); + virtual bool isCompleted(); virtual void read(std::vector<char>&) throw(std::runtime_error); @@ -65,4 +67,4 @@ protected: } //End of namespace -#endif /* FILEMANAGER_H */ +#endif /* FILEWRAPPER_H */ diff --git a/src/PlainSession.cpp b/src/PlainSession.cpp index a8c6a631e947815323bda5ac005b189c877c6980..459de6bfd7724ed1de308068e3c48733c35b0243 100644 --- a/src/PlainSession.cpp +++ b/src/PlainSession.cpp @@ -154,52 +154,40 @@ void PlainSession::startWriteResponse() { ERROR_STREAM << "SSLSession::startWriteResponse() unknown error from " << m_remoteEndpoint << endl; - } } //============================================================================== // PlainSession::startWriteData() //============================================================================== -void PlainSession::startWriteData() +void PlainSession::startWriteData(FileWrapper::SP fileWrapper_sp) { try { - if(!m_inputStream.bad()) + if(!fileWrapper_sp->isBad()) { - if(m_inputStream.tellg()<m_inputStreamSize) + if(!fileWrapper_sp->isCompleted()) { - int leftToRead = m_inputStreamSize - m_inputStream.tellg(); - - int bufferSize = 0; - - if(leftToRead < BUFFER_SIZE) - bufferSize = leftToRead; - else - bufferSize = BUFFER_SIZE; - std::vector<char> writeBuff; - writeBuff.resize(bufferSize); - m_inputStream.read(&writeBuff[0], bufferSize); + fileWrapper_sp->read(writeBuff); boost::asio::async_write(m_plainSocket, boost::asio::buffer(writeBuff), m_strand.wrap(boost::bind(&PlainSession::handleWriteData, - shared_from_this(), boost::asio::placeholders::error))); + shared_from_this(), fileWrapper_sp, + boost::asio::placeholders::error))); } else { - INFO_STREAM << "SSLSession::startWriteData() " + INFO_STREAM << "PlainSession::startWriteData() " << " transfer completed " << endl; - m_inputStream.close(); - startReadRequestHeader(); } } else { - ERROR_STREAM << "SSLSession::startWriteData() error on file I/O " + ERROR_STREAM << "PlainSession::startWriteData() error on file I/O " << "from " << m_remoteEndpoint << endl; } } diff --git a/src/PlainSession.h b/src/PlainSession.h index 73ef35e41af71ce2aa5d47937b421e07fe766c3f..a0fa74af35038ae64f5c5a2e1bd0ebbe035a80e2 100644 --- a/src/PlainSession.h +++ b/src/PlainSession.h @@ -54,7 +54,7 @@ protected: virtual void startWriteResponse(); - virtual void startWriteData(); + virtual void startWriteData(FileWrapper::SP); //------------------------------------------------------------------------------ // [Protected] Class variables diff --git a/src/ProtocolManager.cpp b/src/ProtocolManager.cpp index d7da7eb88fc3e73eadfd53f07a32d9c5b017e135..1254e2e6f51144e1f8ebd0689b320b115992bf88 100644 --- a/src/ProtocolManager.cpp +++ b/src/ProtocolManager.cpp @@ -11,8 +11,8 @@ namespace DataExporter_ns //============================================================================== ProtocolManager::ProtocolManager(Tango::DeviceImpl* deviceImpl_p, Configuration::SP configuration_sp, DBManager::SP dBManager_sp) : - Tango::LogAdapter(deviceImpl_p), m_configuration_sp(configuration_sp), - m_dBManager_sp(dBManager_sp) + Tango::LogAdapter(deviceImpl_p), m_deviceImpl_p(deviceImpl_p), + m_configuration_sp(configuration_sp), m_dBManager_sp(dBManager_sp) { DEBUG_STREAM << "ProtocolManager::ProtocolManager()" << endl; } @@ -55,14 +55,17 @@ ResponseSP ProtocolManager::prepareResponse(RequestSP request_sp) { DEBUG_STREAM << "ProtocolManager::prepareResponse()" << endl; + m_fileFound = false; + m_fileWrapper_sp.reset(); + if(!request_sp->IsInitialized()) throw std::runtime_error("Not initialized request!"); + ResponseSP response_sp(new Response()); + std::string username = request_sp->username(); std::string password = request_sp->password(); - ResponseSP response_sp(new Response()); - if(m_configuration_sp->isUserAuthorized(username, password)) { std::string schema = request_sp->schema(); @@ -91,8 +94,13 @@ ResponseSP ProtocolManager::prepareResponse(RequestSP request_sp) boost::uint64_t fileSize = boost::filesystem::file_size(absolutePath); - DEBUG_STREAM << "ProtocolManager::prepareResponse() " - << "file size " << fileSize << endl; + INFO_STREAM << "ProtocolManager::prepareResponse() " + << " transfer file " << fileName << " version " + << fileVersion << " size " << fileSize << endl; + + m_fileFound = true; + m_fileWrapper_sp = + FileWrapper::create(m_deviceImpl_p, absolutePath); response_sp->set_state(Response::REQUEST_ACCEPTED); response_sp->set_status("Request accepted"); @@ -153,6 +161,29 @@ ResponseSP ProtocolManager::prepareResponse(RequestSP request_sp) return response_sp; } +//============================================================================== +// ProtocolManager::isFileFound() +//============================================================================== +bool ProtocolManager::isFileFound() +{ + DEBUG_STREAM << "ProtocolManager::isFileFound()" << endl; + + return m_fileFound; +} + +//============================================================================== +// ProtocolManager::getFileWrapper() +//============================================================================== +FileWrapper::SP ProtocolManager::getFileWrapper() throw(std::runtime_error) +{ + DEBUG_STREAM << "ProtocolManager::getFileWrapper()" << endl; + + if(!m_fileWrapper_sp) + throw std::runtime_error("File wrapper not created"); + + return m_fileWrapper_sp; +} + //============================================================================== // ProtocolManager::composePath() //============================================================================== diff --git a/src/ProtocolManager.h b/src/ProtocolManager.h index b2468b738ebd9b8699f4384e66ccd25337fcef40..6f8d00d9867587fd304282b635a3e721f43f68e3 100644 --- a/src/ProtocolManager.h +++ b/src/ProtocolManager.h @@ -5,6 +5,7 @@ #include <Request.pb.h> #include <Configuration.h> #include <DBManager.h> +#include <FileWrapper.h> #include <tango.h> @@ -61,6 +62,13 @@ public: virtual ResponseSP prepareResponse(RequestSP) throw(std::runtime_error); +//------------------------------------------------------------------------------ +// [Public] File transfer methods +//------------------------------------------------------------------------------ + bool isFileFound(); + + FileWrapper::SP getFileWrapper() throw(std::runtime_error); + protected: //------------------------------------------------------------------------------ // [Protected] @@ -71,6 +79,9 @@ protected: //------------------------------------------------------------------------------ // [Protected] Class variables //------------------------------------------------------------------------------ + //Device implementation pointer + Tango::DeviceImpl* m_deviceImpl_p; + //Configuration parameters shared pointer Configuration::SP m_configuration_sp; @@ -79,6 +90,12 @@ protected: //Address and port of remote endpoint std::string m_remoteEndpoint; + + //File to transfer found + bool m_fileFound; + + //File to transfer wrapper + FileWrapper::SP m_fileWrapper_sp; }; } //End of namespace diff --git a/src/SSLSession.cpp b/src/SSLSession.cpp index 02a29b87e450687fbe64eab9454f573bcafa7a4f..379688a012c7bfc618ff109bec53fefdbf48efb3 100644 --- a/src/SSLSession.cpp +++ b/src/SSLSession.cpp @@ -196,39 +196,28 @@ void SSLSession::startWriteResponse() //============================================================================== // SSLSession::startWriteData() //============================================================================== -void SSLSession::startWriteData() +void SSLSession::startWriteData(FileWrapper::SP fileWrapper_sp) { try { - if(!m_inputStream.bad()) + if(!fileWrapper_sp->isBad()) { - if(m_inputStream.tellg() < m_inputStreamSize) + if(!fileWrapper_sp->isCompleted()) { - int leftToRead = m_inputStreamSize - m_inputStream.tellg(); - - int bufferSize = 0; - - if(leftToRead < BUFFER_SIZE) - bufferSize = leftToRead; - else - bufferSize = BUFFER_SIZE; - std::vector<char> writeBuff; - writeBuff.resize(bufferSize); - m_inputStream.read(&writeBuff[0], bufferSize); + fileWrapper_sp->read(writeBuff); boost::asio::async_write(m_sslSocket, boost::asio::buffer(writeBuff), m_strand.wrap(boost::bind(&SSLSession::handleWriteData, - shared_from_this(), boost::asio::placeholders::error))); + shared_from_this(), fileWrapper_sp, + boost::asio::placeholders::error))); } else { INFO_STREAM << "SSLSession::startWriteData() " << " transfer completed " << endl; - m_inputStream.close(); - startReadRequestHeader(); } } diff --git a/src/SSLSession.h b/src/SSLSession.h index 3fe39d44d8b4fe7d66ed53a6c47b3302a7065d22..a4ef0190f295d0cd467aba5953ba73648b7fe9b3 100644 --- a/src/SSLSession.h +++ b/src/SSLSession.h @@ -64,7 +64,7 @@ protected: virtual void startWriteResponse(); - virtual void startWriteData(); + virtual void startWriteData(FileWrapper::SP); //------------------------------------------------------------------------------ // [Protected] Class variables diff --git a/src/Session.cpp b/src/Session.cpp index 56af6c25eeffbd299e5183b9b50f7cb95e93d968..e0e2a388e727ea31287b1894b2932cc21b276a5d 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -82,14 +82,14 @@ void Session::handleWriteResponse(const boost::system::error_code& errorCode) if(!errorCode) { -// if() -// { -// //TODO: open file and start read -// } -// else -// { -// startReadRequestHeader(); -// } + if(m_protocolManager_sp->isFileFound()) + { + startWriteData(m_protocolManager_sp->getFileWrapper()); + } + else + { + startReadRequestHeader(); + } } else if(errorCode == boost::asio::error::eof) { @@ -106,11 +106,12 @@ void Session::handleWriteResponse(const boost::system::error_code& errorCode) //============================================================================== // Session::handleWriteData() //============================================================================== -void Session::handleWriteData(const boost::system::error_code& errorCode) +void Session::handleWriteData(FileWrapper::SP fileWrapper_sp, + const boost::system::error_code& errorCode) { if(!errorCode) { - startWriteData(); + startWriteData(fileWrapper_sp); } else if(errorCode == boost::asio::error::eof) { diff --git a/src/Session.h b/src/Session.h index 8ff8cc82be121d2b6a48d3b0eeffcff2f214ad9c..1ccae616e45de15a21a8e1bb2ec58dfa1d5630d1 100644 --- a/src/Session.h +++ b/src/Session.h @@ -4,6 +4,7 @@ #include <Configuration.h> #include <ProtocolManager.h> #include <DBManager.h> +#include <FileWrapper.h> #include <tango.h> @@ -64,9 +65,10 @@ protected: //------------------------------------------------------------------------------ // [Protected] Write data methods //------------------------------------------------------------------------------ - virtual void startWriteData() = 0; + virtual void startWriteData(FileWrapper::SP) = 0; - virtual void handleWriteData(const boost::system::error_code&); + virtual void handleWriteData(FileWrapper::SP, + const boost::system::error_code&); //------------------------------------------------------------------------------ // [Protected] Encode decode header methods @@ -97,13 +99,6 @@ protected: //Address and port of remote endpoint std::string m_remoteEndpoint; - - //TODO: to delete - const int BUFFER_SIZE = 1024; - - std::ifstream m_inputStream; - - int m_inputStreamSize; }; } //End of namespace