src/common/trailer.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 13 Jun 2007 16:37:04 +0200
changeset 763 e124f772d9c7
parent 640 028db47a657f
child 775 7d6a3f079355
permissions -rw-r--r--
add a rationale for the way Trailers work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
150
663120712cd9 fix coding style
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 131
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     2
/*
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
 * Copyright (c) 2005 INRIA
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     4
 * All rights reserved.
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
 *
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License version 2 as
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
 * published by the Free Software Foundation;
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
 *
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
 * GNU General Public License for more details.
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
 *
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
 *
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    20
 */
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    21
107
2cbaa2a1b116 add trailer
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 105
diff changeset
    22
#ifndef TRAILER_H
2cbaa2a1b116 add trailer
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 105
diff changeset
    23
#define TRAILER_H
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
463
c2082308e01a merge Packet API changes needed for Packet pretty printing.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 180
diff changeset
    25
#include "chunk.h"
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
16
99e833adbb46 change yans namespace to ns3
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 9
diff changeset
    27
namespace ns3 {
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    28
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    29
/**
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    30
 * \brief Protocol trailer serialization and deserialization.
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    31
 *
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    32
 * Every Protocol trailer which needs to be inserted or removed
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    33
 * from a Packet instance must derive from this abstract base class
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    34
 * and implement the private pure virtual methods listed below:
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    35
 *   - ns3::Trailer::SerializeTo
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    36
 *   - ns3::Trailer::DeserializeFrom
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    37
 *   - ns3::Trailer::GetSerializedSize
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    38
 *   - ns3::Trailer::PrintTo
763
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    39
 *
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    40
 * Note that the SerializeTo and DeserializeFrom methods behave
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    41
 * in a way which might seem surprising to users: the input iterator
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    42
 * really points to the end of the buffer to which and from which
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    43
 * the user is expected to write and read respectively. This means that
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    44
 * if the trailer has a fixed size and if the user wishes to read or
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    45
 * write that trailer from front to back, the user must rewind the 
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    46
 * iterator by hand to go to the start of the trailer. Typical code
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    47
 * looks like this:
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    48
 * \code
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    49
 * void CrcTrailer::SerializeTo (Buffer::Iterator end)
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    50
 * {
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    51
 *   end.Prev (4);
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    52
 *   end.WriteHtonU32 (m_crc);
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    53
 * }
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    54
 * \endcode
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    55
 *
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    56
 * Some users would have expected that the iterator would be rewinded 
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    57
 * to the "start" of the trailer before calling SerializeTo and DeserializeFrom.
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    58
 * However, this behavior was not implemented because it cannot be made to
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    59
 * work reliably for trailers which have a variable size. i.e., if the trailer 
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    60
 * contains options, the code which calls DeserializeFrom cannot rewind
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    61
 * to the start of the trailer because it does not know the real size of the 
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    62
 * trailer. Hence, to make this legitimate use-case work (variable-sized 
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    63
 * trailers), the input iterator to DeserializeFrom and SerializeTo points
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    64
 * to the end of the trailer, and not its start.
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    65
 */
463
c2082308e01a merge Packet API changes needed for Packet pretty printing.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 180
diff changeset
    66
class Trailer : public Chunk {
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    67
public:
180
3e6bfa625fb7 remove purity from virtual destructor in Header and Trailer
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 150
diff changeset
    68
  virtual ~Trailer ();
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    69
private:
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    70
  /**
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    71
   * \param os the std output stream in which this 
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    72
   *       protocol trailer must print itself.
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    73
   */
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    74
  virtual void PrintTo (std::ostream &os) const = 0;
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    75
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    76
  /**
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    77
   * \returns the size of the serialized Trailer.
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    78
   */
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    79
  virtual uint32_t GetSerializedSize (void) const = 0;
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    80
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    81
  /**
640
028db47a657f beautify the dox output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 628
diff changeset
    82
   * \param end the buffer iterator in which the protocol trailer
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    83
   *    must serialize itself. This iterator identifies 
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    84
   *    the end of the buffer.
763
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    85
   *
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    86
   * This iterator must be typically moved with the Buffer::Iterator::Prev
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    87
   * method before writing any byte in the buffer.
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    88
   */
640
028db47a657f beautify the dox output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 628
diff changeset
    89
  virtual void SerializeTo (Buffer::Iterator end) const = 0;
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    90
  /**
640
028db47a657f beautify the dox output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 628
diff changeset
    91
   * \param end the buffer iterator from which the protocol trailer must
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    92
   *    deserialize itself. This iterator identifies 
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    93
   *    the end of the buffer.
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    94
   * \returns the number of bytes read from the buffer
763
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    95
   *
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    96
   * This iterator must be typically moved with the Buffer::Iterator::Prev
e124f772d9c7 add a rationale for the way Trailers work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 640
diff changeset
    97
   * method before reading any byte in the buffer.
628
21122b21924d move doxygen doc into Header and Trailer classes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 463
diff changeset
    98
   */
640
028db47a657f beautify the dox output
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 628
diff changeset
    99
  virtual uint32_t DeserializeFrom (Buffer::Iterator end) = 0;
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   100
};
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   101
16
99e833adbb46 change yans namespace to ns3
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 9
diff changeset
   102
}; // namespace ns3
9
2c31ae7c94db import from yans
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   103
107
2cbaa2a1b116 add trailer
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 105
diff changeset
   104
#endif /* TRAILER_H */