-
Notifications
You must be signed in to change notification settings - Fork 20
/
recordstream.h
288 lines (243 loc) · 10.1 KB
/
recordstream.h
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
/***************************************************************************
* Copyright (C) gempa GmbH *
* All rights reserved. *
* Contact: gempa GmbH (seiscomp-dev@gempa.de) *
* *
* GNU Affero General Public License Usage *
* This file may be used under the terms of the GNU Affero *
* Public License version 3.0 as published by the Free Software Foundation *
* and appearing in the file LICENSE included in the packaging of this *
* file. Please review the following information to ensure the GNU Affero *
* Public License version 3.0 requirements will be met: *
* https://www.gnu.org/licenses/agpl-3.0.html. *
* *
* Other Usage *
* Alternatively, this file may be used in accordance with the terms and *
* conditions contained in a signed written agreement between you and *
* gempa GmbH. *
***************************************************************************/
#ifndef SEISCOMP_IO_RECORDSTREAM_H
#define SEISCOMP_IO_RECORDSTREAM_H
#include <seiscomp/core/interruptible.h>
#include <seiscomp/core/record.h>
#include <seiscomp/io/recordstreamexceptions.h>
namespace Seiscomp {
namespace IO {
DEFINE_SMARTPOINTER(RecordStream);
/**
* @brief The RecordStream class defines an abstract interface to read data
* records from arbitrary sources.
*
* \code{.cpp}
* RecordStreamPtr rs = RecordStream::Open(URL);
* if ( rs != nullptr ) {
* rs->addStream("XY", "ABCD", "", "BHZ");
* RecordPtr rec;
* while ( (rec = rs->next()) ) {
* // Do something with rec
* }
* rs->close();
* }
* \endcode
*/
class SC_SYSTEM_CORE_API RecordStream : public Core::InterruptibleObject {
DECLARE_SC_CLASS(RecordStream)
// ------------------------------------------------------------------
// X'truction
// ------------------------------------------------------------------
protected:
/**
* @brief The constructor is protected because this is an
* abstract base class.
*/
RecordStream();
public:
//! D'tor
virtual ~RecordStream() {}
// ------------------------------------------------------------------
// RecordStream interface
// ------------------------------------------------------------------
public:
/**
* @brief Sets the source location of the data. This is implementation
* specific. It can be a path to a file on disk or a hostname
* with port or something else.
* @param source The source definition.
* @return Status flag
*/
virtual bool setSource(const std::string &source) = 0;
/**
* @brief Closes the recordstream. This method will usually be called
* from within another thread while reading data. So it must be
* implemented with thread safety in mind.
*/
virtual void close() = 0;
/**
* @brief Adds a data channel to the request. This will not yet start
* the request. Some implementations may support wildcard
* characters (* and ?) at any level.
*
* The time window request for this channel will be using the globally
* configured time window, see setStartTime(const Seiscomp::Core::Time &),
* setEndTime(const Seiscomp::Core::Time &) and setTimeWindow(const Seiscomp::Core::TimeWindow &timeWindow).
*
* If addStream() is called another time with the same channel
* identifiers then most implementations will overwrite the previous
* request. In short: multiple requests (different time windows) for
* the same channel are not supported.
*
* @param networkCode The network code
* @param stationCode The station code
* @param locationCode The location code
* @param channelCode The channel code
* @return Status flag
*/
virtual bool addStream(const std::string &networkCode,
const std::string &stationCode,
const std::string &locationCode,
const std::string &channelCode) = 0;
/**
* @brief Same as addStream(const std::string &, const std::string &, const std::string &, const std::string &)
* but with an additional time window for this particular channel.
*
* If addStream() is called another time with the same channel
* identifiers then most implementations will overwrite the previous
* request. In short: multiple requests (different time windows) for
* the same channel are not supported.
*
* @param networkCode The network code
* @param stationCode The station code
* @param locationCode The location code
* @param channelCode The channel code
* @param startTime The start time for this particular stream
* @param endTime The end time for this particular stream
* @return
*/
virtual bool addStream(const std::string &networkCode,
const std::string &stationCode,
const std::string &locationCode,
const std::string &channelCode,
const Seiscomp::Core::Time &startTime,
const Seiscomp::Core::Time &endTime) = 0;
/**
* @brief Sets the start time for all streams that haven't been
* requested with a specific time window.
* @param startTime The start time.
* @return Status flag
*/
virtual bool setStartTime(const Seiscomp::Core::Time &startTime) = 0;
/**
* @brief Sets the end time for all streams that haven't been
* requested with a specific time window.
* @param endTime The end time. And invalid time is treated as open
* end time and will return as much data as is available.
* @return Status flag
*/
virtual bool setEndTime(const Seiscomp::Core::Time &endTime) = 0;
/**
* @brief Convenience function to set start time and end time.
* @param timeWindow The time window
* @return Status flag
*/
virtual bool setTimeWindow(const Seiscomp::Core::TimeWindow &timeWindow);
/**
* @brief Sets an optional timeout for data retrieval. If within \p seconds
* seconds no data is returned then the recordstream will abort.
* The default implementation return false.
* @param seconds The maximum number of seconds to wait for data.
* @return Status flag.
*/
virtual bool setTimeout(int seconds);
/**
* @brief Sets the type of the record to be generated. Not all
* implementations support this call or will just ignore it as
* the type of data is defined in the data protocol. This is
* most useful for files.
* @param type The type name. Currently the following record types are
* supported:
* - 'mseed' MiniSeed
* - 'ah' AH format
* - 'sac' SAC
* @return Status flag. The default implementation will return false.
*/
virtual bool setRecordType(const char *type);
// ------------------------------------------------------------------
// Data retrieval interface
// ------------------------------------------------------------------
public:
/**
* @brief Sets the desired data type of the records returned. The
* default is DOUBLE. This method must be called before
* calling next().
* @param dataType The data type
*/
void setDataType(Array::DataType dataType);
/**
* @brief Sets the hint how records should be created. The default
* is SAVE_RAW. This method must be called before calling
* next().
* @param hint The record creation hint
*/
void setDataHint(Record::Hint hint);
/**
* @brief Returns the next record from the source.
* @return The ownership of the returned instance goes to the
* caller. Iteration stops of nullptr is returned.
*/
virtual Record *next() = 0;
// ------------------------------------------------------------------
// RecordStream static interface
// ------------------------------------------------------------------
public:
/**
* @brief Creates a recordstream for the given service.
* @param service The service name
* @return A pointer to the recordstream object
*
* \note
* The returned pointer has to be deleted by the caller!
*/
static RecordStream *Create(const char *service);
/**
* @brief Opens a recordstream at source. This will call @Create
* @param url A source URL of format [service://]address[#type],
//! e.g. file:///data/record.mseed#mseed. Service defaults
//! 'file' and the default type is 'mseed'.
* @return A pointer to the recordstream object. If the recordstream
* does not support the requested type, nullptr will be returned.
*
* \note
* The returned pointer has to be deleted by the caller!
*/
static RecordStream *Open(const char *url);
// ------------------------------------------------------------------
// Protected interface
// ------------------------------------------------------------------
protected:
// Does nothing
virtual void handleInterrupt(int);
/**
* @brief Helper function to set up a created record. Basically
* this will set the desired data type and hint.
* @param rec
*/
void setupRecord(Record *rec);
// ------------------------------------------------------------------
// Protected members
// ------------------------------------------------------------------
protected:
Array::DataType _dataType;
Record::Hint _hint;
};
DEFINE_INTERFACE_FACTORY(RecordStream);
#define REGISTER_RECORDSTREAM_VAR(Class, Service) \
Seiscomp::Core::Generic::InterfaceFactory<Seiscomp::IO::RecordStream, Class> __##Class##InterfaceFactory__(Service)
#define REGISTER_RECORDSTREAM(Class, Service) \
static REGISTER_RECORDSTREAM_VAR(Class, Service)
inline void RecordStream::setupRecord(Record *rec) {
rec->setDataType(_dataType);
rec->setHint(_hint);
}
}
}
#endif