Tapir provides a framework for building Anonymous / metadata resistant Services
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.

59 lines
1.9 KiB

  1. package applications
  2. import (
  3. "cwtch.im/tapir"
  4. )
  5. // ApplicationChain is a meta-app that can be used to build complex applications from other applications
  6. type ApplicationChain struct {
  7. TranscriptApp
  8. apps []tapir.Application
  9. endapp tapir.InteractiveApplication
  10. capabilities []tapir.Capability
  11. }
  12. // ChainApplication adds a new application to the chain. Returns a pointer to app so this call
  13. // can itself be chained.
  14. func (appchain *ApplicationChain) ChainApplication(app tapir.Application, capability tapir.Capability) *ApplicationChain {
  15. appchain.apps = append(appchain.apps, app.NewInstance())
  16. appchain.capabilities = append(appchain.capabilities, capability)
  17. return appchain
  18. }
  19. // ChainInteractiveApplication adds an interactive application to the chain. There can only be 1 interactive application.
  20. func (appchain *ApplicationChain) ChainInteractiveApplication(app tapir.InteractiveApplication) *ApplicationChain {
  21. appchain.endapp = app
  22. return appchain
  23. }
  24. // NewInstance should always return a new instantiation of the application.
  25. func (appchain *ApplicationChain) NewInstance() tapir.Application {
  26. applicationChain := new(ApplicationChain)
  27. for _, app := range appchain.apps {
  28. applicationChain.apps = append(applicationChain.apps, app.NewInstance())
  29. }
  30. applicationChain.capabilities = appchain.capabilities
  31. return applicationChain
  32. }
  33. // Init is run when the connection is first started.
  34. func (appchain *ApplicationChain) Init(connection tapir.Connection) {
  35. appchain.TranscriptApp.Init(connection)
  36. for i, app := range appchain.apps {
  37. app.PropagateTranscript(appchain.transcript)
  38. app.Init(connection)
  39. if connection.HasCapability(appchain.capabilities[i]) == false {
  40. connection.Close()
  41. return
  42. }
  43. connection.SetApp(app)
  44. }
  45. }
  46. // Listen calls listen on the Interactive application
  47. func (appchain *ApplicationChain) Listen() {
  48. if appchain.endapp != nil {
  49. appchain.endapp.Listen()
  50. }
  51. }