StreamHandler.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 "StreamHandler.hpp"
00025 #include <Exception.hpp>
00026 #include <utility>
00027 #include <iostream>
00028 #include <sstream>
00029 #include <cassert>
00030 #include <ace/Message_Block.h>
00031 
00032 using namespace mermaid::support::io;
00033 
00034 using mermaid::support::errorhandling::Exception;
00035 
00036 StreamHandler::~StreamHandler()
00037 {
00038 
00039 }
00040 
00041 void StreamHandler::associateFileDescriptorWithStreamReader (int fileDescriptor, shared_ptr<StreamReader> streamReader)
00042 {
00043   streamReaderMap_.insert (std::make_pair (fileDescriptor, streamReader));
00044 }
00045 
00046 void StreamHandler::associateFileDescriptorWithStreamWriter (int fileDescriptor, shared_ptr<StreamWriter> streamWriter)
00047 {
00048   streamWriterMap_.insert (std::make_pair (fileDescriptor, streamWriter));
00049 }
00050 
00051 void StreamHandler::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
00052 {
00053 #ifdef DEBUG
00054   std::cerr << "handle_write_stream  " << result.message_block().length() << std::endl;
00055 #endif
00056   int fileDescriptor = result.handle();
00057   
00058   if (streamReaderMap_.find (fileDescriptor) == streamReaderMap_.end()) {
00059     std::ostringstream oss;
00060     oss << fileDescriptor;
00061     throw Exception ("StreamHandler::handle_read_stream: File descriptor has no associated StreamReader with file descriptor number: " + oss.str());
00062   }
00063   
00064   // use a copy of data, so we can release message block later
00065   char* msg = new char[result.message_block().length() ];
00066   std::copy (result.message_block().base(), result.message_block().base() + result.message_block().length(), msg);
00067   
00068   // call StreamReader
00069   streamReaderMap_[fileDescriptor]->onRead (result.message_block().length(), result.bytes_to_read(), msg, result.success());
00070   
00071   result.message_block().release();
00072 }
00073 
00074 void StreamHandler::handle_write_stream (const ACE_Asynch_Write_Stream::Result &result)
00075 {
00076   int fileDescriptor = result.handle();
00077   
00078   if (streamWriterMap_.find (fileDescriptor) == streamWriterMap_.end()) {
00079     std::ostringstream oss;
00080     oss << fileDescriptor;
00081     throw Exception ("StreamHandler::handle_write_stream: File descriptor has no associated StreamWriter with file descriptor number: " + oss.str());
00082   }
00083   
00084   assert (result.act() != 0);
00085   std::cerr << result.act() << std::endl;
00086   shared_ptr<StreamOperationHandlerMethodBase>* streamOperationHandlerWritePtr = (shared_ptr<StreamOperationHandlerMethodBase>*) result.act();
00087   shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite = *streamOperationHandlerWritePtr;
00088   delete streamOperationHandlerWritePtr;
00089   
00090   // use a copy of data, so we can release message block later
00091   char* msg = new char[result.message_block().length() ];
00092   std::copy (result.message_block().base(), result.message_block().base() + result.message_block().length(), msg);
00093   
00094   // call StreamWriter
00095   streamWriterMap_[fileDescriptor]->onWrite (result.message_block().length(), result.bytes_to_write(), msg, result.success(), streamOperationHandlerWrite);
00096   
00097   result.message_block().release();
00098 }
00099 
00100 shared_ptr<StreamWriter> StreamHandler::getAssociatedStreamWriter (int fileDescriptor)
00101 {
00102   assert (streamWriterMap_.find (fileDescriptor) != streamWriterMap_.end());
00103   return streamWriterMap_[fileDescriptor];
00104 }
00105 
00106 void StreamHandler::closeStreamReader (int fileDescriptor)
00107 {
00108   assert (streamReaderMap_.find (fileDescriptor) != streamReaderMap_.end());
00109   streamReaderMap_.erase (streamReaderMap_.find (fileDescriptor));
00110 }
00111 
00112 void StreamHandler::closeStreamWriter (int fileDescriptor)
00113 {
00114   assert (streamWriterMap_.find (fileDescriptor) != streamWriterMap_.end());
00115   streamWriterMap_.erase (streamWriterMap_.find (fileDescriptor));
00116 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3