Commit 5c55c648 authored by Julien David's avatar Julien David
Browse files

Merge branch '005-editing-server' into 'master'

WIP : 005 editing server

See merge request !7
parents b14cf2b0 b3cdcd31
Pipeline #1426 passed with stage
in 13 seconds
......@@ -368,3 +368,37 @@ ENTRYPOINT ["python3","rdos_cleaner.py"]
# RUN make
# RUN mv bin/arbogen.byte .
# ENTRYPOINT ["python3","launcher.py"]
#######################################################################################
# Server #
#######################################################################################
# # base image
# FROM alpine:latest
# # maintainer
# LABEL maintainer="david@lipn.fr"
# # variable
# ENV AUTHOR Docker
# ADD database/ /usr/local/server/
# ADD server/ /usr/local/server
# WORKDIR /usr/local/server/
# # Dépendances
# RUN apk update
# RUN apk add python3
# RUN apk add py-pip
# RUN pip install mysql-connector-python
# RUN pip install get_docker_secret
# RUN pip install docker
# CMD ["python3", "server.py"]
# # TO BUILD the image from parent directory "Softwares" Command : docker build --tag "tag" -f ./server/Dockerfile .
# # TO run the build in DOCKER : docker run -d -p 9393:9393 --name "name" ser-app
......@@ -114,12 +114,25 @@ def get_generators():
for result in myresult:
generators[result[0]] = [result]
# Insert in database query send from client and returns insert status
def db_insert(query: dict):
# Returns all existing generators from database in a list
def get_generator_list():
database = database_connection()
generators = {}
sql_get_generators = "SELECT id, tool from generators"
mycur = database.cursor(buffered=False)
mycur.execute(sql_get_generators)
res = mycur.fetchall()
mycur.close()
for a, b in res:
generators[b] = a
return generators
# Insert in database query send from client to server and returns insert status
def add_job(query: dict):
if(query is not None):
database = database_connection()
if(db_job_check(query) != 0):
if(job_check(query) != 0):
query['id'] = str(random.getrandbits(64))
try:
values = query.values()
......@@ -127,16 +140,18 @@ def db_insert(query: dict):
sql_new_job_row = "INSERT INTO jobs (%s) VALUES ('%s');" % (", ".join(cols), "','".join(values))
mycursor = database.cursor(buffered=True)
mycursor.execute(sql_new_job_row)
print("Record inserteds in DB :", query.values)
database.commit()
mycursor.close()
database.close()
return (mycursor.rowcount, "Record inserted successfully into job table")
except mysql.connector.Error as err:
print("MYSQL Error :{}".format(err))
return ("MYSQL Error :{}".format(err))
# Checks existance of query ID in database and returns the status
def db_job_check(query: dict):
# job_check returns 1 if query exist in database else returns 0
def job_check(query: dict):
if (query is not None):
db = database_connection()
try:
......
#!/usr/bin/env python3
# authors: Julien DAVID & Ismail MOUMNI
import socket
import json
import datetime
import os
import sys
import database.database as db
sys.path.append(os.path.realpath('../softwares/'))
import database as db # noqa E402
__RDOS_Dict__ = {}
__RDOS_Dict__ = {"parameters": "request"}
# tools List
__RDOS_Tool__ = {}
......@@ -23,26 +24,67 @@ class RdosServer:
serv.bind((address, PORT))
# Listening to 5 CLients
serv.listen(5)
while True:
# accepting the connection from client and getting client IP
conn, addr = serv.accept()
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')))
print("Requete reçu : ", query)
match = verif_param(query)
if match != query:
print('Erreur query : ', match)
conn.send(bytes(json.dumps(match), "utf-8"))
if (query is not None):
print(query)
query_process(conn, query)
conn.close()
# Function query_process checks query to get generators or to insert query
# Function takes 2 parameters socket and query (get generator query or to insert in database a new job)
# Function returns generators list to client or returns insert result to client
def query_process(sock: socket, query: dict):
# Sending GENERATORS
if (match_query_dict(query, __RDOS_Dict__)):
response = db.get_generators()
sock.send(bytes(json.dumps(response), "utf-8"))
print("database generators ", response)
# Inserting client query in DATABASE
else:
print("ok")
conn.send(bytes(json.dumps(match), "utf-8"))
print("check", check_and_complete_parameters(query))
res = db_send_job(sock, query_fields(sock, check_and_complete_parameters(query)))
print(res)
# Function query_fields generates default fields to insert query send from client to database
# Function takes 2 parameters addr : IP address of client and data : Generators values
# Function return a valid insert query
def query_fields(addr, data):
__RDOS_Tool__ = db.get_generator_list()
sa = __RDOS_Tool__.get(list(data.keys())[0])
lm = list((data.values()))
s = {"id": "", "idGenerator": "", "IP": "", "timeSubmitted": "", "timeExecuted": "",
"timeFinished": "", "status": "", "parametersJSON": "", "directory": "", "url": "", "message": "", "email": ""}
s["id"] = str(os.getuid())
s["idGenerator"] = str(sa)
s["IP"] = addr.getpeername()[0]
s["timeSubmitted"] = (str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
s["parametersJSON"] = json.dumps(lm[0])
return s
# Function query_valwid matches the query send from client side
# with the the parameter dict
# Function db_send_job send a query to insert in database by socket
# Function takes a socket and dictionary for input
# Function returns a string if query send
def db_send_job(s: socket, req: dict):
if(req is not None):
ins = db.add_job(req)
s.send(bytes(json.dumps(ins), "utf-8"))
print("Query send")
else:
raise Exception("Empty Query")
# Function query_valid matches the query send from client side with the the parameter dict
# Function input dictionary from client
# Function output boolean true if match else False
def query_valid(data: dict):
......@@ -53,22 +95,28 @@ def query_valid(data: dict):
return False
# Function json_loads changes a json file into a Dict
# Function json_loads returns a dictionary
def json_to_dict(js):
data = json.loads(js)
return data
# Function json_to_dict changes a json file into a Dict
# Function json_to_dict returns a dictionary
def json_to_dict(data):
if data is not None:
result = json.loads(data)
return result
else:
raise Exception("Dictionnaire Vide!!")
# Function db_req sends a request to get parameters of tools from database
# Function takes a dictionary for input
# Function return a Dict containing tool parameters and default parameters
def db_req(tool: dict):
if dict is not None:
tool_dict = db.getParameters(tool)
return tool_dict
else:
raise Exception("Dictionnaire Vide!!")
# Function match_dict returns non existing fields in a dict
# Function missing_keys 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):
......@@ -84,60 +132,65 @@ def missing_keys(biblio: dict, gen: dict):
# 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
# Function output return trur if dictionaries matches
def match_query_dict(biblio: dict, data: dict):
if biblio is not None and data is not None:
return biblio.keys() == data.keys()
else:
raise ValueError("Error Dict Values ")
raise ValueError("Dictionnaire Vide ")
# Function verif_param verifies json send from client to server by matching it with database query
# Function check_and_complete_parameters 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
def verif_param(data: dict):
tool = data.keys().strip('[]')
s = db.getParameters(data['tool'])
# Function output returns query if it matches with database query keys
def check_and_complete_parameters(data: dict):
if data is not None:
tool = list(data.keys())[0]
s = db.getParameters({tool})
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
# gen = s[0]
parameter = s[1]
q = data.get(tool)
if match_query_dict(parameter.get(tool), q):
check_and_replace(data.get(tool), parameter.get(tool))
return data
else:
raise Exception("Query Doesn't match")
else:
raise Exception("Error Database")
else:
return Exception("Dictionnaire Vide!!", tool)
# check_and_replace checks query with default_parameters of generator and replaces empty values with default_parameters
# Function input takes 2 dictionaries
# Function output returns matched query
def check_and_replace(query: dict, default_parameters: dict):
if query is not None and default_parameters is not None:
for a, b in query.items():
if b == '':
query[a] = default_parameters.get(a)
return query
else:
raise Exception("Dictionnaire Vide!!")
# Function db_generators returns database generators
# Function db_generators returns all 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):
if req is not None:
match = {"request": "generators"}
if(req == match):
s = db.get_generators()
return s
else:
raise Exception("Error Database")
raise Exception("Dictionnaire non Valid")
else:
return Exception("Dictionnaire Vide!!", req)
# 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")
if __name__ == '__main__':
# TEST FOR Docker
RdosServer.server_conn('127.0.0.1', '9393')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment