Skip to content

Commit

Permalink
add documentation of functions for Python Server
Browse files Browse the repository at this point in the history
  • Loading branch information
kimokipo committed Jul 6, 2022
1 parent 9c26782 commit e367701
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
Binary file renamed Server/Server.exe → Server/ScratchHome_Server.exe
Binary file not shown.
55 changes: 32 additions & 23 deletions Server/Server.py → Server/ScratchHome_Server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,73 @@
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
s.connect((host,port)) #Connect to socket
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()

0 comments on commit e367701

Please sign in to comment.