<commit_before>import socket
import struct
from .telegram import Telegram
from .address import Address
from .devices import devices_
from .globals import Globals

class Multicast:
    MCAST_GRP = '192.168.3.11'
    MCAST_PORT = 3671

    def __init__(self):
        pass

    def send(self, telegram):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)

        if(Globals.get_own_ip()):
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(Globals.get_own_ip()))

        sock.sendto(telegram.str(), (self.MCAST_GRP, self.MCAST_PORT))

    def recv(self, callback = None):
        print("Starting daemon...")
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind(("", self.MCAST_PORT))

        if(Globals.get_own_ip()):
            sock.setsockopt(socket.IPPROTO_IP,
                                 socket.IP_ADD_MEMBERSHIP,
                                 socket.inet_aton(self.MCAST_GRP) +
                                 socket.inet_aton(Globals.get_own_ip()))
        else:
            mreq = struct.pack("4sl", socket.inet_aton(self.MCAST_GRP), socket.INADDR_ANY)
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)

        while True:
            telegram_data = sock.recv(10240)
            if telegram_data:

                if len(telegram_data) < 17:
                    print("WARNING: Telegram has size {0} too small, ignoring".format(len(telegram_data)))
                    continue

                telegram = Telegram()
                telegram.read(telegram_data)

                #telegram.dump()

                if telegram.sender == Globals.get_own_address():
                    #print("Ignoring own telegram")
                    pass

                else:
                    device = devices_.device_by_group_address(telegram.group_address)
                    device.process(telegram)

                    if ( callback ):
                        callback(device,telegram)
<commit_msg>Issue #1: better error handling within Multicast when receiving a packet from an unknown device<commit_after>import socket
import struct
from .telegram import Telegram
from .address import Address
from .devices import devices_,CouldNotResolveAddress
from .globals import Globals

class Multicast:
    MCAST_GRP = '192.168.3.11'
    MCAST_PORT = 3671

    def __init__(self):
        pass

    def send(self, telegram):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)

        if(Globals.get_own_ip()):
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_IF, socket.inet_aton(Globals.get_own_ip()))

        sock.sendto(telegram.str(), (self.MCAST_GRP, self.MCAST_PORT))

    def recv(self, callback = None):
        print("Starting daemon...")
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind(("", self.MCAST_PORT))

        if(Globals.get_own_ip()):
            sock.setsockopt(socket.IPPROTO_IP,
                                 socket.IP_ADD_MEMBERSHIP,
                                 socket.inet_aton(self.MCAST_GRP) +
                                 socket.inet_aton(Globals.get_own_ip()))
        else:
            mreq = struct.pack("4sl", socket.inet_aton(self.MCAST_GRP), socket.INADDR_ANY)
            sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)

        while True:
            telegram_data = sock.recv(10240)
            if telegram_data:

                if len(telegram_data) < 17:
                    print("WARNING: Telegram has size {0} too small, ignoring".format(len(telegram_data)))
                    continue

                telegram = Telegram()
                telegram.read(telegram_data)

                #telegram.dump()

                if telegram.sender == Globals.get_own_address():
                    #print("Ignoring own telegram")
                    pass

                else:
                    try:
                        device = devices_.device_by_group_address(telegram.group_address)
                        device.process(telegram)

                        if ( callback ):
                            callback(device,telegram)

                    except CouldNotResolveAddress as c:
                        print(c)

{'fraction_non_alphanumeric': 0.09227095093356491, 'fraction_numerical': 0.010638297872340425, 'mean_word_length': 1.8974842767295597, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '720480', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1329, 'n_words': 238}