Bogdan Caprita | 3c31921 | 2015-08-19 18:12:53 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package discovery |
| 6 | |
| 7 | import ( |
| 8 | "v.io/v23/context" |
| 9 | "v.io/v23/discovery" |
Jungho Ahn | c3cfceb | 2015-09-28 14:56:20 -0700 | [diff] [blame] | 10 | "v.io/v23/security" |
Jungho Ahn | 2411232 | 2015-09-15 10:10:24 -0700 | [diff] [blame] | 11 | "v.io/v23/verror" |
| 12 | ) |
| 13 | |
| 14 | var ( |
Jungho Ahn | c3cfceb | 2015-09-28 14:56:20 -0700 | [diff] [blame] | 15 | 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 Caprita | 3c31921 | 2015-08-19 18:12:53 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | // Advertise implements discovery.Advertiser. |
Jungho Ahn | d587154 | 2015-09-08 18:48:11 -0700 | [diff] [blame] | 22 | // |
| 23 | // TODO(jhahn): Handle ACL. |
Jungho Ahn | c3cfceb | 2015-09-28 14:56:20 -0700 | [diff] [blame] | 24 | func (ds *ds) Advertise(ctx *context.T, service discovery.Service, perms []security.BlessingPattern) error { |
Jungho Ahn | 2411232 | 2015-09-15 10:10:24 -0700 | [diff] [blame] | 25 | 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 Ahn | 438a3d5 | 2015-10-01 15:46:08 -0700 | [diff] [blame] | 31 | if err := validateAttributes(service.Attrs); err != nil { |
| 32 | return err |
Jungho Ahn | c3cfceb | 2015-09-28 14:56:20 -0700 | [diff] [blame] | 33 | } |
Jungho Ahn | 438a3d5 | 2015-10-01 15:46:08 -0700 | [diff] [blame] | 34 | |
Jungho Ahn | d587154 | 2015-09-08 18:48:11 -0700 | [diff] [blame] | 35 | if len(service.InstanceUuid) == 0 { |
| 36 | service.InstanceUuid = NewInstanceUUID() |
| 37 | } |
Jungho Ahn | c3cfceb | 2015-09-28 14:56:20 -0700 | [diff] [blame] | 38 | |
| 39 | ad := Advertisement{ |
Jungho Ahn | d587154 | 2015-09-08 18:48:11 -0700 | [diff] [blame] | 40 | ServiceUuid: NewServiceUUID(service.InterfaceName), |
| 41 | Service: service, |
| 42 | } |
Jungho Ahn | c3cfceb | 2015-09-28 14:56:20 -0700 | [diff] [blame] | 43 | if err := encrypt(&ad, perms); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
Jungho Ahn | d587154 | 2015-09-08 18:48:11 -0700 | [diff] [blame] | 47 | 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 Caprita | 3c31921 | 2015-08-19 18:12:53 -0700 | [diff] [blame] | 55 | return nil |
| 56 | } |