blob: 5abdbe4709d5268b597dcab0e80fbbe47809a9d6 [file] [log] [blame]
Bogdan Caprita3c319212015-08-19 18:12:53 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package discovery
6
7import (
8 "v.io/v23/context"
9 "v.io/v23/discovery"
Jungho Ahnc3cfceb2015-09-28 14:56:20 -070010 "v.io/v23/security"
Jungho Ahn24112322015-09-15 10:10:24 -070011 "v.io/v23/verror"
12)
13
14var (
Jungho Ahnc3cfceb2015-09-28 14:56:20 -070015 errNoInterfaceName = verror.Register(pkgPath+".errNoInterfaceName", verror.NoRetry, "{1:}{2:} interface name not provided")
16 errNotPackableAttributes = verror.Register(pkgPath+".errNotPackableAttributes", verror.NoRetry, "{1:}{2:} attribute not packable")
17 errNoAddresses = verror.Register(pkgPath+".errNoAddress", verror.NoRetry, "{1:}{2:} address not provided")
18 errNotPackableAddresses = verror.Register(pkgPath+".errNotPackableAddresses", verror.NoRetry, "{1:}{2:} address not packable")
Bogdan Caprita3c319212015-08-19 18:12:53 -070019)
20
21// Advertise implements discovery.Advertiser.
Jungho Ahnd5871542015-09-08 18:48:11 -070022//
23// TODO(jhahn): Handle ACL.
Jungho Ahnc3cfceb2015-09-28 14:56:20 -070024func (ds *ds) Advertise(ctx *context.T, service discovery.Service, perms []security.BlessingPattern) error {
Jungho Ahn24112322015-09-15 10:10:24 -070025 if len(service.InterfaceName) == 0 {
26 return verror.New(errNoInterfaceName, ctx)
27 }
28 if len(service.Addrs) == 0 {
29 return verror.New(errNoAddresses, ctx)
30 }
Jungho Ahn438a3d52015-10-01 15:46:08 -070031 if err := validateAttributes(service.Attrs); err != nil {
32 return err
Jungho Ahnc3cfceb2015-09-28 14:56:20 -070033 }
Jungho Ahn438a3d52015-10-01 15:46:08 -070034
Jungho Ahnd5871542015-09-08 18:48:11 -070035 if len(service.InstanceUuid) == 0 {
36 service.InstanceUuid = NewInstanceUUID()
37 }
Jungho Ahnc3cfceb2015-09-28 14:56:20 -070038
39 ad := Advertisement{
Jungho Ahnd5871542015-09-08 18:48:11 -070040 ServiceUuid: NewServiceUUID(service.InterfaceName),
41 Service: service,
42 }
Jungho Ahnc3cfceb2015-09-28 14:56:20 -070043 if err := encrypt(&ad, perms); err != nil {
44 return err
45 }
46
Jungho Ahnd5871542015-09-08 18:48:11 -070047 ctx, cancel := context.WithCancel(ctx)
48 for _, plugin := range ds.plugins {
49 err := plugin.Advertise(ctx, ad)
50 if err != nil {
51 cancel()
52 return err
53 }
54 }
Bogdan Caprita3c319212015-08-19 18:12:53 -070055 return nil
56}