Official cwtch.im peer and server implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

peerserverconnection.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package connections
  2. import (
  3. "errors"
  4. "git.mascherari.press/cwtch/peer/fetch"
  5. "git.mascherari.press/cwtch/peer/listen"
  6. "git.mascherari.press/cwtch/peer/send"
  7. "git.mascherari.press/cwtch/protocol"
  8. "github.com/s-rah/go-ricochet"
  9. "github.com/s-rah/go-ricochet/channels"
  10. "github.com/s-rah/go-ricochet/connection"
  11. "github.com/s-rah/go-ricochet/identity"
  12. "github.com/s-rah/go-ricochet/utils"
  13. "log"
  14. "time"
  15. )
  16. type PeerServerConnection struct {
  17. connection.AutoConnectionHandler
  18. Server string
  19. state ConnectionState
  20. connection connection.Connection
  21. GroupMessageHandler func(string, *protocol.GroupMessage)
  22. }
  23. func NewPeerServerConnection(serverhostname string) *PeerServerConnection {
  24. psc := new(PeerServerConnection)
  25. psc.Server = serverhostname
  26. psc.Init()
  27. return psc
  28. }
  29. // GetState returns the current connection state
  30. func (psc *PeerServerConnection) GetState() ConnectionState {
  31. return psc.state
  32. }
  33. // Run manages the setup and teardown of a peer server connection
  34. func (psc *PeerServerConnection) Run() error {
  35. log.Printf("Connecting to %v", psc.Server)
  36. rc, err := goricochet.Open(psc.Server)
  37. if err == nil {
  38. rc.TraceLog(true)
  39. psc.connection = *rc
  40. psc.state = CONNECTED
  41. pk, err := utils.GeneratePrivateKey()
  42. if err == nil {
  43. _, err := connection.HandleOutboundConnection(&psc.connection).ProcessAuthAsClient(identity.Initialize("cwtchpeer", pk))
  44. if err == nil {
  45. psc.state = AUTHENTICATED
  46. go func() {
  47. psc.connection.Do(func() error {
  48. psc.connection.RequestOpenChannel("im.cwtch.server.fetch", &fetch.CwtchPeerFetchChannel{Handler: psc})
  49. return nil
  50. })
  51. psc.connection.Do(func() error {
  52. psc.connection.RequestOpenChannel("im.cwtch.server.listen", &listen.CwtchPeerListenChannel{Handler: psc})
  53. return nil
  54. })
  55. }()
  56. psc.connection.Process(psc)
  57. }
  58. }
  59. }
  60. psc.state = FAILED
  61. return err
  62. }
  63. // Break makes Run() return and prevents processing, but doesn't close the connection.
  64. func (psc *PeerServerConnection) Break() error {
  65. return psc.connection.Break()
  66. }
  67. func (psc *PeerServerConnection) SendGroupMessage(gm *protocol.GroupMessage) {
  68. for psc.state != AUTHENTICATED {
  69. time.Sleep(time.Second * 2)
  70. }
  71. log.Printf("Opening a Channel to Send")
  72. psc.connection.Do(func() error {
  73. psc.connection.RequestOpenChannel("im.cwtch.server.send", &send.CwtchPeerSendChannel{})
  74. return nil
  75. })
  76. log.Printf("Waiting...")
  77. // TODO We have to wait to receive the channel result before we can continue
  78. // We should have a better mechanism for this kindof interaction
  79. log.Printf("CWTCH PEER Sending...")
  80. send:
  81. time.Sleep(time.Second * 2)
  82. err := psc.connection.Do(func() error {
  83. channel := psc.connection.Channel("im.cwtch.server.send", channels.Outbound)
  84. if channel == nil {
  85. return errors.New("No Channel")
  86. }
  87. sendchannel, ok := channel.Handler.(*send.CwtchPeerSendChannel)
  88. if ok {
  89. sendchannel.SendGroupMessage(gm)
  90. } else {
  91. return errors.New("Failed")
  92. }
  93. return nil
  94. })
  95. for err != nil {
  96. log.Printf("CHANNEL ERROR %v", err)
  97. goto send
  98. }
  99. log.Printf("Done")
  100. }
  101. func (psc *PeerServerConnection) HandleGroupMessage(gm *protocol.GroupMessage) {
  102. log.Printf("Received Group Message: %v", gm)
  103. psc.GroupMessageHandler(psc.Server, gm)
  104. }