X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=server.coffee;h=b1cdac9c95fa437ac130866ec311f254ec9b9ca0;hb=8c6e422dbf34db0f751e392e2e9d86fcdedf4aa7;hp=211ac7d4cb0c24a2347171bc690f13007c705411;hpb=8cb28c7d6dfb4667081726aa3cb200a1f8cb9ca8;p=peach-cgt.git diff --git a/server.coffee b/server.coffee index 211ac7d..b1cdac9 100644 --- a/server.coffee +++ b/server.coffee @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -listen_port = 8333 +listen_port = process.env.app_port ? 9988 sys = require 'sys' fs = require 'fs' http = require 'http' @@ -25,10 +25,43 @@ coffee = require 'coffee-script' model = require './common.coffee' games = {} +max_concurrent_games = 50 +max_game_idle = 3 * 60 * 60 * 1000 # three hours (in miliseconds) # timeout function with args in convenient order timeout = (ms, func) -> setTimeout func, ms +now_s = -> + d = new Date() + return d.getTime() + +expire_old_games = -> + count = 0 + for slug, g of games + count += 1 + oldest_slug = slug + oldest_seen = g.last_seen + + return unless count > 0 + + # check all the games + # track oldest + # delete old ones + too_old = now_s() - max_game_idle + kills = [] + for slug, g of games + if g.last_seen < oldest_seen + oldest_seen = g.last_seen + oldest_slug = slug + if g.last_seen < too_old + kills.push slug + if count > max_concurrent_games and kills.length is 0 + console.log "hit max_concurrent_games, destroying oldest" + kills.push oldest_slug + for slug in kills + console.log "killing game #{slug}" + delete games[slug] + css_handler = (args, out, request, url_parts) -> fs.readFile 'style.less', 'utf8', (err, data) -> if err @@ -71,7 +104,7 @@ js_handler = (args, out, request, url_parts) -> html_handler = (args, out, request, url_parts) -> fs.readFile 'index.html', 'utf8', (err, data) -> if err - return out.end 'Server failed to read index.html' + return out.end "Server failed to read index.html: #{err}" out.end data clean_pathname_regex = new RegExp('[^a-zA-Z/_.-]') @@ -153,6 +186,9 @@ set_handler = (args, out, request, url_parts) -> out.end '{"status":6,"text_status":"Game already exists"}' return game = games[slug] = new_game slug, 'server' + game.last_seen = now_s() + console.log "new game: #{slug}" + expire_old_games() unless games[args.game]? out.writeHead 404, "Content-Type": 'text/plain' @@ -161,6 +197,8 @@ set_handler = (args, out, request, url_parts) -> game = games[args.game] + game.last_seen = now_s() + game.process_messages messages out.writeHead 200, "Content-Type": 'text/plain' @@ -263,5 +301,12 @@ http_server = http.createServer (req, res) -> return html_handler url_parts.query, res, req, url_parts +################## INIT #################### +# nodester starts this app with the current working directory set to / and working copy in /app +if process.cwd() is '/' + process.chdir '/app' + +setInterval expire_old_games, 2 * 60 * 1000 # check every 2 minutes for expired games + http_server.listen listen_port, "127.0.0.1" console.log "Server running at http://127.0.0.1:#{listen_port}/"