##############################################################################1#2# CoCalc: Collaborative Calculation in the Cloud3#4# Copyright (C) 2017, Sagemath Inc.5#6# This program is free software: you can redistribute it and/or modify7# it under the terms of the GNU Affero General Public License as8# published by the Free Software Foundation, either version 3 of the9# License, or (at your option) any later version.10#11# This program is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14# GNU Affero General Public License for more details.15#16# You should have received a copy of the GNU Affero General Public License17# along with this program. If not, see <http://www.gnu.org/licenses/>.18#19###############################################################################2021###22Hub Registration (recording number of clients)23###2425winston = require('winston')26misc = require('smc-util/misc')27{defaults, required} = misc2829# Global variables30database_is_working = false31the_database = undefined32the_host = undefined33the_port = undefined34the_interval = undefined35the_clients = []3637number_of_clients = () ->38return (C for id,C of the_clients when not C._destroy_timer? and not C.closed).length3940exports.number_of_clients = () ->41if not the_database?42throw new Error("database not yet set")43return number_of_clients()4445register_hub = (cb) ->46if not the_database?47cb?("database not yet set")48return49the_database.register_hub50host : the_host51port : the_port52clients : number_of_clients()53ttl : 3*the_interval54cb : (err) ->55if err56database_is_working = false57winston.debug("Error registering with database - #{err}")58else59database_is_working = true60cb?(err)6162exports.database_is_working = ->63return database_is_working6465exports.start = (opts) ->66opts = defaults opts,67database : required68clients : required69host : required70port : required71interval_s : required72the_database = opts.database73the_clients = opts.clients74the_host = opts.host75the_port = opts.port76the_interval = opts.interval_s77register_hub()78setInterval(register_hub, the_interval*1000)798081