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 #ifndef _IOTHREAD_HPP_ 00023 #define _IOTHREAD_HPP_ 00024 00025 00026 #include <ActiveObject.hpp> 00027 #include <Task.hpp> 00028 #include <ace/Proactor.h> 00029 #include <vector> 00030 #include <map> 00031 00032 #include "StreamReader.hpp" 00033 #include "StreamWriter.hpp" 00034 #include "StreamHandler.hpp" 00035 00036 namespace mermaid 00037 { 00038 namespace support 00039 { 00040 namespace io 00041 { 00042 00043 using mermaid::support::io::shared_ptr; 00044 using mermaid::support::activeobject::ActiveObject; 00045 using mermaid::support::activeobject::Task; 00046 00047 /** 00048 * @Class IOThread IOThread.hpp "IOThread.hpp" 00049 * @Description IOThread class 00050 * 00051 * A separate context of execution that 'waits' for device events (read/write operations completed) and handles them. 00052 * @Author Joao Carreira 00053 */ 00054 class IOThread 00055 { 00056 00057 friend class AddStreamReaderTask; 00058 friend class AddStreamWriterTask; 00059 friend class CloseTask; 00060 friend class WriteTask; 00061 friend class IOThreadUpdateTask; 00062 00063 public: 00064 00065 /** 00066 * @Description Constructor. 00067 * @Author Joao Carreira 00068 */ 00069 IOThread(); 00070 00071 /** 00072 * @Description Add a StreamReader object 00073 * 00074 * This method adds a StreamReader object to the IOThread. This turns on reading for the StreamReader's device. 00075 * @Argument streamReader StreamReader object to add to the IOThread 00076 * @Author Joao Carreira 00077 */ 00078 virtual void addStreamReader (shared_ptr<StreamReader> streamReader); 00079 00080 /** 00081 * @Description Add a StreamWriter object 00082 * 00083 * This method adds a StreamWriter object to the IOThread. This makes the StreamWriter's device available to write. 00084 * @Argument streamWriter StreamWriter object to add to the IOThread 00085 * @Author Joao Carreira 00086 */ 00087 virtual void addStreamWriter (shared_ptr<StreamWriter> streamWriter); 00088 00089 /** 00090 * @Description Creates and adds a WriteTask to be run in the IOThread 00091 * 00092 * @Argument fileDescriptor The file descriptor to which the write will be done 00093 * @Argument bytesToWrite The number of bytes to be written 00094 * @Argument data The data to be written 00095 * @Argument streamOperationHandlerWrite The handler which will receive the write reply 00096 * @Author Joao Carreira 00097 */ 00098 virtual void addWriteOperation (int fileDescriptor, size_t bytesToWrite, const char* data, shared_ptr<StreamOperationHandlerMethodBase> streamOperationHandlerWrite); 00099 00100 /** 00101 * @Description Creates and adds a CloseTask to be run in the IOThread 00102 * 00103 * @Argument fileDescriptor The file descriptor which will be closed 00104 * @Author Joao Carreira 00105 */ 00106 virtual void addCloseOperation (int fileDescriptor, bool writeEnabled, bool readEnabled); 00107 00108 /** 00109 * @Description Run the event loop 00110 * 00111 * This method runs the proactor event loop which handles the io events (read and write replies) 00112 * @Author Joao Carreira 00113 */ 00114 virtual void run(); 00115 00116 /** 00117 * @Description ACE_Proactor getter 00118 * 00119 * @Returns ACE_Proactor instance 00120 * @Author Joao Carreira 00121 */ 00122 virtual ACE_Proactor* getProactor(); 00123 00124 /** 00125 * @Description Active Object getter 00126 * 00127 * @Returns The IOThread's active object 00128 * @Author Joao Carreira 00129 */ 00130 shared_ptr<ActiveObject> getActiveObject() { 00131 return activeObject_; 00132 } 00133 00134 /** 00135 * @Description Destructor 00136 * @Author Joao Carreira 00137 */ 00138 virtual ~IOThread(); 00139 private: 00140 00141 /** 00142 * @Description Add a taks to the IOThread's active object 00143 * 00144 * @Argument task The task to be added 00145 * @Author Joao Carreira 00146 */ 00147 void addTask (shared_ptr<Task> task); 00148 00149 const shared_ptr<ActiveObject> activeObject_; 00150 shared_ptr<StreamHandler> streamHandler_; 00151 00152 ACE_Proactor proactor_; 00153 00154 }; // class IOThread 00155 00156 } // namespace io 00157 } // namespace support 00158 } // namespace mermaid 00159 00160 #endif // _IOTHREAD_HPP_ 00161