'Handle packet in messages from the switch to implement above algorithm.'

def _handle_PacketIn(self, event):

    packet = event.parsed
    def flood(message=None):
        '    Floods    the    packet    '
        msg = of.ofp_packet_out()
        if ((time.time() - self.connection.connect_time) >= _flood_delay):
            if (self.hold_down_expired is False):
                self.hold_down_expired = True
                log.info('%s:    Flood    hold-down    expired    --    flooding', dpid_to_str(event.dpid))
            if (message is not None):
                log.debug(message)
            msg.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD))
        else:
            pass
        msg.data = event.ofp
        msg.in_port = event.port
        self.connection.send(msg)
    def drop(duration=None):
        '\n                        Drops    this    packet    and    optionally    installs    a    flow    to    continue\n                        dropping    similar    ones    for    a    while\n                        '
        if (duration is not None):
            if (not isinstance(duration, tuple)):
                duration = (duration, duration)
            msg = of.ofp_flow_mod()
            msg.match = of.ofp_match.from_packet(packet)
            msg.idle_timeout = duration[0]
            msg.hard_timeout = duration[1]
            msg.buffer_id = event.ofp.buffer_id
            self.connection.send(msg)
        elif (event.ofp.buffer_id is not None):
            msg = of.ofp_packet_out()
            msg.buffer_id = event.ofp.buffer_id
            msg.in_port = event.port
            self.connection.send(msg)
    self.macToPort[packet.src] = event.port
    if (not self.transparent):
        if ((packet.type == packet.LLDP_TYPE) or packet.dst.isBridgeFiltered()):
            drop()
            return
    if packet.dst.is_multicast:
        flood()
    elif (packet.dst not in self.macToPort):
        flood(('Port    for    %s    unknown    --    flooding' % (packet.dst,)))
    else:
        port = self.macToPort[packet.dst]
        if (port == event.port):
            log.warning(('Same    port    for    packet    from    %s    ->    %s    on    %s.%s.        Drop.' % (packet.src, packet.dst, dpid_to_str(event.dpid), port)))
            drop(10)
            return
        log.debug(('installing    flow    for    %s.%i    ->    %s.%i' % (packet.src, event.port, packet.dst, port)))
        msg = of.ofp_flow_mod()
        msg.match = of.ofp_match.from_packet(packet, event.port)
        msg.idle_timeout = 10
        msg.hard_timeout = 30
        msg.actions.append(of.ofp_action_output(port=port))
        msg.data = event.ofp
        self.connection.send(msg)
