summaryrefslogtreecommitdiff
path: root/mc/mc.go
blob: 710e20310f688db2e770a018ece4316e4e3a9667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package mc

import (
	"context"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
)

var chunkSize int = 5000

type MeasurableConnection interface {
	Start(context.Context, bool) bool
	Transferred() uint64
	Client() *http.Client
}

type LoadBearingDownload struct {
	Path       string
	downloaded uint64
	client     *http.Client
}

func (lbd *LoadBearingDownload) Transferred() uint64 {
	return lbd.downloaded
}

func (lbd *LoadBearingDownload) Client() *http.Client {
	return lbd.client
}

func (lbd *LoadBearingDownload) Start(ctx context.Context, debug bool) bool {
	lbd.downloaded = 0
	lbd.client = &http.Client{}

	// At some point this might be useful: It is a snippet of code that will enable
	// logging of per-session TLS key material in order to make debugging easier in
	// Wireshark.
	/*
		lbd.client = &http.Client{
			Transport: &http2.Transport{
				TLSClientConfig: &tls.Config{
					KeyLogWriter: w,

					Rand:               utilities.RandZeroSource{}, // for reproducible output; don't do this.
					InsecureSkipVerify: true,                       // test server certificate is not trusted.
				},
			},
		}
	*/

	if debug {
		fmt.Printf("Started a load bearing download.\n")
	}
	go doDownload(ctx, lbd.client, lbd.Path, &lbd.downloaded, debug)
	return true
}

func doDownload(ctx context.Context, client *http.Client, path string, count *uint64, debug bool) {
	get, err := client.Get(path)
	if err != nil {
		return
	}
	for ctx.Err() == nil {
		n, err := io.CopyN(ioutil.Discard, get.Body, int64(chunkSize))
		if err != nil {
			break
		}
		*count += uint64(n)
	}
	get.Body.Close()
	if debug {
		fmt.Printf("Ending a load-bearing download.\n")
	}
}

type LoadBearingUpload struct {
	Path     string
	uploaded uint64
	client   *http.Client
}

func (lbu *LoadBearingUpload) Transferred() uint64 {
	return lbu.uploaded
}

func (lbd *LoadBearingUpload) Client() *http.Client {
	return lbd.client
}

type syntheticCountingReader struct {
	n   *uint64
	ctx context.Context
}

func (s *syntheticCountingReader) Read(p []byte) (n int, err error) {
	if s.ctx.Err() != nil {
		return 0, io.EOF
	}
	err = nil
	n = len(p)
	n = chunkSize
	*s.n += uint64(n)
	return
}

func doUpload(ctx context.Context, client *http.Client, path string, count *uint64, debug bool) bool {
	*count = 0
	s := &syntheticCountingReader{n: count, ctx: ctx}
	resp, _ := client.Post(path, "application/octet-stream", s)
	resp.Body.Close()
	if debug {
		fmt.Printf("Ending a load-bearing upload.\n")
	}
	return true
}

func (lbu *LoadBearingUpload) Start(ctx context.Context, debug bool) bool {
	lbu.uploaded = 0
	lbu.client = &http.Client{}
	fmt.Printf("Started a load bearing upload.\n")
	go doUpload(ctx, lbu.client, lbu.Path, &lbu.uploaded, debug)
	return true
}