deviceconfiguration.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 config
  7. import "github.com/syncthing/syncthing/lib/protocol"
  8. type DeviceConfiguration struct {
  9. DeviceID protocol.DeviceID `xml:"id,attr" json:"deviceID"`
  10. Name string `xml:"name,attr,omitempty" json:"name"`
  11. Addresses []string `xml:"address,omitempty" json:"addresses"`
  12. Compression protocol.Compression `xml:"compression,attr" json:"compression"`
  13. CertName string `xml:"certName,attr,omitempty" json:"certName"`
  14. Introducer bool `xml:"introducer,attr" json:"introducer"`
  15. SkipIntroductionRemovals bool `xml:"skipIntroductionRemovals,attr" json:"skipIntroductionRemovals"`
  16. IntroducedBy protocol.DeviceID `xml:"introducedBy,attr" json:"introducedBy"`
  17. Paused bool `xml:"paused" json:"paused"`
  18. AllowedNetworks []string `xml:"allowedNetwork,omitempty" json:"allowedNetworks"`
  19. AutoAcceptFolders bool `xml:"autoAcceptFolders" json:"autoAcceptFolders"`
  20. MaxSendKbps int `xml:"maxSendKbps" json:"maxSendKbps"`
  21. MaxRecvKbps int `xml:"maxRecvKbps" json:"maxRecvKbps"`
  22. }
  23. func NewDeviceConfiguration(id protocol.DeviceID, name string) DeviceConfiguration {
  24. d := DeviceConfiguration{
  25. DeviceID: id,
  26. Name: name,
  27. }
  28. d.prepare()
  29. return d
  30. }
  31. func (cfg DeviceConfiguration) Copy() DeviceConfiguration {
  32. c := cfg
  33. c.Addresses = make([]string, len(cfg.Addresses))
  34. copy(c.Addresses, cfg.Addresses)
  35. c.AllowedNetworks = make([]string, len(cfg.AllowedNetworks))
  36. copy(c.AllowedNetworks, cfg.AllowedNetworks)
  37. return c
  38. }
  39. func (cfg *DeviceConfiguration) prepare() {
  40. if len(cfg.Addresses) == 0 || len(cfg.Addresses) == 1 && cfg.Addresses[0] == "" {
  41. cfg.Addresses = []string{"dynamic"}
  42. }
  43. if len(cfg.AllowedNetworks) == 0 {
  44. cfg.AllowedNetworks = []string{}
  45. }
  46. }
  47. type DeviceConfigurationList []DeviceConfiguration
  48. func (l DeviceConfigurationList) Less(a, b int) bool {
  49. return l[a].DeviceID.Compare(l[b].DeviceID) == -1
  50. }
  51. func (l DeviceConfigurationList) Swap(a, b int) {
  52. l[a], l[b] = l[b], l[a]
  53. }
  54. func (l DeviceConfigurationList) Len() int {
  55. return len(l)
  56. }