forked from DNS-OARC/ripeatlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.go
161 lines (143 loc) · 4.75 KB
/
stream.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
// Author Jerry Lundström <[email protected]>
// Copyright (c) 2017, OARC, Inc.
// All rights reserved.
//
// This file is part of ripeatlas.
//
// ripeatlas is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ripeatlas is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with ripeatlas. If not, see <http://www.gnu.org/licenses/>.
package ripeatlas
import (
"fmt"
"github.com/DNS-OARC/ripeatlas/measurement"
"github.com/graarh/golang-socketio"
"github.com/graarh/golang-socketio/transport"
)
// A Stream reads RIPE Atlas data from the streaming API
// (https://atlas.ripe.net/docs/result-streaming/).
type Stream struct {
}
const (
StreamUrl = "wss://atlas-stream.ripe.net:443/stream/socket.io/?EIO=3&transport=websocket"
)
// NewHttp returns a new Atlaser for reading from the RIPE Atlas streaming API.
func NewStream() *Stream {
return &Stream{}
}
// MeasurementLatest streams the latest measurement results, as described
// by the Params, and sends them to the returned channel.
//
// Params available are:
//
// "msm": int - The measurement id to get results from.
//
// "type": string - The measurement result type to stream.
//
// "sourceAddress": none - Unimplemented
//
// "sourcePrefix": none - Unimplemented
//
// "destinationAddress": none - Unimplemented
//
// "destinationPrefix": none - Unimplemented
//
// "passThroughHost": none - Unimplemented
//
// "passThroughPrefix": none - Unimplemented
//
// "sendBacklog": none - Unimplemented
//
// "buffering": none - Unimplemented
func (h *Stream) MeasurementLatest(p Params) (<-chan *measurement.Result, error) {
subscribe := make(map[string]interface{})
subscribe["stream_type"] = "result"
for k, v := range p {
switch k {
case "msm":
v, ok := v.(int)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be int", k)
}
subscribe["msm"] = v
case "type":
v, ok := v.(string)
if !ok {
return nil, fmt.Errorf("Invalid %s parameter, must be string", k)
}
subscribe["type"] = v
case "sourceAddress":
fallthrough
case "sourcePrefix":
fallthrough
case "destinationAddress":
fallthrough
case "destinationPrefix":
fallthrough
case "passThroughHost":
fallthrough
case "passThroughPrefix":
fallthrough
case "sendBacklog":
fallthrough
case "buffering":
return nil, fmt.Errorf("Unimplemented parameter %s", k)
default:
return nil, fmt.Errorf("Invalid parameter %s", k)
}
}
ch := make(chan *measurement.Result)
c, err := gosocketio.Dial(StreamUrl, transport.GetDefaultWebsocketTransport())
if err != nil {
return nil, fmt.Errorf("gosocketio.Dial(%s): %s", StreamUrl, err.Error())
}
err = c.On("atlas_error", func(h *gosocketio.Channel, args interface{}) {
r := &measurement.Result{ParseError: fmt.Errorf("atlas_error: %v", args)}
ch <- r
c.Close()
close(ch)
})
if err != nil {
return nil, fmt.Errorf("c.On(atlas_error): %s", err.Error())
}
err = c.On("atlas_result", func(h *gosocketio.Channel, r measurement.Result) {
ch <- &r
})
if err != nil {
return nil, fmt.Errorf("c.On(atlas_result): %s", err.Error())
}
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
c.Close()
close(ch)
})
if err != nil {
return nil, fmt.Errorf("c.On(disconnect): %s", err.Error())
}
err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
err := h.Emit("atlas_subscribe", subscribe)
if err != nil {
r := &measurement.Result{ParseError: fmt.Errorf("h.Emit(atlas_subscribe): %s", err.Error())}
ch <- r
c.Close()
close(ch)
}
})
if err != nil {
return nil, fmt.Errorf("c.On(connect): %s", err.Error())
}
return ch, nil
}
// Since Stream streams the latest results (more or less, backlog sending
// is available), MeasurementResults will just call MeasurementLatest.
func (h *Stream) MeasurementResults(p Params) (<-chan *measurement.Result, error) {
return h.MeasurementResults(p)
}