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 3.7KB

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