AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.4.5
SOWRecoveryPointAdapter.hpp
1 //
3 // Copyright (c) 2010-2025 60East Technologies Inc., All Rights Reserved.
4 //
5 // This computer software is owned by 60East Technologies Inc. and is
6 // protected by U.S. copyright laws and other laws and by international
7 // treaties. This computer software is furnished by 60East Technologies
8 // Inc. pursuant to a written license agreement and may be used, copied,
9 // transmitted, and stored only in accordance with the terms of such
10 // license agreement and with the inclusion of the above copyright notice.
11 // This computer software or any other copies thereof may not be provided
12 // or otherwise made available to any other person.
13 //
14 // U.S. Government Restricted Rights. This computer software: (a) was
15 // developed at private expense and is in all respects the proprietary
16 // information of 60East Technologies Inc.; (b) was not developed with
17 // government funds; (c) is a trade secret of 60East Technologies Inc.
18 // for all purposes of the Freedom of Information Act; and (d) is a
19 // commercial item and thus, pursuant to Section 12.212 of the Federal
20 // Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
21 // Government's use, duplication or disclosure of the computer software
22 // is subject to the restrictions set forth by 60East Technologies Inc..
23 //
25 
26 #ifndef _SOWRECOVERYPOINTADAPTER_H_
27 #define _SOWRECOVERYPOINTADAPTER_H_
28 
29 #include <amps/ampsplusplus.hpp>
30 #include <amps/Field.hpp>
31 #include <amps/RecoveryPoint.hpp>
33 #include <assert.h>
34 #include <memory>
35 #include <string>
36 
37 using std::string;
38 
39 #define AMPS_SOW_STORE_DEFAULT_TOPIC "/ADMIN/bookmark_store"
40 #define AMPS_SOW_STORE_DEFAULT_CLIENT_FIELD "clientName"
41 #define AMPS_SOW_STORE_DEFAULT_SUB_FIELD "subId"
42 #define AMPS_SOW_STORE_DEFAULT_BOOKMARK_FIELD "bookmark"
43 
44 #define SOW_RECOVERY_HANDLE_EXCEPTION(x) \
45  catch (const AMPSException& ex_) \
46  { \
47  std::ostringstream os; \
48  os << x << ": AMPSException " << ex_.what(); \
49  StoreException ex(os.str()); \
50  if (_throwNotListen) \
51  { \
52  throw ex;\
53  } \
54  else if (_pExceptionListener) \
55  { \
56  _pExceptionListener->exceptionThrown(ex); \
57  } \
58  } \
59  catch (const std::exception& ex_) \
60  { \
61  std::ostringstream os; \
62  os << x << ": std::exception " << ex_.what(); \
63  StoreException ex(os.str()); \
64  if (_throwNotListen) \
65  { \
66  throw ex;\
67  } \
68  else if (_pExceptionListener) \
69  { \
70  _pExceptionListener->exceptionThrown(ex); \
71  } \
72  } \
73  catch (...) \
74  { \
75  std::ostringstream os; \
76  os << x << ": Unknown exception"; \
77  StoreException ex(os.str()); \
78  if (_throwNotListen) \
79  { \
80  throw ex;\
81  } \
82  else if (_pExceptionListener) \
83  { \
84  _pExceptionListener->exceptionThrown(ex); \
85  } \
86  }
87 
88 namespace AMPS
89 {
90 
94  {
95  public:
96 
128  SOWRecoveryPointAdapter(const Client& storeClient_,
129  const string& trackedClientName_,
130  unsigned timeoutMillis_ = 5000,
131  bool useTimestamp_ = false,
132  bool closeClient_ = true,
133  bool updateFailureThrows_ = false,
134  const string& topic_ = AMPS_SOW_STORE_DEFAULT_TOPIC,
135  const string& clientNameField_ = AMPS_SOW_STORE_DEFAULT_CLIENT_FIELD,
136  const string& subIdField_ = AMPS_SOW_STORE_DEFAULT_SUB_FIELD,
137  const string& bookmarkField_ = AMPS_SOW_STORE_DEFAULT_BOOKMARK_FIELD
138  )
140  , _serializeLen(0)
141  , _serializeBuffer(0)
142  , _deserializeLen(0)
143  , _deserializeBuffer(0)
144  , _client(storeClient_)
145  , _trackedName(trackedClientName_)
146  , _topic(topic_)
147  , _nameField(clientNameField_)
148  , _subIdField(subIdField_)
149  , _bookmarkField(bookmarkField_)
150  , _timeoutMillis(timeoutMillis_)
151  , _closeClient(closeClient_)
152  , _executed(false)
153  , _throwNotListen(updateFailureThrows_)
154  , _useTimestamp(useTimestamp_)
155  , _closed(false)
156  {
157  if (_client.getName() == _trackedName)
158  {
159  throw UsageException("The SOWRecoveryPointAdapter cannot use the tracked client to update AMPS");
160  }
161  _initSerialization();
162  }
163 
164  virtual ~SOWRecoveryPointAdapter()
165  {
166  _close();
167  delete[] _serializeBuffer;
168  delete[] _deserializeBuffer;
169  }
170 
175  virtual bool next(RecoveryPoint& current_)
176  {
177  static Field emptyField;
178  try
179  {
180  if (!_executed)
181  {
182  Command cmd("sow");
183  cmd.setTopic(_topic)
184  .setFilter("/" + _nameField + "='" + _trackedName + "'")
185  .setTimeout(_timeoutMillis);
186  if (_useTimestamp)
187  {
188  cmd.setOptions("select=[-/,+/" + _subIdField + ",+/"
189  + _bookmarkField + "],timestamp");
190  }
191  else
192  {
193  cmd.setOptions("select=[-/,+/" + _subIdField + ",+/"
194  + _bookmarkField + "]");
195  }
196  _stream = _client.execute(cmd).timeout(_timeoutMillis);
197  _msIter = _stream.begin();
198  _executed = true;
199  }
200  else
201  {
202  ++_msIter;
203  }
204  if (_msIter == MessageStream::iterator())
205  {
206  return false;
207  }
208  Message m = *_msIter;
209  if (!m.isValid())
210  {
211  current_ = RecoveryPoint(NULL);
212  return false;
213  }
214  if (m.getCommand() == "group_begin")
215  {
216  return next(current_);
217  }
218  else if (m.getCommand() == "sow")
219  {
220  if (_useTimestamp)
221  {
222  current_ = RecoveryPoint(deserialize(m.getData(),
223  m.getTimestamp()));
224  }
225  else
226  {
227  current_ = RecoveryPoint(deserialize(m.getData(),
228  emptyField));
229  }
230  return true;
231  }
232  else if (m.getCommand() == "group_end" || m.getCommand() == "ack")
233  {
234  current_ = RecoveryPoint(NULL);
235  _msIter = MessageStream::iterator();
236  _stream = MessageStream();
237  return false;
238  }
239  }
240  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::next")
241  return false;
242  }
243 
246  virtual void update(RecoveryPoint& recoveryPoint_)
247  {
248  try
249  {
250  Field data = serialize(recoveryPoint_);
251  _client.publish(_topic.data(), _topic.length(), data.data(), data.len());
252  }
253  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::update")
254  }
255 
257  virtual void purge()
258  {
259  try
260  {
261  Message m = _client.sowDelete(_topic, "/" + _nameField
262  + "='" + _trackedName + "'");
263  }
264  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::purge")
265  }
266 
269  virtual void purge(const Field& subId_)
270  {
271  try
272  {
273  Message m = _client.sowDelete(_topic, "/" + _nameField + "='"
274  + _trackedName + "' and /"
275  + _subIdField + "='"
276  + subId_ + "'");
277  }
278  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::purge(subId)")
279  }
280 
282  virtual void close()
283  {
284  _close();
285  }
286 
294  void setExceptionListener(const std::shared_ptr<const ExceptionListener>& pListener_)
295  {
296  _pExceptionListener = pListener_;
297  }
298  protected:
299  void _close()
300  {
301  if (_closed || !_client.isValid())
302  {
303  return;
304  }
305  try
306  {
307  _client.publishFlush();
308  }
309  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::close publishFlush")
310  try
311  {
312  if (_closeClient)
313  {
314  _closed = true;
315  _client.disconnect();
316  _client = Client();
317  }
318  }
319  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::close disconnect")
320  }
321 
322  void _initSerialization()
323  {
324  try
325  {
326  // Set up json serialization
327  if (_serializeLen == 0)
328  {
329  _serializeLen = (size_t) (_nameField.length()
330  + _trackedName.length()
331  + _subIdField.length()
332  + _bookmarkField.length()
333  + (AMPS_MAX_BOOKMARK_LEN * 4UL)
334  + SUBID_LEN + JSON_EXTRA);
335  _serializeLen += (128 - (_serializeLen % 128));
336  }
337  _serializeBuffer = new char[_serializeLen];
338  AMPS_snprintf(_serializeBuffer, _serializeLen,
339  "{\"%s\":\"%s\",\"%s\":\"", _nameField.c_str()
340  , _trackedName.c_str()
341  , _subIdField.c_str());
342  _serializeStart = JSON_START + _nameField.length()
343  + _trackedName.length() + _subIdField.length();
344  }
345  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::initSerialization")
346  }
347 
348  // Subclasses can override this to set up for something other than json
349  // serialization if not using json.
350  virtual void initSerialization()
351  {
352  _initSerialization();
353  }
354 
355  // Subclasses can override this function if not using json data type.
356  // It needs to return an allocated RecoveryPointImpl based on the data
357  // field from a sow message that contains only 2 fields: _subIdField and
358  // _bookmarkField. If you'd like more, override begin()
359  virtual RecoveryPointImpl* deserialize(const Field& data_,
360  const Field& timestamp_)
361  {
362  Field subId;
363  Field bookmark;
364  try
365  {
366  // We have 2 fields subId and bookmark and we only need the
367  // values. Find : then start ", then end ".
368  const char* start = (const char*)memchr((const void*)data_.data(),
369  (int)':', data_.len());
370  if (!start)
371  {
372  throw StoreException("Failure parsing json RecoveryPoint subId, no :");
373  }
374  size_t remain = data_.len() - (size_t)(start - data_.data());
375  start = (const char*)memchr((const void*)start, (int)'"', remain);
376  if (!start)
377  {
378  throw StoreException("Failure parsing json RecoveryPoint subId, no start \"");
379  }
380  ++start;
381  remain = data_.len() - (size_t)(start - data_.data());
382  const char* end = (const char*)memchr((const void*)start,
383  (int)'"', remain);
384  if (!end)
385  {
386  throw StoreException("Failure parsing json RecoveryPoint subId, no end \"");
387  }
388  size_t len = (size_t)(end - start);
389  subId = Field(start, len);
390  start = (const char*)memchr((const void*)start, (int)':', data_.len());
391  if (!start)
392  {
393  throw StoreException("Failure parsing json RecoveryPoint bookmark, no :");
394  }
395  remain = data_.len() - (size_t)(start - data_.data());
396  start = (const char*)memchr((const void*)start, (int)'"', remain);
397  if (!start)
398  {
399  throw StoreException("Failure parsing json RecoveryPoint bookmark, no start \"");
400  }
401  ++start;
402  remain = data_.len() - (size_t)(start - data_.data());
403  end = (const char*)memchr((const void*)start, (int)'"', remain);
404  if (!end)
405  {
406  throw StoreException("Failure parsing json RecoveryPoint bookmark, no end \"");
407  }
408  len = (size_t)(end - start);
409  if (_useTimestamp && !timestamp_.empty())
410  {
411  if (_deserializeLen < len + timestamp_.len())
412  {
413  delete[] _deserializeBuffer;
414  _deserializeBuffer = 0;
415  }
416  if (!_deserializeBuffer)
417  {
418  _deserializeLen = len + timestamp_.len() + 1;
419  _deserializeBuffer = new char[_deserializeLen];
420  }
421  memcpy((void*)_deserializeBuffer, (const void*)start, len);
422  _deserializeBuffer[len] = ',';
423  memcpy((void*)(_deserializeBuffer + len + 1),
424  (const void*)timestamp_.data(), timestamp_.len());
425  bookmark = Field(_deserializeBuffer, _deserializeLen);
426  }
427  else
428  {
429  bookmark = Field(start, len);
430  }
431  }
432  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::deserialize")
433  // Return a recovery point that will copy current field values and
434  // clear them when destructed.
435  return new FixedRecoveryPoint(subId, bookmark, true);
436  }
437 
438  virtual Field& serialize(const RecoveryPoint& recoveryPoint_)
439  {
440  try
441  {
442  Field subId = recoveryPoint_.getSubId();
443  Field bookmark = recoveryPoint_.getBookmark();
444  size_t fullLen = _serializeStart + subId.len()
445  + _bookmarkField.length() + bookmark.len() + JSON_END;
446  if (fullLen >= _serializeLen)
447  {
448  _serializeLen = fullLen + (128 - (fullLen % 128));
449  delete[] _serializeBuffer;
450  // This will reallocate the buffer and fill with predicate
451  initSerialization();
452  }
453  AMPS_snprintf(_serializeBuffer + _serializeStart,
454  _serializeLen - _serializeStart,
455  "%.*s\",\"%s\":\"%.*s\"}", (int)subId.len()
456  , subId.data()
457  , _bookmarkField.c_str()
458  , (int)bookmark.len()
459  , bookmark.data());
460  _serializeField.assign(_serializeBuffer, fullLen);
461  }
462  SOW_RECOVERY_HANDLE_EXCEPTION("SOWRecoveryPoint::serialize")
463  return _serializeField;
464  }
465 
466  enum Constants : size_t
467  {
468  JSON_START = 11, // '{', 7 '"', 2 ':', 1 ','
469  JSON_END = 8, // '}', 5 '"', 1 ':', 1 ','
470  JSON_EXTRA = 19, // '{', '}', 3 ':', 12 '"', 2 ','
471  SUBID_LEN = 64 // rough guess on typical max len
472  };
473 
474  private:
475  size_t _serializeLen;
476  size_t _serializeStart;
477  Field _serializeField;
478  char* _serializeBuffer;
479  size_t _deserializeLen;
480  char* _deserializeBuffer;
481  Client _client;
482  std::string _trackedName;
483  std::string _topic;
484  std::string _nameField;
485  std::string _subIdField;
486  std::string _bookmarkField;
487  unsigned _timeoutMillis;
488  MessageStream _stream;
489  MessageStream::iterator _msIter;
490  std::shared_ptr<const ExceptionListener> _pExceptionListener;
491  bool _closeClient;
492  bool _executed;
493  bool _throwNotListen;
494  bool _useTimestamp;
495  bool _closed;
496  };
497 } // namespace AMPS
498 #endif //_SOWRECOVERYPOINTADAPTER_H_
499 
Command & setOptions(const std::string &options_)
Sets the options string for this command: see Message.Options for a helper class for constructing the...
Definition: ampsplusplus.hpp:789
virtual void update(RecoveryPoint &recoveryPoint_)
Update the storage information with the given recovery point.
Definition: SOWRecoveryPointAdapter.hpp:246
Message encapsulates a single message sent to or received from an AMPS server, and provides methods f...
Definition: Message.hpp:531
iterator begin(void)
Returns an iterator representing the beginning of the topic or subscription.
Definition: ampsplusplus.hpp:5049
const char * data() const
Returns the (non-null-terminated) data underlying this field.
Definition: Field.hpp:260
Field getCommand() const
Retrieves the value of the Command header of the Message as a new Field.
Definition: Message.hpp:1195
MessageStream execute(Command &command_)
Execute the provided command and return messages received in response in a MessageStream.
Definition: ampsplusplus.hpp:8908
RecoveryPointAdapter virtual base class for implementing external storage of subscription recovery po...
Definition: SOWRecoveryPointAdapter.hpp:93
Command & setFilter(const std::string &filter_)
Definition: ampsplusplus.hpp:692
Client represents a connection to an AMPS server, but does not provide failover or reconnection behav...
Definition: ampsplusplus.hpp:5130
Provides access to the subId and bookmark needed to restart a subscription.
Definition: RecoveryPoint.hpp:67
RecoveryPointImpl virtual base class provides access to the subId and bookmark needed to restart a su...
Definition: RecoveryPoint.hpp:49
Command & setTimeout(unsigned timeout_)
Sets the client-side timeout for this command.
Definition: ampsplusplus.hpp:851
virtual void purge()
Remove all data from the storage.
Definition: SOWRecoveryPointAdapter.hpp:257
virtual bool next(RecoveryPoint &current_)
Recovery is done by iteration over elements in storage.
Definition: SOWRecoveryPointAdapter.hpp:175
bool empty() const
Returns &#39;true&#39; if empty, &#39;false&#39; otherwise.
Definition: Field.hpp:128
Defines the AMPS::Field class, which represents the value of a field in a message.
Core type, function, and class declarations for the AMPS C++ client.
size_t len() const
Returns the length of the data underlying this field.
Definition: Field.hpp:267
Provides AMPS::RecoveryPointAdapter, an iterface for implementing external storage of bookmark subscr...
virtual void purge(const Field &subId_)
Remove the specified subId_ from the storage.
Definition: SOWRecoveryPointAdapter.hpp:269
void setExceptionListener(const std::shared_ptr< const ExceptionListener > &pListener_)
Set an exception listener on this adapter that will be notified of all exceptions that occur rather t...
Definition: SOWRecoveryPointAdapter.hpp:294
SOWRecoveryPointAdapter(const Client &storeClient_, const string &trackedClientName_, unsigned timeoutMillis_=5000, bool useTimestamp_=false, bool closeClient_=true, bool updateFailureThrows_=false, const string &topic_=AMPS_SOW_STORE_DEFAULT_TOPIC, const string &clientNameField_=AMPS_SOW_STORE_DEFAULT_CLIENT_FIELD, const string &subIdField_=AMPS_SOW_STORE_DEFAULT_SUB_FIELD, const string &bookmarkField_=AMPS_SOW_STORE_DEFAULT_BOOKMARK_FIELD)
Create a SOWRecoveryPointAdapter for a BookmarkStore that writes updated RecoveryPoints to the server...
Definition: SOWRecoveryPointAdapter.hpp:128
Provides AMPS::RecoveryPoint, AMPS::RecoveryPointFactory, AMPS::FixedRecoveryPoint, and AMPS::DynamicRecoveryPoint.
Represents an iterator over messages in an AMPS topic.
Definition: ampsplusplus.hpp:5004
void publishFlush(long timeout_=0, unsigned ackType_=Message::AckType::Processed)
Ensure that AMPS messages are sent and have been processed by the AMPS server.
Definition: ampsplusplus.hpp:5730
virtual void close()
Take any necessary actions to close the associated storage.
Definition: SOWRecoveryPointAdapter.hpp:282
Field represents the value of a single field in a Message.
Definition: Field.hpp:86
An iterable object representing the results of an AMPS subscription and/or query. ...
Definition: ampsplusplus.hpp:4996
Field getTimestamp() const
Retrieves the value of the Timestamp header of the Message as a new Field.
Definition: Message.hpp:1430
Field getData() const
Returns the data from this message.
Definition: Message.hpp:1460
FixedRecoveryPoint is a RecoveryPoint implementation where subId and bookmark are set explicitly...
Definition: RecoveryPoint.hpp:133
Command & setTopic(const std::string &topic_)
Definition: ampsplusplus.hpp:679
void disconnect()
Disconnect from an AMPS server.
Definition: ampsplusplus.hpp:5349
RecoveryPointAdapterImpl virtual base class for implementing external storage of subscription recover...
Definition: RecoveryPointAdapter.hpp:52
amps_uint64_t publish(const std::string &topic_, const std::string &data_)
Publish a message to an AMPS topic, returning the sequence number assigned by the publish store if on...
Definition: ampsplusplus.hpp:5608
Definition: ampsplusplus.hpp:103
const Field & getSubId() const
Get the sub id for this recovery point.
Definition: RecoveryPoint.hpp:84
MessageStream timeout(unsigned timeout_)
Sets the maximum time to wait for the next message in milliseconds; if no message is available within...
Definition: ampsplusplus.hpp:8879
const std::string & getName() const
Returns the name of this client passed in the constructor.
Definition: ampsplusplus.hpp:5192
Command is an encapsulation of a single AMPS command sent by the client.
Definition: ampsplusplus.hpp:472
std::string sowDelete(const MessageHandler &messageHandler, const std::string &topic, const std::string &filter, long timeout)
Deletes one or more messages from a topic&#39;s SOW cache.
Definition: ampsplusplus.hpp:6772
const Field & getBookmark() const
Get the bookmark for this recovery point.
Definition: RecoveryPoint.hpp:91