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.

applets.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package app
  2. import (
  3. "cwtch.im/cwtch/event"
  4. "git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
  5. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  6. "sync"
  7. "cwtch.im/cwtch/app/plugins"
  8. "cwtch.im/cwtch/peer"
  9. )
  10. type appletPeers struct {
  11. peers map[string]peer.CwtchPeer
  12. launched bool // bit hacky, place holder while we transition to full multi peer support and a better api
  13. }
  14. type appletACN struct {
  15. acn connectivity.ACN
  16. }
  17. type appletPlugins struct {
  18. plugins sync.Map //map[string] []plugins.Plugin
  19. }
  20. // ***** applet ACN
  21. func (a *appletACN) init(acn connectivity.ACN, publish func(int, string)) {
  22. a.acn = acn
  23. acn.SetStatusCallback(publish)
  24. prog, status := acn.GetBootstrapStatus()
  25. publish(prog, status)
  26. }
  27. func (a *appletACN) Shutdown() {
  28. a.acn.Close()
  29. }
  30. // ***** appletPeers
  31. func (ap *appletPeers) init() {
  32. ap.peers = make(map[string]peer.CwtchPeer)
  33. ap.launched = false
  34. }
  35. // LaunchPeers starts each peer Listening and connecting to peers and groups
  36. func (ap *appletPeers) LaunchPeers() {
  37. log.Debugf("appletPeers LaunchPeers\n")
  38. if ap.launched {
  39. return
  40. }
  41. for _, p := range ap.peers {
  42. p.Listen()
  43. p.StartPeersConnections()
  44. p.StartGroupConnections()
  45. }
  46. ap.launched = true
  47. }
  48. // ListPeers returns a map of onions to their profile's Name
  49. func (ap *appletPeers) ListPeers() map[string]string {
  50. keys := map[string]string{}
  51. for k, p := range ap.peers {
  52. keys[k] = p.GetProfile().Name
  53. }
  54. return keys
  55. }
  56. // GetPeer returns a cwtchPeer for a given onion address
  57. func (ap *appletPeers) GetPeer(onion string) peer.CwtchPeer {
  58. if peer, ok := ap.peers[onion]; ok {
  59. return peer
  60. }
  61. return nil
  62. }
  63. // ***** applet Plugins
  64. func (ap *appletPlugins) Shutdown() {
  65. log.Debugf("shutting down applet plugins...")
  66. ap.plugins.Range(func(k, v interface{}) bool {
  67. log.Debugf("shutting down plugins for %v", k)
  68. ap.ShutdownPeer(k.(string))
  69. return true
  70. })
  71. }
  72. func (ap *appletPlugins) ShutdownPeer(peerid string) {
  73. log.Debugf("shutting down plugins for %v", peerid)
  74. pluginsI, ok := ap.plugins.Load(peerid)
  75. if ok {
  76. plugins := pluginsI.([]plugins.Plugin)
  77. for _, plugin := range plugins {
  78. log.Debugf("shutting down plugin: %v", plugin)
  79. plugin.Shutdown()
  80. }
  81. }
  82. }
  83. func (ap *appletPlugins) AddPlugin(peerid string, id plugins.PluginID, bus event.Manager, acn connectivity.ACN) {
  84. if _, exists := ap.plugins.Load(peerid); !exists {
  85. ap.plugins.Store(peerid, []plugins.Plugin{})
  86. }
  87. pluginsinf, _ := ap.plugins.Load(peerid)
  88. peerPlugins := pluginsinf.([]plugins.Plugin)
  89. newp := plugins.Get(id, bus, acn)
  90. newp.Start()
  91. peerPlugins = append(peerPlugins, newp)
  92. log.Debugf("storing plugin for %v %v", peerid, peerPlugins)
  93. ap.plugins.Store(peerid, peerPlugins)
  94. }