client.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 http://mozilla.org/MPL/2.0/.
  6. package discover
  7. import (
  8. "fmt"
  9. "net/url"
  10. "time"
  11. "github.com/syncthing/protocol"
  12. )
  13. type Announcer interface {
  14. Announcement() Announce
  15. }
  16. type Factory func(*url.URL, Announcer) (Client, error)
  17. var (
  18. factories = make(map[string]Factory)
  19. DefaultErrorRetryInternval = 60 * time.Second
  20. DefaultGlobalBroadcastInterval = 1800 * time.Second
  21. )
  22. func Register(proto string, factory Factory) {
  23. factories[proto] = factory
  24. }
  25. func New(addr string, announcer Announcer) (Client, error) {
  26. uri, err := url.Parse(addr)
  27. if err != nil {
  28. return nil, err
  29. }
  30. factory, ok := factories[uri.Scheme]
  31. if !ok {
  32. return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
  33. }
  34. client, err := factory(uri, announcer)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return client, nil
  39. }
  40. type Client interface {
  41. Lookup(device protocol.DeviceID) (Announce, error)
  42. StatusOK() bool
  43. Address() string
  44. Stop()
  45. }