IOTestWriteService.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 "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 };
00051
00052 void IOTestWriteService::initialize()
00053 {
00054
00055
00056 char* buf = new char[CHUNK_SIZE];
00057 file_.read (buf, CHUNK_SIZE);
00058
00059
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 };
00067
00068 void IOTestWriteService::update()
00069 {
00070 };
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
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
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