IOTestReadService.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00078 croak();
00079 }
00080
00081 if (fsize_ == 0) endTest();
00082 delete[] buf;
00083
00084 }
00085
00086 void IOTestReadService::croak()
00087 {
00088
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