client.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package discover
  16. import (
  17. "fmt"
  18. "net/url"
  19. "time"
  20. "github.com/syncthing/syncthing/internal/protocol"
  21. )
  22. type Factory func(*url.URL, *Announce) (Client, error)
  23. var (
  24. factories = make(map[string]Factory)
  25. DefaultErrorRetryInternval = 60 * time.Second
  26. DefaultGlobalBroadcastInterval = 1800 * time.Second
  27. )
  28. func Register(proto string, factory Factory) {
  29. factories[proto] = factory
  30. }
  31. func New(addr string, pkt *Announce) (Client, error) {
  32. uri, err := url.Parse(addr)
  33. if err != nil {
  34. return nil, err
  35. }
  36. factory, ok := factories[uri.Scheme]
  37. if !ok {
  38. return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
  39. }
  40. client, err := factory(uri, pkt)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return client, nil
  45. }
  46. type Client interface {
  47. Lookup(device protocol.DeviceID) []string
  48. StatusOK() bool
  49. Address() string
  50. Stop()
  51. }