client.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 Factory func(*url.URL, *Announce) (Client, error)
  14. var (
  15. factories = make(map[string]Factory)
  16. DefaultErrorRetryInternval = 60 * time.Second
  17. DefaultGlobalBroadcastInterval = 1800 * time.Second
  18. )
  19. func Register(proto string, factory Factory) {
  20. factories[proto] = factory
  21. }
  22. func New(addr string, pkt *Announce) (Client, error) {
  23. uri, err := url.Parse(addr)
  24. if err != nil {
  25. return nil, err
  26. }
  27. factory, ok := factories[uri.Scheme]
  28. if !ok {
  29. return nil, fmt.Errorf("Unsupported scheme: %s", uri.Scheme)
  30. }
  31. client, err := factory(uri, pkt)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return client, nil
  36. }
  37. type Client interface {
  38. Lookup(device protocol.DeviceID) []string
  39. StatusOK() bool
  40. Address() string
  41. Stop()
  42. }