-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
151 lines (132 loc) · 3.07 KB
/
server.go
File metadata and controls
151 lines (132 loc) · 3.07 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gocomet
import (
"github.com/serverhorror/uuid"
"log"
"strings"
"sync"
)
/*
The Bayeux protocol implementation V1.0.
A common scenario is that, the client should handshake the server
first, to determine which version of the protocol is used. It doesn't
have any transport related fields or logic either becuase they should
be already taken care of before it's used.
*/
type Server struct {
*sync.RWMutex
names *UniqueStringPool
sessions map[string]*Session
broker *Broker
}
func newServer() *Server {
return &Server{
RWMutex: &sync.RWMutex{},
names: newUniqueStringPool(uuid.UUID4),
sessions: make(map[string]*Session),
broker: newBroker(),
}
}
func (c *Server) handshake() (clientId string, err error) {
clientId, err = c.names.get()
c.Lock()
defer c.Unlock()
routerOutput := c.broker.register(clientId)
c.sessions[clientId] = newSession(clientId, routerOutput, func() {
c.Lock()
defer c.Unlock()
delete(c.sessions, clientId)
})
return
}
/*
Connect may supercede other non-connect waiting channels.
*/
func (c *Server) connect(clientId string) (ch chan *Message, stop chan bool, ok bool) {
if ok = c.names.touch(clientId); !ok {
return
}
c.RLock()
defer c.RUnlock()
var ss *Session
if ss, ok = c.sessions[clientId]; ok {
ch, stop = ss.obtainChannel(true)
}
return
}
func (c *Server) disconnect(clientId string) (ch chan *Message, ok bool) {
if ok = c.names.touch(clientId); !ok {
return
}
c.Lock()
defer c.Unlock()
var ss *Session
if ss, ok = c.sessions[clientId]; ok {
delete(c.sessions, clientId)
ch = ss.close()
}
return
}
func (c *Server) subscribe(clientId, subscription string) (ch chan *Message, ok bool) {
if strings.Contains(subscription, ",") {
panic("not supported yet")
}
if ok = c.names.touch(clientId); !ok {
return
}
c.broker.subscribe(clientId, subscription)
c.RLock()
defer c.RUnlock()
var ss *Session
if ss, ok = c.sessions[clientId]; ok {
ch, _ = ss.obtainChannel(false)
}
return
}
func (c *Server) unsubscribe(clientId, subscription string) (ch chan *Message, ok bool) {
if ok = c.names.touch(clientId); !ok {
return
}
if ok = c.broker.unsubscribe(clientId, subscription); !ok {
return
}
c.RLock()
defer c.RUnlock()
var ss *Session
if ss, ok = c.sessions[clientId]; ok {
ch, _ = ss.obtainChannel(false)
}
return
}
func (c *Server) publish(clientId, channel, data string) (ch chan *Message, ok bool) {
if ok = c.names.touch(clientId); !ok {
return
}
log.Printf("[%8.8v]Publish '%v' at '%v'", clientId, data, channel)
c.broker.broadcast(channel, data)
c.RLock()
defer c.RUnlock()
var ss *Session
if ss, ok = c.sessions[clientId]; ok {
ch, _ = ss.obtainChannel(false)
}
return
}
/*
Publish message without client ID.
*/
func (c *Server) whisper(channel, data string) {
c.broker.broadcast(channel, data)
}
/*
Send message directly to target client.
*/
func (c *Server) Send(toClientId, channel, data string) bool {
c.RLock()
defer c.Unlock()
if ss, ok := c.sessions[toClientId]; ok {
ss.input <- &Message{channel: channel, data: data}
return true
} else {
return false
}
}