Official cwtch.im peer and server implementations. https://cwtch.im
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.

server_instance.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package server
  2. import (
  3. "cwtch.im/cwtch/protocol"
  4. "cwtch.im/cwtch/server/listen"
  5. "cwtch.im/cwtch/server/storage"
  6. "git.openprivacy.ca/openprivacy/libricochet-go/application"
  7. "git.openprivacy.ca/openprivacy/libricochet-go/channels"
  8. )
  9. // Instance encapsulates the Ricochet application.
  10. type Instance struct {
  11. rai *application.Instance
  12. ra *application.RicochetApplication
  13. msi storage.MessageStoreInterface
  14. }
  15. // Init sets up a Server Instance
  16. func (si *Instance) Init(rai *application.Instance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
  17. si.rai = rai
  18. si.ra = ra
  19. si.msi = msi
  20. }
  21. // HandleFetchRequest returns a list of all messages in the servers buffer
  22. func (si *Instance) HandleFetchRequest() []*protocol.GroupMessage {
  23. return si.msi.FetchMessages()
  24. }
  25. // HandleGroupMessage takes in a group message and distributes it to all listening peers
  26. func (si *Instance) HandleGroupMessage(gm *protocol.GroupMessage) {
  27. si.msi.AddMessage(*gm)
  28. go si.ra.Broadcast(func(rai *application.Instance) {
  29. rai.Connection.Do(func() error {
  30. channel := rai.Connection.Channel("im.cwtch.server.listen", channels.Inbound)
  31. if channel != nil {
  32. cslc, ok := channel.Handler.(*listen.CwtchServerListenChannel)
  33. if ok {
  34. cslc.SendGroupMessage(gm)
  35. }
  36. }
  37. return nil
  38. })
  39. })
  40. }