def on_send_gap(match, state, logger):
    """It happens when the writer send a GAP message."""
    writer_oid = get_oid(match[0])
    remote_part = parse_guid(state, match[1], match[2], match[3])
    reader_oid = get_oid(match[4])
    sn_start = parse_sn(match[5])
    sn_end = parse_sn(match[6]) - 1
    verb = 1 if is_builtin_entity(match[0]) else 0
    logger.send(
        remote_part, writer_oid,
        "Sent GAP to reader %s for samples in [%d, %d]" %
        (reader_oid, sn_start, sn_end), verb)
    add_statistics_packet(writer_oid, 'send', 'GAP', state)

    # Check for large sequence number issues.
    if sn_end - sn_start >= (1 << 31):
        logger.warning("[LP-1] Large Sequence Number difference in GAP")

    # Check for reliable packet lost
    if 'packets_lost' not in state:
        return
    losts = []
    for k in state['packets_lost']:
        info = k.split("-")
        oid = info[0]
        seqnum = int(info[1])
        if oid == writer_oid and seqnum >= sn_start and seqnum < sn_end:
            logger.warning("DATA [%d] may have been lost" % seqnum)
            losts.append(k)
    for k in losts:
        state['packets_lost'].remove(k)
def on_send_gap(match, state, logger):
    """It happens when the writer send a GAP message."""
    writer_oid = get_oid(match[0])
    remote_part = parse_guid(state, match[1], match[2], match[3])
    reader_oid = get_oid(match[4])
    sn_start = parse_sn(match[5])
    sn_end = parse_sn(match[6]) - 1
    verb = 1 if is_builtin_entity(match[0]) else 0
    logger.send(remote_part, writer_oid,
                "Sent GAP to reader %s for samples in [%d, %d]" %
                (reader_oid, sn_start, sn_end), verb)
    add_statistics_packet(writer_oid, 'send', 'GAP', state)

    # Check for large sequence number issues.
    if sn_end - sn_start >= (1 << 31):
        logger.warning("[LP-1] Large Sequence Number difference in GAP")

    # Check for reliable packet lost
    if 'packets_lost' not in state:
        return
    losts = []
    for k in state['packets_lost']:
        info = k.split("-")
        oid = info[0]
        seqnum = int(info[1])
        if oid == writer_oid and seqnum >= sn_start and seqnum < sn_end:
            logger.warning("DATA [%d] may have been lost" % seqnum)
            losts.append(k)
    for k in losts:
        state['packets_lost'].remove(k)
def on_send_piggyback_hb(match, state, logger):
    """It happens when sending a piggyback HB message."""
    writer_oid = get_oid(match[0])
    sn_first = parse_sn(match[1])
    sn_last = parse_sn(match[2])
    verb = 1 if is_builtin_entity(match[0]) else 0
    logger.send("", writer_oid,
                "Sent piggyback HB to acknowledge samples in [%d, %d]" %
                (sn_first, sn_last), verb)
    add_statistics_packet(writer_oid, "send", "PIGGYBACK HB", state)
def on_send_data(match, state, logger):
    """It happens when a DATA packet is sent."""
    writer_oid = get_oid(match[0])
    seqnum = parse_sn(match[1])
    logger.send("", writer_oid, "Sent DATA [%d]" % seqnum)
    add_statistics_packet(writer_oid, "send", "DATA", state)

    key = writer_oid + "-" + str(seqnum)
    if 'packets_lost' in state and key in state['packets_lost']:
        state['packets_lost'].remove(key)
def on_send_data(match, state, logger):
    """It happens when a DATA packet is sent."""
    writer_oid = get_oid(match[0])
    seqnum = parse_sn(match[1])
    logger.send("", writer_oid, "Sent DATA [%d]" % seqnum)
    add_statistics_packet(writer_oid, "send", "DATA", state)

    key = writer_oid + "-" + str(seqnum)
    if 'packets_lost' in state and key in state['packets_lost']:
        state['packets_lost'].remove(key)
def on_send_piggyback_hb(match, state, logger):
    """It happens when sending a piggyback HB message."""
    writer_oid = get_oid(match[0])
    sn_first = parse_sn(match[1])
    sn_last = parse_sn(match[2])
    verb = 1 if is_builtin_entity(match[0]) else 0
    logger.send(
        "", writer_oid,
        "Sent piggyback HB to acknowledge samples in [%d, %d]" %
        (sn_first, sn_last), verb)
    add_statistics_packet(writer_oid, "send", "PIGGYBACK HB", state)
def on_send_piggyback_hb_syncrepair(match, state, logger):
    """It happens when sending a piggyback HB message from repair."""
    writer_oid = get_oid(match[0])
    sn_first = parse_sn(match[1])
    sn_last = parse_sn(match[2])
    epoch = int(match[3])
    verb = 1 if is_builtin_entity(match[0]) else 0
    logger.send(
        "", writer_oid,
        ("Sent piggyback HB [%d] from synchronous reparation" % epoch) +
        " to acknowledge samples in [%d, %d]" % (sn_first, sn_last), verb)
    add_statistics_packet(writer_oid, "send", "PIGGYBACK HB", state)
def on_send_piggyback_hb_syncrepair(match, state, logger):
    """It happens when sending a piggyback HB message from repair."""
    writer_oid = get_oid(match[0])
    sn_first = parse_sn(match[1])
    sn_last = parse_sn(match[2])
    epoch = int(match[3])
    verb = 1 if is_builtin_entity(match[0]) else 0
    logger.send("",
                writer_oid,
                ("Sent piggyback HB [%d] from synchronous reparation"
                 % epoch) + " to acknowledge samples in [%d, %d]"
                % (sn_first, sn_last),
                verb)
    add_statistics_packet(writer_oid, "send", "PIGGYBACK HB", state)
def on_parse_packet(match, state, logger):
    """It happens when an RTPS message is parsed."""
    addr = parse_guid(state, match[1], match[2])
    logger.recv(addr, "", "Received %s packet" % match[0], 2)
    add_statistics_packet(addr, 'receive', match[0], state)
def on_parse_packet(match, state, logger):
    """It happens when an RTPS message is parsed."""
    addr = parse_guid(state, match[1], match[2])
    logger.recv(addr, "", "Received %s packet" % match[0], 2)
    add_statistics_packet(addr, 'receive', match[0], state)