<commit_before>import os
import random
import re
import time
import twisted
from twisted.application.service import Application
from twisted.internet.error import AlreadyCancelled
from twisted.python import log
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import DailyLogFile
from coapthon import defines
from coapthon.messages.message import Message
from coapthon.messages.option import Option
from coapthon.messages.request import Request
from coapthon.messages.response import Response
from coapthon.resources.resource import Resource
from coapthon.serializer import Serializer
from twisted.internet import task
from coapthon.utils import Tree
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

__author__ = '<NAME>'
__version__ = "2.0"


home = os.path.expanduser("~")
if not os.path.exists(home + "/.coapthon/"):
    os.makedirs(home + "/.coapthon/")

# First, startLogging to capture stdout
logfile = DailyLogFile("CoAPthon_client.log", home + "/.coapthon/")
# Now add an observer that logs to a file
application = Application("CoAPthon_Client")
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)


class CoAP(DatagramProtocol):
    def __init__(self, server, forward):
        print "INIT CLIENT\n"
        self._forward = forward
        self.received = {}
        self.sent = {}
        self.sent_token = {}
        self.received_token = {}
        self.call_id = {}
        self.relation = {}
        self._currentMID = 1
        import socket
        try:
            socket.inet_aton(server[0])
            self.server = server
            # legal
        except socket.error:
            # Not legal
            data = socket.getaddrinfo(server[0], server[1])

            self.server = (server[0], server[1])

        # defer = reactor.resolve('coap.me')
        # defer.addCallback(self.start)
        # self.server = (None, 5683)

        root = Resource('root', visible=False, observable=False, allow_children=True)
        root.path = '/'
        self.root = Tree(root)
        self.operations = []
        self.l = None
        self.transport = None

    @property
    def current_mid(self):
        return self._currentMID

    @current_mid.setter
    def current_mid(self, c):
        self._currentMID = c

    def set_operations(self, operations):
        for op in operations:
            function, args, kwargs, client_callback = op
            self.operations.append((function, args, kwargs, client_callback))
        host, port = self.server
        if host is not None:
            self.start(host)

    def startProtocol(self):
        print "STARTPROTOCOL\n"
        if self.server is None:
            log.err("Server address for the client is not initialized")
            exit()
        self.l = task.LoopingCall(self.purge_mids)
        self.l.start(defines.EXCHANGE_LIFETIME)

    def stopProtocol(self):
        self.l.stop()

    def purge_mids(self):
        log.msg("Purge mids")
        now = time.time()
        sent_key_to_delete = []
        for key in self.sent.keys():
            message, timestamp, callback, client_callback = self.sent.get(key)
            if timestamp + defines.EXCHANGE_LIFETIME <= now:
                sent_key_to_delete.append(key)
        for key in sent_key_to_delete:
            message, timestamp, callback, client_callback = self.sent.get(key)
            key_token = hash(str(self.server[0]) + str(self.server[1]) + str(message.token))
            try:
                del self.sent[key]
            except KeyError:
                pass
            try:
                del self.received[key]
            except KeyError:
                pass
            try:
                del self.sent_token[key_token]
            except KeyError:
                pass
            try:
                del self.received_token[key_token]
            except KeyError:
                pass

    def start(self, host):
        print "START\n"
        # self.transport.connect(host, self.server[1])
        function, args, kwargs, client_callback = self.get_operation()
        function(client_callback, *args, **kwargs)

    def start_test(self, transport):
        self.transport = transport
        function, args, kwargs, client_callback = self.get_operation()
        function(client_callback, *args, **kwargs)

    def get_operation(self):
        try:
            to_exec = self.operations.pop(0)
            args = []
            kwargs = {}
            if len(to_exec) == 4:
                function, args, kwargs, client_callback = to_exec
            elif len(to_exec) == 3:
                function, args, client_callback = to_exec
            elif len(to_exec) == 2:
                function, client_callback = to_exec[0]
            else:
                return None, None, None, None
            return function, args, kwargs, client_callback
        except IndexError:
            return None, None, None, None

    def send(self, message):
        print "SEND\n"
        serializer = Serializer()
        # host, port = message.destination
        # print "Message sent to " + host + ":" + str(port)
        # print "----------------------------------------"
        # print message
        # print "----------------------------------------"
        datagram = serializer.serialize(message)
        log.msg("Send datagram")
        print self.transport
        print self.transport.getPeer()
        print self.transport.getHost()
        self.transport.write(datagram, self.server)

    def send_callback(self, req, callback, client_callback):
        if req.mid is None:
            self._currentMID += 1
            req.mid = self._currentMID
        key = hash(str(self.server[0]) + str(self.server[1]) + str(req.mid))
        key_token = hash(str(self.server[0]) + str(self.server[1]) + str(req.token))
        self.sent[key] = (req, time.time(), callback, client_callback)
        self.sent_token[key_token] = (req, time.time(), callback, client_callback)
        if isinstance(client_callback, tuple) and len(client_callback) > 1:
            client_callback, err_callback = client_callback
        else:
            err_callback = None
        self.schedule_retrasmission(req, err_callback)
        self.send(req)

    def datagramReceived(self, datagram, host):<commit_msg>bug fix<commit_after>import os
import random
import re
import time
import twisted
from twisted.application.service import Application
from twisted.internet.error import AlreadyCancelled
from twisted.python import log
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import DailyLogFile
from coapthon import defines
from coapthon.messages.message import Message
from coapthon.messages.option import Option
from coapthon.messages.request import Request
from coapthon.messages.response import Response
from coapthon.resources.resource import Resource
from coapthon.serializer import Serializer
from twisted.internet import task
from coapthon.utils import Tree
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor

__author__ = '<NAME>'
__version__ = "2.0"


home = os.path.expanduser("~")
if not os.path.exists(home + "/.coapthon/"):
    os.makedirs(home + "/.coapthon/")

# First, startLogging to capture stdout
logfile = DailyLogFile("CoAPthon_client.log", home + "/.coapthon/")
# Now add an observer that logs to a file
application = Application("CoAPthon_Client")
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)


class CoAP(DatagramProtocol):
    def __init__(self, server, forward):
        print "INIT CLIENT\n"
        self._forward = forward
        self.received = {}
        self.sent = {}
        self.sent_token = {}
        self.received_token = {}
        self.call_id = {}
        self.relation = {}
        self._currentMID = 1
        import socket
        try:
            socket.inet_aton(server[0])
            self.server = server
            # legal
        except socket.error:
            # Not legal
            data = socket.getaddrinfo(server[0], server[1])

            self.server = (server[0], server[1])

        # defer = reactor.resolve('coap.me')
        # defer.addCallback(self.start)
        # self.server = (None, 5683)

        root = Resource('root', visible=False, observable=False, allow_children=True)
        root.path = '/'
        self.root = Tree(root)
        self.operations = []
        self.l = None
        self.transport = None

    @property
    def current_mid(self):
        return self._currentMID

    @current_mid.setter
    def current_mid(self, c):
        self._currentMID = c

    def set_operations(self, operations):
        for op in operations:
            function, args, kwargs, client_callback = op
            self.operations.append((function, args, kwargs, client_callback))
        host, port = self.server
        if host is not None:
            self.start(host)

    def startProtocol(self):
        print "STARTPROTOCOL\n"
        if self.server is None:
            log.err("Server address for the client is not initialized")
            exit()
        self.l = task.LoopingCall(self.purge_mids)
        self.l.start(defines.EXCHANGE_LIFETIME)

    def stopProtocol(self):
        self.l.stop()

    def purge_mids(self):
        log.msg("Purge mids")
        now = time.time()
        sent_key_to_delete = []
        for key in self.sent.keys():
            message, timestamp, callback, client_callback = self.sent.get(key)
            if timestamp + defines.EXCHANGE_LIFETIME <= now:
                sent_key_to_delete.append(key)
        for key in sent_key_to_delete:
            message, timestamp, callback, client_callback = self.sent.get(key)
            key_token = hash(str(self.server[0]) + str(self.server[1]) + str(message.token))
            try:
                del self.sent[key]
            except KeyError:
                pass
            try:
                del self.received[key]
            except KeyError:
                pass
            try:
                del self.sent_token[key_token]
            except KeyError:
                pass
            try:
                del self.received_token[key_token]
            except KeyError:
                pass

    def start(self, host):
        print "START\n"
        # self.transport.connect(host, self.server[1])
        function, args, kwargs, client_callback = self.get_operation()
        function(client_callback, *args, **kwargs)

    def start_test(self, transport):
        self.transport = transport
        function, args, kwargs, client_callback = self.get_operation()
        function(client_callback, *args, **kwargs)

    def get_operation(self):
        try:
            to_exec = self.operations.pop(0)
            args = []
            kwargs = {}
            if len(to_exec) == 4:
                function, args, kwargs, client_callback = to_exec
            elif len(to_exec) == 3:
                function, args, client_callback = to_exec
            elif len(to_exec) == 2:
                function, client_callback = to_exec[0]
            else:
                return None, None, None, None
            return function, args, kwargs, client_callback
        except IndexError:
            return None, None, None, None

    def send(self, message):
        print "SEND\n"
        serializer = Serializer()
        # host, port = message.destination
        # print "Message sent to " + host + ":" + str(port)
        # print "----------------------------------------"
        # print message
        # print "----------------------------------------"
        datagram = serializer.serialize(message)
        log.msg("Send datagram")
        print self.transport
        print self.transport.getPeer()
        print self.transport.getHost()
        self.transport.write(datagram, message.destination)

    def send_callback(self, req, callback, client_callback):
        if req.mid is None:
            self._currentMID += 1
            req.mid = self._currentMID
        key = hash(str(self.server[0]) + str(self.server[1]) + str(req.mid))
        key_token = hash(str(self.server[0]) + str(self.server[1]) + str(req.token))
        self.sent[key] = (req, time.time(), callback, client_callback)
        self.sent_token[key_token] = (req, time.time(), callback, client_callback)
        if isinstance(client_callback, tuple) and len(client_callback) > 1:
            client_callback, err_callback = client_callback
        else:
            err_callback = None
        self.schedule_retrasmission(req, err_callback)
        self.send(req)

    def datagramReceived(self, datagram, host):
{'fraction_non_alphanumeric': 0.10983658076681332, 'fraction_numerical': 0.004085480829666876, 'mean_word_length': 2.1554288547347547, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '4603820', 'n_tokens_mistral': 3586, 'n_tokens_neox': 3499, 'n_words': 874}