def open(mainSocket, server_address): if mainSocket: mainSocket.listen(5) mlog.show('Listening on server|port {}|{}'.format(*server_address)) return True else: mlog.show('Listening error: not socecket founded !') return False
async def game(websocket, path): mlog.show("New Client Connection") listen = Listen(websocket) if await listen.run(): await websocket.send("CLOSE_GAME") else: await websocket.send("CLOSE_GAME_WITH_WEBSOCKET_ERRORS") mlog.show("Client Connection Closed")
def init(server_address): mainSocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM) # create a new socket connection mainSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # set the new connection mlog.show('Starting a new socket connection on server|port {}|{}'.format( *server_address)) mainSocket.bind(server_address) return mainSocket
def close(mainSocket, server_address): if mainSocket: mainSocket.close() mlog.show('Socket Connection closed on server|port {}|{}'.format( *server_address)) return True else: mlog.show('Close error: not socecket founded !') return False
async def sendCommandToClient(self, value): try: mlog.show("Send command to client: " + value) await self.socket.send(value) return True except: mlog.show("Client is disconnected.. Process will be close") self.process.subprocess.terminate() return None return None
async def getReponseFromClient(self): try: message = await self.socket.recv() # wait for client confirmation mlog.show("Received confirmation from client: " + message) return message except: mlog.show("Client is disconnected.. Process will be close") self.process.subprocess.terminate() return None return None
async def run(self): mlog.show("Loading a new process.... Wait") # prepare the subprocess command cmd = self.languageName + " " + self.tempfile.getName() # execute a new process for the current game self.subprocess = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) mlog.show("Process loaded... Ready to go!") return
async def run(self, socket): mlog.show( "Starting the new game. Wait for a new process and a new proxy...." ) # init and create new file with the game engine and the client code tmpFileToRun = TempFile(self.languageObject.getFileEngine(), self.classes, self.content) # init a new process for this game self.process = Process(self.socket, tmpFileToRun, self.languageName) # init a new server prxy for this game self.proxy = Proxy(self.socket, self.process) # run the process await self.process.run() # run the proxy server await self.proxy.run() return
async def run(self): mlog.show("Run a new proxy server. Ready to iterate with client...") #loop until process is running count = 0 while self.process.subprocess.poll() is None: # get error form the current executing commande errorMsg = await io.stderrGet(self.process.subprocess) # run the current command using the server-process protocol if errorMsg == "none": await self.waitForReady("start") # wait until server is ready cmdsJS = await io.stdoutGet( self.process.subprocess ) # get command from process to send to client await self.sendCommandToClient( cmdsJS) # send the command to the client message = await self.getReponseFromClient( ) # get reponse from client await self.waitForReady("insert") # wait until server is ready await io.stdinWrite( self.process.subprocess, message) # send to process the client confirmation await self.waitForReady("close") # wait until server is ready if message == 'PROCESS_ENDED_BY_USER': # exit request from client mlog.show("Game closed by User. By...") self.process.subprocess.terminate() break count = count + 1 # count executed commamdes # for any error if errorMsg.strip() != "none" and errorMsg.strip() != "": mlog.show("Process error.. Game has been stopped..") mlog.show("Error message: " + errorMsg) await self.socket.send("PROCESS_ERROR" + errorMsg ) # send the error to client self.process.subprocess.terminate() # stop the subprocess break mlog.show("Commandes executed: " + str(count)) return
async def get_language(self): mlog.show("Waiting for the game language information ...") message = await self.mainSocket.recv() mlog.show("Language game for client: " + message) selectedLanguage = languages.loadLanguage(message) if (selectedLanguage): # create a new game for the selected language self.game = Game(selectedLanguage, self.mainSocket) await self.mainSocket.send('OK') else: await self.mainSocket.send('ERROR/Not game for this language') mlog.show("The language " + message + " is not defined for this game") return False return True
def loadLanguage(name): mlog.show("Loading " + name + " language...") for dir in directories: # start to search manifest.py file into all sub folders currentDirectory = path + dir + "/" files = os.listdir(currentDirectory) for file in files: # search manifest.py in all file if fnmatch.fnmatch(file, pattern): # only directory with manifest file content = open(currentDirectory + file, "r").read().replace( "'", "\"") # read language from manifest file languageName = json.loads(content)['targetLanguage'].lower() if languageName == name: # load the selected language fileName = json.loads(content)['file name'] exec(open(currentDirectory + fileName).read()) mlog.show('Language ' + languageName + " has been loaded") return languageName mlog.show( name + " language is not defined" ) # default action.. nothing to do if selected language is not defined return None
from core.listen import Listen from core.connection import Connection from mod import mlog import configparser import asyncio import websockets # get configuration form sconfig file config = configparser.ConfigParser() config.read("config.ini") host = config.get("SOCKET CONFIGURATION", "HOST") port = int(config.get("SOCKET CONFIGURATION", "PORT")) #define the websocket main function async def game(websocket, path): mlog.show("New Client Connection") listen = Listen(websocket) if await listen.run(): await websocket.send("CLOSE_GAME") else: await websocket.send("CLOSE_GAME_WITH_WEBSOCKET_ERRORS") mlog.show("Client Connection Closed") mlog.show("Starting Live Coding Server .... Wait") start_server = websockets.serve(game, host, port) # initialize the websocket asyncio.get_event_loop().run_until_complete(start_server) mlog.show("Live Coding Server has been loaded.... Waiting for new connection") asyncio.get_event_loop().run_forever() # run the websocket service
async def get_content(self): mlog.show("Waiting for the client game code ...") self.game.content = await self.mainSocket.recv() mlog.show("Client game code loaded successfully!") await self.mainSocket.send('OK') return True
async def get_classes(self): mlog.show("Waiting for client game engine...") self.game.classes = await self.mainSocket.recv() mlog.show("Client game engine loaded successfully!") await self.mainSocket.send('OK') return True