Browse Source

Renamed everything which was named user to session in preperation for user implementation.

pull/28/head
Simon Eisenmann 11 years ago committed by Simon Eisenmann
parent
commit
c7bddeda5f
  1. 6
      src/app/spreed-speakfreely-server/channeling.go
  2. 6
      src/app/spreed-speakfreely-server/connection.go
  3. 68
      src/app/spreed-speakfreely-server/hub.go
  4. 30
      src/app/spreed-speakfreely-server/images.go
  5. 28
      src/app/spreed-speakfreely-server/roomworker.go
  6. 20
      src/app/spreed-speakfreely-server/server.go
  7. 37
      src/app/spreed-speakfreely-server/session.go

6
src/app/spreed-speakfreely-server/wsdata.go → src/app/spreed-speakfreely-server/channeling.go

@ -61,7 +61,7 @@ type DataTurn struct { @@ -61,7 +61,7 @@ type DataTurn struct {
Urls []string `json:"urls"`
}
type DataUser struct {
type DataSession struct {
Type string
Id string
Ua string
@ -120,9 +120,9 @@ type DataOutgoing struct { @@ -120,9 +120,9 @@ type DataOutgoing struct {
To string
}
type DataUsers struct {
type DataSessions struct {
Type string
Users []*DataUser
Users []*DataSession
Index uint64
Batch uint64
}

6
src/app/spreed-speakfreely-server/connection.go

@ -66,9 +66,9 @@ type Connection struct { @@ -66,9 +66,9 @@ type Connection struct {
// Metadata.
Id string
Roomid string // Keep Roomid here for quick acess without locking c.User.
Roomid string // Keep Roomid here for quick acess without locking c.Session.
Idx uint64
User *User
Session *Session
IsRegistered bool
Hello bool
Version string
@ -93,7 +93,7 @@ func (c *Connection) close() { @@ -93,7 +93,7 @@ func (c *Connection) close() {
if !c.isClosed {
c.ws.Close()
c.mutex.Lock()
c.User = nil
c.Session = nil
c.isClosed = true
for {
head := c.queue.Front()

68
src/app/spreed-speakfreely-server/hub.go

@ -51,21 +51,21 @@ type MessageRequest struct { @@ -51,21 +51,21 @@ type MessageRequest struct {
}
type HubStat struct {
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"`
Rooms int `json:"rooms"`
Connections int `json:"connections"`
Sessions int `json:"sessions"`
Count uint64 `json:"count"`
BroadcastChatMessages uint64 `json:"broadcastchatmessages"`
UnicastChatMessages uint64 `json:"unicastchatmessages"`
IdsInRoom map[string][]string `json:"idsinroom,omitempty"`
SessionsById map[string]*DataSession `json:"sessionsbyid,omitempty"`
ConnectionsByIdx map[string]string `json:"connectionsbyidx,omitempty"`
}
type Hub struct {
server *Server
connectionTable map[string]*Connection
userTable map[string]*User
sessionTable map[string]*Session
roomTable map[string]*RoomWorker
version string
config *Config
@ -84,7 +84,7 @@ func NewHub(version string, config *Config, sessionSecret, turnSecret string) *H @@ -84,7 +84,7 @@ func NewHub(version string, config *Config, sessionSecret, turnSecret string) *H
h := &Hub{
connectionTable: make(map[string]*Connection),
userTable: make(map[string]*User),
sessionTable: make(map[string]*Session),
roomTable: make(map[string]*RoomWorker),
version: version,
config: config,
@ -105,7 +105,7 @@ func (h *Hub) Stat(details bool) *HubStat { @@ -105,7 +105,7 @@ func (h *Hub) Stat(details bool) *HubStat {
stat := &HubStat{
Rooms: len(h.roomTable),
Connections: len(h.connectionTable),
Users: len(h.userTable),
Sessions: len(h.sessionTable),
Count: h.count,
BroadcastChatMessages: atomic.LoadUint64(&h.broadcastChatMessages),
UnicastChatMessages: atomic.LoadUint64(&h.unicastChatMessages),
@ -113,18 +113,18 @@ func (h *Hub) Stat(details bool) *HubStat { @@ -113,18 +113,18 @@ func (h *Hub) Stat(details bool) *HubStat {
if details {
rooms := make(map[string][]string)
for roomid, room := range h.roomTable {
users := make([]string, 0, len(room.connections))
sessions := make([]string, 0, len(room.connections))
for id := range room.connections {
users = append(users, id)
sessions = append(sessions, id)
}
rooms[roomid] = users
rooms[roomid] = sessions
}
stat.IdsInRoom = rooms
users := make(map[string]*DataUser)
for userid, user := range h.userTable {
users[userid] = user.Data()
sessions := make(map[string]*DataSession)
for sessionid, session := range h.sessionTable {
sessions[sessionid] = session.Data()
}
stat.UsersById = users
stat.SessionsById = sessions
connections := make(map[string]string)
for id, connection := range h.connectionTable {
connections[fmt.Sprintf("%d", connection.Idx)] = id
@ -248,12 +248,12 @@ func (h *Hub) registerHandler(c *Connection) { @@ -248,12 +248,12 @@ func (h *Hub) registerHandler(c *Connection) {
h.mutex.Lock()
// Create new user instance.
// Create new session instance.
h.count++
c.Idx = h.count
u := &User{Id: c.Id}
h.userTable[c.Id] = u
c.User = u
s := &Session{Id: c.Id}
h.sessionTable[c.Id] = s
c.Session = s
c.IsRegistered = true
// Register connection or replace existing one.
@ -281,13 +281,13 @@ func (h *Hub) unregisterHandler(c *Connection) { @@ -281,13 +281,13 @@ func (h *Hub) unregisterHandler(c *Connection) {
h.mutex.Unlock()
return
}
user := c.User
session := c.Session
c.close()
delete(h.connectionTable, c.Id)
delete(h.userTable, c.Id)
delete(h.sessionTable, c.Id)
h.mutex.Unlock()
if user != nil {
h.buddyImages.DeleteUserImage(user.Id)
if session != nil {
h.buddyImages.Delete(session.Id)
}
//log.Printf("Unregister (%d) from %s: %s\n", c.Idx, c.RemoteAddr, c.Id)
h.server.OnUnregister(c)
@ -322,21 +322,21 @@ func (h *Hub) aliveHandler(c *Connection, alive *DataAlive) { @@ -322,21 +322,21 @@ func (h *Hub) aliveHandler(c *Connection, alive *DataAlive) {
}
func (h *Hub) userupdateHandler(u *UserUpdate) uint64 {
func (h *Hub) sessionupdateHandler(s *SessionUpdate) uint64 {
//fmt.Println("Userupdate", u)
h.mutex.RLock()
user, ok := h.userTable[u.Id]
session, ok := h.sessionTable[s.Id]
h.mutex.RUnlock()
var rev uint64
if ok {
rev = user.Update(u)
if u.Status != nil {
status, ok := u.Status.(map[string]interface{})
rev = session.Update(s)
if s.Status != nil {
status, ok := s.Status.(map[string]interface{})
if ok && status["buddyPicture"] != nil {
pic := status["buddyPicture"].(string)
if strings.HasPrefix(pic, "data:") {
imageId := h.buddyImages.Update(u.Id, pic[5:])
imageId := h.buddyImages.Update(s.Id, pic[5:])
if imageId != "" {
status["buddyPicture"] = "img:" + imageId
}
@ -344,7 +344,7 @@ func (h *Hub) userupdateHandler(u *UserUpdate) uint64 { @@ -344,7 +344,7 @@ func (h *Hub) userupdateHandler(u *UserUpdate) uint64 {
}
}
} else {
log.Printf("Update data for unknown user %s\n", u.Id)
log.Printf("Update data for unknown user %s\n", s.Id)
}
return rev

30
src/app/spreed-speakfreely-server/images.go

@ -38,29 +38,29 @@ type Image struct { @@ -38,29 +38,29 @@ type Image struct {
updateIdx int
lastChange time.Time
lastChangeId string
userid string
sessionid string
mimetype string
data []byte
}
type ImageCache interface {
Update(userId string, image string) string
Update(sessionId string, image string) string
Get(imageId string) *Image
DeleteUserImage(userId string)
Delete(sessionId string)
}
type imageCache struct {
images map[string]*Image
userImages map[string]string
mutex sync.RWMutex
images map[string]*Image
sessionImages map[string]string
mutex sync.RWMutex
}
func NewImageCache() ImageCache {
result := &imageCache{}
result.images = make(map[string]*Image)
result.userImages = make(map[string]string)
result.sessionImages = make(map[string]string)
if imageFilenames == nil {
imageFilenames = map[string]string{
"image/png": "picture.png",
@ -71,7 +71,7 @@ func NewImageCache() ImageCache { @@ -71,7 +71,7 @@ func NewImageCache() ImageCache {
return result
}
func (self *imageCache) Update(userId string, image string) string {
func (self *imageCache) Update(sessionId string, image string) string {
mimetype := "image/x-unknown"
pos := strings.Index(image, ";")
if pos != -1 {
@ -98,7 +98,7 @@ func (self *imageCache) Update(userId string, image string) string { @@ -98,7 +98,7 @@ func (self *imageCache) Update(userId string, image string) string {
}
var img *Image
self.mutex.RLock()
result, ok := self.userImages[userId]
result, ok := self.sessionImages[sessionId]
if !ok {
self.mutex.RUnlock()
imageId := make([]byte, 15, 15)
@ -106,11 +106,11 @@ func (self *imageCache) Update(userId string, image string) string { @@ -106,11 +106,11 @@ func (self *imageCache) Update(userId string, image string) string {
return ""
}
result = base64.URLEncoding.EncodeToString(imageId)
img = &Image{userid: userId}
img = &Image{sessionid: sessionId}
self.mutex.Lock()
resultTmp, ok := self.userImages[userId]
resultTmp, ok := self.sessionImages[sessionId]
if !ok {
self.userImages[userId] = result
self.sessionImages[sessionId] = result
self.images[result] = img
} else {
result = resultTmp
@ -145,11 +145,11 @@ func (self *imageCache) Get(imageId string) *Image { @@ -145,11 +145,11 @@ func (self *imageCache) Get(imageId string) *Image {
return image
}
func (self *imageCache) DeleteUserImage(userId string) {
func (self *imageCache) Delete(sessionId string) {
self.mutex.Lock()
imageId, ok := self.userImages[userId]
imageId, ok := self.sessionImages[sessionId]
if ok {
delete(self.userImages, userId)
delete(self.sessionImages, sessionId)
delete(self.images, imageId)
}
self.mutex.Unlock()

28
src/app/spreed-speakfreely-server/roomworker.go

@ -35,7 +35,7 @@ const ( @@ -35,7 +35,7 @@ const (
type RoomConnectionUpdate struct {
Id string
Userid string
Sessionid string
Status bool
Connection *Connection
}
@ -134,15 +134,15 @@ func (r *RoomWorker) Run(f func()) bool { @@ -134,15 +134,15 @@ func (r *RoomWorker) Run(f func()) bool {
func (r *RoomWorker) usersHandler(c *Connection) {
worker := func() {
users := &DataUsers{Type: "Users"}
var ul []*DataUser
users := &DataSessions{Type: "Users"}
var sl []*DataSession
appender := func(ec *Connection) bool {
ecuser := ec.User
if ecuser != nil {
user := ecuser.Data()
user.Type = "Online"
ul = append(ul, user)
if len(ul) > maxUsersLength {
ecsession := ec.Session
if ecsession != nil {
session := ecsession.Data()
session.Type = "Online"
sl = append(sl, session)
if len(sl) > maxUsersLength {
log.Println("Limiting users response length in channel", r.Id)
return false
}
@ -150,7 +150,7 @@ func (r *RoomWorker) usersHandler(c *Connection) { @@ -150,7 +150,7 @@ func (r *RoomWorker) usersHandler(c *Connection) {
return true
}
r.mutex.RLock()
ul = make([]*DataUser, 0, len(r.connections))
sl = make([]*DataSession, 0, len(r.connections))
// Include connections in this room.
for _, ec := range r.connections {
if !appender(ec) {
@ -164,7 +164,7 @@ func (r *RoomWorker) usersHandler(c *Connection) { @@ -164,7 +164,7 @@ func (r *RoomWorker) usersHandler(c *Connection) {
break
}
}
users.Users = ul
users.Users = sl
usersJson := c.h.buffers.New()
encoder := json.NewEncoder(usersJson)
err := encoder.Encode(&DataOutgoing{From: c.Id, Data: users})
@ -209,10 +209,10 @@ func (r *RoomWorker) connectionHandler(rcu *RoomConnectionUpdate) { @@ -209,10 +209,10 @@ func (r *RoomWorker) connectionHandler(rcu *RoomConnectionUpdate) {
r.mutex.Lock()
defer r.mutex.Unlock()
if rcu.Status {
r.connections[rcu.Userid] = rcu.Connection
r.connections[rcu.Sessionid] = rcu.Connection
} else {
if _, ok := r.connections[rcu.Userid]; ok {
delete(r.connections, rcu.Userid)
if _, ok := r.connections[rcu.Sessionid]; ok {
delete(r.connections, rcu.Sessionid)
}
}
}

20
src/app/spreed-speakfreely-server/server.go

@ -49,7 +49,7 @@ func (s *Server) OnUnregister(c *Connection) { @@ -49,7 +49,7 @@ func (s *Server) OnUnregister(c *Connection) {
//log.Println("OnUnregister", c.id)
if c.Hello {
s.UpdateRoomConnection(c, &RoomConnectionUpdate{Id: c.Roomid})
s.Broadcast(c, &DataUser{Type: "Left", Id: c.Id, Status: "hard"})
s.Broadcast(c, &DataSession{Type: "Left", Id: c.Id, Status: "hard"})
} else {
//log.Println("Ingoring OnUnregister because of no Hello", c.Idx)
}
@ -72,17 +72,17 @@ func (s *Server) OnText(c *Connection, b Buffer) { @@ -72,17 +72,17 @@ func (s *Server) OnText(c *Connection, b Buffer) {
case "Hello":
//log.Println("Hello", msg.Hello, c.Idx)
// TODO(longsleep): Filter room id and user agent.
s.UpdateUser(c, &UserUpdate{Types: []string{"Roomid", "Ua"}, Roomid: msg.Hello.Id, Ua: msg.Hello.Ua})
s.UpdateSession(c, &SessionUpdate{Types: []string{"Roomid", "Ua"}, Roomid: msg.Hello.Id, Ua: msg.Hello.Ua})
if c.Hello && c.Roomid != msg.Hello.Id {
// Room changed.
s.UpdateRoomConnection(c, &RoomConnectionUpdate{Id: c.Roomid})
s.Broadcast(c, &DataUser{Type: "Left", Id: c.Id, Status: "soft"})
s.Broadcast(c, &DataSession{Type: "Left", Id: c.Id, Status: "soft"})
}
c.Roomid = msg.Hello.Id
if c.h.config.defaultRoomEnabled || !c.h.isDefaultRoomid(c.Roomid) {
c.Hello = true
s.UpdateRoomConnection(c, &RoomConnectionUpdate{Id: c.Roomid, Status: true})
s.Broadcast(c, &DataUser{Type: "Joined", Id: c.Id, Ua: msg.Hello.Ua})
s.Broadcast(c, &DataSession{Type: "Joined", Id: c.Id, Ua: msg.Hello.Ua})
} else {
c.Hello = false
}
@ -103,9 +103,9 @@ func (s *Server) OnText(c *Connection, b Buffer) { @@ -103,9 +103,9 @@ func (s *Server) OnText(c *Connection, b Buffer) {
s.Unicast(c, msg.Bye.To, msg.Bye)
case "Status":
//log.Println("Status", msg.Status)
rev := s.UpdateUser(c, &UserUpdate{Types: []string{"Status"}, Status: msg.Status.Status})
rev := s.UpdateSession(c, &SessionUpdate{Types: []string{"Status"}, Status: msg.Status.Status})
if c.h.config.defaultRoomEnabled || !c.h.isDefaultRoomid(c.Roomid) {
s.Broadcast(c, &DataUser{Type: "Status", Id: c.Id, Status: msg.Status.Status, Rev: rev})
s.Broadcast(c, &DataSession{Type: "Status", Id: c.Id, Status: msg.Status.Status, Rev: rev})
}
case "Chat":
// TODO(longsleep): Limit sent chat messages per incoming connection.
@ -170,10 +170,10 @@ func (s *Server) Alive(c *Connection, alive *DataAlive) { @@ -170,10 +170,10 @@ func (s *Server) Alive(c *Connection, alive *DataAlive) {
}
func (s *Server) UpdateUser(c *Connection, userupdate *UserUpdate) uint64 {
func (s *Server) UpdateSession(c *Connection, su *SessionUpdate) uint64 {
userupdate.Id = c.Id
return c.h.userupdateHandler(userupdate)
su.Id = c.Id
return c.h.sessionupdateHandler(su)
}
@ -211,7 +211,7 @@ func (s *Server) Users(c *Connection) { @@ -211,7 +211,7 @@ func (s *Server) Users(c *Connection) {
func (s *Server) UpdateRoomConnection(c *Connection, rcu *RoomConnectionUpdate) {
rcu.Userid = c.Id
rcu.Sessionid = c.Id
rcu.Connection = c
room := c.h.GetRoom(c.Roomid)
room.connectionHandler(rcu)

37
src/app/spreed-speakfreely-server/user.go → src/app/spreed-speakfreely-server/session.go

@ -25,7 +25,7 @@ import ( @@ -25,7 +25,7 @@ import (
"sync"
)
type User struct {
type Session struct {
Id string
Roomid string
Ua string
@ -34,46 +34,45 @@ type User struct { @@ -34,46 +34,45 @@ type User struct {
mutex sync.RWMutex
}
func (u *User) Update(update *UserUpdate) uint64 {
func (s *Session) Update(update *SessionUpdate) uint64 {
//user := reflect.ValueOf(&u).Elem()
u.mutex.Lock()
defer u.mutex.Unlock()
s.mutex.Lock()
defer s.mutex.Unlock()
for _, key := range update.Types {
//fmt.Println("type update", key)
switch key {
case "Roomid":
u.Roomid = update.Roomid
s.Roomid = update.Roomid
case "Ua":
u.Ua = update.Ua
s.Ua = update.Ua
case "Status":
u.Status = update.Status
s.Status = update.Status
}
}
u.UpdateRev++
return u.UpdateRev
s.UpdateRev++
return s.UpdateRev
}
func (u *User) Data() *DataUser {
func (s *Session) Data() *DataSession {
u.mutex.RLock()
defer u.mutex.RUnlock()
s.mutex.RLock()
defer s.mutex.RUnlock()
return &DataUser{
Id: u.Id,
Ua: u.Ua,
Status: u.Status,
Rev: u.UpdateRev,
return &DataSession{
Id: s.Id,
Ua: s.Ua,
Status: s.Status,
Rev: s.UpdateRev,
}
}
type UserUpdate struct {
type SessionUpdate struct {
Id string
Types []string
Roomid string
Loading…
Cancel
Save