IOTestReadService.cpp

00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 #include "config.h"
00023 
00024 #include "IOTestReadService.hpp"
00025 #include <iostream>
00026 #include <cassert>
00027 #include <IOManager.hpp>
00028 #include <DeviceConfigurator.hpp>
00029 #include <StreamOperationHandlerMethod.hpp>
00030 #include <Exception.hpp>
00031 
00032 using namespace components::ioservice;
00033 
00034 using mermaid::support::io::IOManager;
00035 using mermaid::support::io::DeviceConfigurator;
00036 using mermaid::support::io::StreamOperationHandlerMethod;
00037 using mermaid::support::io::StreamOperationHandlerMethodBase;
00038 using mermaid::support::errorhandling::Exception;
00039 
00040 #define PIPE_NAME "/tmp/test_read_pipe"
00041 #define FILE_NAME  "test_file.txt"
00042 
00043 IOTestReadService::IOTestReadService (shared_ptr<ActiveObject> ao, shared_ptr<Entity> entity, shared_ptr<ServiceInstanceDescription> serviceInstanceDescription, shared_ptr<ServiceConfiguration> serviceConfiguration) : Service (ao, entity, serviceInstanceDescription, serviceConfiguration),
00044     device_ (IOManager::getInstance()->createDevice (PIPE_NAME, getActiveObject(), shared_ptr<DeviceConfigurator> (new DeviceConfigurator), 100)), file_ (FILE_NAME, ifstream::in)
00045 {
00046   std::cerr << "IOTestRead constructor" << std::endl;
00047   assert (file_.good());
00048   file_.seekg (0, ios::end);
00049   fsize_ = file_.tellg();
00050   file_.seekg (0, ios::beg);
00051   
00052 };
00053 
00054 void IOTestReadService::initialize()
00055 {
00056   std::cerr << "IOTestRead initialized" << std::endl;
00057   device_->enableRead (shared_ptr<StreamOperationHandlerMethodBase> (new StreamOperationHandlerMethod<IOTestReadService> (this, &IOTestReadService::handler)));
00058 };
00059 
00060 void IOTestReadService::update()
00061 {
00062 };
00063 
00064 void IOTestReadService::handler (size_t bytesRead, size_t requestedBytes, const char* data, bool success)
00065 {
00066   if (bytesRead == 0)
00067     croak();
00068     
00069   char* buf = new char[bytesRead];
00070   assert (buf);
00071   
00072   file_.read (buf, bytesRead);
00073   fsize_ -= bytesRead;
00074   
00075   buf[bytesRead - 1] = 0;
00076   if (file_.gcount() != (int) bytesRead || !std::equal (buf, buf + bytesRead - 1, data)) {
00077     //std::cerr << data << " " << file_.gcount() << " " << bytesRead << " " << file_.fail() << " " << file_.eof() << std::endl << buf << std::endl;
00078     croak();
00079   }
00080   
00081   if (fsize_ == 0) endTest();
00082   delete[] buf;
00083   // puts(data);
00084 }
00085 
00086 void IOTestReadService::croak()
00087 {
00088   // there was an error somewhere
00089   assert (0);
00090 }
00091 
00092 IOTestReadService::~IOTestReadService()
00093 {
00094   file_.close();
00095 }
00096 
00097 void IOTestReadService::endTest()
00098 {
00099   device_->close();
00100   getActiveObject()->stop();
00101   IOManager::getInstance()->getIOThread()->getActiveObject()->stop();
00102 }
00103 
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3