00001 /** 00002 * @file MessageQueue.h 00003 * 00004 * Definition of class MessageQueue. 00005 * 00006 * Include this file if for declaring instances of MessageQueue. 00007 * 00008 * @author Martin Lötzsch 00009 */ 00010 00011 #ifndef __MessageQueue_h_ 00012 #define __MessageQueue_h_ 00013 00014 #include "InMessage.h" 00015 #include "OutMessage.h" 00016 00017 /** 00018 * @class MessageQueue 00019 * 00020 * A queue for size varying, time stamped and type safe messages. 00021 * It can be used to collect very different types of messages for exchange between different 00022 * threads, processes or systems. 00023 * 00024 * @author Martin Lötzsch 00025 * 00026 * Usage: 00027 * <pre> 00028 * MessageQueue myQueue; 00029 * myQueue.setSize(100000); // set the size of data that can be stored in the queue 00030 * Image image1; 00031 * myQueue.out.bin << image1; // write the binary message 00032 * myQueue.out.finishMessage(idImage); // finish the message, set the type id of the message 00033 * // 00034 * // ... copy the queue between processes, systems 00035 * // 00036 * if (myQueue.in.getMessageID() == idImage) // check for the type of the next message 00037 * { 00038 * Image image2; 00039 * myQueue.in.bin >> image2; // read the image from the queue 00040 * } 00041 * </pre> 00042 */ 00043 class MessageQueue 00044 { 00045 protected: 00046 /** the system dependend message queue base which is responsible for the data storage */ 00047 MessageQueueBase queue; 00048 00049 public: 00050 /** An interface for reading messages from the queue */ 00051 InMessage in; 00052 00053 /** An interface for writing messages to the queue */ 00054 OutMessage out; 00055 00056 /** Default constructor */ 00057 MessageQueue(); 00058 00059 /** 00060 * Sets the size of memory which is allocated for the queue. 00061 * Ignored on the Win32 platform (dynamic allocation). 00062 * @param size The maximum size of the queue in Bytes. 00063 */ 00064 void setSize(int size); 00065 00066 /** Returns the size of memory which is needed to write the queue to a stream. */ 00067 int getStreamedSize(); 00068 00069 /** 00070 * Specifies a team color and a player number that is atached to every new 00071 * message. If not set, Player::undefinedTeamColor and Player::undefinedPlayerNumber 00072 * is used. 00073 */ 00074 void setPlayerForNewMessages(const Player& player); 00075 00076 /** 00077 * Calls a given MessageHandler for all messages of a kind in the queue. Note that the messages 00078 * still remain in the queue and have to be removed manually with clear(). 00079 * @param id handle only messages with this MessageID 00080 * @param handler a reference to a MessageHandler derivate 00081 */ 00082 void handleSpecificMessages(MessageID id, MessageHandler& handler); 00083 00084 /** 00085 * Calls a given MessageHandler for all messages in the queue. Note that the messages 00086 * still remain in the queue and have to be removed manually with clear(). 00087 * @param handler a reference to a MessageHandler derivate 00088 */ 00089 void handleAllMessages(MessageHandler& handler); 00090 00091 /** 00092 * Copies all messages from this queue to another queue. 00093 * @param other the destination queue. 00094 */ 00095 void copyAllMessages(MessageQueue& other); 00096 00097 /** 00098 * Moves all messages from this queue to another queue. 00099 * @param other the destination queue. 00100 */ 00101 void moveAllMessages(MessageQueue& other); 00102 00103 /** Deletes all older messages from the queue if a newer 00104 * message of same type is already in the queue. 00105 * This method should not be called during message handling. */ 00106 void removeRepetitions() { queue.removeRepetitions(); } 00107 00108 /** Removes all messages from the queue */ 00109 void clear(); 00110 00111 /** Returns if the queue contains no messages */ 00112 bool isEmpty() const; 00113 00114 protected: 00115 /** 00116 * Copies a single message to another queue 00117 * @param message The number of the message 00118 * @param other The other queue. 00119 */ 00120 void copyMessage(int message, MessageQueue& other); 00121 00122 /** Gives the stream operator access to protected members */ 00123 friend In& operator>>(In& stream, MessageQueue& messageQueue); 00124 00125 /** Gives the stream operator access to protected members */ 00126 friend Out& operator<<(Out& stream, const MessageQueue& messageQueue); 00127 }; 00128 00129 /** 00130 * Streaming operator that reads a MessageQueue from a stream. 00131 * @param stream The stream from which is read. 00132 * @param messageQueue The MessageQueue object. 00133 * @return The stream. 00134 */ 00135 In& operator>>(In& stream,MessageQueue& messageQueue); 00136 00137 00138 /** 00139 * Streaming operator that writes a MessageQueue to a stream. 00140 * @param stream The stream to write on. 00141 * @param messageQueue The MessageQueue object. 00142 * @return The stream. 00143 */ 00144 Out& operator<<(Out& stream, const MessageQueue& messageQueue); 00145 00146 /** 00147 * Streaming operator that writes a InMessage to another MessageQueue. 00148 * @param message The InMessage to write. 00149 * @param queue The MessageQueue object. 00150 */ 00151 void operator >> (InMessage& message, MessageQueue& queue); 00152 00153 #endif //__MesssageQueue_h_ 00154 00155 /* 00156 * Change Log: 00157 * 00158 * $Log: MessageQueue.h,v $ 00159 * Revision 1.1.1.1 2004/05/22 17:37:18 cvsadm 00160 * created new repository GT2004_WM 00161 * 00162 * Revision 1.3 2004/01/10 18:04:58 loetzsch 00163 * added MessageQueue::getStreamedSize() 00164 * 00165 * Revision 1.2 2003/12/06 23:23:55 loetzsch 00166 * messages in a MessageQueue now contain 00167 * - the team color of the robot which sent the message 00168 * - the player number of the robot which sent the message 00169 * - if the message was sent from a physical robot or not 00170 * 00171 * Revision 1.1 2003/10/07 10:13:24 cvsadm 00172 * Created GT2004 (M.J.) 00173 * 00174 * Revision 1.1.1.1 2003/07/02 09:40:28 cvsadm 00175 * created new repository for the competitions in Padova from the 00176 * tamara CVS (Tuesday 2:00 pm) 00177 * 00178 * removed unused solutions 00179 * 00180 * Revision 1.5 2003/02/05 12:37:14 dueffert 00181 * handleSpecificMessage added 00182 * 00183 * Revision 1.4 2003/01/13 22:01:27 timrie 00184 * corrected doxygen comments 00185 * 00186 * Revision 1.3 2002/12/05 16:12:48 dueffert 00187 * started implementing realtime sending 00188 * 00189 * Revision 1.2 2002/11/19 17:38:32 dueffert 00190 * doxygen bugs corrected 00191 * 00192 * Revision 1.1 2002/09/10 15:53:59 cvsadm 00193 * Created new project GT2003 (M.L.) 00194 * - Cleaned up the /Src/DataTypes directory 00195 * - Removed challenge related source code 00196 * - Removed processing of incoming audio data 00197 * - Renamed AcousticMessage to SoundRequest 00198 * 00199 * Revision 1.3 2002/08/22 14:41:04 risler 00200 * added some doxygen comments 00201 * 00202 * Revision 1.2 2002/08/08 16:38:47 loetzsch 00203 * moved some members to the derived class LogPlayer because they are not 00204 * needed in the normal MessageQueue 00205 * 00206 * Revision 1.1 2002/07/23 13:47:14 loetzsch 00207 * - new streaming classes 00208 * - new debug message handling 00209 * - exchanged StaticQueue by MessageQueue 00210 * 00211 */ 00212