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.

appClient.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package app
  2. import (
  3. "cwtch.im/cwtch/app/plugins"
  4. "cwtch.im/cwtch/event"
  5. "cwtch.im/cwtch/peer"
  6. "cwtch.im/cwtch/storage"
  7. "fmt"
  8. "git.openprivacy.ca/openprivacy/libricochet-go/log"
  9. "path"
  10. "strconv"
  11. )
  12. type applicationClient struct {
  13. applicationBridge
  14. appletPeers
  15. appBus event.Manager
  16. }
  17. // NewAppClient returns an Application that acts as a client to a AppService, connected by the IPCBridge supplied
  18. func NewAppClient(appDirectory string, bridge event.IPCBridge) Application {
  19. appClient := &applicationClient{appletPeers: appletPeers{peers: make(map[string]peer.CwtchPeer)}, applicationBridge: applicationBridge{applicationCore: *newAppCore(appDirectory), bridge: bridge}, appBus: event.NewEventManager()}
  20. appClient.handle = appClient.handleEvent
  21. go appClient.listen()
  22. appClient.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadClient)})
  23. log.Infoln("Created new App Client")
  24. return appClient
  25. }
  26. // GetPrimaryBus returns the bus the Application uses for events that aren't peer specific
  27. func (ac *applicationClient) GetPrimaryBus() event.Manager {
  28. return ac.appBus
  29. }
  30. func (ac *applicationClient) handleEvent(ev *event.Event) {
  31. switch ev.EventType {
  32. case event.NewPeer:
  33. localID := ev.Data[event.Identity]
  34. password := ev.Data[event.Password]
  35. reload := ev.Data[event.Status] == "running"
  36. ac.newPeer(localID, password, reload)
  37. case event.PeerError:
  38. ac.appBus.Publish(*ev)
  39. case event.AppError:
  40. ac.appBus.Publish(*ev)
  41. case event.ACNStatus:
  42. ac.appBus.Publish(*ev)
  43. case event.ReloadDone:
  44. ac.appBus.Publish(*ev)
  45. }
  46. }
  47. func (ac *applicationClient) newPeer(localID, password string, reload bool) {
  48. profile, err := storage.ReadProfile(path.Join(ac.directory, "profiles", localID), password)
  49. if err != nil {
  50. log.Errorf("Could not read profile for NewPeer event: %v\n", err)
  51. ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("Could not read profile for NewPeer event: %v\n", err)))
  52. return
  53. }
  54. _, exists := ac.peers[profile.Onion]
  55. if exists {
  56. log.Errorf("profile for onion %v already exists", profile.Onion)
  57. ac.appBus.Publish(event.NewEventList(event.PeerError, event.Error, fmt.Sprintf("profile for onion %v already exists", profile.Onion)))
  58. return
  59. }
  60. eventBus := event.NewIPCEventManager(ac.bridge, profile.Onion)
  61. peer := peer.FromProfile(profile)
  62. peer.Init(eventBus)
  63. ac.mutex.Lock()
  64. defer ac.mutex.Unlock()
  65. ac.peers[profile.Onion] = peer
  66. ac.eventBuses[profile.Onion] = eventBus
  67. npEvent := event.NewEvent(event.NewPeer, map[event.Field]string{event.Identity: profile.Onion})
  68. if reload {
  69. npEvent.Data[event.Status] = "running"
  70. }
  71. ac.appBus.Publish(npEvent)
  72. if reload {
  73. ac.bridge.Write(&event.IPCMessage{Dest: DestApp, Message: event.NewEventList(event.ReloadPeer, event.Identity, profile.Onion)})
  74. }
  75. }
  76. // CreatePeer messages the service to create a new Peer with the given name
  77. func (ac *applicationClient) CreatePeer(name string, password string) {
  78. log.Infof("appClient CreatePeer %v\n", name)
  79. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.CreatePeer, map[event.Field]string{event.ProfileName: name, event.Password: password})}
  80. ac.bridge.Write(&message)
  81. }
  82. func (ac *applicationClient) AddPeerPlugin(onion string, pluginID plugins.PluginID) {
  83. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.AddPeerPlugin, map[event.Field]string{event.Identity: onion, event.Data: strconv.Itoa(int(pluginID))})}
  84. ac.bridge.Write(&message)
  85. }
  86. // LoadProfiles messages the service to load any profiles for the given password
  87. func (ac *applicationClient) LoadProfiles(password string) {
  88. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.LoadProfiles, map[event.Field]string{event.Password: password})}
  89. ac.bridge.Write(&message)
  90. }
  91. func (ac *applicationClient) QueryACNStatus() {
  92. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.GetACNStatus, map[event.Field]string{})}
  93. ac.bridge.Write(&message)
  94. }
  95. // ShutdownPeer shuts down a peer and removes it from the app's management
  96. func (ac *applicationClient) ShutdownPeer(onion string) {
  97. ac.mutex.Lock()
  98. defer ac.mutex.Unlock()
  99. ac.eventBuses[onion].Shutdown()
  100. delete(ac.eventBuses, onion)
  101. ac.peers[onion].Shutdown()
  102. delete(ac.peers, onion)
  103. message := event.IPCMessage{Dest: DestApp, Message: event.NewEvent(event.ShutdownPeer, map[event.Field]string{event.Identity: onion})}
  104. ac.bridge.Write(&message)
  105. }
  106. // Shutdown shuts down the application client and all front end peer components
  107. func (ac *applicationClient) Shutdown() {
  108. for id := range ac.peers {
  109. ac.ShutdownPeer(id)
  110. }
  111. ac.applicationBridge.Shutdown()
  112. ac.appBus.Shutdown()
  113. }