Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Platform/Aperios1.3.2/MessageQueueBase.h

Go to the documentation of this file.

    00001 /**
    00002 * @file Platform/Aperios1.3.2/MessageQueueBase.h
    00003 *
    00004 * Declaration of class MessageQueueBase for Aperios.
    00005 *
    00006 * @author Martin Lötzsch
    00007 */
    00008
    00009 #ifndef __MessageQueueBase_h_
    00010 #define __MessageQueueBase_h_
    00011
    00012 #include "Tools/MessageQueue/MessageIDs.h"
    00013 #include "Tools/Player.h"
    00014
    00015 /**
    00016 * @class MessageQueueBase
    00017 *
    00018 * The platform dependend base of class MessageQueue.
    00019 * This version works on a static memory buffer to avoid often memory allocations on the
    00020 * Aperios platform.
    00021 *
    00022 * Use setSize(int size) to set the total size of data the queue can store. If a message
    00023 * doesn't fit into the queue, it is ignored.
    00024 *
    00025 * @author Martin Lötzsch
    00026 */
    00027 class MessageQueueBase
    00028 {
    00029 public:
    00030   /** Constructor */
    00031   MessageQueueBase();
    00032
    00033   /** Destructor */
    00034   ~MessageQueueBase();
    00035
    00036   /**
    00037   * Sets the size of the queue. Ignored on the Win32 platform.
    00038   * @param size The maximum size of the queue in Bytes.
    00039   */
    00040   void setSize(int size);
    00041
    00042   /** Removes all messages from the queue */
    00043   void clear();
    00044
    00045   /** Returns the number of the messages in the queue */
    00046   int getNumberOfMessages() const;
    00047
    00048   /**
    00049   * The function adds a number of bytes to the last message in the queue.
    00050   * @param p The address the data is located at.
    00051   * @param size The number of bytes to be written.
    00052   */
    00053   virtual void write(const void* p,int size);
    00054
    00055   /**
    00056   * Finishes the last message in the queue.
    00057   * The number of messages becomes increased and a new message can be started.
    00058   * @param id The type id of the message.
    00059   * @param timeStamp The time stamp of the message
    00060   * @param teamColor the team color of the robot that sent the message
    00061   * @param playerNumber the player number of the robot that sent the message
    00062   * @param messageWasSentFromAPhysicalRobot If true, then the message was sent
    00063   *     from a physical robot. Is set to false on all non-Aperios/OpenR platforms.
    00064   */
    00065   void finishMessage(MessageID id, unsigned long timeStamp,
    00066     Player::teamColor teamColor,Player::playerNumber playerNumber,
    00067     bool messageWasSentFromAPhysicalRobot=true);
    00068
    00069   /** Returns if the the currently selected message for reading was read completely. */
    00070   bool eof() const;
    00071
    00072   /**
    00073   * Reads a number of bytes from the currently selected message for reading
    00074   * @param p The address the data is written to. Note that p
    00075   *          must point to a memory area that is at least
    00076   *          "size" bytes large.
    00077   * @param size The number of bytes to be read.
    00078   */
    00079   void read(void* p,int size);
    00080
    00081   /**
    00082   * Gives the MessageQueue direct read access to the selected message for reading.
    00083   * @return the address of the first byte of the message
    00084   */
    00085   const char* getData() const;
    00086
    00087   /** Returns the message id of the currently selected message for reading */
    00088   MessageID getMessageID() const;
    00089
    00090   /**
    00091   * Returns the time stamp of a message.
    00092   * @param message the number of the message
    00093   */
    00094   unsigned long getTimeStamp(int message);
    00095
    00096   /** Returns the time stamp of the currently selected message for reading */
    00097   unsigned long getTimeStamp() const;
    00098
    00099   /** Returns the message size of the currently selected message for reading */
    00100   int getMessageSize() const;
    00101
    00102   /** returns the team color of the robot that sent the currently selected message for reading. */
    00103   Player::teamColor getTeamColor() const;
    00104
    00105   /** returns the player number of the robot that sent the currently selected message for reading.  */
    00106   Player::playerNumber getPlayerNumber() const;
    00107
    00108   /** returns whether the currently selected message for reading was sent from a physical robot */
    00109   bool getMessageWasSentFromAPhysicalRobot() const;
    00110
    00111   /**
    00112   * Returns the read position of the currently selected message for reading
    00113   * so that the message can be read again.
    00114   */
    00115   void resetReadPosition();
    00116
    00117   /*
    00118   * Sets which message is selected for reading.
    00119   * @param message The number of the message
    00120   */
    00121   void setSelectedMessageForReading(int message);
    00122
    00123   /** Deletes all older messages from the queue if a newer
    00124     * message of same type is already in the queue.
    00125     * This method should not be called during message handling. */
    00126   void removeRepetitions();
    00127
    00128 private:
    00129   /** the buffer on that the queue works */
    00130   char* buf;
    00131
    00132   /** the position of the message that is selected for reading */
    00133   int selectedMessageForReadingPosition;
    00134
    00135   /** the number of stored messages */
    00136   int numOfMessages;
    00137
    00138   /** the queue size */
    00139   int queueSize;
    00140
    00141   /** the position where the next message starts (in bytes) */
    00142   int nextMessagePosition;
    00143
    00144   /** the current size of the next message */
    00145   int nextMessageSize;
    00146
    00147   /** If true, then the writing of the last message failed because there was not enough space */
    00148   bool writingOfLastMessageFailed;
    00149
    00150   /** The position up to where a message is already read */
    00151   int readPosition;
    00152
    00153   /** Cache the current message in the message queue. */
    00154   int lastMessage;
    00155 };
    00156
    00157 #endif // __MessageQueueBase_h_