Svc -> Service

This commit is contained in:
Audrius Butkevicius
2015-12-23 15:31:12 +00:00
parent d98290c17f
commit 1827dda0c6
13 changed files with 222 additions and 231 deletions

View File

@@ -42,9 +42,9 @@ type Model interface {
IsPaused(remoteID protocol.DeviceID) bool
}
// The connection service listens on TLS and dials configured unconnected
// The connection connectionService listens on TLS and dials configured unconnected
// devices. Successful connections are handed to the model.
type connectionSvc struct {
type connectionService struct {
*suture.Supervisor
cfg *config.Wrapper
myID protocol.DeviceID
@@ -52,7 +52,7 @@ type connectionSvc struct {
tlsCfg *tls.Config
discoverer discover.Finder
conns chan model.IntermediateConnection
relaySvc *relay.Svc
relayService *relay.Service
bepProtocolName string
tlsDefaultCommonName string
lans []*net.IPNet
@@ -66,16 +66,16 @@ type connectionSvc struct {
relaysEnabled bool
}
func NewConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, relaySvc *relay.Svc,
func NewConnectionService(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tlsCfg *tls.Config, discoverer discover.Finder, relayService *relay.Service,
bepProtocolName string, tlsDefaultCommonName string, lans []*net.IPNet) suture.Service {
svc := &connectionSvc{
Supervisor: suture.NewSimple("connectionSvc"),
service := &connectionService{
Supervisor: suture.NewSimple("connectionService"),
cfg: cfg,
myID: myID,
model: mdl,
tlsCfg: tlsCfg,
discoverer: discoverer,
relaySvc: relaySvc,
relayService: relayService,
conns: make(chan model.IntermediateConnection),
bepProtocolName: bepProtocolName,
tlsDefaultCommonName: tlsDefaultCommonName,
@@ -85,20 +85,20 @@ func NewConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tl
relaysEnabled: cfg.Options().RelaysEnabled,
lastRelayCheck: make(map[protocol.DeviceID]time.Time),
}
cfg.Subscribe(svc)
cfg.Subscribe(service)
if svc.cfg.Options().MaxSendKbps > 0 {
svc.writeRateLimit = ratelimit.NewBucketWithRate(float64(1000*svc.cfg.Options().MaxSendKbps), int64(5*1000*svc.cfg.Options().MaxSendKbps))
if service.cfg.Options().MaxSendKbps > 0 {
service.writeRateLimit = ratelimit.NewBucketWithRate(float64(1000*service.cfg.Options().MaxSendKbps), int64(5*1000*service.cfg.Options().MaxSendKbps))
}
if svc.cfg.Options().MaxRecvKbps > 0 {
svc.readRateLimit = ratelimit.NewBucketWithRate(float64(1000*svc.cfg.Options().MaxRecvKbps), int64(5*1000*svc.cfg.Options().MaxRecvKbps))
if service.cfg.Options().MaxRecvKbps > 0 {
service.readRateLimit = ratelimit.NewBucketWithRate(float64(1000*service.cfg.Options().MaxRecvKbps), int64(5*1000*service.cfg.Options().MaxRecvKbps))
}
// There are several moving parts here; one routine per listening address
// to handle incoming connections, one routine to periodically attempt
// outgoing connections, one routine to the the common handling
// regardless of whether the connection was incoming or outgoing.
// Furthermore, a relay service which handles incoming requests to connect
// Furthermore, a relay connectionService which handles incoming requests to connect
// via the relays.
//
// TODO: Clean shutdown, and/or handling config changes on the fly. We
@@ -106,8 +106,8 @@ func NewConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tl
// not new listen addresses and we don't support disconnecting devices
// that are removed and so on...
svc.Add(serviceFunc(svc.connect))
for _, addr := range svc.cfg.Options().ListenAddress {
service.Add(serviceFunc(service.connect))
for _, addr := range service.cfg.Options().ListenAddress {
uri, err := url.Parse(addr)
if err != nil {
l.Infoln("Failed to parse listen address:", addr, err)
@@ -122,20 +122,20 @@ func NewConnectionSvc(cfg *config.Wrapper, myID protocol.DeviceID, mdl Model, tl
l.Debugln("listening on", uri)
svc.Add(serviceFunc(func() {
listener(uri, svc.tlsCfg, svc.conns)
service.Add(serviceFunc(func() {
listener(uri, service.tlsCfg, service.conns)
}))
}
svc.Add(serviceFunc(svc.handle))
service.Add(serviceFunc(service.handle))
if svc.relaySvc != nil {
svc.Add(serviceFunc(svc.acceptRelayConns))
if service.relayService != nil {
service.Add(serviceFunc(service.acceptRelayConns))
}
return svc
return service
}
func (s *connectionSvc) handle() {
func (s *connectionService) handle() {
next:
for c := range s.conns {
cs := c.Conn.ConnectionState()
@@ -257,7 +257,7 @@ next:
}
}
func (s *connectionSvc) connect() {
func (s *connectionService) connect() {
delay := time.Second
for {
l.Debugln("Reconnect loop")
@@ -340,7 +340,7 @@ func (s *connectionSvc) connect() {
}
}
func (s *connectionSvc) resolveAddresses(deviceID protocol.DeviceID, inAddrs []string) (addrs []string, relays []discover.Relay) {
func (s *connectionService) resolveAddresses(deviceID protocol.DeviceID, inAddrs []string) (addrs []string, relays []discover.Relay) {
for _, addr := range inAddrs {
if addr == "dynamic" {
if s.discoverer != nil {
@@ -356,7 +356,7 @@ func (s *connectionSvc) resolveAddresses(deviceID protocol.DeviceID, inAddrs []s
return
}
func (s *connectionSvc) connectDirect(deviceID protocol.DeviceID, addr string) *tls.Conn {
func (s *connectionService) connectDirect(deviceID protocol.DeviceID, addr string) *tls.Conn {
uri, err := url.Parse(addr)
if err != nil {
l.Infoln("Failed to parse connection url:", addr, err)
@@ -379,7 +379,7 @@ func (s *connectionSvc) connectDirect(deviceID protocol.DeviceID, addr string) *
return conn
}
func (s *connectionSvc) connectViaRelay(deviceID protocol.DeviceID, addr discover.Relay) *tls.Conn {
func (s *connectionService) connectViaRelay(deviceID protocol.DeviceID, addr discover.Relay) *tls.Conn {
uri, err := url.Parse(addr.URL)
if err != nil {
l.Infoln("Failed to parse relay connection url:", addr, err)
@@ -418,9 +418,9 @@ func (s *connectionSvc) connectViaRelay(deviceID protocol.DeviceID, addr discove
return tc
}
func (s *connectionSvc) acceptRelayConns() {
func (s *connectionService) acceptRelayConns() {
for {
conn := s.relaySvc.Accept()
conn := s.relayService.Accept()
s.conns <- model.IntermediateConnection{
Conn: conn,
Type: model.ConnectionTypeRelayAccept,
@@ -428,7 +428,7 @@ func (s *connectionSvc) acceptRelayConns() {
}
}
func (s *connectionSvc) shouldLimit(addr net.Addr) bool {
func (s *connectionService) shouldLimit(addr net.Addr) bool {
if s.cfg.Options().LimitBandwidthInLan {
return true
}
@@ -445,11 +445,11 @@ func (s *connectionSvc) shouldLimit(addr net.Addr) bool {
return !tcpaddr.IP.IsLoopback()
}
func (s *connectionSvc) VerifyConfiguration(from, to config.Configuration) error {
func (s *connectionService) VerifyConfiguration(from, to config.Configuration) error {
return nil
}
func (s *connectionSvc) CommitConfiguration(from, to config.Configuration) bool {
func (s *connectionService) CommitConfiguration(from, to config.Configuration) bool {
s.mut.Lock()
s.relaysEnabled = to.Options.RelaysEnabled
s.mut.Unlock()

View File

@@ -65,8 +65,8 @@ func (m *CachingMux) Add(finder Finder, cacheTime, negCacheTime time.Duration, p
m.caches = append(m.caches, newCache())
m.mut.Unlock()
if svc, ok := finder.(suture.Service); ok {
m.Supervisor.Add(svc)
if service, ok := finder.(suture.Service); ok {
m.Supervisor.Add(service)
}
}

View File

@@ -26,7 +26,7 @@ const (
eventBroadcasterCheckInterval = 10 * time.Second
)
type Svc struct {
type Service struct {
*suture.Supervisor
cfg *config.Wrapper
tlsCfg *tls.Config
@@ -38,11 +38,11 @@ type Svc struct {
conns chan *tls.Conn
}
func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config) *Svc {
func NewService(cfg *config.Wrapper, tlsCfg *tls.Config) *Service {
conns := make(chan *tls.Conn)
svc := &Svc{
Supervisor: suture.New("Svc", suture.Spec{
service := &Service{
Supervisor: suture.New("Service", suture.Spec{
Log: func(log string) {
l.Debugln(log)
},
@@ -61,28 +61,28 @@ func NewSvc(cfg *config.Wrapper, tlsCfg *tls.Config) *Svc {
}
rcfg := cfg.Raw()
svc.CommitConfiguration(rcfg, rcfg)
cfg.Subscribe(svc)
service.CommitConfiguration(rcfg, rcfg)
cfg.Subscribe(service)
receiver := &invitationReceiver{
tlsCfg: tlsCfg,
conns: conns,
invitations: svc.invitations,
invitations: service.invitations,
stop: make(chan struct{}),
}
eventBc := &eventBroadcaster{
svc: svc,
stop: make(chan struct{}),
Service: service,
stop: make(chan struct{}),
}
svc.Add(receiver)
svc.Add(eventBc)
service.Add(receiver)
service.Add(eventBc)
return svc
return service
}
func (s *Svc) VerifyConfiguration(from, to config.Configuration) error {
func (s *Service) VerifyConfiguration(from, to config.Configuration) error {
for _, addr := range to.Options.RelayServers {
_, err := url.Parse(addr)
if err != nil {
@@ -92,7 +92,7 @@ func (s *Svc) VerifyConfiguration(from, to config.Configuration) error {
return nil
}
func (s *Svc) CommitConfiguration(from, to config.Configuration) bool {
func (s *Service) CommitConfiguration(from, to config.Configuration) bool {
existing := make(map[string]*url.URL, len(to.Options.RelayServers))
for _, addr := range to.Options.RelayServers {
@@ -142,7 +142,7 @@ type Status struct {
}
// Relays return the list of relays that currently have an OK status.
func (s *Svc) Relays() []string {
func (s *Service) Relays() []string {
if s == nil {
// A nil client does not have a status, really. Yet we may be called
// this way, for raisins...
@@ -162,7 +162,7 @@ func (s *Svc) Relays() []string {
}
// RelayStatus returns the latency and OK status for a given relay.
func (s *Svc) RelayStatus(uri string) (time.Duration, bool) {
func (s *Service) RelayStatus(uri string) (time.Duration, bool) {
if s == nil {
// A nil client does not have a status, really. Yet we may be called
// this way, for raisins...
@@ -182,7 +182,7 @@ func (s *Svc) RelayStatus(uri string) (time.Duration, bool) {
}
// Accept returns a new *tls.Conn. The connection is already handshaken.
func (s *Svc) Accept() *tls.Conn {
func (s *Service) Accept() *tls.Conn {
return <-s.conns
}
@@ -234,8 +234,8 @@ func (r *invitationReceiver) Stop() {
// no way to get the event feed directly from the relay lib. This may be
// something to revisit later, possibly.
type eventBroadcaster struct {
svc *Svc
stop chan struct{}
Service *Service
stop chan struct{}
}
func (e *eventBroadcaster) Serve() {
@@ -247,7 +247,7 @@ func (e *eventBroadcaster) Serve() {
for {
select {
case <-timer.C:
curOKRelays := e.svc.Relays()
curOKRelays := e.Service.Relays()
changed := len(curOKRelays) != len(prevOKRelays)
if !changed {

View File

@@ -58,13 +58,9 @@ func (n *IGD) URL() *url.URL {
// An IGDService is a specific service provided by an IGD.
type IGDService struct {
serviceID string
serviceURL string
serviceURN string
}
func (s *IGDService) ID() string {
return s.serviceID
ID string
URL string
URN string
}
type Protocol string
@@ -75,9 +71,9 @@ const (
)
type upnpService struct {
ServiceID string `xml:"serviceId"`
ServiceType string `xml:"serviceType"`
ControlURL string `xml:"controlURL"`
ID string `xml:"serviceId"`
Type string `xml:"serviceType"`
ControlURL string `xml:"controlURL"`
}
type upnpDevice struct {
@@ -132,8 +128,8 @@ nextResult:
if existingResult.uuid == result.uuid {
if shouldDebug() {
l.Debugf("Skipping duplicate result %s with services:", result.uuid)
for _, svc := range result.services {
l.Debugf("* [%s] %s", svc.serviceID, svc.serviceURL)
for _, service := range result.services {
l.Debugf("* [%s] %s", service.ID, service.URL)
}
}
continue nextResult
@@ -143,8 +139,8 @@ nextResult:
results = append(results, result)
if shouldDebug() {
l.Debugf("UPnP discovery result %s with services:", result.uuid)
for _, svc := range result.services {
l.Debugf("* [%s] %s", svc.serviceID, svc.serviceURL)
for _, service := range result.services {
l.Debugf("* [%s] %s", service.ID, service.URL)
}
}
}
@@ -317,9 +313,9 @@ func getChildDevices(d upnpDevice, deviceType string) []upnpDevice {
func getChildServices(d upnpDevice, serviceType string) []upnpService {
var result []upnpService
for _, svc := range d.Services {
if svc.ServiceType == serviceType {
result = append(result, svc)
for _, service := range d.Services {
if service.Type == serviceType {
result = append(result, service)
}
}
return result
@@ -352,7 +348,7 @@ func getServiceDescriptions(rootURL string, device upnpDevice) ([]IGDService, er
return result, nil
}
func getIGDServices(rootURL string, device upnpDevice, wanDeviceURN string, wanConnectionURN string, serviceURNs []string) []IGDService {
func getIGDServices(rootURL string, device upnpDevice, wanDeviceURN string, wanConnectionURN string, URNs []string) []IGDService {
var result []IGDService
devices := getChildDevices(device, wanDeviceURN)
@@ -370,21 +366,21 @@ func getIGDServices(rootURL string, device upnpDevice, wanDeviceURN string, wanC
}
for _, connection := range connections {
for _, serviceURN := range serviceURNs {
services := getChildServices(connection, serviceURN)
for _, URN := range URNs {
services := getChildServices(connection, URN)
l.Debugln(rootURL, "- no services of type", serviceURN, " found on connection.")
l.Debugln(rootURL, "- no services of type", URN, " found on connection.")
for _, service := range services {
if len(service.ControlURL) == 0 {
l.Infoln(rootURL+"- malformed", service.ServiceType, "description: no control URL.")
l.Infoln(rootURL+"- malformed", service.Type, "description: no control URL.")
} else {
u, _ := url.Parse(rootURL)
replaceRawPath(u, service.ControlURL)
l.Debugln(rootURL, "- found", service.ServiceType, "with URL", u)
l.Debugln(rootURL, "- found", service.Type, "with URL", u)
service := IGDService{serviceID: service.ServiceID, serviceURL: u.String(), serviceURN: service.ServiceType}
service := IGDService{ID: service.ID, URL: u.String(), URN: service.Type}
result = append(result, service)
}
@@ -525,9 +521,9 @@ func (s *IGDService) AddPortMapping(localIPAddress string, protocol Protocol, ex
<NewPortMappingDescription>%s</NewPortMappingDescription>
<NewLeaseDuration>%d</NewLeaseDuration>
</u:AddPortMapping>`
body := fmt.Sprintf(tpl, s.serviceURN, externalPort, protocol, internalPort, localIPAddress, description, timeout)
body := fmt.Sprintf(tpl, s.URN, externalPort, protocol, internalPort, localIPAddress, description, timeout)
response, err := soapRequest(s.serviceURL, s.serviceURN, "AddPortMapping", body)
response, err := soapRequest(s.URL, s.URN, "AddPortMapping", body)
if err != nil && timeout > 0 {
// Try to repair error code 725 - OnlyPermanentLeasesSupported
envelope := &soapErrorResponse{}
@@ -549,9 +545,9 @@ func (s *IGDService) DeletePortMapping(protocol Protocol, externalPort int) erro
<NewExternalPort>%d</NewExternalPort>
<NewProtocol>%s</NewProtocol>
</u:DeletePortMapping>`
body := fmt.Sprintf(tpl, s.serviceURN, externalPort, protocol)
body := fmt.Sprintf(tpl, s.URN, externalPort, protocol)
_, err := soapRequest(s.serviceURL, s.serviceURN, "DeletePortMapping", body)
_, err := soapRequest(s.URL, s.URN, "DeletePortMapping", body)
if err != nil {
return err
@@ -566,9 +562,9 @@ func (s *IGDService) DeletePortMapping(protocol Protocol, externalPort int) erro
func (s *IGDService) GetExternalIPAddress() (net.IP, error) {
tpl := `<u:GetExternalIPAddress xmlns:u="%s" />`
body := fmt.Sprintf(tpl, s.serviceURN)
body := fmt.Sprintf(tpl, s.URN)
response, err := soapRequest(s.serviceURL, s.serviceURN, "GetExternalIPAddress", body)
response, err := soapRequest(s.URL, s.URN, "GetExternalIPAddress", body)
if err != nil {
return nil, err