diff --git a/src/app/spreed-webrtc-server/channeling.go b/src/app/spreed-webrtc-server/channeling.go index d49e5300..726d3005 100644 --- a/src/app/spreed-webrtc-server/channeling.go +++ b/src/app/spreed-webrtc-server/channeling.go @@ -74,6 +74,7 @@ type DataSession struct { Rev uint64 `json:",omitempty"` Prio int `json:",omitempty"` Status interface{} + stamp int64 } type DataUser struct { diff --git a/src/app/spreed-webrtc-server/hub.go b/src/app/spreed-webrtc-server/hub.go index 4d81a644..777eecbd 100644 --- a/src/app/spreed-webrtc-server/hub.go +++ b/src/app/spreed-webrtc-server/hub.go @@ -483,7 +483,6 @@ func (h *Hub) sessionupdateHandler(s *SessionUpdate) uint64 { h.mutex.RUnlock() var rev uint64 if ok { - rev = session.Update(s) if s.Status != nil { status, ok := s.Status.(map[string]interface{}) if ok && status["buddyPicture"] != nil { @@ -496,6 +495,7 @@ func (h *Hub) sessionupdateHandler(s *SessionUpdate) uint64 { } } } + rev = session.Update(s) } else { log.Printf("Update data for unknown user %s\n", s.Id) } diff --git a/src/app/spreed-webrtc-server/session.go b/src/app/spreed-webrtc-server/session.go index 18b03c56..a2ccb07b 100644 --- a/src/app/spreed-webrtc-server/session.go +++ b/src/app/spreed-webrtc-server/session.go @@ -26,6 +26,7 @@ import ( "fmt" "github.com/gorilla/securecookie" "sync" + "time" ) var sessionNonces *securecookie.SecureCookie @@ -40,14 +41,16 @@ type Session struct { Prio int mutex sync.RWMutex userid string + stamp int64 } func NewSession(id, sid string) *Session { return &Session{ - Id: id, - Sid: sid, - Prio: 100, + Id: id, + Sid: sid, + Prio: 100, + stamp: time.Now().Unix(), } } @@ -119,6 +122,7 @@ func (s *Session) Authenticate(realm string, st *SessionToken, userid string) er } s.userid = userid + s.stamp = time.Now().Unix() s.UpdateRev++ return nil @@ -143,6 +147,8 @@ func (s *Session) Data() *DataSession { Ua: s.Ua, Status: s.Status, Rev: s.UpdateRev, + Prio: s.Prio, + stamp: s.stamp, } } diff --git a/src/app/spreed-webrtc-server/user.go b/src/app/spreed-webrtc-server/user.go index fc041bc0..057e37a6 100644 --- a/src/app/spreed-webrtc-server/user.go +++ b/src/app/spreed-webrtc-server/user.go @@ -23,6 +23,7 @@ package main import ( "fmt" + "sort" "sync" ) @@ -85,6 +86,27 @@ func (u *User) SessionsData() []*DataSession { for _, session := range u.sessionTable { sessions = append(sessions, session.Data()) } + sort.Sort(ByPrioAndStamp(sessions)) return sessions } + +type ByPrioAndStamp []*DataSession + +func (a ByPrioAndStamp) Len() int { + return len(a) +} + +func (a ByPrioAndStamp) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + +func (a ByPrioAndStamp) Less(i, j int) bool { + if a[i].Prio < a[j].Prio { + return true + } + if a[i].Prio == a[j].Prio { + return a[i].stamp < a[j].stamp + } + return false +}