Update awdl.go to use new link stuff (untested)

This commit is contained in:
Neil Alexander 2019-01-19 00:42:53 +00:00
parent 4ae36dfffe
commit c51a3340b1
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944
3 changed files with 94 additions and 9 deletions

86
src/yggdrasil/awdl.go Normal file
View File

@ -0,0 +1,86 @@
package yggdrasil
import (
"fmt"
"sync"
)
type awdl struct {
core *Core
mutex sync.RWMutex // protects interfaces below
interfaces map[string]*awdlInterface
}
type awdlInterface struct {
awdl *awdl
fromAWDL chan []byte
toAWDL chan []byte
shutdown chan bool
peer *peer
link *linkInterface
}
func (l *awdl) init(c *Core) error {
l.core = c
l.mutex.Lock()
l.interfaces = make(map[string]*awdlInterface)
l.mutex.Unlock()
return nil
}
func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name string) (*awdlInterface, error) {
link, err := l.core.link.create(fromAWDL, toAWDL, name)
if err != nil {
return nil, err
}
intf := awdlInterface{
awdl: l,
link: link,
fromAWDL: fromAWDL,
toAWDL: toAWDL,
shutdown: make(chan bool),
}
l.mutex.Lock()
l.interfaces[name] = &intf
l.mutex.Unlock()
return &intf, nil
}
func (l *awdl) getInterface(identity string) *awdlInterface {
l.mutex.RLock()
defer l.mutex.RUnlock()
if intf, ok := l.interfaces[identity]; ok {
return intf
}
return nil
}
func (l *awdl) shutdown(identity string) error {
if err := l.core.link.shutdown(identity); err != nil {
return err
}
if intf, ok := l.interfaces[identity]; ok {
intf.shutdown <- true
l.mutex.Lock()
delete(l.interfaces, identity)
l.mutex.Unlock()
return nil
}
return fmt.Errorf("interface '%s' doesn't exist or already shutdown", identity)
}
func (ai *awdlInterface) handler() {
for {
select {
case <-ai.shutdown:
return
case <-ai.link.shutdown:
return
case in := <-ai.fromAWDL:
ai.link.fromlink <- in
case out := <-ai.link.tolink:
ai.toAWDL <- out
}
}
}

View File

@ -44,7 +44,8 @@ type Core struct {
searches searches
multicast multicast
tcp tcpInterface
link link
link link // TODO: not sure if this wants to be here?
awdl awdl
log *log.Logger
}
@ -198,12 +199,10 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) error {
return err
}
/*
if err := c.awdl.init(c); err != nil {
c.log.Println("Failed to start AWDL interface")
return err
}
*/
if err := c.awdl.init(c); err != nil {
c.log.Println("Failed to start AWDL interface")
return err
}
if nc.SwitchOptions.MaxTotalQueueSize >= SwitchQueueTotalMinSize {
c.switchTable.queueTotalMaxSize = nc.SwitchOptions.MaxTotalQueueSize

View File

@ -35,7 +35,7 @@ func (l *link) init(c *Core) error {
return nil
}
func (l *link) create(fromlink chan []byte, tolink chan []byte /*boxPubKey *crypto.BoxPubKey, sigPubKey *crypto.SigPubKey*/, name string) (*linkInterface, error) {
func (l *link) create(fromlink chan []byte, tolink chan []byte, name string) (*linkInterface, error) {
intf := linkInterface{
link: l,
fromlink: fromlink,
@ -101,7 +101,7 @@ func (l *link) shutdown(identity string) error {
l.mutex.Unlock()
return nil
} else {
return errors.New(fmt.Sprintf("Interface '%s' doesn't exist or already shutdown", identity))
return fmt.Errorf("interface '%s' doesn't exist or already shutdown", identity)
}
}