Ocean
Loading...
Searching...
No Matches
Streaming.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef FACEBOOK_NETWORK_STREAMING_H
9#define FACEBOOK_NETWORK_STREAMING_H
10
13
14namespace Ocean
15{
16
17namespace Network
18{
19
20/**
21 * This class is the base class for all streaming objects.
22 * @ingroup network
23 */
24class OCEAN_NETWORK_EXPORT Streaming
25{
26 public:
27
28 /**
29 * Definition of a vector holding bytes.
30 */
31 using Buffer = std::vector<uint8_t>;
32
33 /**
34 * Definition of different streaming states.
35 */
36 enum State
37 {
38 /// Start state.
40 /// Pause state.
42 /// Stop state.
44 /// Type change state.
45 STATE_TYPE_CHANGED
46 };
47
48 /**
49 * Definition of a session id.
50 */
52
53 protected:
54
55 /// Definition of a connect command.
56 static const std::string& connectCommand();
57 /// Definition of a positive connect response.
58 static const std::string& connectResponseP();
59 /// Definition of a negative connect response.
60 static const std::string& connectResponseN();
61
62 /// Definition of a disconnect command.
63 static const std::string& disconnectCommand();
64 /// Definition of a positive disconnect response.
65 static const std::string& disconnectResponseP();
66 /// Definition of a negative disconnect response.
67 static const std::string& disconnectResponseN();
68
69 /// Definition of a channel select command.
70 static const std::string& channelSelectCommand();
71 /// Definition of a positive channel select response.
72 static const std::string& channelSelectResponseP();
73 /// Definition of a negative channel select response.
74 static const std::string& channelSelectResponseN();
75
76 /// Definition of a start command.
77 static const std::string& startCommand();
78 /// Definition of a positiv start response.
79 static const std::string& startResponseP();
80 /// Definition fo a negative start response.
81 static const std::string& startResponseN();
82
83 /// Definition of a pause command.
84 static const std::string& pauseCommand();
85 /// Definition of a positive pause response.
86 static const std::string& pauseResponseP();
87 /// Definition of a negative pause response.
88 static const std::string& pauseResponseN();
89
90 /// Definition of a stop command.
91 static const std::string& stopCommand();
92 /// Definition of a positive stop response.
93 static const std::string& stopResponseP();
94 /// Definition of a negative stop response.
95 static const std::string& stopResponseN();
96
97 /// Definition of a client port command.
98 static const std::string& clientPortCommand();
99 /// Definition of a positive client port response.
100 static const std::string& clientPortResponseP();
101 /// Definition of a negative client port response.
102 static const std::string& clientPortResponseN();
103
104 /// Definition of a server port command.
105 static const std::string& serverPortCommand();
106 /// Definition of a positive server port response.
107 static const std::string& serverPortResponseP();
108 /// Definition of a negative server port response.
109 static const std::string& serverPortResponseN();
110
111 /// Definition of a channel request command.
112 static const std::string& channelRequestCommand();
113 /// Definition of a positive channel request response.
114 static const std::string& channelRequestResponseP();
115 /// Definition of a negative channel request response.
116 static const std::string& channelRequestResponseN();
117
118 /// Definition of a changed data type command.
119 static const std::string& changedDataTypeCommand();
120 /// Definition of a positive changed data type request.
121 static const std::string& changedDataTypeResponseP();
122 /// Definition of a negative change data type request.
123 static const std::string& changedDataTypeResponseN();
124
125 /// Definition of a data type request command.
126 static const std::string& dataTypeRequestCommand();
127 /// Definition of a positive data type request response.
128 static const std::string& dataTypeRequestResponseP();
129 /// Definition of a negative data type request response.
130 static const std::string& dataTypeRequestResponseN();
131
132 /// Definition of an extra data request command.
133 static const std::string& extraDataRequestCommand();
134 /// Definition of a positive extra data request response.
135 static const std::string& extraDataRequestResponseP();
136 /// Definition of a negative extra data request response.
137 static const std::string& extraDataRequestResponseN();
138
139 /// Definition of a changed extra data command.
140 static const std::string& changedExtraDataCommand();
141 /// Definition of a positive changed extra data response.
142 static const std::string& changedExtraDataResponseP();
143 /// Definition of a negative changed extra data response.
144 static const std::string& changedExtraDataResponseN();
145
146 public:
147
148 /**
149 * Returns the name of this streaming object.
150 * @return Streaming object name
151 */
152 inline const std::string& name() const;
153
154 /**
155 * Returns the description of this streaming object.
156 * @return Streaming object description
157 */
158 inline const std::string& description() const;
159
160 /**
161 * Returns the timeout value the streaming object waits for response messages.
162 * @return timeout The timeout value in seconds
163 */
164 inline double responseTimeout() const;
165
166 /**
167 * Sets the name of this streaming object.
168 * @param name Streaming object name to set
169 * @return True, if succeeded
170 */
171 virtual bool setName(const std::string& name);
172
173 /**
174 * Sets the description of this streaming object.
175 * @param description Streaming object description to set
176 * @return True, if succeeded
177 */
178 virtual bool setDescription(const std::string& description);
179
180 /**
181 * Sets the timeout value the streaming object waits for response messages.
182 * @param timeout The timeout value in seconds
183 * @return True, if succeeded
184 */
185 bool setResponseTimeout(const double timeout);
186
187 protected:
188
189 /**
190 * Creates a new streaming object.
191 */
193
194 /**
195 * Disabled copy constructor.
196 * @param streaming Object which would be copied
197 */
198 Streaming(const Streaming& streaming) = delete;
199
200 /**
201 * Destructs an object.
202 */
203 virtual ~Streaming() = default;
204
205 /**
206 * Parses a command or a response.
207 * @param data The data to parse, must be valid
208 * @param size The size of the data to parse in bytes
209 * @param isResponse True, if the parse string was a command, a response otherwise
210 * @param message Command or response message
211 * @param value Optional command or response value
212 * @param sessionId Unique session id
213 * @return True, if succeeded
214 */
215 bool parse(const unsigned char* data, const size_t size, bool& isResponse, std::string& message, std::string& value, SessionId& sessionId);
216
217 /**
218 * Disabled copy operator.
219 * @param streaming Object which would be copied
220 */
221 Streaming& operator=(const Streaming& streaming) = delete;
222
223 /**
224 * Creates a new command message.
225 * @param command The command
226 * @param sessionId Unique session id connected to this command
227 * @return Command message
228 */
229 static std::string createCommand(const std::string& command, const SessionId sessionId);
230
231 /**
232 * Creates a new command message with a value.
233 * @param command The command
234 * @param value Command value
235 * @param sessionId Unique session id connected to this command
236 * @return Command message
237 */
238 static std::string createCommand(const std::string& command, const std::string& value, const SessionId sessionId);
239
240 /**
241 * Creates a new response message.
242 * @param response The response
243 * @param sessionId Unique session id connected to this response
244 * @return Response message
245 */
246 static std::string createResponse(const std::string& response, const SessionId sessionId);
247
248 /**
249 * Creates a new response message with a value.
250 * @param response The response
251 * @param value Response value
252 * @param sessionId Unique session id connected to this response
253 * @return Response message
254 */
255 static std::string createResponse(const std::string& response, const std::string& value, const SessionId sessionId);
256
257 protected:
258
259 /// Name of this streaming object.
260 std::string name_;
261
262 /// Description of this streaming object.
263 std::string description_;
264
265 /// Timeout value for response messages.
266 double responseTimeout_ = 2.0;
267
268 /// Message queue.
269 MessageQueue messageQueue_ = MessageQueue(100);
270};
271
272inline const std::string& Streaming::name() const
273{
274 return name_;
275}
276
277inline const std::string& Streaming::description() const
278{
279 return description_;
280}
281
282inline double Streaming::responseTimeout() const
283{
284 return responseTimeout_;
285}
286
287}
288
289}
290
291#endif // FACEBOOK_NETWORK_STREAMING_H
This class implements a message queue.
Definition MessageQueue.h:28
uint32_t Id
Definition of a message id.
Definition MessageQueue.h:34
This class is the base class for all streaming objects.
Definition Streaming.h:25
MessageQueue::Id SessionId
Definition of a session id.
Definition Streaming.h:51
static const std::string & connectResponseP()
Definition of a positive connect response.
static const std::string & pauseResponseN()
Definition of a negative pause response.
static const std::string & serverPortResponseN()
Definition of a negative server port response.
double responseTimeout_
Timeout value for response messages.
Definition Streaming.h:266
virtual ~Streaming()=default
Destructs an object.
bool setResponseTimeout(const double timeout)
Sets the timeout value the streaming object waits for response messages.
static const std::string & connectCommand()
Definition of a connect command.
static std::string createCommand(const std::string &command, const std::string &value, const SessionId sessionId)
Creates a new command message with a value.
static const std::string & clientPortResponseN()
Definition of a negative client port response.
std::string description_
Description of this streaming object.
Definition Streaming.h:263
static const std::string & channelRequestResponseN()
Definition of a negative channel request response.
virtual bool setName(const std::string &name)
Sets the name of this streaming object.
static const std::string & channelSelectCommand()
Definition of a channel select command.
static const std::string & startResponseN()
Definition fo a negative start response.
const std::string & description() const
Returns the description of this streaming object.
Definition Streaming.h:277
static const std::string & channelSelectResponseN()
Definition of a negative channel select response.
static std::string createResponse(const std::string &response, const std::string &value, const SessionId sessionId)
Creates a new response message with a value.
static const std::string & pauseCommand()
Definition of a pause command.
static const std::string & disconnectResponseP()
Definition of a positive disconnect response.
static const std::string & stopCommand()
Definition of a stop command.
static const std::string & startCommand()
Definition of a start command.
static const std::string & stopResponseP()
Definition of a positive stop response.
const std::string & name() const
Returns the name of this streaming object.
Definition Streaming.h:272
double responseTimeout() const
Returns the timeout value the streaming object waits for response messages.
Definition Streaming.h:282
static const std::string & changedDataTypeResponseN()
Definition of a negative change data type request.
static const std::string & channelRequestCommand()
Definition of a channel request command.
static const std::string & startResponseP()
Definition of a positiv start response.
static const std::string & extraDataRequestResponseN()
Definition of a negative extra data request response.
virtual bool setDescription(const std::string &description)
Sets the description of this streaming object.
Streaming(const Streaming &streaming)=delete
Disabled copy constructor.
bool parse(const unsigned char *data, const size_t size, bool &isResponse, std::string &message, std::string &value, SessionId &sessionId)
Parses a command or a response.
static const std::string & dataTypeRequestResponseN()
Definition of a negative data type request response.
static const std::string & channelRequestResponseP()
Definition of a positive channel request response.
std::string name_
Name of this streaming object.
Definition Streaming.h:260
static const std::string & serverPortResponseP()
Definition of a positive server port response.
static const std::string & extraDataRequestResponseP()
Definition of a positive extra data request response.
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition Streaming.h:31
static const std::string & changedExtraDataResponseN()
Definition of a negative changed extra data response.
static const std::string & changedExtraDataResponseP()
Definition of a positive changed extra data response.
static const std::string & changedDataTypeCommand()
Definition of a changed data type command.
Streaming & operator=(const Streaming &streaming)=delete
Disabled copy operator.
static const std::string & changedExtraDataCommand()
Definition of a changed extra data command.
static const std::string & disconnectCommand()
Definition of a disconnect command.
static const std::string & disconnectResponseN()
Definition of a negative disconnect response.
static const std::string & channelSelectResponseP()
Definition of a positive channel select response.
static const std::string & connectResponseN()
Definition of a negative connect response.
static std::string createResponse(const std::string &response, const SessionId sessionId)
Creates a new response message.
static const std::string & clientPortCommand()
Definition of a client port command.
static const std::string & pauseResponseP()
Definition of a positive pause response.
State
Definition of different streaming states.
Definition Streaming.h:37
@ STATE_START
Start state.
Definition Streaming.h:39
@ STATE_STOP
Stop state.
Definition Streaming.h:43
@ STATE_PAUSE
Pause state.
Definition Streaming.h:41
Streaming()
Creates a new streaming object.
static const std::string & dataTypeRequestResponseP()
Definition of a positive data type request response.
static const std::string & dataTypeRequestCommand()
Definition of a data type request command.
static const std::string & serverPortCommand()
Definition of a server port command.
static const std::string & clientPortResponseP()
Definition of a positive client port response.
static std::string createCommand(const std::string &command, const SessionId sessionId)
Creates a new command message.
static const std::string & changedDataTypeResponseP()
Definition of a positive changed data type request.
static const std::string & stopResponseN()
Definition of a negative stop response.
static const std::string & extraDataRequestCommand()
Definition of an extra data request command.
The namespace covering the entire Ocean framework.
Definition Accessor.h:15