<commit_before>import socket
import threading
import SocketServer
import json

from nysa.host.driver.gpio import GPIO

from server_base import ServerBase

MAX_RECV_SIZE = 4096

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
    commands = {}

    def setup(self):
        self.commands = {}
        self.commands["set_direction"] = self.set_direction
        self.commands["set_value"] = self.set_value
        self.commands["get_value"] = self.get_value
        self.commands["setup_interrupt"] = self.setup_interrupt

    def handle(self):
        print "In listening thread handler..."
        data = self.request.recv(MAX_RECV_SIZE)
        cur_thread = threading.current_thread()
        request_dict = {}
        rd = {}
        response = ""
        try:
            request_dict = json.loads(data)
        except ValueError as e:
            rd["response"] = "error"
            rd["error"] = "{}: Error while parsing JSON String: %s".format(str(e))
            self.request.sendall(json.dumps(rd))
            return

        #Process request....
        print "request dict: %s" % str(request_dict)

        if "command" not in request_dict:
            rd["response"] = "error"
            rd["error"] = "%s not in commands" % request_dict["command"]
            self.request.sendall(json.dumps(rd))
            return

        #response = "{}: {}".format(cur_thread.name, data)
        rd = self.commands[request_dict["command"]](request_dict)
        #rd["port"] = request_dict["port"]
        self.request.sendall(json.dumps(rd))

    def set_direction(self, d):
        rd = {}
        rd["response"] = "ok"
        print "TODO"
        print "setting direction"
        return rd

    def set_value(self, d):
        rd = {}
        rd["response"] = "ok"
        print "TODO"
        print "set value"
        return rd

    def get_value(self, d):
        rd = {}
        rd["response"] = "ok"
        print "TODO"
        print "get value"
        return rd

    def setup_interrupt(self, d):
        rd = {}
        rd["response"] = "ok"
        print "TODO"
        print "setup interrupt"
        return rd

    def interrupt_callback(self, callback):
        rd = {}
        rd["response"] = "ok"
        print "GPIO Interrupt callback!"
        return rd


class NysaGPIOServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer, ServerBase):

    @staticmethod
    def name():
        return "gpio"
    
    def setup(self, nysa):
        super (NysaGPIOServer, nysa)
        self.urn = nysa.find_device(GPIO)[0]
        print "Found a GPIO device: %s" % self.urn
        self.gpio = GPIO(self.n, self.urn)

def start_gpio_server(host = "localhost", port = None, nysa = None):
    server = NysaGPIOServer((host, port), ThreadedTCPRequestHandler)
    ip, port = server.server_address
<commit_msg>Filling in functions for the gpio server<commit_after>import socket
import threading
import SocketServer
import json

from nysa.host.driver.gpio import GPIO

from server_base import ServerBase

MAX_RECV_SIZE = 4096

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
    commands = {}

    def setup(self):
        self.commands = {}
        self.commands["set_direction"] = self.set_direction
        self.commands["set_value"] = self.set_value
        self.commands["get_value"] = self.get_value
        self.commands["setup_interrupt"] = self.setup_interrupt

    def handle(self):
        print "In listening thread handler..."
        data = self.request.recv(MAX_RECV_SIZE)
        cur_thread = threading.current_thread()
        request_dict = {}
        rd = {}
        response = ""
        try:
            request_dict = json.loads(data)
        except ValueError as e:
            rd["response"] = "error"
            rd["error"] = "{}: Error while parsing JSON String: %s".format(str(e))
            self.request.sendall(json.dumps(rd))
            return

        #Process request....
        print "request dict: %s" % str(request_dict)

        if "command" not in request_dict:
            rd["response"] = "error"
            rd["error"] = "%s not in commands" % request_dict["command"]
            self.request.sendall(json.dumps(rd))
            return

        #response = "{}: {}".format(cur_thread.name, data)
        rd = self.commands[request_dict["command"]](request_dict)
        #rd["port"] = request_dict["port"]
        self.request.sendall(json.dumps(rd))

    def set_direction(self, d):
        rd = {}
        rd["response"] = "ok"
        print "setting direction"
        if "pin" not in rd:
            rd["response"] = "error"
            rd["error"] = "pin entry not in request"
            return rd

        if "direction" not in rd:
            rd["response"] = "error"
            rd["error"] = "direction entry not in request"
            return rd


        if isinstance(rd["pin"], list):
            if not isinstance(rd["direction"], list):
                rd["response"] = "error"
                rd["error"] = "pin is a list and direction is not a list"
                return rd

            if len(rd["direction"]) != len(rd["pin"]):
                rd["response"] = "error"
                rd["error"] = "pin is a list and direction is not a list"
                return rd
               
            for i in range(len(rd["pin"])):
                self.gpio.set_pin_direction(rd["pin"[i], rd["direction"][i])

        else:
            pin = rd["pin"]
            direction = rd["direction"]
            self.gpio.set_pin_direction(rd["pin"], rd["direction")
        
        return rd

    def set_value(self, d):
        rd = {}
        print "set value"
        rd["response"] = "ok"
        print "setting value"
        if "pin" not in rd:
            rd["response"] = "error"
            rd["error"] = "pin entry not in request"
            return rd

        if "value" not in rd:
            rd["response"] = "error"
            rd["error"] = "value entry not in request"
            return rd


        if isinstance(rd["pin"], list):
            if not isinstance(rd["value"], list):
                rd["response"] = "error"
                rd["error"] = "pin is a list and value is not a list"
                return rd

            if len(rd["value"]) != len(rd["pin"]):
                rd["response"] = "error"
                rd["error"] = "pin is a list and value is not a list"
                return rd
               
            for i in range(len(rd["pin"])):
                self.gpio.set_bit_value(rd["pin"[i], rd["value"][i])

        else:
            pin = rd["pin"]
            value = rd["value"]
            self.gpio.set_bit_value(rd["pin"], rd["value")
        
        return rd



    def get_value(self, d):
        rd = {}
        rd["response"] = "ok"
        print "TODO"
        print "get value"
        return rd

    def setup_interrupt(self, d):
        print "setup interrupt"
        rd = {}
        print "set interrupt"
        rd["response"] = "ok"
        print "setting interrupt"
        if "pin" not in rd:
            rd["response"] = "error"
            rd["error"] = "pin entry not in request"
            return rd

        if "interrupt" not in rd:
            rd["response"] = "error"
            rd["error"] = "interrupt entry not in request"
            return rd


        if isinstance(rd["pin"], list):
            if not isinstance(rd["interrupt"], list):
                rd["response"] = "error"
                rd["error"] = "pin is a list and interrupt is not a list"
                return rd

            if len(rd["interrupt"]) != len(rd["pin"]):
                rd["response"] = "error"
                rd["error"] = "pin is a list and interrupt is not a list"
                return rd
               
            for i in range(len(rd["pin"])):
                self.gpio.set_interrupt_both_edge(rd["pin"[i], rd["interrupt"][i])

        else:
            pin = rd["pin"]
            interrupt = rd["interrupt"]
            self.gpio.set_bit_interrupt_both_edge(rd["pin"], rd["interrupt")
        
        return rd

    def interrupt_callback(self, callback):
        rd = {}
        rd["response"] = "ok"
        print "GPIO Interrupt callback!"
        return rd


class NysaGPIOServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer, ServerBase):

    @staticmethod
    def name():
        return "gpio"
    
    def setup(self, nysa):
        super (NysaGPIOServer, nysa)
        self.urn = nysa.find_device(GPIO)[0]
        print "Found a GPIO device: %s" % self.urn
        self.gpio = GPIO(self.n, self.urn)

def start_gpio_server(host = "localhost", port = None, nysa = None):
    server = NysaGPIOServer((host, port), ThreadedTCPRequestHandler)
    ip, port = server.server_address

{'fraction_non_alphanumeric': 0.1266096866096866, 'fraction_numerical': 0.0011396011396011395, 'mean_word_length': 1.9214380825565913, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3382494', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2503, 'n_words': 656}