engine.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package syncthingserv
  2. import "time"
  3. /*
  4. Engine
  5. The embedded Syncthing instance, behind an interface so the manager can be
  6. tested without it. engine_syncthing.go has the real one; on platforms the
  7. Syncthing database cannot be built for, newEngine stays nil and the file
  8. server is left out of the list.
  9. */
  10. // DefaultPort is Syncthing's own default for sync connections
  11. const DefaultPort = 22000
  12. // newEngine is set by engine_syncthing.go where Syncthing is built in.
  13. var newEngine func() engine
  14. // qrCode draws text as a PNG QR code, set beside newEngine.
  15. var qrCode func(text string) ([]byte, error)
  16. // Available reports whether this build carries Syncthing.
  17. func Available() bool {
  18. return newEngine != nil
  19. }
  20. type engineConfig struct {
  21. Dir string //Keys, configuration and index of the server device
  22. Name string //Device name the phones see
  23. Settings Settings
  24. }
  25. type engine interface {
  26. //Start runs Syncthing with the given plan already applied
  27. Start(cfg engineConfig, p plan) error
  28. Stop()
  29. //Apply replaces the devices and folders, and the network settings
  30. Apply(cfg engineConfig, p plan) error
  31. //DeviceID is the server's device ID, "" while stopped
  32. DeviceID() string
  33. //ParseDeviceID checks a device ID and returns it in its usual form
  34. ParseDeviceID(s string) (string, error)
  35. FolderStatus(id string) FolderStatus
  36. DeviceStatus(id string) DeviceStatus
  37. //Offers lists the folders a device wants to share with the server
  38. Offers(deviceID string) []Offer
  39. Rescan(folderID string) error
  40. //RestartFolder stops a folder and starts it again, like pausing and
  41. //resuming it, which gives up the requests a device never answered
  42. RestartFolder(folderID string) error
  43. //LocalBytes is the size of a folder as Syncthing counts it
  44. LocalBytes(folderID string) (int64, bool)
  45. //UnknownDevices tried to connect but nobody added them
  46. UnknownDevices() []UnknownDevice
  47. }
  48. // FolderStatus is what Syncthing reports about a folder.
  49. type FolderStatus struct {
  50. State string //idle, scanning, syncing, error, paused, ... or "" when unknown
  51. Error string
  52. LocalFiles int
  53. LocalBytes int64
  54. GlobalFiles int
  55. GlobalBytes int64
  56. NeedFiles int
  57. NeedBytes int64
  58. PulledBytes int64 //What arrived of the files being pulled right now
  59. Changed time.Time
  60. }
  61. // DeviceStatus is what Syncthing reports about a device.
  62. type DeviceStatus struct {
  63. Connected bool
  64. LastSeen time.Time
  65. }
  66. // Offer is a folder a device shares with the server.
  67. type Offer struct {
  68. FolderID string
  69. Label string
  70. Time time.Time
  71. }
  72. // UnknownDevice tried to connect without being added by anyone.
  73. type UnknownDevice struct {
  74. ID string
  75. Name string
  76. Address string
  77. Time time.Time
  78. }