cwtch/peer/connections/peerserverconnection.go

117 lines
3.2 KiB
Go
Raw Normal View History

package connections
import (
2018-03-30 21:16:51 +00:00
"errors"
"git.mascherari.press/cwtch/peer/fetch"
"git.mascherari.press/cwtch/peer/listen"
"git.mascherari.press/cwtch/peer/send"
2018-03-10 22:05:48 +00:00
"git.mascherari.press/cwtch/protocol"
"github.com/s-rah/go-ricochet"
"github.com/s-rah/go-ricochet/channels"
2018-03-10 22:05:48 +00:00
"github.com/s-rah/go-ricochet/connection"
"github.com/s-rah/go-ricochet/identity"
"github.com/s-rah/go-ricochet/utils"
2018-03-30 21:16:51 +00:00
"log"
"time"
)
type PeerServerConnection struct {
connection.AutoConnectionHandler
Server string
state ConnectionState
connection connection.Connection
2018-03-10 22:05:48 +00:00
GroupMessageHandler func(string, *protocol.GroupMessage)
}
func NewPeerServerConnection(serverhostname string) *PeerServerConnection {
psc := new(PeerServerConnection)
psc.Server = serverhostname
psc.Init()
return psc
}
// GetState returns the current connection state
func (psc *PeerServerConnection) GetState() ConnectionState {
return psc.state
}
// Run manages the setup and teardown of a peer server connection
func (psc *PeerServerConnection) Run() error {
2018-03-30 21:16:51 +00:00
log.Printf("Connecting to %v", psc.Server)
rc, err := goricochet.Open(psc.Server)
if err == nil {
2018-03-10 22:05:48 +00:00
rc.TraceLog(true)
psc.connection = *rc
psc.state = CONNECTED
pk, err := utils.GeneratePrivateKey()
if err == nil {
_, err := connection.HandleOutboundConnection(&psc.connection).ProcessAuthAsClient(identity.Initialize("cwtchpeer", pk))
if err == nil {
psc.state = AUTHENTICATED
2018-03-10 22:05:48 +00:00
go func() {
psc.connection.Do(func() error {
psc.connection.RequestOpenChannel("im.cwtch.server.fetch", &fetch.CwtchPeerFetchChannel{Handler: psc})
return nil
})
psc.connection.Do(func() error {
psc.connection.RequestOpenChannel("im.cwtch.server.listen", &listen.CwtchPeerListenChannel{Handler: psc})
return nil
})
2018-03-10 22:05:48 +00:00
}()
psc.connection.Process(psc)
}
}
}
psc.state = FAILED
return err
}
// Break makes Run() return and prevents processing, but doesn't close the connection.
func (psc *PeerServerConnection) Break() error {
return psc.connection.Break()
}
func (psc *PeerServerConnection) SendGroupMessage(gm *protocol.GroupMessage) {
2018-03-30 21:16:51 +00:00
for psc.state != AUTHENTICATED {
time.Sleep(time.Second * 2)
}
log.Printf("Opening a Channel to Send")
2018-03-10 22:05:48 +00:00
psc.connection.Do(func() error {
psc.connection.RequestOpenChannel("im.cwtch.server.send", &send.CwtchPeerSendChannel{})
return nil
})
2018-03-30 21:16:51 +00:00
log.Printf("Waiting...")
2018-03-10 22:05:48 +00:00
// TODO We have to wait to receive the channel result before we can continue
// We should have a better mechanism for this kindof interaction
2018-03-30 21:16:51 +00:00
log.Printf("CWTCH PEER Sending...")
send:
time.Sleep(time.Second * 2)
err := psc.connection.Do(func() error {
2018-03-10 22:05:48 +00:00
channel := psc.connection.Channel("im.cwtch.server.send", channels.Outbound)
2018-03-30 21:16:51 +00:00
if channel == nil {
return errors.New("No Channel")
}
sendchannel, ok := channel.Handler.(*send.CwtchPeerSendChannel)
if ok {
sendchannel.SendGroupMessage(gm)
} else {
return errors.New("Failed")
2018-03-10 22:05:48 +00:00
}
return nil
})
2018-03-30 21:16:51 +00:00
for err != nil {
log.Printf("CHANNEL ERROR %v", err)
goto send
}
log.Printf("Done")
}
2018-03-10 22:05:48 +00:00
func (psc *PeerServerConnection) HandleGroupMessage(gm *protocol.GroupMessage) {
2018-03-30 21:16:51 +00:00
log.Printf("Received Group Message: %v", gm)
2018-03-10 22:05:48 +00:00
psc.GroupMessageHandler(psc.Server, gm)
}