diff --git a/Server/Server.exe b/Server/ScratchHome_Server.exe similarity index 99% rename from Server/Server.exe rename to Server/ScratchHome_Server.exe index 4a1d655..0902e42 100644 Binary files a/Server/Server.exe and b/Server/ScratchHome_Server.exe differ diff --git a/Server/Server.py b/Server/ScratchHome_Server.py similarity index 54% rename from Server/Server.py rename to Server/ScratchHome_Server.py index 7222b9e..1dde2c9 100644 --- a/Server/Server.py +++ b/Server/ScratchHome_Server.py @@ -4,45 +4,53 @@ import socket import unidecode import threading - -async def servir(socketScratch): - HOST = "127.0.0.1" # Standard loopback interface address (localhost) +# This program allow to launch WebSocket Server receiving Connections from Scratch Extension (ScratchHome), +# and connect to Server Socket created by the Sweethome3D plugin, +# and also it creates its own Server Socket to receive connections from the same plugin + + +# Create an async function that launch Server Socket on port 2022 to receive messages from SweetHome3D +# it takes in parameter the WebSocket got from the connection of Scratch Extension to this WebSocket Server +async def serve(socketScratch): + HOST = "localhost" # Standard loopback interface address (localhost) PORT = 2022 # Port to listen on (non-privileged ports are > 1023) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen() while True: - print('before accept') conn, addr = s.accept() with conn: - print(f"Connected by {addr}") while True: data = conn.recv(1024) if not data: break msgrecv=jpysocket.jpydecode(data) #Decript msg + + # send data to Scratch await socketScratch.send(msgrecv) - + +# define a function that run the async function serve with a new event loop def between_callback(args): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) - loop.run_until_complete(servir(args)) + loop.run_until_complete(serve(args)) loop.close() -# create handler for each connection - +# create handler for each WebSocket connection async def handler(websocket, path): - t1 = threading.Thread(target=between_callback,args=(websocket,)) - # démarrer le thread t1 + # Launch the thread of this Server Socket + t1 = threading.Thread(target=between_callback,args=(websocket,)) t1.start() + while True: - + # receive data from Scratch data = await websocket.recv() - print(data) + + # Create connection with SH3D plugin's Server Socket on port 2016 host='localhost' #Host Name port=2016 #Port Number s=socket.socket() #Create Socket @@ -50,18 +58,19 @@ async def handler(websocket, path): msgenv = f"GET /{data} HTTP/1" msgenvsansaccent = unidecode.unidecode(msgenv) msgsend=jpysocket.jpyencode(msgenvsansaccent) #Encript The Msg - s.send(msgsend) #Send Msg - msgrecv=s.recv(1024) #Recieve msg + + #Send Msg to SweetHome3D + s.send(msgsend) + + #Recieve msg from SH3D + msgrecv=s.recv(1024) msgrecv=jpysocket.jpydecode(msgrecv) #Decript msg - - reply = msgrecv - - await websocket.send(reply) - - + + # Respond to Scratch + await websocket.send(msgrecv) +# Launch the WebSocket Server on port 8000 start_server = websockets.serve(handler, "localhost", 8000) asyncio.get_event_loop().run_until_complete(start_server) -asyncio.get_event_loop().run_forever() - +asyncio.get_event_loop().run_forever() \ No newline at end of file