mns_client.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. // Copyright (C) 2015, Alibaba Cloud Computing
  2. #ifndef MNS_CLIENT_H
  3. #define MNS_CLIENT_H
  4. #include "mns_protocol.h"
  5. #include "mns_network_tool.h"
  6. #include <map>
  7. #include <stdint.h>
  8. #include <vector>
  9. #ifdef _WIN32
  10. #include <memory>
  11. #else
  12. #include <tr1/memory>
  13. #endif
  14. namespace mns
  15. {
  16. namespace sdk
  17. {
  18. class Queue;
  19. class Topic;
  20. class MNSClient;
  21. typedef std::shared_ptr<Queue> QueuePtr;
  22. typedef std::shared_ptr<Topic> TopicPtr;
  23. /*
  24. * use MNSClient to access MNS service
  25. *
  26. * CAUTION:
  27. * Make sure to catch Exception when calling any function
  28. * MNSClient throws MNSServerException when the request fails.
  29. * MNSClient throws MNSExceptionBase for client errors
  30. */
  31. class MNSClient
  32. {
  33. public:
  34. /* init the MNSClient for calling MNS service
  35. *
  36. * @param endpoint:
  37. * http://{AccountId}.mns.cn-hangzhou.aliyuncs.com
  38. * "cn-hangzhou" is the region place
  39. * @param accessId: accessId from aliyun.com
  40. * @param accessKey: accessKey from aliyun.com
  41. * @param connPoolSize:
  42. * MNSClient keeps a pool of connections and reuse them
  43. */
  44. MNSClient(const std::string& endpoint,
  45. const std::string& accessId,
  46. const std::string& accessKey,
  47. const int32_t connPoolSize = 200,
  48. const int32_t timeout = 35,
  49. const int32_t connectTimeout = 35);
  50. /* init the MNSClient for calling MNS service
  51. *
  52. * @param endpoint:
  53. * http://{AccountId}.mns.cn-hangzhou.aliyuncs.com
  54. * "cn-hangzhou" is the region place
  55. * @param accessId: the sts accessId
  56. * @param accessKey: the sts accessKey
  57. * @param stsToken: the sts token
  58. * @param connPoolSize:
  59. * MNSClient keeps a pool of connections and reuse them
  60. */
  61. MNSClient(const std::string& endpoint,
  62. const std::string& accessId,
  63. const std::string& accessKey,
  64. const std::string& stsToken,
  65. const int32_t connPoolSize = 200,
  66. const int32_t timeout = 35,
  67. const int32_t connectTimeout = 35);
  68. /* update the AccessId/AccessKey
  69. *
  70. * @param accessId: accessId from aliyun.com
  71. * @param accessKey: accessKey from aliyun.com
  72. */
  73. void updateAccessId(const std::string& accessId,
  74. const std::string& accessKey);
  75. /* update the AccessId/AccessKey/StsToken
  76. *
  77. * @param accessId: the sts accessId
  78. * @param accessKey: the sts accessKey
  79. * @param stsToken: the sts token
  80. */
  81. void updateAccessId(const std::string& accessId,
  82. const std::string& accessKey,
  83. const std::string& stsToken);
  84. virtual ~MNSClient() {}
  85. /* get Account attributes
  86. *
  87. * @param attributes: the returned Account attributes
  88. */
  89. void getAccountAttributes(AccountAttributes& attributes);
  90. /* set Account attributes
  91. *
  92. * @param attributes: the attributes to set
  93. */
  94. void setAccountAttributes(const AccountAttributes& attributes);
  95. /* Create Queue with specified name
  96. *
  97. * @param queueName: the queue name
  98. *
  99. * @return: the created queue instance
  100. * @throws: MNSServerException
  101. */
  102. QueuePtr createQueue(const std::string& queueName);
  103. /* Create Queue with specified name and attributes
  104. *
  105. * @param queueName: the queue name
  106. * @param attributes: the queue attributes
  107. *
  108. * @return: the created queue instance
  109. */
  110. QueuePtr createQueue(const std::string& queueName,
  111. const QueueAttributes& attributes);
  112. /* init Queue instance with specified name without creating queue
  113. *
  114. * @param queueName: the queue name
  115. *
  116. * @return: the inited queue instance
  117. */
  118. QueuePtr getQueueRef(const std::string& queueName);
  119. /* delete queue with specified name
  120. *
  121. * @param queueName: the queue name
  122. */
  123. void deleteQueue(const std::string& queueName);
  124. /* list queues with retNum=1000 and marker=""
  125. *
  126. * @param prefix: the prefix of queueName
  127. *
  128. * @param queueNames: the response queueNames
  129. */
  130. void listQueue(const std::string& prefix,
  131. std::vector<std::string>& queueNames);
  132. /* list queues with retNum=1000 and marker=""
  133. *
  134. * @param prefix: the prefix of queueName
  135. *
  136. * @param queueNames: the response queueNames
  137. * @param nextMarker: the page marker for next listQueue
  138. */
  139. void listQueue(const std::string& prefix,
  140. std::vector<std::string>& queueNames,
  141. std::string& nextMarker);
  142. /* list queues
  143. *
  144. * @param req: the ListQueueRequest containing filters
  145. * @param queueNames: the response queueNames
  146. */
  147. void listQueue(ListQueueRequest& req,
  148. std::vector<std::string>& queueNames);
  149. /* list queues
  150. *
  151. * @param req: the ListQueueRequest containing filters
  152. * @param queueNames: the response queueNames
  153. * @param nextMarker: the page marker for next listQueue
  154. */
  155. void listQueue(ListQueueRequest& req,
  156. std::vector<std::string>& queueNames,
  157. std::string& nextMarker);
  158. /* Create Topic with specified name
  159. *
  160. * @param topicName: the topic name
  161. *
  162. * @return: the created topic instance
  163. * @throws: MNSServerException
  164. */
  165. TopicPtr createTopic(const std::string& topicName);
  166. /* Create Topic with specified name and attributes
  167. *
  168. * @param topicName: the topic name
  169. * @param attributes: the topic attributes
  170. *
  171. * @return: the created topic instance
  172. */
  173. TopicPtr createTopic(const std::string& topicName,
  174. const TopicAttributes& attributes);
  175. /* init Topic instance with specified name without creating topic
  176. *
  177. * @param topicName: the topic name
  178. *
  179. * @return: the inited topic instance
  180. */
  181. TopicPtr getTopicRef(const std::string& topicName);
  182. /* delete topic with specified name
  183. *
  184. * @param topicName: the topic name
  185. */
  186. void deleteTopic(const std::string& topicName);
  187. /* list topics with retNum=1000 and marker=""
  188. *
  189. * @param prefix: the prefix of topicName
  190. * @param topicNames: the response topicNames
  191. */
  192. void listTopic(const std::string& prefix,
  193. std::vector<std::string>& topicNames);
  194. /* list topics with retNum=1000 and marker=""
  195. *
  196. * @param prefix: the prefix of topicName
  197. * @param topicNames: the response topicNames
  198. * @param nextMarker: the page marker for next listTopic
  199. */
  200. void listTopic(const std::string& prefix,
  201. std::vector<std::string>& topicNames,
  202. std::string& nextMarker);
  203. /* list topics
  204. *
  205. * @param req: the ListTopicRequest containing filters
  206. * @param topicNames: the response topicNames
  207. */
  208. void listTopic(ListTopicRequest& req,
  209. std::vector<std::string>& topicNames);
  210. /* list topics
  211. *
  212. * @param req: the ListTopicRequest containing filters
  213. * @param topicNames: the response topicNames
  214. * @param nextMarker: the page marker for next listTopic
  215. */
  216. void listTopic(ListTopicRequest& req,
  217. std::vector<std::string>& topicNames,
  218. std::string& nextMarker);
  219. const std::string& getEndpoint() const
  220. {
  221. return mEndPoint;
  222. }
  223. const std::string& GetAccessId() const
  224. {
  225. return mAccessId;
  226. }
  227. const std::string& GetAccessKey() const
  228. {
  229. return mAccessKey;
  230. }
  231. const std::string& GetStsToken() const
  232. {
  233. return mStsToken;
  234. }
  235. public:
  236. static void sendRequest(Request& req,
  237. Response& response,
  238. const std::string& endpoint,
  239. const std::string& accessId,
  240. const std::string& accessKey,
  241. const std::string& stsToken,
  242. MNSConnectionToolPtr mnsConnTool);
  243. static void signRequest(Request& req,
  244. const std::string& endpoint,
  245. const std::string& accessId,
  246. const std::string& accessKey,
  247. const std::string& stsToken);
  248. protected:
  249. std::string mEndPoint;
  250. std::string mAccessId;
  251. std::string mAccessKey;
  252. std::string mStsToken;
  253. MNSConnectionToolPtr mMnsConnTool;
  254. };
  255. /*
  256. * access MNS Queue service
  257. *
  258. * CAUTION:
  259. * Make sure to catch Exception when calling any function
  260. * Queue throws MNSServerException when the request is failed.
  261. * Queue throws MNSExceptionBase for client errors
  262. */
  263. class Queue
  264. {
  265. public:
  266. virtual ~Queue() {}
  267. /* update the AccessId/AccessKey
  268. *
  269. * @param accessId: accessId from aliyun.com
  270. * @param accessKey: accessKey from aliyun.com
  271. */
  272. void updateAccessId(const std::string& accessId,
  273. const std::string& accessKey);
  274. /* update the AccessId/AccessKey/StsToken
  275. *
  276. * @param accessId: the sts accessId
  277. * @param accessKey: the sts accessKey
  278. * @param stsToken: the sts token
  279. */
  280. void updateAccessId(const std::string& accessId,
  281. const std::string& accessKey,
  282. const std::string& stsToken);
  283. const std::string& getQueueName()
  284. {
  285. return mQueueName;
  286. }
  287. /* get queue attributes
  288. *
  289. * @param attributes: the returned queue attributes
  290. */
  291. void getAttributes(QueueAttributes& attributes);
  292. /* set queue attributes
  293. *
  294. * @param attributes: the attributes to set
  295. */
  296. void setAttributes(const QueueAttributes& attributes);
  297. /* send one message
  298. *
  299. * @param messageBody: the message body
  300. * @param resp: the Response containing MessageId and BodyMD5
  301. */
  302. void sendMessage(const std::string& messageBody,
  303. SendMessageResponse& resp);
  304. /* send one message
  305. *
  306. * @param messageBody: the message body
  307. * @delaySeconds: the message cannot be received in DelaySeconds
  308. * @priority:
  309. * whether this message has a higher/lower priority, default is 8
  310. * @param resp: the Response containing MessageId and BodyMD5
  311. */
  312. void sendMessage(const std::string& messageBody,
  313. const int32_t delaySeconds,
  314. const int32_t priority,
  315. SendMessageResponse& resp);
  316. /* send a batch of messages
  317. *
  318. * @param sendMessageItems: the messages to send
  319. * @param resp: the Response containing MessageIds and BodyMD5s
  320. * 1. responses of sent messages are in mSendMessageSucceedItems
  321. * 2. check resp.isSuccess(), if not success, some messages are not sent
  322. * the failInfos are recorded in mSendMessageFailedItems
  323. */
  324. void batchSendMessage(const std::vector<SendMessageItem>& sendMessageItems,
  325. BatchSendMessageResponse& resp);
  326. /* peek one message, the message still can be received by others
  327. *
  328. * @param message: the peeked message
  329. */
  330. void peekMessage(Message& message);
  331. /* batch peek
  332. *
  333. * @param numOfMessages: the batch size
  334. * @param messages: the peeked messages
  335. */
  336. void batchPeekMessage(const int32_t numOfMessages,
  337. std::vector<Message>& messages);
  338. /* receive one active message,
  339. * the message is changed to be inactive.
  340. * message will become active after the "MessageVisibilityTimeout"
  341. * ChangeMessageVisibility could be used to change the "VisibilityTime"
  342. *
  343. * @param message: the received message
  344. */
  345. void receiveMessage(Message& message);
  346. /* receive one active message,
  347. * the message is changed to be inactive.
  348. * message will become active after the "MessageVisibilityTimeout"
  349. * ChangeMessageVisibility could be used to change the "VisibilityTime"
  350. *
  351. * @param waitSeconds:
  352. * if no message exist in the queue, the request will block for
  353. * "waitSeconds" util timeout or any message coming.
  354. * @param message: the received message
  355. */
  356. void receiveMessage(const int32_t waitSeconds,
  357. Message& message);
  358. /* receive active messages,
  359. * the message is changed to be inactive.
  360. * message will become active after the "MessageVisibilityTimeout"
  361. * ChangeMessageVisibility could be used to change the "VisibilityTime"
  362. *
  363. * @param numOfMessages: the batch size
  364. * @param messages: received messages
  365. */
  366. void batchReceiveMessage(const int32_t numOfMessages,
  367. std::vector<Message>& messages);
  368. /* receive active messages,
  369. * the message is changed to be inactive.
  370. * message will become active after the "MessageVisibilityTimeout"
  371. * ChangeMessageVisibility could be used to change the "VisibilityTime"
  372. *
  373. * @param numOfMessages: the batch size
  374. * @param waitSeconds:
  375. * if no message exist in the queue, the request will block for
  376. * "waitSeconds" util timeout or any message coming.
  377. * @param messages: received messages
  378. */
  379. void batchReceiveMessage(const int32_t numOfMessages,
  380. const int32_t waitSeconds,
  381. std::vector<Message>& messages);
  382. /* delete one inactive message,
  383. * after receiving the message, a ReceiptHandle is returned in Message
  384. * this ReceiptHandle is valid util the "VisibilityTimeout"
  385. * this ReceiptHandle could be used to delete the message
  386. *
  387. * @param receiptHandle: the ReceiptHandle
  388. */
  389. void deleteMessage(const std::string& receiptHandle);
  390. /* delete several inactive messages,
  391. * after receiving the message, a ReceiptHandle is returned in Message
  392. * this ReceiptHandle is valid util the "VisibilityTimeout"
  393. * this ReceiptHandle could be used to delete the message
  394. *
  395. * @param receiptHandles: the ReceiptHandles
  396. */
  397. void batchDeleteMessage(const std::vector<std::string>& receiptHandles,
  398. BatchDeleteMessageResponse& resp);
  399. /* change the visibilityTimeout for inactive message
  400. * after receiving the message, a ReceiptHandle is returned in Message
  401. * this ReceiptHandle is valid util the "VisibilityTimeout"
  402. * a valid ReceiptHandle could be used to change the "VisibilityTimeout"
  403. *
  404. * @param receiptHandle: the ReceiptHandle
  405. * @param visibilityTimeout:
  406. * the message will be active again after "visibilityTimeout" seconds
  407. */
  408. void changeMessageVisibility(const std::string& receiptHandle,
  409. const int32_t visibilityTimeout,
  410. ChangeMessageVisibilityResponse& resp);
  411. friend class MNSClient;
  412. protected:
  413. Queue(const std::string& queueName,
  414. const std::string& endpoint,
  415. const std::string& accessId,
  416. const std::string& accessKey,
  417. const std::string& stsToken,
  418. MNSConnectionToolPtr mnsConnTool);
  419. protected:
  420. std::string mQueueName;;
  421. std::string mEndPoint;
  422. std::string mAccessId;
  423. std::string mAccessKey;
  424. std::string mStsToken;
  425. MNSConnectionToolPtr mMnsConnTool;
  426. };
  427. /*
  428. * access MNS Topic service
  429. *
  430. * CAUTION:
  431. * Make sure to catch Exception when calling any function
  432. * Topic throws MNSServerException when the request is failed.
  433. * Topic throws MNSExceptionBase for client errors
  434. */
  435. class Topic
  436. {
  437. public:
  438. virtual ~Topic() {}
  439. /* update the AccessId/AccessKey
  440. *
  441. * @param accessId: accessId from aliyun.com
  442. * @param accessKey: accessKey from aliyun.com
  443. */
  444. void updateAccessId(const std::string& accessId,
  445. const std::string& accessKey);
  446. /* update the AccessId/AccessKey/StsToken
  447. *
  448. * @param accessId: the sts accessId
  449. * @param accessKey: the sts accessKey
  450. * @param stsToken: the sts token
  451. */
  452. void updateAccessId(const std::string& accessId,
  453. const std::string& accessKey,
  454. const std::string& stsToken);
  455. const std::string& getTopicName()
  456. {
  457. return mTopicName;
  458. }
  459. /* get topic attributes
  460. *
  461. * @param attributes: the returned topic attributes
  462. */
  463. void getAttributes(TopicAttributes& attributes);
  464. /* set optic attributes
  465. *
  466. * @param attributes: the attributes to set
  467. */
  468. void setAttributes(const TopicAttributes& attributes);
  469. /* publish one message to topic
  470. *
  471. * @param messageBody: the message body
  472. * @param resp: the Response containing MessageId and BodyMD5
  473. */
  474. void publishMessage(const std::string& messageBody,
  475. PublishMessageResponse& resp);
  476. /* publish one message to topic
  477. *
  478. * @param messageBody: the message body
  479. * @param messageAttributes: related message attributes
  480. * @param resp: the Response containing MessageId and BodyMD5
  481. */
  482. void publishMessage(const std::string& messageBody,
  483. const MessageAttributes& messageAttributes,
  484. PublishMessageResponse& resp);
  485. /* publish one message to topic with messageTag
  486. *
  487. * @param messageBody: the message body
  488. * @param messageTag: the message Tag
  489. * @param resp: the Response containing MessageId and BodyMD5
  490. */
  491. void publishMessage(const std::string& messageBody,
  492. const std::string& messageTag,
  493. PublishMessageResponse& resp);
  494. /* publish one message to topic with messageTag
  495. *
  496. * @param messageBody: the message body
  497. * @param messageTag: the message Tag
  498. * @param messageAttributes: related message attributes
  499. * @param resp: the Response containing MessageId and BodyMD5
  500. */
  501. void publishMessage(const std::string& messageBody,
  502. const std::string& messageTag,
  503. const MessageAttributes& messageAttributes,
  504. PublishMessageResponse& resp);
  505. /** generate queue endpoint for subscription
  506. *
  507. * @param queueName: mns queue name
  508. * @return: mns queue endpoint
  509. */
  510. std::string generateQueueEndpoint(const std::string& queueName);
  511. /** generate queue endpoint for subscription
  512. *
  513. * @param queueName: mns queue name
  514. * @param region: mns region in endpoint url, such as cn-hangzhou, cn-shenzhen, cn-beijing, ...
  515. * @return: mns queue endpoint
  516. */
  517. std::string generateQueueEndpoint(const std::string& queueName, const std::string& region);
  518. /** generate mail endpoint for subscription
  519. *
  520. * @param mailAddress: mail address for receiving mails
  521. * @return: mns mail endpoint
  522. */
  523. std::string generateMailEndpoint(const std::string& mailAddress);
  524. /** generate sms endpoint for subscription
  525. *
  526. * @param phone: phone number for receiving sms, could be empty
  527. * (empty phone means that the number could be specified in SMSAttributes)
  528. * @return: mns sms endpoint
  529. */
  530. std::string generateSMSEndpoint(const std::string& phone = "");
  531. /* subscribe to topic
  532. *
  533. * @param subscriptionName: the unique name for the subscription
  534. * @param endPoint: the subscription endpoint
  535. */
  536. void subscribe(const std::string& subscriptionName,
  537. const std::string& endPoint);
  538. /* subscribe to topic
  539. *
  540. * @param subscriptionName: the unique name for the subscription
  541. * @param endPoint: the subscription endpoint
  542. * @param attributes: the subscription attributes
  543. */
  544. void subscribe(const std::string& subscriptionName,
  545. const std::string& endPoint,
  546. const SubscriptionAttributes& attributes);
  547. /* subscribe to topic with fiterTag
  548. *
  549. * @param subscriptionName: the unique name for the subscription
  550. * @param filterTag: the filterTag for the subscription
  551. * @param endPoint: the subscription endpoint
  552. */
  553. void subscribe(const std::string& subscriptionName,
  554. const std::string& filterTag,
  555. const std::string& endPoint);
  556. /* subscribe to topic with fiterTag
  557. *
  558. * @param subscriptionName: the unique name for the subscription
  559. * @param filterTag: the filterTag for the subscription
  560. * @param endPoint: the subscription endpoint
  561. * @param attributes: the subscription attributes
  562. */
  563. void subscribe(const std::string& subscriptionName,
  564. const std::string& filterTag,
  565. const std::string& endPoint,
  566. const SubscriptionAttributes& attributes);
  567. /* change the subscription attributes
  568. *
  569. * @param subscriptionName: name of the subscription to change
  570. * @param attributes: the attributes to set
  571. */
  572. void setSubscriptionAttributes(const std::string& subscriptionName,
  573. const SubscriptionAttributes& attributes);
  574. /* get the subscription attributes
  575. *
  576. * @param subscriptionName: name of the subscription
  577. * @param attributes: the returned attributes
  578. */
  579. void getSubscriptionAttributes(const std::string& subscriptionName,
  580. SubscriptionAttributes& attributes);
  581. /* Unsubscribe
  582. *
  583. * @param subscriptionName: name of the subscription
  584. */
  585. void unsubscribe(const std::string& subscriptionName);
  586. /* List the subscriptions under current topic
  587. *
  588. * @param prefix: the prefix of subscriptionName
  589. * @param subscriptionNames: the subscription names
  590. */
  591. void listSubscription(const std::string& prefix,
  592. std::vector<std::string>& subscriptionNames);
  593. /* List the subscriptions under current topic
  594. *
  595. * @param prefix: the prefix of subscriptionName
  596. * @param subscriptionNames: the subscription names
  597. * @param nextMarker: the page marker for next listSubscription
  598. */
  599. void listSubscription(const std::string& prefix,
  600. std::vector<std::string>& subscriptionNames,
  601. std::string& nextMarker);
  602. /* List the subscriptions under current topic
  603. *
  604. * @param prefix: the prefix of subscriptionName
  605. * @param subscriptionNames: the subscription names
  606. */
  607. void listSubscription(ListSubscriptionRequest& request,
  608. std::vector<std::string>& subscriptionNames);
  609. /* List the subscriptions under current topic
  610. *
  611. * @param prefix: the prefix of subscriptionName
  612. * @param subscriptionNames: the subscription names
  613. * @param nextMarker: the page marker for next listSubscription
  614. */
  615. void listSubscription(ListSubscriptionRequest& request,
  616. std::vector<std::string>& subscriptionNames,
  617. std::string& nextMarker);
  618. friend class MNSClient;
  619. protected:
  620. Topic(const std::string& topicName,
  621. const std::string& endpoint,
  622. const std::string& accessId,
  623. const std::string& accessKey,
  624. const std::string& stsToken,
  625. MNSConnectionToolPtr mnsConnTool);
  626. protected:
  627. std::string mTopicName;
  628. std::string mEndPoint;
  629. std::string mAccessId;
  630. std::string mAccessKey;
  631. std::string mStsToken;
  632. MNSConnectionToolPtr mMnsConnTool;
  633. std::string mRegion;
  634. std::string mAccountId;
  635. };
  636. }
  637. }
  638. #endif