diff options
author | =Corey Hulen <corey@hulen.com> | 2015-06-14 23:53:32 -0800 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2015-06-14 23:53:32 -0800 |
commit | 56e74239d6b34df8f30ef046f0b0ff4ff0866a71 (patch) | |
tree | 044da29848cf0f5c8607eac34de69065171669cf /api/web_hub.go | |
download | chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.gz chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.bz2 chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.zip |
first commit
Diffstat (limited to 'api/web_hub.go')
-rw-r--r-- | api/web_hub.go | 71 |
1 files changed, 71 insertions, 0 deletions
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() + } + } + } + }() +} |