From a0220f6eb186b3dadb4b118668399301d7d0c2d2 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Mon, 17 Mar 2014 10:37:25 +0100 Subject: [PATCH] Include number of chat message in hub statistics. --- src/app/spreed-speakfreely-server/hub.go | 45 ++++++++++++--------- src/app/spreed-speakfreely-server/server.go | 3 ++ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/app/spreed-speakfreely-server/hub.go b/src/app/spreed-speakfreely-server/hub.go index 36237ba4..2a2ea3f9 100644 --- a/src/app/spreed-speakfreely-server/hub.go +++ b/src/app/spreed-speakfreely-server/hub.go @@ -30,6 +30,7 @@ import ( "github.com/gorilla/securecookie" "log" "sync" + "sync/atomic" "time" ) @@ -47,28 +48,32 @@ type MessageRequest struct { } type HubStat struct { - Rooms int `json:"rooms"` - Connections int `json:"connections"` - Users int `json:"users"` - Count uint64 `json:"count"` - IdsInRoom map[string][]string `json:"idsinroom,omitempty"` - UsersById map[string]*DataUser `json:"usersbyid,omitempty"` - ConnectionsByIdx map[string]string `json:"connectionsbyidx,omitempty"` + Rooms int `json:"rooms"` + Connections int `json:"connections"` + Users int `json:"users"` + Count uint64 `json:"count"` + BroadcastChatMessages uint64 `json:"broadcastchatmessages"` + UnicastChatMessages uint64 `json:"unicastchatmessages"` + IdsInRoom map[string][]string `json:"idsinroom,omitempty"` + UsersById map[string]*DataUser `json:"usersbyid,omitempty"` + ConnectionsByIdx map[string]string `json:"connectionsbyidx,omitempty"` } type Hub struct { - server *Server - connectionTable map[string]*Connection - userTable map[string]*User - roomTable map[string]*RoomWorker - version string - config *Config - sessionSecret []byte - turnSecret []byte - tickets *securecookie.SecureCookie - count uint64 - mutex sync.RWMutex - buffers BufferCache + server *Server + connectionTable map[string]*Connection + userTable map[string]*User + roomTable map[string]*RoomWorker + version string + config *Config + sessionSecret []byte + turnSecret []byte + tickets *securecookie.SecureCookie + count uint64 + mutex sync.RWMutex + buffers BufferCache + broadcastChatMessages uint64 + unicastChatMessages uint64 } func NewHub(version string, config *Config, sessionSecret string, turnSecret string) *Hub { @@ -97,6 +102,8 @@ func (h *Hub) Stat(details bool) *HubStat { Connections: len(h.connectionTable), Users: len(h.userTable), Count: h.count, + BroadcastChatMessages: atomic.LoadUint64(&h.broadcastChatMessages), + UnicastChatMessages: atomic.LoadUint64(&h.unicastChatMessages), } if details { rooms := make(map[string][]string) diff --git a/src/app/spreed-speakfreely-server/server.go b/src/app/spreed-speakfreely-server/server.go index ae677bfe..dc4438bd 100644 --- a/src/app/spreed-speakfreely-server/server.go +++ b/src/app/spreed-speakfreely-server/server.go @@ -23,6 +23,7 @@ package main import ( "encoding/json" "log" + "sync/atomic" "time" ) @@ -114,9 +115,11 @@ func (s *Server) OnText(c *Connection, b Buffer) { if msg.Chat.To == "" { // TODO(longsleep): Check if chat broadcast is allowed. if c.h.config.defaultRoomEnabled || !c.h.isDefaultRoomid(c.Roomid) { + atomic.AddUint64(&c.h.broadcastChatMessages, 1) s.Broadcast(c, msg.Chat) } } else { + atomic.AddUint64(&c.h.unicastChatMessages, 1) s.Unicast(c, msg.Chat.To, msg.Chat) if msg.Chat.Chat.Mid != "" { // Send out delivery confirmation status chat message.