From c51a3340b14402d496082bcd29fef5ee91a3acbf Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 19 Jan 2019 00:42:53 +0000 Subject: [PATCH] Update awdl.go to use new link stuff (untested) --- src/yggdrasil/awdl.go | 86 +++++++++++++++++++++++++++++++++++++++++++ src/yggdrasil/core.go | 13 +++---- src/yggdrasil/link.go | 4 +- 3 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 src/yggdrasil/awdl.go diff --git a/src/yggdrasil/awdl.go b/src/yggdrasil/awdl.go new file mode 100644 index 00000000..43db934a --- /dev/null +++ b/src/yggdrasil/awdl.go @@ -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 + } + } +} diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index fa5d02ad..1f26b69a 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -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 diff --git a/src/yggdrasil/link.go b/src/yggdrasil/link.go index 1bbd0c34..386fef88 100644 --- a/src/yggdrasil/link.go +++ b/src/yggdrasil/link.go @@ -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) } }