bep_clusterconfig.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "fmt"
  9. "log/slog"
  10. "github.com/syncthing/syncthing/internal/gen/bep"
  11. )
  12. type Compression bep.Compression
  13. const (
  14. CompressionMetadata = Compression(bep.Compression_COMPRESSION_METADATA)
  15. CompressionNever = Compression(bep.Compression_COMPRESSION_NEVER)
  16. CompressionAlways = Compression(bep.Compression_COMPRESSION_ALWAYS)
  17. )
  18. type FolderType bep.FolderType
  19. const (
  20. FolderTypeSendReceive = FolderType(bep.FolderType_FOLDER_TYPE_SEND_RECEIVE)
  21. FolderTypeSendOnly = FolderType(bep.FolderType_FOLDER_TYPE_SEND_ONLY)
  22. FolderTypeReceiveOnly = FolderType(bep.FolderType_FOLDER_TYPE_RECEIVE_ONLY)
  23. FolderTypeReceiveEncrypted = FolderType(bep.FolderType_FOLDER_TYPE_RECEIVE_ENCRYPTED)
  24. )
  25. type FolderStopReason bep.FolderStopReason
  26. const (
  27. FolderStopReasonRunning = FolderStopReason(bep.FolderStopReason_FOLDER_STOP_REASON_RUNNING)
  28. FolderStopReasonPaused = FolderStopReason(bep.FolderStopReason_FOLDER_STOP_REASON_PAUSED)
  29. )
  30. type ClusterConfig struct {
  31. Folders []Folder
  32. Secondary bool
  33. }
  34. func (c *ClusterConfig) toWire() *bep.ClusterConfig {
  35. folders := make([]*bep.Folder, len(c.Folders))
  36. for i, f := range c.Folders {
  37. folders[i] = f.toWire()
  38. }
  39. return &bep.ClusterConfig{
  40. Folders: folders,
  41. Secondary: c.Secondary,
  42. }
  43. }
  44. func clusterConfigFromWire(w *bep.ClusterConfig) *ClusterConfig {
  45. if w == nil {
  46. return nil
  47. }
  48. c := &ClusterConfig{
  49. Secondary: w.Secondary,
  50. }
  51. c.Folders = make([]Folder, len(w.Folders))
  52. for i, f := range w.Folders {
  53. c.Folders[i] = folderFromWire(f)
  54. }
  55. return c
  56. }
  57. type Folder struct {
  58. ID string
  59. Label string
  60. Type FolderType
  61. StopReason FolderStopReason
  62. Devices []Device
  63. }
  64. func (f *Folder) toWire() *bep.Folder {
  65. devices := make([]*bep.Device, len(f.Devices))
  66. for i, d := range f.Devices {
  67. devices[i] = d.toWire()
  68. }
  69. return &bep.Folder{
  70. Id: f.ID,
  71. Label: f.Label,
  72. Type: bep.FolderType(f.Type),
  73. StopReason: bep.FolderStopReason(f.StopReason),
  74. Devices: devices,
  75. }
  76. }
  77. func folderFromWire(w *bep.Folder) Folder {
  78. devices := make([]Device, len(w.Devices))
  79. for i, d := range w.Devices {
  80. devices[i] = deviceFromWire(d)
  81. }
  82. return Folder{
  83. ID: w.Id,
  84. Label: w.Label,
  85. Type: FolderType(w.Type),
  86. StopReason: FolderStopReason(w.StopReason),
  87. Devices: devices,
  88. }
  89. }
  90. func (f Folder) Description() string {
  91. // used by logging stuff
  92. if f.Label == "" {
  93. return f.ID
  94. }
  95. return fmt.Sprintf("%q (%s)", f.Label, f.ID)
  96. }
  97. func (f Folder) LogAttr() slog.Attr {
  98. if f.Label == "" || f.Label == f.ID {
  99. return slog.Group("folder", slog.String("id", f.ID))
  100. }
  101. return slog.Group("folder", slog.String("label", f.Label), slog.String("id", f.ID))
  102. }
  103. func (f Folder) IsRunning() bool {
  104. switch f.StopReason {
  105. case FolderStopReasonPaused:
  106. return false
  107. default:
  108. return true
  109. }
  110. }
  111. type Device struct {
  112. ID DeviceID
  113. Name string
  114. Addresses []string
  115. Compression Compression
  116. CertName string
  117. MaxSequence int64
  118. Introducer bool
  119. IndexID IndexID
  120. SkipIntroductionRemovals bool
  121. EncryptionPasswordToken []byte
  122. }
  123. func (d *Device) toWire() *bep.Device {
  124. return &bep.Device{
  125. Id: d.ID[:],
  126. Name: d.Name,
  127. Addresses: d.Addresses,
  128. Compression: bep.Compression(d.Compression),
  129. CertName: d.CertName,
  130. MaxSequence: d.MaxSequence,
  131. Introducer: d.Introducer,
  132. IndexId: uint64(d.IndexID),
  133. SkipIntroductionRemovals: d.SkipIntroductionRemovals,
  134. EncryptionPasswordToken: d.EncryptionPasswordToken,
  135. }
  136. }
  137. func deviceFromWire(w *bep.Device) Device {
  138. return Device{
  139. ID: DeviceID(w.Id),
  140. Name: w.Name,
  141. Addresses: w.Addresses,
  142. Compression: Compression(w.Compression),
  143. CertName: w.CertName,
  144. MaxSequence: w.MaxSequence,
  145. Introducer: w.Introducer,
  146. IndexID: IndexID(w.IndexId),
  147. SkipIntroductionRemovals: w.SkipIntroductionRemovals,
  148. EncryptionPasswordToken: w.EncryptionPasswordToken,
  149. }
  150. }