StreamHandler.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 "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
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
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
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
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 }