StreamReader.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 "StreamReader.hpp"
00025 #include "IOManager.hpp"
00026 #include <ace/Asynch_IO.h>
00027 #include <ace/Message_Block.h>
00028 #include <ace/Proactor.h>
00029 #include <Exception.hpp>
00030 
00031 using namespace mermaid::support::io;
00032 
00033 using mermaid::support::errorhandling::Exception;
00034 
00035 StreamReader::~StreamReader()
00036 {
00037 
00038 }
00039 
00040 StreamReader::StreamReader (Device* device) : device_ (device)
00041 {
00042 }
00043 
00044 void StreamReader::onRead (size_t bytesRead, size_t requestedBytesRead, const char* data, bool success)
00045 {
00046   device_->callReadHandler (bytesRead, requestedBytesRead, data, success);
00047   doRead();
00048 }
00049 
00050 void StreamReader::open (ACE_Handler* streamHandler, ACE_Proactor* proactor)
00051 {
00052   int ret = readStream_.open (*streamHandler, device_->getFileDescriptor(), 0, proactor);
00053   if (ret != 0)
00054     throw Exception ("StreamReader::open(): Error opening ACE_Asynch_Read_Stream.");
00055 }
00056 
00057 void StreamReader::doRead()
00058 {
00059   ACE_Message_Block* mb;
00060   ACE_NEW_NORETURN (mb, ACE_Message_Block (device_->getExpectedDataSize()));
00061   int ret = readStream_.read (*mb, mb->space());
00062   
00063   if (ret != 0) {
00064     mb->release();
00065     throw Exception ("StreamReader::doRead(): Error performing an asynch read operation.");
00066   }
00067 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3