/**
   * Construct a datagram with the given flag. <br>
   * <br>
   * Note: This method *depends* on the values of `remotePort', `remoteAddress', `myPort',
   * `myAddress' and `sequenceNo'. Failing to set these before calling this method causes undefined
   * behaviour. Also note that if you want to set values to something else than the default, you
   * must construct the packet manually or alter the returned object.<br>
   * <br>
   * This method also increments the sequenceNo.<br>
   * <br>
   * This method sets the following fields:
   *
   * <ol>
   *   <li>Remote address
   *   <li>Remote port
   *   <li>Local address
   *   <li>Local port
   *   <li>Flag
   *   <li>Sequence no.
   *   <li>Payload to null.
   * </ol>
   *
   * @param flag Flag for the packet, see {@link KtnDatagram.Flag}. Setting this to
   *     KtnDatagram.Flag.NONE constructs a packet with no flag or data, and makes no sense.
   * @return Initialised flagged datagram.
   */
  protected KtnDatagram constructInternalPacket(Flag flag) {

    KtnDatagram packet = new KtnDatagram();
    packet.setDest_port(remotePort);
    packet.setDest_addr(remoteAddress);
    packet.setSrc_addr(myAddress);
    packet.setSrc_port(myPort);
    packet.setFlag(flag);
    packet.setSeq_nr(nextSequenceNo++);
    packet.setPayload(null);

    return packet;
  }
  /**
   * Construct a datagram with the given payload. <br>
   * <br>
   * Note: This method *depends* on the values of `remotePort', `remoteAddress', `myPort',
   * `myAddress' and `sequenceNo'. Failing to set these before calling this method causes undefined
   * behaviour. Also note that if you want to set values to something else than the default, you
   * must construct the packet manually or alter the returned object.<br>
   * <br>
   * This method also increments the sequenceNo.<br>
   * <br>
   * This method sets the following fields:
   *
   * <ol>
   *   <li>Remote address
   *   <li>Remote port
   *   <li>Local address
   *   <li>Local port
   *   <li>Flag to NONE.
   *   <li>Sequence no.
   *   <li>Payload.
   * </ol>
   *
   * @param payload Payload for packet, can not be null.
   * @return Initialised datagram.
   */
  protected KtnDatagram constructDataPacket(String payload) {
    if (payload == null) throw new IllegalArgumentException("Payload can not be null.");

    KtnDatagram packet = new KtnDatagram();
    packet.setDest_port(remotePort);
    packet.setDest_addr(remoteAddress);
    packet.setSrc_addr(myAddress);
    packet.setSrc_port(myPort);
    packet.setFlag(Flag.NONE);
    packet.setSeq_nr(nextSequenceNo++);
    packet.setPayload(payload);

    return packet;
  }