Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Julien David
rdos
Commits
58eced46
Commit
58eced46
authored
Oct 09, 2020
by
Ismail Moumni
Browse files
adding process query Functions
parent
838d8878
Pipeline
#1373
failed with stage
in 14 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
softwares/database/database.py
View file @
58eced46
...
...
@@ -115,7 +115,7 @@ def get_generators():
generators
[
result
[
0
]]
=
[
result
]
def
li_gen
():
def
li
st
_gen
():
database
=
database_connection
()
generators
=
{}
sql_get_generators
=
"SELECT id, tool from generators"
...
...
@@ -129,7 +129,7 @@ def li_gen():
# Insert in database query send from client and returns insert status 1 if inserted
def
db_
new_job
(
query
:
dict
):
def
db_
insert
(
query
:
dict
):
if
(
query
is
not
None
):
database
=
database_connection
()
if
(
db_job_check
(
query
)
!=
0
):
...
...
@@ -141,15 +141,13 @@ def db_new_job(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
()
res
=
db_job_check
(
query
)
print
(
"res:"
,
res
)
return
res
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
...
...
softwares/server/server.py
View file @
58eced46
...
...
@@ -11,7 +11,7 @@ import database as db # noqa E402
__RDOS_Dict__
=
{
"parameters"
:
"request"
}
# tools List
# __RDOS_Tool__ = db.list_gen
erators
()
# __RDOS_Tool__ = db.list_gen()
class
RdosServer
:
...
...
@@ -24,7 +24,7 @@ class RdosServer:
serv
.
bind
((
address
,
PORT
))
# Listening to 5 CLients
serv
.
listen
(
5
)
while
1
:
while
True
:
# accepting the connection from client and getting client IP
conn
,
addr
=
serv
.
accept
()
if
conn
:
...
...
@@ -34,43 +34,57 @@ class RdosServer:
query
=
(
json
.
loads
(
data
.
decode
(
'utf-8'
)))
print
(
"Requete reçu : "
,
query
)
if
(
query
is
not
None
):
# Sending GENERATORS
print
(
query
)
if
(
match_query_dict
(
query
,
__RDOS_Dict__
)):
response
=
db
.
get_generators
()
conn
.
send
(
bytes
(
json
.
dumps
(
response
),
"utf-8"
))
print
(
"database generators "
,
response
)
conn
.
close
()
# Inserting client query in DATABASE
else
:
print
(
"check"
,
check_and_complete_parameters
(
query
))
res
=
db_server_insert
(
conn
,
query_client
(
addr
,
check_and_complete_parameters
(
query
)))
# print("query : " , query)
if
(
res
==
1
):
conn
.
send
(
bytes
(
res
,
"utf-8"
))
else
:
conn
.
send
(
bytes
(
"query not inserted !!"
,
"utf-8"
))
conn
.
close
()
# Function query_client creates an insert query to send to server
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
(
"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_
client
(
addr
,
data
):
__RDOS_Tool__
=
db
.
li_gen
()
def
query_
fields
(
addr
,
data
):
__RDOS_Tool__
=
db
.
li
st
_gen
()
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"
]
=
str
(
addr
[
0
]
)
s
[
"IP"
]
=
addr
.
getpeername
()
[
0
]
s
[
"timeSubmitted"
]
=
(
str
(
datetime
.
datetime
.
now
().
strftime
(
"%Y-%m-%d %H:%M:%S"
)))
s
[
"parametersJSON"
]
=
json
.
dumps
(
lm
[
0
])
print
(
s
)
return
s
# Function db_new_job send a query to insert in database by socket
# Function db_new_job takes a socket and dictionary for input
# Function db_new_job returns a string if query send
def
db_send_job
(
s
:
socket
,
req
:
dict
):
if
(
req
is
not
None
):
ins
=
db
.
db_new_job
(
req
)
s
.
send
(
bytes
(
json
.
dumps
(
ins
),
"utf-8"
))
print
(
"Query send"
)
else
:
raise
Exception
(
"Empty Query"
)
# Function query_valwid matches the query send from client side
# with the the parameter dict
# Function input dictionary from client
...
...
@@ -125,7 +139,7 @@ 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 check_and_complete_parameters verifies json send from client to server by matching it with database query
...
...
@@ -151,15 +165,17 @@ def check_and_complete_parameters(data):
raise
Exception
(
"Dictionnaire Vide!!"
)
# Function check_and_replace checks query with
that
and replaces
the
empty values
# Function check_and_replace checks query with
default_parameters of generator
and replaces empty values
with default_parameters from generator
# Function input takes 2 dictionaries
# Function output returns matched query
def
check_and_replace
(
query
,
data
):
if
query
is
not
None
and
d
ata
is
not
None
:
def
check_and_replace
(
query
:
dict
,
default_parameters
:
dict
):
if
query
is
not
None
and
d
efault_parameters
is
not
None
:
for
a
,
b
in
query
.
items
():
if
b
==
''
:
query
[
a
]
=
data
.
get
(
a
)
return
query
query
[
a
]
=
default_parameters
.
get
(
a
)
return
query
else
:
raise
Exception
(
"Dictionnaire Vide!!"
)
# Function db_generators returns database generators
...
...
@@ -177,18 +193,6 @@ def db_generators(req: dict):
raise
Exception
(
"Dictionnaire Vide!!"
)
# 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_server_insert
(
s
:
socket
,
req
:
dict
):
if
(
req
is
not
None
):
ins
=
db
.
db_insert
(
req
)
s
.
send
(
bytes
(
json
.
dumps
(
ins
),
"utf-8"
))
print
(
"Query send"
)
else
:
raise
Exception
(
"Invalid Query"
)
if
__name__
==
'__main__'
:
# TEST FOR Docker
RdosServer
.
server_conn
(
'127.0.0.1'
,
'9393'
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment