2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
26
"github.com/juju/govmomi/object"
27
"github.com/juju/govmomi/vim25"
28
"github.com/juju/govmomi/vim25/progress"
29
"github.com/juju/govmomi/vim25/types"
30
"golang.org/x/net/context"
33
type ovfFileItem struct {
35
item types.OvfFileItem
36
ch chan progress.Report
39
func (o ovfFileItem) Sink() chan<- progress.Report {
43
type leaseUpdater struct {
45
lease *object.HttpNfcLease
47
pos int64 // Number of bytes
48
total int64 // Total number of bytes
50
done chan struct{} // When lease updater should stop
52
wg sync.WaitGroup // Track when update loop is done
55
func newLeaseUpdater(client *vim25.Client, lease *object.HttpNfcLease, items []ovfFileItem) *leaseUpdater {
60
done: make(chan struct{}),
63
for _, item := range items {
64
l.total += item.item.Size
65
go l.waitForProgress(item)
68
// Kickstart update loop
75
func (l *leaseUpdater) waitForProgress(item ovfFileItem) {
78
total = item.item.Size
84
case p, ok := <-item.ch:
85
// Return in case of error
86
if ok && p.Error() != nil {
91
// Last element on the channel, add to total
92
atomic.AddInt64(&l.pos, total-pos)
96
// Approximate progress in number of bytes
97
x := int64(float32(total) * (p.Percentage() / 100.0))
98
atomic.AddInt64(&l.pos, x-pos)
104
func (l *leaseUpdater) run() {
107
tick := time.NewTicker(2 * time.Second)
115
// From the vim api HttpNfcLeaseProgress(percent) doc, percent ==
116
// "Completion status represented as an integer in the 0-100 range."
117
// Always report the current value of percent, as it will renew the
118
// lease even if the value hasn't changed or is 0.
119
percent := int(float32(100*atomic.LoadInt64(&l.pos)) / float32(l.total))
120
err := l.lease.HttpNfcLeaseProgress(context.TODO(), percent)
122
fmt.Printf("from lease updater: %s\n", err)
128
func (l *leaseUpdater) Done() {