Device.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 "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 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3