26 #ifndef _SOWRECOVERYPOINTADAPTER_H_ 27 #define _SOWRECOVERYPOINTADAPTER_H_ 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" 44 #define SOW_RECOVERY_HANDLE_EXCEPTION(x) \ 45 catch (const AMPSException& ex_) \ 47 std::ostringstream os; \ 48 os << x << ": AMPSException " << ex_.what(); \ 49 StoreException ex(os.str()); \ 50 if (!_throwNotListen && _pExceptionListener) \ 52 _pExceptionListener->exceptionThrown(ex); \ 56 catch (const std::exception& ex_) \ 58 std::ostringstream os; \ 59 os << x << ": std::exception " << ex_.what(); \ 60 StoreException ex(os.str()); \ 61 if (!_throwNotListen && _pExceptionListener) \ 63 _pExceptionListener->exceptionThrown(ex); \ 69 std::ostringstream os; \ 70 os << x << ": Unknown exception"; \ 71 StoreException ex(os.str()); \ 72 if (!_throwNotListen && _pExceptionListener) \ 74 _pExceptionListener->exceptionThrown(ex); \ 120 const string& trackedClientName_,
121 unsigned timeoutMillis_ = 5000,
122 bool useTimestamp_ =
false,
123 bool closeClient_ =
true,
124 bool updateFailureThrows_ =
false,
125 const string& topic_ = AMPS_SOW_STORE_DEFAULT_TOPIC,
126 const string& clientNameField_ = AMPS_SOW_STORE_DEFAULT_CLIENT_FIELD,
127 const string& subIdField_ = AMPS_SOW_STORE_DEFAULT_SUB_FIELD,
128 const string& bookmarkField_ = AMPS_SOW_STORE_DEFAULT_BOOKMARK_FIELD
132 , _serializeBuffer(0)
134 , _deserializeBuffer(0)
135 , _client(storeClient_)
136 , _trackedName(trackedClientName_)
138 , _nameField(clientNameField_)
139 , _subIdField(subIdField_)
140 , _bookmarkField(bookmarkField_)
141 , _timeoutMillis(timeoutMillis_)
143 , _closeClient(closeClient_)
145 , _throwNotListen(updateFailureThrows_)
146 , _useTimestamp(useTimestamp_)
149 if (_client.
getName() == _trackedName)
151 throw UsageException(
"The SOWRecoveryPointAdapter cannot use the tracked client to update AMPS");
153 _initSerialization();
160 delete[] _serializeBuffer;
161 delete[] _deserializeBuffer;
170 static Field emptyField;
177 .
setFilter(
"/" + _nameField +
"='" + _trackedName +
"'")
181 cmd.
setOptions(
"select=[-/,+/" + _subIdField +
",+/" 182 + _bookmarkField +
"],timestamp");
186 cmd.
setOptions(
"select=[-/,+/" + _subIdField +
",+/" 187 + _bookmarkField +
"]");
190 _msIter = _stream.
begin();
209 return next(current_);
233 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::next")
243 Field data = serialize(recoveryPoint_);
247 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::update")
256 +
"='" + _trackedName +
"'");
258 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::purge")
268 + _trackedName +
"' and /" 272 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::purge(subId)")
290 _pExceptionListener = pListener_;
295 if (_closed || !_client.isValid())
303 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::close publishFlush")
313 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::close disconnect")
316 void _initSerialization()
321 if (_serializeLen == 0)
323 _serializeLen = (size_t) (_nameField.length()
324 + _trackedName.length()
325 + _subIdField.length()
326 + _bookmarkField.length()
327 + (AMPS_MAX_BOOKMARK_LEN * 4UL)
328 + SUBID_LEN + JSON_EXTRA);
329 _serializeLen += (128 - (_serializeLen % 128));
331 _serializeBuffer =
new char[_serializeLen];
332 AMPS_snprintf(_serializeBuffer, _serializeLen,
333 "{\"%s\":\"%s\",\"%s\":\"", _nameField.c_str()
334 , _trackedName.c_str()
335 , _subIdField.c_str());
336 _serializeStart = JSON_START + _nameField.length()
337 + _trackedName.length() + _subIdField.length();
339 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::initSerialization")
344 virtual void initSerialization()
346 _initSerialization();
354 const Field& timestamp_)
362 const char* start = (
const char*)memchr((
const void*)data_.
data(),
363 (int)
':', data_.
len());
366 throw StoreException(
"Failure parsing json RecoveryPoint subId, no :");
368 size_t remain = data_.
len() - (size_t)(start - data_.
data());
369 start = (
const char*)memchr((
const void*)start, (int)
'"', remain);
372 throw StoreException(
"Failure parsing json RecoveryPoint subId, no start \"");
375 remain = data_.
len() - (size_t)(start - data_.
data());
376 const char* end = (
const char*)memchr((
const void*)start,
380 throw StoreException(
"Failure parsing json RecoveryPoint subId, no end \"");
382 size_t len = (size_t)(end - start);
383 subId =
Field(start, len);
384 start = (
const char*)memchr((
const void*)start, (int)
':', data_.
len());
387 throw StoreException(
"Failure parsing json RecoveryPoint bookmark, no :");
389 remain = data_.
len() - (size_t)(start - data_.
data());
390 start = (
const char*)memchr((
const void*)start, (int)
'"', remain);
393 throw StoreException(
"Failure parsing json RecoveryPoint bookmark, no start \"");
396 remain = data_.
len() - (size_t)(start - data_.
data());
397 end = (
const char*)memchr((
const void*)start, (int)
'"', remain);
400 throw StoreException(
"Failure parsing json RecoveryPoint bookmark, no end \"");
402 len = (size_t)(end - start);
403 if (_useTimestamp && !timestamp_.
empty())
405 if (_deserializeLen < len + timestamp_.
len())
407 delete[] _deserializeBuffer;
408 _deserializeBuffer = 0;
410 if (!_deserializeBuffer)
412 _deserializeLen = len + timestamp_.
len() + 1;
413 _deserializeBuffer =
new char[_deserializeLen];
415 memcpy((
void*)_deserializeBuffer, (
const void*)start, len);
416 _deserializeBuffer[len] =
',';
417 memcpy((
void*)(_deserializeBuffer + len + 1),
418 (
const void*)timestamp_.
data(), timestamp_.
len());
419 bookmark =
Field(_deserializeBuffer, _deserializeLen);
423 bookmark =
Field(start, len);
426 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::deserialize")
438 size_t fullLen = _serializeStart + subId.
len()
439 + _bookmarkField.length() + bookmark.
len() + JSON_END;
440 if (fullLen >= _serializeLen)
442 _serializeLen = fullLen + (128 - (fullLen % 128));
443 delete[] _serializeBuffer;
447 AMPS_snprintf(_serializeBuffer + _serializeStart,
448 _serializeLen - _serializeStart,
449 "%.*s\",\"%s\":\"%.*s\"}", (
int)subId.
len()
451 , _bookmarkField.c_str()
452 , (int)bookmark.
len()
454 _serializeField.assign(_serializeBuffer, fullLen);
456 SOW_RECOVERY_HANDLE_EXCEPTION(
"SOWRecoveryPoint::serialize")
457 return _serializeField;
460 enum Constants :
size_t 469 size_t _serializeLen;
470 size_t _serializeStart;
471 Field _serializeField;
472 char* _serializeBuffer;
473 size_t _deserializeLen;
474 char* _deserializeBuffer;
476 std::string _trackedName;
478 std::string _nameField;
479 std::string _subIdField;
480 std::string _bookmarkField;
481 unsigned _timeoutMillis;
485 std::shared_ptr<const ExceptionListener> _pExceptionListener;
488 bool _throwNotListen;
493 #endif //_SOWRECOVERYPOINTADAPTER_H_ 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:785
virtual void update(RecoveryPoint &recoveryPoint_)
Update the storage information with the given recovery point.
Definition: SOWRecoveryPointAdapter.hpp:239
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:4984
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:8745
RecoveryPointAdapter virtual base class for implementing external storage of subscription recovery po...
Definition: SOWRecoveryPointAdapter.hpp:84
Command & setData(const std::string &data_)
Sets the data for this command from an existing string.
Definition: ampsplusplus.hpp:825
Command & setFilter(const std::string &filter_)
Definition: ampsplusplus.hpp:688
Client represents a connection to an AMPS server, but does not provide failover or reconnection behav...
Definition: ampsplusplus.hpp:5063
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:847
virtual void purge()
Remove all data from the storage.
Definition: SOWRecoveryPointAdapter.hpp:251
virtual bool next(RecoveryPoint ¤t_)
Recovery is done by iteration over elements in storage.
Definition: SOWRecoveryPointAdapter.hpp:168
bool empty() const
Returns 'true' if empty, 'false' 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:263
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:288
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:119
Provides AMPS::RecoveryPoint, AMPS::RecoveryPointFactory, AMPS::FixedRecoveryPoint, and AMPS::DynamicRecoveryPoint.
Represents an iterator over messages in an AMPS topic.
Definition: ampsplusplus.hpp:4939
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:5631
virtual void close()
Take any necessary actions to close the associated storage.
Definition: SOWRecoveryPointAdapter.hpp:276
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:4931
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:675
void disconnect()
Disconnect from an AMPS server.
Definition: ampsplusplus.hpp:5250
RecoveryPointAdapterImpl virtual base class for implementing external storage of subscription recover...
Definition: RecoveryPointAdapter.hpp:52
Definition: ampsplusplus.hpp:102
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:8716
const std::string & getName() const
Returns the name of this client passed in the constructor.
Definition: ampsplusplus.hpp:5125
Command is an encapsulation of a single AMPS command sent by the client.
Definition: ampsplusplus.hpp:468
std::string sowDelete(const MessageHandler &messageHandler, const std::string &topic, const std::string &filter, long timeout)
Deletes one or more messages from a topic's SOW cache.
Definition: ampsplusplus.hpp:6673
const Field & getBookmark() const
Get the bookmark for this recovery point.
Definition: RecoveryPoint.hpp:91