<commit_before># main.py - Everything is in there.
# Author: <NAME> < <EMAIL> >
# License: New BSD License (See LICENSE)

import hashlib
import logging
import os
import random
import signal
import string
import sys
import time

from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
from twisted.web import resource, server, static

_DEBUG = '-d' in sys.argv[1:] or '--debug' in sys.argv[1:]

_LOGGING_FORMAT = '%(message)s'
_LOGGING_FORMAT_DEBUG = '[%(levelname)s] %(module)s.%(funcName)s: %(message)s'\

_ROOT_PKG_CACHE = '/var/cache/pacman/pkg/'

_WAITING_TIMER = 1
_REANNOUNCE_TIMER = 60 * 3
_TIMEOUT = 1.5 * _REANNOUNCE_TIMER

_MULTICAST_GROUP = '172.16.17.32'
_MULTICAST_PORT = 19432
_MULTICAST_ADDR = (_MULTICAST_GROUP, _MULTICAST_PORT)

_HTTP_FILE_PORT = _MULTICAST_PORT
_HTTP_PACMAN_PORT = _HTTP_FILE_PORT + 1

_ID = hashlib.sha1('{name}_{random_value}'.format(
    name = os.uname()[1],
    random_value = int(random.random() * 100000000),
)).hexdigest()

_DISCONNECT_MSG = 'flocon: DISCONNECT'
_HAS_MSG = 'flocon: HAS'
_NO_MSG = 'flocon: NO'
_PING_MSG = 'flocon: PING'
_PONG_MSG = 'flocon: PONG'
_YES_MSG = 'flocon: YES'

_SEPARATOR = '-'
_SEPARATOR_F = ' = '

_CLIENTS = dict()

def _list_clients(signum, stack_frame):
    c_len = len(_CLIENTS)
    logging.info('\nThere %s %s client%s connected.',
                 'are' if c_len > 1 else 'is', c_len, 's' if c_len > 1 else '')
    for client in _CLIENTS.values():
        logging.info(' - %s', client)
    logging.info('')

def _find_fallback_mirror():
    with open('/etc/pacman.d/mirrorlist') as f:
        for line in f.readlines():
            if line.startswith("# flocon: Server"):
                try:
                    return line.split('=')[1].strip()
                except IndexError:
                    return None
    return None

_FALLBACK_MIRROR = str(_find_fallback_mirror()) + '/$filename'
_FILE_SERVER = 'http://$ip:$port/$filename'


class Client:
    def __init__(self, id, addr):
        self.id, self.last, self.addr, self.connected = id, None, addr, True
        self.ip, self.port = addr
        self.update()

    def __str__(self):
        if _DEBUG:
            return '{{ip = {}; port = {}; id = {}}}'.format(
                self.ip, self.port, self.id
            )
        else:
            return '{{ip = {}; port = {}}}'.format(self.ip, self.port)

    def update(self):
        self.last = time.time()

    def is_valid(self):
        return self.connected and (time.time() - self.last) < _TIMEOUT

_REQUEST = None

class MulticastClientManager(DatagramProtocol):
    def startProtocol(self):
        self.transport.joinGroup(_MULTICAST_GROUP)
        self.announce_presence()

    def datagramReceived(self, datagram, addr):
        logging.debug('Received a new UDP message from %r', addr)
        logging.debug('Message was: %s', datagram)
        try:
            _id, _msg = datagram.split(_SEPARATOR, 1)
        except ValueError:
            logging.error('I couldn\'t understand last message.')
            return
        if _msg == _PING_MSG or _msg == _PONG_MSG:
            if _id == _ID:
                return
            try:
                _CLIENTS[_id].update()
            except KeyError:
                c = Client(_id, addr)
                logging.info('Client %s connected!', c)
                _CLIENTS[_id] = c
                if _msg == _PING_MSG:
                    self.send_data(_PONG_MSG, addr)
            return
        elif _msg == _DISCONNECT_MSG:
            client.connected = False
            timeout_clients()
            return

        # From here, if we don't know the client, we just ignore the message.
        try:
            client = _CLIENTS[_id]
        except KeyError:
            logging.error('I don\'t know this client!')
            return
        try:
            _msg, _filename = _msg.split(_SEPARATOR_F)
        except KeyError:
            logging.error('I couldn\'t understand last file message.')
            return
        if _msg == _HAS_MSG:
            self.has_file(client, _filename)
            return
        if _REQUEST is None or _REQUEST.filename != _filename:
            return
        if _msg == _YES_MSG and _REQUEST is not None:
            _REQUEST.redirect_file_server(_id)
        elif _msg == _NO_MSG and _REQUEST is not None:
            _REQUEST.client_answered_no()

    def send_data(self, msg, addr):
        self.transport.write(_SEPARATOR.join([_ID, msg]), addr)

    def send_with_filename(self, msg, filename, addr):<commit_msg>fix disconnection.<commit_after># main.py - Everything is in there.
# Author: <NAME> < <EMAIL> >
# License: New BSD License (See LICENSE)

import hashlib
import logging
import os
import random
import signal
import string
import sys
import time

from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
from twisted.web import resource, server, static

_DEBUG = '-d' in sys.argv[1:] or '--debug' in sys.argv[1:]

_LOGGING_FORMAT = '%(message)s'
_LOGGING_FORMAT_DEBUG = '[%(levelname)s] %(module)s.%(funcName)s: %(message)s'\

_ROOT_PKG_CACHE = '/var/cache/pacman/pkg/'

_WAITING_TIMER = 1
_REANNOUNCE_TIMER = 60 * 3
_TIMEOUT = 1.5 * _REANNOUNCE_TIMER

_MULTICAST_GROUP = '172.16.17.32'
_MULTICAST_PORT = 19432
_MULTICAST_ADDR = (_MULTICAST_GROUP, _MULTICAST_PORT)

_HTTP_FILE_PORT = _MULTICAST_PORT
_HTTP_PACMAN_PORT = _HTTP_FILE_PORT + 1

_ID = hashlib.sha1('{name}_{random_value}'.format(
    name = os.uname()[1],
    random_value = int(random.random() * 100000000),
)).hexdigest()

_DISCONNECT_MSG = 'flocon: DISCONNECT'
_HAS_MSG = 'flocon: HAS'
_NO_MSG = 'flocon: NO'
_PING_MSG = 'flocon: PING'
_PONG_MSG = 'flocon: PONG'
_YES_MSG = 'flocon: YES'

_SEPARATOR = '-'
_SEPARATOR_F = ' = '

_CLIENTS = dict()

def _list_clients(signum, stack_frame):
    c_len = len(_CLIENTS)
    logging.info('\nThere %s %s client%s connected.',
                 'are' if c_len > 1 else 'is', c_len, 's' if c_len > 1 else '')
    for client in _CLIENTS.values():
        logging.info(' - %s', client)
    logging.info('')

def _find_fallback_mirror():
    with open('/etc/pacman.d/mirrorlist') as f:
        for line in f.readlines():
            if line.startswith("# flocon: Server"):
                try:
                    return line.split('=')[1].strip()
                except IndexError:
                    return None
    return None

_FALLBACK_MIRROR = str(_find_fallback_mirror()) + '/$filename'
_FILE_SERVER = 'http://$ip:$port/$filename'


class Client:
    def __init__(self, id, addr):
        self.id, self.last, self.addr, self.connected = id, None, addr, True
        self.ip, self.port = addr
        self.update()

    def __str__(self):
        if _DEBUG:
            return '{{ip = {}; port = {}; id = {}}}'.format(
                self.ip, self.port, self.id
            )
        else:
            return '{{ip = {}; port = {}}}'.format(self.ip, self.port)

    def update(self):
        self.last = time.time()

    def is_valid(self):
        return self.connected and (time.time() - self.last) < _TIMEOUT

_REQUEST = None

class MulticastClientManager(DatagramProtocol):
    def startProtocol(self):
        self.transport.joinGroup(_MULTICAST_GROUP)
        self.announce_presence()

    def datagramReceived(self, datagram, addr):
        logging.debug('Received a new UDP message from %r', addr)
        logging.debug('Message was: %s', datagram)
        try:
            _id, _msg = datagram.split(_SEPARATOR, 1)
        except ValueError:
            logging.error('I couldn\'t understand last message.')
            return
        if _msg == _PING_MSG or _msg == _PONG_MSG:
            if _id == _ID:
                return
            try:
                _CLIENTS[_id].update()
            except KeyError:
                c = Client(_id, addr)
                logging.info('Client %s connected!', c)
                _CLIENTS[_id] = c
                if _msg == _PING_MSG:
                    self.send_data(_PONG_MSG, addr)
            return

        # From here, if we don't know the client, we just ignore the message.
        try:
            client = _CLIENTS[_id]
        except KeyError:
            logging.error('I don\'t know this client!')
            return

        if _msg == _DISCONNECT_MSG:
            client.connected = False
            timeout_clients()
            return

        # File message.
        try:
            _msg, _filename = _msg.split(_SEPARATOR_F)
        except KeyError:
            logging.error('I couldn\'t understand last file message.')
            return
        if _msg == _HAS_MSG:
            self.has_file(client, _filename)
            return
        if _REQUEST is None or _REQUEST.filename != _filename:
            return
        if _msg == _YES_MSG and _REQUEST is not None:
            _REQUEST.redirect_file_server(_id)
        elif _msg == _NO_MSG and _REQUEST is not None:
            _REQUEST.client_answered_no()

    def send_data(self, msg, addr):
        self.transport.write(_SEPARATOR.join([_ID, msg]), addr)

    def send_with_filename(self, msg, filename, addr):
{'fraction_non_alphanumeric': 0.11645708037178787, 'fraction_numerical': 0.008310552214324767, 'mean_word_length': 2.3221939702143115, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1301033', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2918, 'n_words': 744}