IOTestWriteService.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 "IOTestWriteService.hpp"
00025 
00026 #include <iostream>
00027 #include <IOManager.hpp>
00028 #include <DeviceConfigurator.hpp>
00029 #include <StreamOperationHandlerMethod.hpp>
00030 #include <Exception.hpp>
00031 #include <cassert>
00032 
00033 using namespace components::ioservice;
00034 
00035 using mermaid::support::io::IOManager;
00036 using mermaid::support::io::DeviceConfigurator;
00037 using mermaid::support::io::StreamOperationHandlerMethod;
00038 using mermaid::support::io::StreamOperationHandlerMethodBase;
00039 using mermaid::support::errorhandling::Exception;
00040 
00041 #define PIPE_NAME "/tmp/test_write_pipe"
00042 #define FILE_NAME "test_file.txt"
00043 #define CHUNK_SIZE 100
00044 
00045 IOTestWriteService::IOTestWriteService (shared_ptr<ActiveObject> ao, shared_ptr<Entity> entity, shared_ptr<ServiceInstanceDescription> serviceInstanceDescription, shared_ptr<ServiceConfiguration> serviceConfiguration) : Service (ao, entity, serviceInstanceDescription, serviceConfiguration),
00046     device_ (IOManager::getInstance()->createDevice (PIPE_NAME, getActiveObject(), shared_ptr<DeviceConfigurator> (new DeviceConfigurator), 100)), file_ (FILE_NAME, fstream::in)
00047 {
00048   assert (file_.is_open());
00049   assert (file_.good());
00050 }; // IOTestWriteService()
00051 
00052 void IOTestWriteService::initialize()
00053 {
00054   // device_->enableWrite();
00055   
00056   char* buf = new char[CHUNK_SIZE];
00057   file_.read (buf, CHUNK_SIZE);
00058   
00059   // not supposed to happen with this test_file.txt
00060   if (file_.eof())
00061     croak();
00062     
00063   device_->write (file_.gcount(), buf, shared_ptr<StreamOperationHandlerMethodBase> (new StreamOperationHandlerMethod<IOTestWriteService> (this, &IOTestWriteService::handler)));
00064   
00065   delete[] buf;
00066 }; // initialize
00067 
00068 void IOTestWriteService::update()
00069 {
00070 }; // update
00071 
00072 void IOTestWriteService::handler (size_t bytesWritten, size_t requestedBytes, const char* data, bool success)
00073 {
00074 
00075   if (file_.eof())
00076     endTest();
00077     
00078   char *buf = new char[CHUNK_SIZE];
00079   
00080   file_.read (buf, CHUNK_SIZE);
00081   
00082   //printf("buf: %s\n", buf);
00083   device_->write (file_.gcount(), buf, shared_ptr<StreamOperationHandlerMethodBase> (new StreamOperationHandlerMethod<IOTestWriteService> (this, &IOTestWriteService::handler)));
00084   
00085   delete[] buf;
00086 }
00087 
00088 void IOTestWriteService::croak()
00089 {
00090   // assert(0);
00091   exit (-1);
00092 }
00093 
00094 void IOTestWriteService::endTest()
00095 {
00096   file_.close();
00097   device_->close();
00098   getActiveObject()->stop();
00099   IOManager::getInstance()->getIOThread()->getActiveObject()->stop();
00100 }
00101 
00102 IOTestWriteService::~IOTestWriteService()
00103 {
00104 }
00105 
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3