-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
108 lines (84 loc) · 2.17 KB
/
types.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
package postmaster
import(
"sync"
)
///////////////////////////////////////////////////////////////////////////////////////
//
// Custom Types
//
///////////////////////////////////////////////////////////////////////////////////////
//
// Connection types
//
type ConnectionID string
type Connection struct{
out chan string
pendingAuth *PendingAuth //Set in between authreq & auth (values kept after sucessful auth for later use)
isAuth bool //Has this client been authenticated (only allow authreq/auth rpc call if not)
id ConnectionID //Used internally
Username string
P *Permissions //Permission for this client
}
//
// Auth Types
//
type PendingAuth struct{
authKey string
authExtra map[string]interface{}
sig string
p Permissions
ch []byte //Challenge in json form
}
type Permissions struct{
RPC map[string] RPCPermission //maps uri to RPCPermission
PubSub map[string] PubSubPermission //maps uri to PubSubPermission
}
type RPCPermission bool
type PubSubPermission struct{
CanPublish bool
CanSubscribe bool
}
//
// Server Types
//
type PublishIntercept func(id *Connection, msg PublishMsg)(bool)
type RPCHandler func(*Connection, string, ...interface{}) (interface{}, *RPCError)
type subscriptionMap struct{
data map[string] (map[ConnectionID]bool) //Allows concurrent access
lock *sync.RWMutex
}
func (subMap *subscriptionMap) Find(uri string)([]ConnectionID,bool){
subMap.lock.RLock()
idMap,ok := subMap.data[uri]
subMap.lock.RUnlock()
//Convert map to slice of keys
var keys []ConnectionID
for key,_ := range idMap{
keys = append(keys,key)
}
return keys,ok
}
func (subMap *subscriptionMap) Add(uri string, id ConnectionID){
subMap.lock.Lock()
idMap,ok := subMap.data[uri]
if !ok{
//Create new map
newMap := make(map[ConnectionID]bool)
newMap[id] = true
subMap.data[uri] = newMap
}else{
idMap[id] = true
}
subMap.lock.Unlock()
}
func (subMap *subscriptionMap) Remove(uri string, id ConnectionID){
subMap.lock.Lock()
delete(subMap.data[uri],id)
subMap.lock.Unlock()
}
func newSubscriptionMap()(*subscriptionMap){
s := new(subscriptionMap)
s.lock = new(sync.RWMutex)
s.data = make(map[string] (map[ConnectionID]bool) )
return s
}