<commit_before>import socket
import socketserver
import threading
import json

from node import Node
from ring import ConsistentRing
import api.tcpclient as client


# The node containing container (our node container only) and the ring metadata
node = None

# Globals for commands
ADD_NODE = {
    "cmd": "addnode",
    "args": 1,
    "response_ok": 1,
    "response_err": -1,
}
RM_NODE = {
    "cmd": "rmnode",
    "args": 1,
    "response_ok": 2,
    "response_err": -2,
}
ADD = {
    "cmd": "add",
    "args": 2,
    "response_ok": 3,
    "response_err": -3,
}
GET = {
    "cmd": "get",
    "args": 1,
    "response_ok": 4,
    "response_err": -4,
}
STATS = {
    "cmd": "stats",
    "args": 0,
    "response_ok": 5,
    "response_err": -5,
}
ERROR = {
    "cmd": None,
    "args": 0,
    "response_ok": 0,
    "response_err": -99,
}

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

    def handle(self):

        # Protocol
        # --------   
        # 
        # addnode {key}
        # rmnode {key}
        # add {key} {value}
        # get {key}
        # stats
        #
        # Returned data
        # -------------
        #
        # Added node: 1 {message}
        # Error added node: -1 {message}
        # Removed node: 2 {message}
        # Error removed node: -2 {message}
        # Added key: 3 {message}
        # Error added key: -3 {message}
        # get key: 4 {key} {value}
        # Missed key: -4 {message}
        # stats: 5 {stats}
        # Error stats: -5 {stats}
        # CMD Error : -99 {message}


        # Get data and split
        data = str(self.request.recv(1024), 'utf-8').split(None, 2)

        command = data[0]

        if command == ADD_NODE["cmd"]:
            if len(data)-1 < ADD_NODE["args"]:
                response = "-99 Wrong parameters"
            else:
                # TODO: Check key
                self._add_node(data[1])
                response = "{0} {1}".format(ADD_NODE["response_ok"], "Added node")

        elif command == RM_NODE["cmd"]:
            if len(data)-1 < RM_NODE["args"]:
                response = "-99 Wrong parameters"
            else:
                # TODO: Check key
                self._rm_node(data[1])
                response = "{0} {1}".format(RM_NODE["response_ok"], "removed node")

        elif command == ADD["cmd"]:
            if len(data)-1 < ADD["args"]:
                response = "-99 Wrong parameters"
            else:
                # TODO: Check key
                try:
                    self._add_data(data[1], data[2])
                    response = "{0} {1}".format(ADD["response_ok"], "Added data")
                except ConnectionRefusedError:
                    response = "{0} {1}".format(ADD["response_err"], "Connection error")

        elif command == GET["cmd"]:
            if len(data)-1 < GET["args"]:
                response = "-99 Wrong parameters"
            else:
                # TODO: Check key
                try:
                    res_data = self._get_data(data[1])
                    if not res_data:
                        response = "{0} {1}".format(GET["response_err"], "Missed data")
                    else:
                        response = "{0} {1}".format(GET["response_ok"], res_data)
                except ConnectionRefusedError:
                    response = "{0} {1}".format(GET["response_err"], "Connection error")

        elif command == STATS["cmd"]:
            if len(data)-1 < STATS["args"]:
                response = "-99 Wrong parameters"
            else:
                response = json.dumps(node.stats())
        else:
            response = "-99 Wrong command"


        self.request.sendall(bytes(response, 'utf-8'))

<commit_msg>Check keys for nodes<commit_after>import socket
import socketserver
import threading
import json
import re

from node import Node
from ring import ConsistentRing
import api.tcpclient as client


# The node containing container (our node container only) and the ring metadata
node = None

# Globals for commands
ADD_NODE = {
    "cmd": "addnode",
    "args": 1,
    "response_ok": 1,
    "response_err": -1,
}
RM_NODE = {
    "cmd": "rmnode",
    "args": 1,
    "response_ok": 2,
    "response_err": -2,
}
ADD = {
    "cmd": "add",
    "args": 2,
    "response_ok": 3,
    "response_err": -3,
}
GET = {
    "cmd": "get",
    "args": 1,
    "response_ok": 4,
    "response_err": -4,
}
STATS = {
    "cmd": "stats",
    "args": 0,
    "response_ok": 5,
    "response_err": -5,
}
ERROR = {
    "cmd": None,
    "args": 0,
    "response_ok": 0,
    "response_err": -99,
}

key_regex = re.compile("\d\.\d\.\d\.\d:\d")

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

    def handle(self):

        # Protocol
        # --------   
        # 
        # addnode {key}
        # rmnode {key}
        # add {key} {value}
        # get {key}
        # stats
        #
        # Returned data
        # -------------
        #
        # Added node: 1 {message}
        # Error added node: -1 {message}
        # Removed node: 2 {message}
        # Error removed node: -2 {message}
        # Added key: 3 {message}
        # Error added key: -3 {message}
        # get key: 4 {key} {value}
        # Missed key: -4 {message}
        # stats: 5 {stats}
        # Error stats: -5 {stats}
        # CMD Error : -99 {message}


        # Get data and split
        data = str(self.request.recv(1024), 'utf-8').split(None, 2)

        command = data[0]

        if command == ADD_NODE["cmd"]:
            if len(data)-1 < ADD_NODE["args"]:
                response = "-99 Wrong parameters"
            else:
                if key_regex.search(data[1]):
                    self._add_node(data[1])
                    response = "{0} {1}".format(ADD_NODE["response_ok"], "Added node")
                else:
                    response = "{0} {1}".format(ADD_NODE["response_err"], "bad argument")

        elif command == RM_NODE["cmd"]:
            if len(data)-1 < RM_NODE["args"]:
                response = "-99 Wrong parameters"
            else:
                if key_regex.search(data[1]):
                    self._rm_node(data[1])
                    response = "{0} {1}".format(RM_NODE["response_ok"], "removed node")
                else:
                    response = "{0} {1}".format(RM_NODE["response_err"], "bad argument")

        elif command == ADD["cmd"]:
            if len(data)-1 < ADD["args"]:
                response = "-99 Wrong parameters"
            else:
                try:
                    self._add_data(data[1], data[2])
                    response = "{0} {1}".format(ADD["response_ok"], "Added data")
                except ConnectionRefusedError:
                    response = "{0} {1}".format(ADD["response_err"], "Connection error")

        elif command == GET["cmd"]:
            if len(data)-1 < GET["args"]:
                response = "-99 Wrong parameters"
            else:
                try:
                    res_data = self._get_data(data[1])
                    if not res_data:
                        response = "{0} {1}".format(GET["response_err"], "Missed data")
                    else:
                        response = "{0} {1}".format(GET["response_ok"], res_data)
                except ConnectionRefusedError:
                    response = "{0} {1}".format(GET["response_err"], "Connection error")

        elif command == STATS["cmd"]:
            if len(data)-1 < STATS["args"]:
                response = "-99 Wrong parameters"
            else:
                response = json.dumps(node.stats())
        else:
            response = "-99 Wrong command"


        self.request.sendall(bytes(response, 'utf-8'))


{'fraction_non_alphanumeric': 0.1478699987051664, 'fraction_numerical': 0.020199404376537614, 'mean_word_length': 1.6236413043478262, 'pattern_counts': {'":': 48, '<': 13, '<?xml version=': 0, '>': 3, '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': '2590940', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2276, 'n_words': 593}