server.py 4.62 KB
Newer Older
Ismail Moumni's avatar
Ismail Moumni committed
1
# authors: Julien DAVID & Ismail MOUMNI
Ismail-Moumni's avatar
Ismail-Moumni committed
2
import socket
Ismail Moumni's avatar
Ismail Moumni committed
3
import json
Ismail Moumni's avatar
Ismail Moumni committed
4
import os
Ismail Moumni's avatar
Ismail Moumni committed
5
import sys
Ismail Moumni's avatar
edit    
Ismail Moumni committed
6
import database.database as db
Ismail Moumni's avatar
Ismail Moumni committed
7
sys.path.append(os.path.realpath('../softwares/'))
Ismail Moumni's avatar
Ismail Moumni committed
8
9


Ismail Moumni's avatar
Ismail Moumni committed
10
__RDOS_Dict__ = {}
Ismail Moumni's avatar
Ismail Moumni committed
11
12

# tools List
Ismail Moumni's avatar
Ismail Moumni committed
13
__RDOS_Tool__ = {}
Ismail Moumni's avatar
Ismail Moumni committed
14

Ismail Moumni's avatar
Ismail Moumni committed
15

Ismail Moumni's avatar
Ismail Moumni committed
16
17
18
19
20
class RdosServer:

    # Function server_conn creates a socket and listens on port 9393
    # Function input address : SERVER IP ADRESS & Port server Port
    # Function output query send from client to server
Ismail Moumni's avatar
Ismail Moumni committed
21
22
23
24
25
26
    def server_conn(address, PORT):
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
            serv.bind((address, PORT))
            # Listening to 5 CLients
            serv.listen(5)
            # accepting the connection from client and getting client IP
Ismail Moumni's avatar
Ismail Moumni committed
27
            conn, addr = serv.accept()
Ismail Moumni's avatar
Ismail Moumni committed
28
29
30
31
32
33
        while True:
            if conn:
                print('Connexion acceptee depuis l IP : ', addr)
                # Receiving Data from Client
                data = conn.recv(4096)
                query = (json.loads(data.decode('utf-8')))
Ismail Moumni's avatar
Ismail Moumni committed
34
                print("Requete reçu : ", query)
Ismail Moumni's avatar
editing    
Ismail Moumni committed
35
                match = verif_param(query)
Ismail Moumni's avatar
Ismail Moumni committed
36
37
                if match != query:
                    print('Erreur query : ', match)
Ismail Moumni's avatar
editing    
Ismail Moumni committed
38
39
                    conn.send(bytes(json.dumps(match), "utf-8"))
                else:
Ismail Moumni's avatar
Ismail Moumni committed
40
                    print("ok")
Ismail Moumni's avatar
editing    
Ismail Moumni committed
41
                    conn.send(bytes(json.dumps(match), "utf-8"))
Ismail Moumni's avatar
Ismail Moumni committed
42
43


Ismail Moumni's avatar
Ismail Moumni committed
44
# Function query_valwid matches the query send from client side
Ismail Moumni's avatar
Ismail Moumni committed
45
#  with the the parameter dict
Ismail Moumni's avatar
Ismail Moumni committed
46
# Function input dictionary from client
Ismail Moumni's avatar
Ismail Moumni committed
47
48
49
# Function output boolean true if match else False
def query_valid(data: dict):
    if 'parameters' in data:
Ismail Moumni's avatar
Ismail Moumni committed
50
        if (data['parameters'] != '') & (len(data) <= 2):
Ismail Moumni's avatar
Ismail Moumni committed
51
            return True
Ismail Moumni's avatar
edit    
Ismail Moumni committed
52
53
        else:
            return False
Ismail Moumni's avatar
Ismail Moumni committed
54
55
56


# Function json_loads changes a json file into a Dict
Ismail Moumni's avatar
Ismail Moumni committed
57
# Function json_loads returns a dictionary
Ismail Moumni's avatar
Ismail Moumni committed
58
59
60
61
62
def json_to_dict(js):
    data = json.loads(js)
    return data


Ismail Moumni's avatar
Ismail Moumni committed
63
# Function db_req sends a request to get parameters of tools from database
Ismail Moumni's avatar
Ismail Moumni committed
64
# Function takes a dictionary for input
Ismail Moumni's avatar
Ismail Moumni committed
65
66
# Function return a Dict containing tool parameters and default parameters
def db_req(tool: dict):
Ismail Moumni's avatar
edit    
Ismail Moumni committed
67
68
    tool_dict = db.getParameters(tool)
    return tool_dict
Ismail Moumni's avatar
Ismail Moumni committed
69
70


Ismail Moumni's avatar
Ismail Moumni committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Function match_dict returns non existing fields in a dict
# Function input biblio and dict
# Function returns a list containing missing values
def missing_keys(biblio: dict, gen: dict):
    missing = []
    if(biblio is not None and gen is not None):
        for key in biblio:
            if key not in gen:
                missing.append(key)
        return missing
    else:
        raise Exception("Dictionnaire Vide!!")


Ismail Moumni's avatar
Ismail Moumni committed
85
86
87
# Function match_query_dict matches two dictionaries
# Function input takes 2 dictionaries to match
# Function output return trur if dictionaries matches else raise a value error exception
Ismail Moumni's avatar
Ismail Moumni committed
88
89
90
91
def match_query_dict(biblio: dict, data: dict):
    if biblio is not None and data is not None:
        return biblio.keys() == data.keys()
    else:
Ismail Moumni's avatar
Ismail Moumni committed
92
        raise ValueError("Error Dict Values ")
Ismail Moumni's avatar
Ismail Moumni committed
93
94


Ismail Moumni's avatar
Ismail Moumni committed
95
96
97
# Function verif_param verifies json send from client to server by matching it with database query
# Function input takes a dictionary
# Function output returns query if it matches with database query keys if not it raises an error
Ismail Moumni's avatar
Ismail Moumni committed
98
99
def verif_param(data: dict):
    tool = data.keys().strip('[]')
Ismail Moumni's avatar
edit    
Ismail Moumni committed
100
    s = db.getParameters(data['tool'])
Ismail Moumni's avatar
Ismail Moumni committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    parameter = {}
    query = {}
    if s is not None:
        gen = s[0]
        gen_default = s[1]
        parameter = gen_default
        query = data.get(tool)
        if match_query_dict(gen.get(tool), query):
            for a in query:
                if query[a] == "":
                    print(query[a])
                    query[a] = parameter.get(a)
            return query
        else:
            raise Exception("Query Doesn't match")
    else:
        raise Exception("Error Database")
Ismail Moumni's avatar
Ismail Moumni committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143


# Function db_generators returns database generators
# Function takes a dictionary request from client side
# Function returns all database generators or raises an error if request doesn't match
def db_generators(req: dict):
    match = {"request": "generators"}
    if(req == match):
        s = db.get_generators()
        return s
    else:
        raise Exception("Error Database")


# Function db_insert send a query to insert in database by socket
# Function db_insert takes a socket and dictionary for input
# Function db_insert returns a string if query send
def db_insert(s: socket, req: dict):
    if(req is not None):
        if (verif_param(req) == req):
            ins = db.insert_query(req)
            s.send(bytes(json.dumps(ins), "utf-8"))
            print("Query send")
        else:
            raise Exception("Query Doesn't match")
    raise Exception("Invalid Query")