From cf7a05f80f68b5b1c8bcc0089679dd497cec2506 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Sun, 14 Jun 2015 23:53:32 -0800 Subject: first commit --- api/web_hub.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 api/web_hub.go (limited to 'api/web_hub.go') diff --git a/api/web_hub.go b/api/web_hub.go new file mode 100644 index 000000000..bf5fbb321 --- /dev/null +++ b/api/web_hub.go @@ -0,0 +1,71 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package api + +import ( + l4g "code.google.com/p/log4go" +) + +type Hub struct { + teamHubs map[string]*TeamHub + register chan *WebConn + unregister chan *WebConn + stop chan string +} + +var hub = &Hub{ + register: make(chan *WebConn), + unregister: make(chan *WebConn), + teamHubs: make(map[string]*TeamHub), + stop: make(chan string), +} + +func (h *Hub) Register(webConn *WebConn) { + h.register <- webConn +} + +func (h *Hub) Unregister(webConn *WebConn) { + h.unregister <- webConn +} + +func (h *Hub) Stop(teamId string) { + h.stop <- teamId +} + +func (h *Hub) Start() { + go func() { + for { + select { + + case c := <-h.register: + nh := h.teamHubs[c.TeamId] + + if nh == nil { + nh = NewTeamHub(c.TeamId) + h.teamHubs[c.TeamId] = nh + nh.Start() + } + + nh.Register(c) + + case c := <-h.unregister: + if nh, ok := h.teamHubs[c.TeamId]; ok { + nh.Unregister(c) + } + + case s := <-h.stop: + if len(s) == 0 { + l4g.Debug("stopping all connections") + for _, v := range h.teamHubs { + v.Stop() + } + return + } else if nh, ok := h.teamHubs[s]; ok { + delete(h.teamHubs, s) + nh.Stop() + } + } + } + }() +} -- cgit v1.2.3-1-g7c22