forked from cwtch.im/cwtch
113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
package connections
|
|
|
|
import (
|
|
"cwtch.im/cwtch/protocol/connections/peer"
|
|
"git.openprivacy.ca/openprivacy/libricochet-go"
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/channels"
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/connection"
|
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
|
"time"
|
|
)
|
|
|
|
// PeerPeerConnection encapsulates a single outgoing cwtchPeer->cwtchPeer connection
|
|
type PeerPeerConnection struct {
|
|
connection.AutoConnectionHandler
|
|
PeerHostname string
|
|
state ConnectionState
|
|
connection *connection.Connection
|
|
protocolEngine *Engine
|
|
}
|
|
|
|
// NewPeerPeerConnection creates a new peer connection for the given hostname and profile.
|
|
func NewPeerPeerConnection(peerhostname string, protocolEngine *Engine) *PeerPeerConnection {
|
|
ppc := new(PeerPeerConnection)
|
|
ppc.PeerHostname = peerhostname
|
|
ppc.protocolEngine = protocolEngine
|
|
ppc.Init()
|
|
return ppc
|
|
}
|
|
|
|
// GetState returns the current connection state
|
|
func (ppc *PeerPeerConnection) GetState() ConnectionState {
|
|
return ppc.state
|
|
}
|
|
|
|
// SendPacket sends data packets on the optional data channel
|
|
func (ppc *PeerPeerConnection) SendPacket(data []byte) {
|
|
ppc.WaitTilAuthenticated()
|
|
ppc.connection.Do(func() error {
|
|
channel := ppc.connection.Channel("im.cwtch.peer.data", channels.Outbound)
|
|
if channel != nil {
|
|
peerchannel, ok := channel.Handler.(*peer.CwtchPeerDataChannel)
|
|
if ok {
|
|
log.Debugf("Sending packet\n")
|
|
peerchannel.SendMessage(data)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// SendGroupInvite sends the given serialized invite packet to the Peer
|
|
func (ppc *PeerPeerConnection) SendGroupInvite(invite []byte) {
|
|
ppc.WaitTilAuthenticated()
|
|
ppc.connection.Do(func() error {
|
|
channel := ppc.connection.Channel("im.cwtch.peer", channels.Outbound)
|
|
if channel != nil {
|
|
peerchannel, ok := channel.Handler.(*peer.CwtchPeerChannel)
|
|
if ok {
|
|
log.Debugf("Sending group invite packet\n")
|
|
peerchannel.SendMessage(invite)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// WaitTilAuthenticated waits until the underlying connection is authenticated
|
|
func (ppc *PeerPeerConnection) WaitTilAuthenticated() {
|
|
for {
|
|
if ppc.GetState() == AUTHENTICATED {
|
|
break
|
|
}
|
|
time.Sleep(time.Second * 1)
|
|
}
|
|
}
|
|
|
|
// Run manages the setup and teardown of a peer->peer connection
|
|
func (ppc *PeerPeerConnection) Run() error {
|
|
ppc.state = CONNECTING
|
|
rc, err := goricochet.Open(ppc.protocolEngine.ACN, ppc.PeerHostname)
|
|
if err == nil {
|
|
ppc.connection = rc
|
|
ppc.state = CONNECTED
|
|
_, err := connection.HandleOutboundConnection(ppc.connection).ProcessAuthAsV3Client(ppc.protocolEngine.Identity)
|
|
if err == nil {
|
|
ppc.state = AUTHENTICATED
|
|
go func() {
|
|
ppc.connection.Do(func() error {
|
|
ppc.connection.RequestOpenChannel("im.cwtch.peer", &peer.CwtchPeerChannel{Handler: ppc.protocolEngine.GetPeerHandler(ppc.PeerHostname)})
|
|
return nil
|
|
})
|
|
|
|
ppc.connection.Do(func() error {
|
|
ppc.connection.RequestOpenChannel("im.cwtch.peer.data", &peer.CwtchPeerDataChannel{Handler: ppc.protocolEngine.GetPeerHandler(ppc.PeerHostname)})
|
|
return nil
|
|
})
|
|
}()
|
|
|
|
ppc.connection.Process(ppc)
|
|
}
|
|
}
|
|
ppc.state = FAILED
|
|
return err
|
|
}
|
|
|
|
// Close closes the connection
|
|
func (ppc *PeerPeerConnection) Close() {
|
|
ppc.state = KILLED
|
|
if ppc.connection != nil {
|
|
ppc.connection.Close()
|
|
}
|
|
}
|