2017-02-02 03:12:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
// ConnectionTable maintains the set of connections
|
|
|
|
type ConnectionTable struct {
|
|
|
|
connections map[*Connection]bool
|
|
|
|
register chan *Connection
|
|
|
|
unregister chan *Connection
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConnectionTable() *ConnectionTable {
|
|
|
|
return &ConnectionTable{
|
|
|
|
register: make(chan *Connection),
|
|
|
|
unregister: make(chan *Connection),
|
|
|
|
connections: make(map[*Connection]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnectionTable) run() {
|
|
|
|
loginfo.Println("ConnectionTable starting")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case connection := <-c.register:
|
|
|
|
loginfo.Println("register fired")
|
|
|
|
c.connections[connection] = true
|
|
|
|
|
|
|
|
for conn := range c.connections {
|
|
|
|
loginfo.Println(conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
case connection := <-c.unregister:
|
2017-02-06 03:19:04 +00:00
|
|
|
loginfo.Println("closing connection ", connection)
|
2017-02-02 03:12:13 +00:00
|
|
|
if _, ok := c.connections[connection]; ok {
|
|
|
|
delete(c.connections, connection)
|
|
|
|
close(connection.send)
|
2017-02-06 03:19:04 +00:00
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|