server side and game instance
This commit is contained in:
parent
3592f07d64
commit
c01c7ff2c2
3 changed files with 197 additions and 0 deletions
136
game_instance/game.py
Normal file
136
game_instance/game.py
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
from flask import Flask, jsonify, request,send_file
|
||||||
|
from flask_cors import CORS, cross_origin
|
||||||
|
import docker
|
||||||
|
import socket
|
||||||
|
import requests
|
||||||
|
import numpy as np
|
||||||
|
import ast
|
||||||
|
from ast import literal_eval
|
||||||
|
import json
|
||||||
|
from PIL import Image
|
||||||
|
from matplotlib import cm
|
||||||
|
from io import StringIO, BytesIO
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
cors=CORS(app)
|
||||||
|
app.config["CORS_HEADERS"]='Content-Type'
|
||||||
|
|
||||||
|
discoveryservice_url = "192.168.37.137:50005"
|
||||||
|
|
||||||
|
cardManager_url = requests.get(url = "http://" + discoveryservice_url + "/CardManager").text
|
||||||
|
cardstext = requests.get(url = "http://" + cardManager_url + "/recupCarte/14/2").text
|
||||||
|
cards=np.array(json.loads(cardstext))
|
||||||
|
print(len(cards))
|
||||||
|
carre=np.uint8(cards)
|
||||||
|
print(type(carre[0][0][0][0]))
|
||||||
|
#res=str(cards[0])
|
||||||
|
#print(res)
|
||||||
|
#print(type(res))
|
||||||
|
i=0
|
||||||
|
list_ai=[]
|
||||||
|
true_box=[]
|
||||||
|
# intelligence artificielle
|
||||||
|
ai_url=requests.get(url = "http://" + discoveryservice_url + "/IA").text
|
||||||
|
for j in range(len(cards)):
|
||||||
|
response=requests.post("http://"+ai_url+"/prediction", json={"card" : cards[i].tolist()})
|
||||||
|
list_ai.append(response.json())
|
||||||
|
for k in range(len(list_ai)-1):
|
||||||
|
json_obj1=list_ai[k]
|
||||||
|
json_obj2=list_ai[k+1]
|
||||||
|
lab_obj1=json_obj1["label"]
|
||||||
|
lab_obj2=json_obj2["label"]
|
||||||
|
common =list(set(lab_obj1).intersection(set(lab_obj2)))
|
||||||
|
index=lab_obj1.index(common[0])
|
||||||
|
coordonne=json_obj1["coord"][index]
|
||||||
|
true_box.append(coordonne)
|
||||||
|
print(true_box)
|
||||||
|
print(response.json())
|
||||||
|
|
||||||
|
|
||||||
|
def serve_pil_image(pil_img):
|
||||||
|
img_io = BytesIO()
|
||||||
|
pil_img.save(img_io, 'JPEG', quality=70)
|
||||||
|
img_io.seek(0)
|
||||||
|
return send_file(img_io, mimetype='image/jpeg')
|
||||||
|
|
||||||
|
@app.route('/route/<id>')
|
||||||
|
def serve_img(id):
|
||||||
|
id=int(id)
|
||||||
|
if (id<len(cards)):
|
||||||
|
img = Image.fromarray(np.uint8(cards[id]))
|
||||||
|
return serve_pil_image(img)
|
||||||
|
else:
|
||||||
|
return "Congratulations !!! You have won the game!"
|
||||||
|
|
||||||
|
@app.route('/pixels/<id>', methods = ['POST'])
|
||||||
|
@cross_origin()
|
||||||
|
def pixels(id):
|
||||||
|
id=int(id)
|
||||||
|
resultat = request.get_json(force=True)
|
||||||
|
print("µµµµµµµ")
|
||||||
|
print(resultat["X"])
|
||||||
|
X=resultat["X"]
|
||||||
|
print("********")
|
||||||
|
print(resultat["Y"])
|
||||||
|
Y=resultat["Y"]
|
||||||
|
print("µµµµµµµ")
|
||||||
|
print(true_box[id])
|
||||||
|
if(X>= true_box[id][0] and X<= true_box[id][1] and Y>= true_box[id][2] and Y<= true_box[id][3]):
|
||||||
|
if id==len(true_box)-1:
|
||||||
|
return "FINI"
|
||||||
|
else:
|
||||||
|
return "OK"
|
||||||
|
else:
|
||||||
|
return "Fail"
|
||||||
|
|
||||||
|
@app.route('/pools', methods = ['GET','POST'])
|
||||||
|
def verify_res():
|
||||||
|
resultat = request.get_json(force=True)
|
||||||
|
print("µµµµµµµ")
|
||||||
|
print(resultat["X"])
|
||||||
|
print("********")
|
||||||
|
print(resultat["Y"])
|
||||||
|
print("µµµµµµµ")
|
||||||
|
return "Bonjour"
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/time', methods = ['GET','POST'])
|
||||||
|
def getTime():
|
||||||
|
global i
|
||||||
|
res0 = str(cards[0].tolist())
|
||||||
|
res1 = str(cards[1].tolist())
|
||||||
|
i = 2
|
||||||
|
return jsonify(
|
||||||
|
message= "this is the first two cards",
|
||||||
|
first_card = res0 ,
|
||||||
|
second_card = res1
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/cards/<id>', methods = ['GET','POST'])
|
||||||
|
@cross_origin()
|
||||||
|
def sendCards(id):
|
||||||
|
id=int(id)
|
||||||
|
if (id<len(cards)):
|
||||||
|
res=str(cards[id].tolist())
|
||||||
|
img = Image.fromarray(np.uint8(cards[id]))
|
||||||
|
return serve_pil_image(img)
|
||||||
|
else:
|
||||||
|
return "Congratulations you have won the game !!!"
|
||||||
|
|
||||||
|
def shutdown_server():
|
||||||
|
func = request.environ.get('werkzeug.server.shutdown')
|
||||||
|
if func is None:
|
||||||
|
raise RuntimeError('Not running with the Werkzeug Server')
|
||||||
|
func()
|
||||||
|
|
||||||
|
@app.route('/shutdown', methods =['POST'] )
|
||||||
|
def shutdown():
|
||||||
|
shutdown_server()
|
||||||
|
return 'Server shutting down...'
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
app.run(host="0.0.0.0",port=50001)
|
||||||
|
|
||||||
|
|
7
server_side/Dockerfile
Normal file
7
server_side/Dockerfile
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
FROM ubuntu
|
||||||
|
RUN apt-get update -y
|
||||||
|
RUN apt install wget -y
|
||||||
|
RUN apt install python3 -y
|
||||||
|
RUN apt install python3-pip -y
|
||||||
|
RUN pip3 install flask
|
||||||
|
RUN wget etud.insa-toulouse.fr/~benassai/microservice.py
|
54
server_side/mainserver.py
Normal file
54
server_side/mainserver.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
|
||||||
|
from flask import Flask, jsonify
|
||||||
|
import docker
|
||||||
|
import socket
|
||||||
|
app = Flask(__name__)
|
||||||
|
client = docker.from_env()
|
||||||
|
d=0
|
||||||
|
for elem in client.images.list():
|
||||||
|
for var in elem.tags:
|
||||||
|
if var=="app:proxy":
|
||||||
|
d=1
|
||||||
|
if d==0:
|
||||||
|
client.images.build(path="./", tag="app:proxy", network_mode = 'host')
|
||||||
|
|
||||||
|
@app.route('/', methods = ['GET'])
|
||||||
|
def getMessages():
|
||||||
|
return jsonify(
|
||||||
|
message="Hello world!!!",
|
||||||
|
username="Driss"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/port', methods = ['GET'])
|
||||||
|
def getports():
|
||||||
|
sock = socket.socket()
|
||||||
|
sock.bind(('',0))
|
||||||
|
port= sock.getsockname()[1]
|
||||||
|
sock.close()
|
||||||
|
client = docker.from_env()
|
||||||
|
cont=client.containers.run(image = "app:proxy", ports={5000:port},name='proxynew'+str(port), command="python3 ./microservice.py", detach = True )
|
||||||
|
return jsonify(
|
||||||
|
message="hello",
|
||||||
|
numport= port
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.route('/newContainer', methods = ['GET'])
|
||||||
|
def createContainer():
|
||||||
|
client = docker.from_env()
|
||||||
|
cont = client.containers.run(image = "app:proxy", ports={5000:50001}, name = 'proxy', command="python3 ./microservice.py", detach = True)
|
||||||
|
return "Success"
|
||||||
|
|
||||||
|
@app.route('/delContainer', methods = ['GET'])
|
||||||
|
def deleteContainer():
|
||||||
|
client = docker.from_env()
|
||||||
|
for c in client.containers.list():
|
||||||
|
c.stop()
|
||||||
|
client.containers.prune()
|
||||||
|
return "Success"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
app.run(host="0.0.0.0",port=50000)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue