From 39901aa8410680de099e40cfb5577ca3cc4a13a3 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 23 Oct 2015 17:44:46 +0200 Subject: 42 --- interactor/index.html | 11 +++++++---- interactor/interactor.js | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/interactor/index.html b/interactor/index.html index ff2edf4..19dc703 100644 --- a/interactor/index.html +++ b/interactor/index.html @@ -11,6 +11,9 @@ var CVSH=500; var COLOURS=["#00F","#F00","#0CC"]; var socket=io(); +setInterval(function(){ + socket.emit("ping"); +},1000); var CELLSZ=~~(CVSH/(H+1)); var CVSW=CELLSZ*(W+1); var CELL0X=~~(CVSW/2-W/2*CELLSZ)+.5,CELL0Y=~~(CVSH/2-H/2*CELLSZ)+.5; @@ -65,7 +68,7 @@ function drawboard(bd,clr){ for(x=0;xPlayer "+(player+1)+" won"); + setstatustext("Player "+(player+1)+" won!"); }); diff --git a/interactor/interactor.js b/interactor/interactor.js index 8a8a32f..32f8517 100755 --- a/interactor/interactor.js +++ b/interactor/interactor.js @@ -36,25 +36,56 @@ app.get("/",function(req,res){ }); }); +var childpids=[]; + +process.on("exit",function(){ + console.log("Exiting:"); + console.log.apply(console,arguments); + childpids.forEach(function(pid){process.kill(pid);}); + process.exit(); +}); +process.on("SIGINT",function(){ + console.log("SIGINT:"); + console.log.apply(console,arguments); + childpids.forEach(function(pid){process.kill(pid);}); + process.exit(); +}); + function stopproc(proc){ + var pid=proc.pid; + console.log("Stopping proc with pid "+pid+", childpids="+JSON.stringify(childpids)); proc.stdin.write("Quit\n"); var timeout=setTimeout(function(){ + console.log("Proc with pid "+pid+" didn't stop in time, killing"); proc.kill(); + var idx=childpids.indexOf(pid); if(idx!=-1)childpids.splice(idx,1); },500); proc.on("exit",function(){ clearTimeout(timeout); + var idx=childpids.indexOf(pid); if(idx!=-1)childpids.splice(idx,1); }); } io.on("connection",function(conn){ var id=uniqid(); console.log("New IO connection id "+id); + var pingtimeout=null; var bd=emptyboard(); var aiplayer=0; conn.on("disconnect",function(){ console.log("Disconnect id "+id); + if(proc){stopproc(proc);proc=null;} + if(pingtimeout)clearTimeout(pingtimeout); + }); + conn.on("ping",function(){ //only enable ping timeouting if a first ping received + if(pingtimeout)clearTimeout(pingtimeout); + pingtimeout=setTimeout(function(){ + console.log("Ping timeout on id "+id+"!"); + if(proc){stopproc(proc);proc=null;} + conn.disconnect(true); + },4000); }); conn.on("usermove",function(idx){ idx=+idx; @@ -71,7 +102,7 @@ io.on("connection",function(conn){ var win=checkwin(bd); if(win!=-1){ conn.emit("win",1-aiplayer); - stopproc(proc); + if(proc){stopproc(proc);proc=null;} return; } } @@ -83,6 +114,7 @@ io.on("connection",function(conn){ var proc=spawn("sh",["-c",aicmd],{ stdio:["pipe","pipe",process.stderr] }); + childpids.push(proc.pid); var buffer=""; proc.stdout.on("data",function(data){ var idx,line,mv,win; @@ -105,7 +137,7 @@ io.on("connection",function(conn){ win=checkwin(bd); if(win!=-1){ conn.emit("win",aiplayer); - stopproc(proc); + if(proc){stopproc(proc);proc=null;} return; } } -- cgit v1.2.3