mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 06:20:26 +00:00
Adapt AWDL to link
This commit is contained in:
parent
7b2460662d
commit
9c6cf50684
@ -1,7 +1,7 @@
|
||||
package yggdrasil
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
"errors"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -12,13 +12,31 @@ type awdl struct {
|
||||
}
|
||||
|
||||
type awdlInterface struct {
|
||||
awdl *awdl
|
||||
link *linkInterface
|
||||
rwc awdlReadWriteCloser
|
||||
peer *peer
|
||||
stream stream
|
||||
}
|
||||
|
||||
type awdlReadWriteCloser struct {
|
||||
fromAWDL chan []byte
|
||||
toAWDL chan []byte
|
||||
shutdown chan bool
|
||||
peer *peer
|
||||
link *linkInterface
|
||||
stream stream
|
||||
}
|
||||
|
||||
func (c awdlReadWriteCloser) Read(p []byte) (n int, err error) {
|
||||
p = <-c.fromAWDL
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c awdlReadWriteCloser) Write(p []byte) (n int, err error) {
|
||||
c.toAWDL <- p
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c awdlReadWriteCloser) Close() error {
|
||||
close(c.fromAWDL)
|
||||
close(c.toAWDL)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *awdl) init(c *Core) error {
|
||||
@ -30,27 +48,26 @@ func (l *awdl) init(c *Core) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
/* temporarily disabled while getting the TCP side to work
|
||||
func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name string) (*awdlInterface, error) {
|
||||
link, err := l.core.link.create(fromAWDL, toAWDL, name)
|
||||
func (l *awdl) create(fromAWDL chan []byte, toAWDL chan []byte, name, local, remote string) (*awdlInterface, error) {
|
||||
rwc := awdlReadWriteCloser{
|
||||
fromAWDL: fromAWDL,
|
||||
toAWDL: toAWDL,
|
||||
}
|
||||
s := stream{}
|
||||
s.init(rwc, nil)
|
||||
link, err := l.core.link.create(&s, name, "awdl", local, remote)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
intf := awdlInterface{
|
||||
awdl: l,
|
||||
link: link,
|
||||
fromAWDL: fromAWDL,
|
||||
toAWDL: toAWDL,
|
||||
shutdown: make(chan bool),
|
||||
link: link,
|
||||
rwc: rwc,
|
||||
}
|
||||
inPacket := func(packet []byte) {
|
||||
intf.link.fromlink <- packet
|
||||
}
|
||||
intf.stream.init(nil, inPacket) // FIXME nil = ReadWriteCloser
|
||||
go intf.handler()
|
||||
intf.stream.init(intf.rwc, nil)
|
||||
l.mutex.Lock()
|
||||
l.interfaces[name] = &intf
|
||||
l.mutex.Unlock()
|
||||
go link.handler()
|
||||
return &intf, nil
|
||||
}
|
||||
|
||||
@ -64,31 +81,13 @@ func (l *awdl) getInterface(identity string) *awdlInterface {
|
||||
}
|
||||
|
||||
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
|
||||
close(intf.link.closed)
|
||||
intf.rwc.Close()
|
||||
l.mutex.Lock()
|
||||
delete(l.interfaces, identity)
|
||||
l.mutex.Unlock()
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("interface '%s' doesn't exist or already shutdown", identity)
|
||||
return errors.New("Interface not found or already closed")
|
||||
}
|
||||
|
||||
func (ai *awdlInterface) handler() {
|
||||
for {
|
||||
select {
|
||||
case <-ai.shutdown:
|
||||
return
|
||||
case <-ai.link.shutdown:
|
||||
return
|
||||
case in := <-ai.fromAWDL:
|
||||
ai.stream.handleInput(in)
|
||||
case out := <-ai.link.tolink:
|
||||
ai.toAWDL <- out
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
@ -87,10 +87,12 @@ func (intf *linkInterface) handler() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
intf.link.core.log.Println("Sent my metadata")
|
||||
metaBytes, err = intf.msgIO._recvMetaBytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
intf.link.core.log.Println("Received their metadata")
|
||||
meta = version_metadata{}
|
||||
if !meta.decode(metaBytes) || !meta.check() {
|
||||
return errors.New("failed to decode metadata")
|
||||
@ -100,6 +102,7 @@ func (intf *linkInterface) handler() error {
|
||||
intf.link.core.log.Println("Failed to connect to node: " + intf.name + " version: " + fmt.Sprintf("%d.%d", meta.ver, meta.minorVer))
|
||||
return errors.New("failed to connect: wrong version")
|
||||
}
|
||||
intf.link.core.log.Println("Do we have a link already?")
|
||||
// Check if we already have a link to this node
|
||||
intf.info.box = meta.box
|
||||
intf.info.sig = meta.sig
|
||||
@ -124,6 +127,7 @@ func (intf *linkInterface) handler() error {
|
||||
intf.link.core.log.Println("DEBUG: registered interface for", intf.name)
|
||||
}
|
||||
intf.link.mutex.Unlock()
|
||||
intf.link.core.log.Println("Create peer")
|
||||
// Create peer
|
||||
shared := crypto.GetSharedKey(myLinkPriv, &meta.link)
|
||||
intf.peer = intf.link.core.peers.newPeer(&meta.box, &meta.sig, shared, intf.name)
|
||||
@ -145,6 +149,7 @@ func (intf *linkInterface) handler() error {
|
||||
intf.peer.close = func() { intf.msgIO.close() }
|
||||
go intf.peer.linkLoop()
|
||||
// Start the writer
|
||||
intf.link.core.log.Println("Start writer")
|
||||
go func() {
|
||||
interval := 4 * time.Second
|
||||
timer := time.NewTimer(interval)
|
||||
@ -190,6 +195,7 @@ func (intf *linkInterface) handler() error {
|
||||
}()
|
||||
intf.link.core.switchTable.idleIn <- intf.peer.port // notify switch that we're idle
|
||||
// Run reader loop
|
||||
intf.link.core.log.Println("Start reader")
|
||||
for {
|
||||
msg, err := intf.msgIO.readMsg()
|
||||
if len(msg) > 0 {
|
||||
|
@ -29,22 +29,14 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *Core) AWDLCreateInterface(name string) error {
|
||||
func (c *Core) AWDLCreateInterface(name, local, remote string) error {
|
||||
fromAWDL := make(chan []byte, 32)
|
||||
toAWDL := make(chan []byte, 32)
|
||||
|
||||
if intf, err := c.awdl.create(fromAWDL, toAWDL, name); err == nil {
|
||||
if intf != nil {
|
||||
c.log.Println(err)
|
||||
return err
|
||||
} else {
|
||||
c.log.Println("c.awdl.create didn't return an interface")
|
||||
return errors.New("c.awdl.create didn't return an interface")
|
||||
}
|
||||
} else {
|
||||
c.log.Println(err)
|
||||
if intf, err := c.awdl.create(fromAWDL, toAWDL, name, local, remote); err != nil || intf == nil {
|
||||
c.log.Println("c.awdl.create:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Core) AWDLShutdownInterface(name string) error {
|
||||
@ -53,7 +45,7 @@ func (c *Core) AWDLShutdownInterface(name string) error {
|
||||
|
||||
func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
|
||||
if intf := c.awdl.getInterface(identity); intf != nil {
|
||||
return <-intf.toAWDL, nil
|
||||
return <-intf.rwc.toAWDL, nil
|
||||
}
|
||||
return nil, errors.New("AWDLRecvPacket identity not known: " + identity)
|
||||
}
|
||||
@ -61,7 +53,7 @@ func (c *Core) AWDLRecvPacket(identity string) ([]byte, error) {
|
||||
func (c *Core) AWDLSendPacket(identity string, buf []byte) error {
|
||||
packet := append(util.GetBytes(), buf[:]...)
|
||||
if intf := c.awdl.getInterface(identity); intf != nil {
|
||||
intf.fromAWDL <- packet
|
||||
intf.rwc.fromAWDL <- packet
|
||||
return nil
|
||||
}
|
||||
return errors.New("AWDLSendPacket identity not known: " + identity)
|
||||
|
Loading…
Reference in New Issue
Block a user