forked from go-gnss/ntrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sourcetable.go
383 lines (316 loc) · 10.2 KB
/
sourcetable.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package ntrip
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
)
// Sourcetable for NTRIP Casters, returned at / as a way for users to discover available mounts
type Sourcetable struct {
Casters []CasterEntry
Networks []NetworkEntry
Mounts []StreamEntry
}
func (st Sourcetable) String() string {
stLength := (len(st.Casters) + len(st.Networks) + len(st.Mounts) + 1)
stStrs := make([]string, 0, stLength)
for _, cas := range st.Casters {
stStrs = append(stStrs, cas.String())
}
for _, net := range st.Networks {
stStrs = append(stStrs, net.String())
}
for _, str := range st.Mounts {
stStrs = append(stStrs, str.String())
}
stStrs = append(stStrs, "ENDSOURCETABLE\r\n")
return strings.Join(stStrs, "\r\n")
}
// CasterEntry for an NTRIP Sourcetable
type CasterEntry struct {
Host string
Port int
Identifier string
Operator string
NMEA bool
Country string
Latitude float32
Longitude float32
FallbackHostAddress string
FallbackHostPort int
Misc string
}
func (c CasterEntry) String() string {
nmea := "0"
if c.NMEA {
nmea = "1"
}
port := strconv.FormatInt(int64(c.Port), 10)
fallbackPort := strconv.FormatInt(int64(c.FallbackHostPort), 10)
lat := strconv.FormatFloat(float64(c.Latitude), 'f', 4, 32)
lng := strconv.FormatFloat(float64(c.Longitude), 'f', 4, 32)
return strings.Join([]string{
"CAS", c.Host, port, c.Identifier, c.Operator, nmea, c.Country, lat, lng,
c.FallbackHostAddress, fallbackPort, c.Misc,
}, ";")
}
// NetworkEntry for an NTRIP Sourcetable
type NetworkEntry struct {
Identifier string
Operator string
// TODO: Authentication type - spec says: B, D, N or a comma separated list of these
Authentication string
Fee bool
NetworkInfoURL string
StreamInfoURL string
// RegistrationAddress is either a URL or Email address
RegistrationAddress string
Misc string
}
func (n NetworkEntry) String() string {
fee := "N"
if n.Fee {
fee = "Y"
}
return strings.Join([]string{"NET",
n.Identifier, n.Operator, n.Authentication, fee, n.NetworkInfoURL, n.StreamInfoURL,
n.RegistrationAddress, n.Misc}, ";")
}
// StreamEntry for an NTRIP Sourcetable
type StreamEntry struct {
Name string
Identifier string
Format string
FormatDetails string
Carrier string
NavSystem string
Network string
CountryCode string
Latitude float32
Longitude float32
NMEA bool
Solution bool
Generator string
Compression string
// TODO: Authentication type
Authentication string
Fee bool
Bitrate int
Misc string
}
// String representation of Mount in NTRIP Sourcetable entry format
func (m StreamEntry) String() string {
nmea := "0"
if m.NMEA {
nmea = "1"
}
solution := "0"
if m.Solution {
solution = "1"
}
fee := "N"
if m.Fee {
fee = "Y"
}
bitrate := strconv.FormatInt(int64(m.Bitrate), 10)
lat := strconv.FormatFloat(float64(m.Latitude), 'f', 4, 32)
lng := strconv.FormatFloat(float64(m.Longitude), 'f', 4, 32)
// Returning joined strings significantly reduced allocs when benchmarking. The old code is
// commented out below for further analysis. There is a benchmark test that can be used
// to compare these results:
// go test ./... -run none -bench=. -benchmem -benchtime 3s
// Make sure your computer is somewhat idle before running benchmarks.
return strings.Join([]string{
"STR", m.Name, m.Identifier, m.Format, m.FormatDetails, m.Carrier, m.NavSystem,
m.Network, m.CountryCode, lat, lng,
nmea, solution, m.Generator, m.Compression, m.Authentication, fee, bitrate, m.Misc,
}, ";")
// return fmt.Sprintf("STR;%s;%s;%s;%s;%s;%s;%s;%s;%.4f;%.4f;%s;%s;%s;%s;%s;%s;%d;%s",
// m.Name, m.Identifier, m.Format, m.FormatDetails, m.Carrier, m.NavSystem, m.Network,
// m.CountryCode, m.Latitude, m.Longitude, nmea, solution, m.Generator, m.Compression,
// m.Authentication, fee, m.Bitrate, m.Misc)
}
// GetSourcetable fetches a source table from a specific caster.
//
// The funciton returns a list of errors which can be treated as warnings.
// These warnings indicate that the caster is returning an improper rtcm3 format.
func GetSourcetable(ctx context.Context, url string) (Sourcetable, []error, error) {
warnings := []error{}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return Sourcetable{}, warnings, errors.Wrap(err, "building request")
}
req.Header.Set("Ntrip-Version", "Ntrip/2.0")
req.Header.Set("User-Agent", "ntrip-mqtt-gateway")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return Sourcetable{}, warnings, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return Sourcetable{}, warnings, err
}
if res.StatusCode != 200 {
return Sourcetable{}, warnings, fmt.Errorf("received a non 200 status code")
}
// Swollowing the errors here is okay because the errors are more like warnings.
// All rows that could be parsed will be present in the source table.
table, warnings := ParseSourcetable(string(body[:]))
return table, warnings, nil
}
// ParseSourcetable parses a sourcetable from an ioreader into a ntrip style source table.
func ParseSourcetable(str string) (Sourcetable, []error) {
table := Sourcetable{}
var allErrors []error
lines := strings.Split(str, "\n")
for lineNo, rawLine := range lines {
line := strings.TrimSpace(rawLine)
if line == "" {
continue
}
if line == "ENDSOURCETABLE" {
break
}
switch line[:3] {
case "CAS":
caster, errs := ParseCasterEntry(line)
if len(errs) != 0 {
for _, err := range errs {
allErrors = append(allErrors, errors.Wrapf(err, "parsing line %v", lineNo))
}
}
table.Casters = append(table.Casters, caster)
case "NET":
net, errs := ParseNetworkEntry(line)
if len(errs) != 0 {
for _, err := range errs {
allErrors = append(allErrors, errors.Wrapf(err, "parsing line %v", lineNo))
}
}
table.Networks = append(table.Networks, net)
case "STR":
mount, errs := ParseStreamEntry(line)
if len(errs) != 0 {
for _, err := range errs {
allErrors = append(allErrors, errors.Wrapf(err, "parsing line %v", lineNo))
}
}
table.Mounts = append(table.Mounts, mount)
}
}
return table, allErrors
}
// ParseCasterEntry parses a single caster from a string.
func ParseCasterEntry(casterString string) (CasterEntry, []error) {
parts := strings.Split(casterString, ";")
p := &parser{parts, []error{}}
return CasterEntry{
Host: p.parseString(1, "host"),
Port: p.parseInt(2, "port"),
Identifier: p.parseString(3, "identifier"),
Operator: p.parseString(4, "operator"),
NMEA: p.parseBool(5, "0", "nmea"),
Country: p.parseString(6, "country"),
Latitude: p.parseFloat32(7, "latitude"),
Longitude: p.parseFloat32(8, "longitude"),
FallbackHostAddress: p.parseString(9, "fallback host address"),
FallbackHostPort: p.parseInt(10, "fallback host port"),
Misc: p.parseString(11, "misc"),
}, p.errors
}
// ParseNetworkEntry parses a single network entry from a string.
func ParseNetworkEntry(netString string) (NetworkEntry, []error) {
parts := strings.Split(netString, ";")
p := &parser{parts, []error{}}
return NetworkEntry{
Identifier: p.parseString(1, "identifier"),
Operator: p.parseString(2, "operator"),
Authentication: p.parseString(3, "authentication"),
Fee: p.parseBool(4, "N", "fee"),
NetworkInfoURL: p.parseString(5, "network info url"),
StreamInfoURL: p.parseString(6, "stream info url"),
RegistrationAddress: p.parseString(7, "registration address"),
Misc: p.parseString(8, "misc"),
}, p.errors
}
// ParseStreamEntry parses a single mount entry.
func ParseStreamEntry(streamString string) (StreamEntry, []error) {
parts := strings.Split(streamString, ";")
p := &parser{parts, []error{}}
streamEntry := StreamEntry{
Name: p.parseString(1, "name"),
Identifier: p.parseString(2, "identifier"),
Format: p.parseString(3, "format"),
FormatDetails: p.parseString(4, "format details"),
Carrier: p.parseString(5, "carrier"),
NavSystem: p.parseString(6, "nav system"),
Network: p.parseString(7, "network"),
CountryCode: p.parseString(8, "country code"),
Latitude: p.parseFloat32(9, "latitude"),
Longitude: p.parseFloat32(10, "logitude"),
NMEA: p.parseBool(11, "0", "nmea"),
Solution: p.parseBool(12, "0", "solution"),
Generator: p.parseString(13, "generator"),
Compression: p.parseString(14, "compression"),
// TODO: Authentication type
Authentication: p.parseString(15, "authentication"),
Fee: p.parseBool(16, "N", "fee"),
Bitrate: p.parseInt(17, "bitrate"),
Misc: p.parseString(18, "misc"),
}
return streamEntry, p.errs()
}
type parser struct {
parts []string
errors []error
}
func (p *parser) parseString(index int, field string) string {
if len(p.parts) <= index {
p.errors = append(p.errors, fmt.Errorf("parsing %s", field))
return ""
}
return p.parts[index]
}
func (p *parser) parseFloat32(index int, field string) float32 {
if len(p.parts) <= index {
p.errors = append(p.errors, fmt.Errorf("parsing %s", field))
return 0
}
floatField, err := strconv.ParseFloat(p.parts[index], 64)
if err != nil {
p.errors = append(p.errors, fmt.Errorf("converting %s to a float32", field))
return 0
}
return float32(floatField)
}
func (p *parser) parseInt(index int, field string) int {
if len(p.parts) <= index {
p.errors = append(p.errors, fmt.Errorf("parsing %s", field))
return 0
}
floatField, err := strconv.ParseInt(p.parts[index], 10, 64)
if err != nil {
p.errors = append(p.errors, fmt.Errorf("converting %s to an int", field))
return 0
}
return int(floatField)
}
func (p *parser) parseBool(index int, falseValue string, field string) bool {
if len(p.parts) <= index {
p.errors = append(p.errors, fmt.Errorf("parsing %s", field))
return false
}
val := true
if p.parts[index] == falseValue {
val = false
}
return val
}
func (p *parser) errs() []error {
return p.errors
}