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