2019-01-19 03:42:53 +03:00
|
|
|
package yggdrasil
|
|
|
|
|
|
|
|
import (
|
2019-01-22 08:08:50 +03:00
|
|
|
//"fmt"
|
2019-01-19 03:42:53 +03:00
|
|
|
"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
|
2019-01-19 15:19:24 +03:00
|
|
|
stream stream
|
2019-01-19 03:42:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *awdl) init(c *Core) error {
|
|
|
|
l.core = c
|
|
|
|
l.mutex.Lock()
|
|
|
|
l.interfaces = make(map[string]*awdlInterface)
|
|
|
|
l.mutex.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-22 08:08:50 +03:00
|
|
|
/* temporarily disabled while getting the TCP side to work
|
2019-01-19 03:42:53 +03:00
|
|
|
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),
|
|
|
|
}
|
2019-01-20 01:37:45 +03:00
|
|
|
inPacket := func(packet []byte) {
|
|
|
|
intf.link.fromlink <- packet
|
|
|
|
}
|
2019-01-22 06:27:52 +03:00
|
|
|
intf.stream.init(nil, inPacket) // FIXME nil = ReadWriteCloser
|
2019-01-19 15:19:24 +03:00
|
|
|
go intf.handler()
|
2019-01-19 03:42:53 +03:00
|
|
|
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:
|
2019-01-20 01:37:45 +03:00
|
|
|
ai.stream.handleInput(in)
|
2019-01-19 03:42:53 +03:00
|
|
|
case out := <-ai.link.tolink:
|
|
|
|
ai.toAWDL <- out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-22 08:08:50 +03:00
|
|
|
*/
|