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.

124 lines
2.9KB

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