Device.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 "Device.hpp"
00025 #include "DeviceConfigurator.hpp"
00026 #include "StreamReader.hpp"
00027 #include "StreamReadReplyTask.hpp"
00028 #include "StreamWriter.hpp"
00029 #include "StreamWriteReplyTask.hpp"
00030 #include "IOManager.hpp"
00031
00032 #include <cassert>
00033
00034 using namespace mermaid::support::io;
00035
00036 using boost::shared_ptr;
00037
00038 Device::~Device()
00039 {
00040
00041 }
00042
00043 Device::Device (int expectedDataSize, int fileDescriptor, shared_ptr<ActiveObject> owner, shared_ptr<DeviceConfigurator> deviceConfigurator) :
00044 expectedDataSize_ (expectedDataSize), fileDescriptor_ (fileDescriptor), readEnabled_ (false), writeEnabled_ (false), deviceConfigurator_ (deviceConfigurator),
00045 owner_ (owner), streamOperationHandlerRead_ ()
00046 {
00047 }
00048
00049 void Device::enableRead (shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerRead)
00050 {
00051 streamOperationHandlerRead_ = streamOperationHandlerRead;
00052 if (readEnabled_ == false) {
00053 shared_ptr<StreamReader> streamReader (new StreamReader (this));
00054 IOManager::getInstance()->getIOThread()->addStreamReader (streamReader);
00055 readEnabled_ = true;
00056 }
00057 }
00058
00059 void Device::enableWrite()
00060 {
00061 if (writeEnabled_ == false) {
00062 shared_ptr<StreamWriter> streamWriter (new StreamWriter (this));
00063 IOManager::getInstance()->getIOThread()->addStreamWriter (streamWriter);
00064 writeEnabled_ = true;
00065 }
00066 }
00067
00068 void Device::write (size_t bytesToWrite, const char* data, shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite)
00069 {
00070 enableWrite();
00071 assert (writeEnabled_ == true);
00072 IOManager::getInstance()->getIOThread()->addWriteOperation (fileDescriptor_, bytesToWrite, data, streamOperationHandlerWrite);
00073 }
00074
00075 void Device::callReadHandler (size_t bytesRead, size_t requestedBytes, const char* data, bool success)
00076 {
00077 owner_->addTask (shared_ptr<Task> (new StreamReadReplyTask (bytesRead, requestedBytes, data, success, streamOperationHandlerRead_)));
00078 }
00079
00080 void Device::callWriteHandler (size_t bytesWritten, size_t requestedBytes, const char* data, bool success, shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite)
00081 {
00082 owner_->addTask (shared_ptr<Task> (new StreamWriteReplyTask (bytesWritten, requestedBytes, data, success,
00083 streamOperationHandlerWrite)));
00084 }
00085
00086 void Device::close()
00087 {
00088 IOManager::getInstance()->getIOThread()->addCloseOperation (fileDescriptor_, writeEnabled_, readEnabled_);
00089 }
00090
00091 void Device::configureDevice (shared_ptr<DeviceConfigurator> deviceConfigurator)
00092 {
00093 deviceConfigurator->configureDevice (fileDescriptor_);
00094 }