2017-02-12 04:13:29 +00:00
|
|
|
package connection
|
2017-02-02 03:12:13 +00:00
|
|
|
|
2017-02-12 20:39:50 +00:00
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
const (
|
|
|
|
initialDomains = 0
|
|
|
|
incrementDomains = 0
|
|
|
|
)
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//Table maintains the set of connections
|
|
|
|
type Table struct {
|
2017-02-12 20:39:50 +00:00
|
|
|
connections map[*Connection][]string
|
|
|
|
domains map[string]*Connection
|
|
|
|
register chan *Connection
|
|
|
|
unregister chan *Connection
|
|
|
|
domainAnnounce chan *DomainMapping
|
|
|
|
domainRevoke chan *DomainMapping
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//NewTable -- consructor
|
2017-02-12 20:39:50 +00:00
|
|
|
func NewTable() (p *Table) {
|
|
|
|
p = new(Table)
|
|
|
|
p.connections = make(map[*Connection][]string)
|
|
|
|
p.domains = make(map[string]*Connection)
|
|
|
|
p.register = make(chan *Connection)
|
|
|
|
p.unregister = make(chan *Connection)
|
|
|
|
p.domainAnnounce = make(chan *DomainMapping)
|
|
|
|
p.domainRevoke = make(chan *DomainMapping)
|
|
|
|
return
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 02:36:01 +00:00
|
|
|
//ConnByDomain -- Obtains a connection from a domain announcement.
|
|
|
|
func (c *Table) ConnByDomain(domain string) (conn *Connection, ok bool) {
|
|
|
|
conn, ok = c.domains[domain]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
//Run -- Execute
|
|
|
|
func (c *Table) Run() {
|
2017-02-02 03:12:13 +00:00
|
|
|
loginfo.Println("ConnectionTable starting")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case connection := <-c.register:
|
|
|
|
loginfo.Println("register fired")
|
2017-02-13 21:59:10 +00:00
|
|
|
c.connections[connection] = make([]string, initialDomains)
|
2017-02-12 20:39:50 +00:00
|
|
|
connection.commCh <- true
|
2017-02-02 03:12:13 +00:00
|
|
|
|
2017-02-12 20:39:50 +00:00
|
|
|
// handle initial domain additions
|
|
|
|
for _, domain := range connection.initialDomains {
|
|
|
|
// add to the domains regirstation
|
|
|
|
|
|
|
|
newDomain := string(domain.(string))
|
|
|
|
loginfo.Println("adding domain ", newDomain, " to connection ", connection)
|
|
|
|
c.domains[newDomain] = connection
|
|
|
|
|
|
|
|
// add to the connection domain list
|
|
|
|
s := c.connections[connection]
|
|
|
|
c.connections[connection] = append(s, newDomain)
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 20:39:50 +00:00
|
|
|
loginfo.Println("register exiting")
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
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 {
|
2017-02-12 20:39:50 +00:00
|
|
|
for _, domain := range c.connections[connection] {
|
|
|
|
fmt.Println("removing domain ", domain)
|
|
|
|
if _, ok := c.domains[domain]; ok {
|
|
|
|
delete(c.domains, domain)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
delete(c.connections, connection)
|
|
|
|
close(connection.send)
|
|
|
|
}
|
2017-02-12 20:39:50 +00:00
|
|
|
|
|
|
|
case domainMapping := <-c.domainAnnounce:
|
|
|
|
loginfo.Println("domainMapping fired ", domainMapping)
|
|
|
|
//check to make sure connection is already regiered, you can no register a domain without an apporved connection
|
|
|
|
//if connection, ok := connections[domainMapping.connection]; ok {
|
|
|
|
|
|
|
|
//} else {
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
2017-02-13 21:59:10 +00:00
|
|
|
fmt.Println("domain ", c.domains)
|
|
|
|
fmt.Println("connections ", c.connections)
|
2017-02-02 03:12:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-12 04:13:29 +00:00
|
|
|
|
|
|
|
//Register -- Property
|
|
|
|
func (c *Table) Register() (r chan *Connection) {
|
|
|
|
r = c.register
|
|
|
|
return
|
|
|
|
}
|