-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
111 lines (90 loc) · 2.53 KB
/
route.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package model
import "sync"
import "ServerCommon/model"
//路由本身有routemanger管理
type Route struct {
appid int64 //每个服务器IM的id
mutex sync.Mutex
uids map[int64]bool
room_ids model.IntSet
}
func NewRoute(appid int64) model.RouteBase {
r := new(Route)
r.appid = appid
r.uids = make(map[int64]bool)
r.room_ids = model.NewIntSet()
return r
}
func (route *Route) GetAppID() int64 {
return route.appid
}
//判断用户是否在该路由下
func (route *Route) ContainUserID(uid int64) bool {
route.mutex.Lock()
defer route.mutex.Unlock()
_, ok := route.uids[uid]
return ok
}
//判断用户是否在线
func (route *Route) IsUserOnline(uid int64) bool {
route.mutex.Lock()
defer route.mutex.Unlock()
return route.uids[uid]
}
//将用户加入该路由
func (route *Route) AddUserID(uid int64, online bool) {
route.mutex.Lock()
defer route.mutex.Unlock()
route.uids[uid] = online
}
func (route *Route) RemoveUserID(uid int64) {
route.mutex.Lock()
defer route.mutex.Unlock()
delete(route.uids, uid)
}
//获取该路由下所有用户
func (route *Route) GetUserIDs() model.IntSet {
route.mutex.Lock()
defer route.mutex.Unlock()
uids := model.NewIntSet()
for uid, _ := range route.uids {
uids.Add(uid)
}
return uids
}
//判断该路由下是否包含该房间
func (route *Route) ContainRoomID(room_id int64) bool {
route.mutex.Lock()
defer route.mutex.Unlock()
return route.room_ids.IsMember(room_id)
}
//将当前房间加入本路由
func (route *Route) AddRoomID(room_id int64) {
route.mutex.Lock()
defer route.mutex.Unlock()
route.room_ids.Add(room_id)
}
//将当前房间移除本路由
func (route *Route) RemoveRoomID(room_id int64) {
route.mutex.Lock()
defer route.mutex.Unlock()
route.room_ids.Remove(room_id)
}