| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> |
| <title>mqttpp: mqtt/token.h Source File</title> |
| <link href="tabs.css" rel="stylesheet" type="text/css"/> |
| <link href="doxygen.css" rel="stylesheet" type="text/css"/> |
| </head> |
| <body> |
| <!-- Generated by Doxygen 1.6.1 --> |
| <div class="navigation" id="top"> |
| <div class="tabs"> |
| <ul> |
| <li><a href="main.html"><span>Main Page</span></a></li> |
| <li><a href="annotated.html"><span>Classes</span></a></li> |
| <li class="current"><a href="files.html"><span>Files</span></a></li> |
| </ul> |
| </div> |
| <div class="tabs"> |
| <ul> |
| <li><a href="files.html"><span>File List</span></a></li> |
| </ul> |
| </div> |
| <h1>mqtt/token.h</h1><a href="token_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 |
| <a name="l00002"></a>00002 |
| <a name="l00003"></a>00003 |
| <a name="l00004"></a>00004 |
| <a name="l00005"></a>00005 |
| <a name="l00006"></a>00006 |
| <a name="l00007"></a>00007 |
| <a name="l00008"></a>00008 <span class="comment">/*******************************************************************************</span> |
| <a name="l00009"></a>00009 <span class="comment"> * Copyright (c) 2013 Frank Pagliughi <fpagliughi@mindspring.com></span> |
| <a name="l00010"></a>00010 <span class="comment"> *</span> |
| <a name="l00011"></a>00011 <span class="comment"> * All rights reserved. This program and the accompanying materials</span> |
| <a name="l00012"></a>00012 <span class="comment"> * are made available under the terms of the Eclipse Public License v1.0</span> |
| <a name="l00013"></a>00013 <span class="comment"> * and Eclipse Distribution License v1.0 which accompany this distribution. </span> |
| <a name="l00014"></a>00014 <span class="comment"> *</span> |
| <a name="l00015"></a>00015 <span class="comment"> * The Eclipse Public License is available at </span> |
| <a name="l00016"></a>00016 <span class="comment"> * http://www.eclipse.org/legal/epl-v10.html</span> |
| <a name="l00017"></a>00017 <span class="comment"> * and the Eclipse Distribution License is available at </span> |
| <a name="l00018"></a>00018 <span class="comment"> * http://www.eclipse.org/org/documents/edl-v10.php.</span> |
| <a name="l00019"></a>00019 <span class="comment"> *</span> |
| <a name="l00020"></a>00020 <span class="comment"> * Contributors:</span> |
| <a name="l00021"></a>00021 <span class="comment"> * Frank Pagliughi - initial implementation and documentation</span> |
| <a name="l00022"></a>00022 <span class="comment"> *******************************************************************************/</span> |
| <a name="l00023"></a>00023 |
| <a name="l00024"></a>00024 <span class="preprocessor">#ifndef __mqtt_token_h</span> |
| <a name="l00025"></a>00025 <span class="preprocessor"></span><span class="preprocessor">#define __mqtt_token_h</span> |
| <a name="l00026"></a>00026 <span class="preprocessor"></span> |
| <a name="l00027"></a>00027 <span class="keyword">extern</span> <span class="stringliteral">"C"</span> { |
| <a name="l00028"></a>00028 <span class="preprocessor"> #include "MQTTAsync.h"</span> |
| <a name="l00029"></a>00029 } |
| <a name="l00030"></a>00030 |
| <a name="l00031"></a>00031 <span class="preprocessor">#include "<a class="code" href="iaction__listener_8h.html" title="Declaration of MQTT iaction_listener class.">mqtt/iaction_listener.h</a>"</span> |
| <a name="l00032"></a>00032 <span class="preprocessor">#include "<a class="code" href="exception_8h.html" title="Declaration of MQTT exception class.">mqtt/exception.h</a>"</span> |
| <a name="l00033"></a>00033 <span class="preprocessor">#include <string></span> |
| <a name="l00034"></a>00034 <span class="preprocessor">#include <vector></span> |
| <a name="l00035"></a>00035 <span class="preprocessor">#include <memory></span> |
| <a name="l00036"></a>00036 <span class="preprocessor">#include <thread></span> |
| <a name="l00037"></a>00037 <span class="preprocessor">#include <mutex></span> |
| <a name="l00038"></a>00038 <span class="preprocessor">#include <condition_variable></span> |
| <a name="l00039"></a>00039 <span class="preprocessor">#include <chrono></span> |
| <a name="l00040"></a>00040 |
| <a name="l00041"></a>00041 <span class="keyword">namespace </span>mqtt { |
| <a name="l00042"></a>00042 |
| <a name="l00043"></a>00043 <span class="keyword">class </span>iasync_client; |
| <a name="l00044"></a>00044 |
| <a name="l00046"></a>00046 |
| <a name="l00050"></a><a class="code" href="classmqtt_1_1itoken.html">00050</a> <span class="keyword">class </span><a class="code" href="classmqtt_1_1itoken.html" title="Provides a mechanism for tracking the completion of an asynchronous task.">itoken</a> |
| <a name="l00051"></a>00051 { |
| <a name="l00052"></a>00052 <span class="keyword">public</span>: |
| <a name="l00053"></a>00053 <span class="keyword">typedef</span> std::shared_ptr<itoken> ptr_t; |
| <a name="l00057"></a><a class="code" href="classmqtt_1_1itoken.html#abcf07d886eaea295702ae44ae32c9110">00057</a> <span class="keyword">virtual</span> <a class="code" href="classmqtt_1_1itoken.html#abcf07d886eaea295702ae44ae32c9110" title="Virtual base destructor.">~itoken</a>() {} |
| <a name="l00062"></a>00062 <span class="keyword">virtual</span> <a class="code" href="classmqtt_1_1iaction__listener.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">iaction_listener</a>* <a class="code" href="classmqtt_1_1itoken.html#afb9395dcf53aa306368b7dfa7e7000a5" title="Return the async listener for this token.">get_action_callback</a>() <span class="keyword">const</span> =0; |
| <a name="l00068"></a>00068 <span class="keyword">virtual</span> <a class="code" href="classmqtt_1_1iasync__client.html" title="Enables an application to communicate with an MQTT server using non-blocking methods...">iasync_client</a>* <a class="code" href="classmqtt_1_1itoken.html#a458008aba7949bdfe21c044b24c49721" title="Returns the MQTT client that is responsible for processing the asynchronous action...">get_client</a>() <span class="keyword">const</span> =0; |
| <a name="l00073"></a>00073 <span class="comment">//virtual exception get_exception() =0;</span> |
| <a name="l00079"></a>00079 <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">int</span> <a class="code" href="classmqtt_1_1itoken.html#a4bfa1320ee0000d0a54fd40ceed24658" title="Returns an exception providing more detail if an operation failed.">get_message_id</a>() <span class="keyword">const</span> =0; |
| <a name="l00085"></a>00085 <span class="keyword">virtual</span> <span class="keyword">const</span> std::vector<std::string>& <a class="code" href="classmqtt_1_1itoken.html#a393ebf9a6d82d1a093273c9a04a41979" title="Returns the topic string(s) for the action being tracked by this token.">get_topics</a>() <span class="keyword">const</span> =0; |
| <a name="l00090"></a>00090 <span class="keyword">virtual</span> <span class="keywordtype">void</span>* <a class="code" href="classmqtt_1_1itoken.html#a01017690ed6efb016700813495c05079" title="Retrieve the context associated with an action.">get_user_context</a>() <span class="keyword">const</span> =0; |
| <a name="l00095"></a>00095 <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code" href="classmqtt_1_1itoken.html#a99af88ac59f2ed5cb696b4566f58d6d2" title="Returns whether or not the action has finished.">is_complete</a>() <span class="keyword">const</span> =0; |
| <a name="l00100"></a>00100 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1itoken.html#aef0ed9396b89b2159a4a103a492fbc2e" title="Register a listener to be notified when an action completes.">set_action_callback</a>(<a class="code" href="classmqtt_1_1iaction__listener.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">iaction_listener</a>& listener) =0; |
| <a name="l00105"></a>00105 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1itoken.html#a777df44320b48c494c3821f314d70f7a" title="Store some context associated with an action.">set_user_context</a>(<span class="keywordtype">void</span>* userContext) =0; |
| <a name="l00110"></a>00110 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1itoken.html#a18517a2721ab65fb28d2b3fe6475fa2b" title="Blocks the current thread until the action this token is associated with has completed...">wait_for_completion</a>() =0; |
| <a name="l00116"></a>00116 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1itoken.html#a18517a2721ab65fb28d2b3fe6475fa2b" title="Blocks the current thread until the action this token is associated with has completed...">wait_for_completion</a>(<span class="keywordtype">long</span> timeout) =0; |
| <a name="l00117"></a>00117 }; |
| <a name="l00118"></a>00118 |
| <a name="l00119"></a>00119 <span class="keyword">typedef</span> itoken::ptr_t itoken_ptr; |
| <a name="l00120"></a>00120 |
| <a name="l00122"></a>00122 |
| <a name="l00127"></a><a class="code" href="classmqtt_1_1token.html">00127</a> <span class="keyword">class </span><a class="code" href="classmqtt_1_1token.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">token</a> : <span class="keyword">public</span> <span class="keyword">virtual</span> <a class="code" href="classmqtt_1_1itoken.html" title="Provides a mechanism for tracking the completion of an asynchronous task.">itoken</a> |
| <a name="l00128"></a>00128 { |
| <a name="l00130"></a>00130 <span class="keyword">typedef</span> std::unique_lock<std::mutex> guard; |
| <a name="l00131"></a>00131 |
| <a name="l00133"></a>00133 <span class="keyword">mutable</span> std::mutex lock_; |
| <a name="l00135"></a>00135 <span class="keyword">mutable</span> std::condition_variable cond_; |
| <a name="l00137"></a>00137 MQTTAsync_token tok_; |
| <a name="l00139"></a>00139 std::vector<std::string> topics_; |
| <a name="l00141"></a>00141 <a class="code" href="classmqtt_1_1iasync__client.html" title="Enables an application to communicate with an MQTT server using non-blocking methods...">iasync_client</a>* cli_; |
| <a name="l00143"></a>00143 <span class="keywordtype">void</span>* userContext_; |
| <a name="l00149"></a>00149 <a class="code" href="classmqtt_1_1iaction__listener.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">iaction_listener</a>* listener_; |
| <a name="l00151"></a>00151 <span class="keywordtype">bool</span> complete_; |
| <a name="l00153"></a>00153 <span class="keywordtype">int</span> rc_; |
| <a name="l00154"></a>00154 |
| <a name="l00156"></a><a class="code" href="classmqtt_1_1token.html#ac39954340d8b15edaa883af1bcfcf7ae">00156</a> <span class="keyword">friend</span> <span class="keyword">class </span><a class="code" href="classmqtt_1_1async__client.html" title="Lightweight client for talking to an MQTT server using non-blocking methods that...">async_client</a>; |
| <a name="l00157"></a>00157 |
| <a name="l00158"></a>00158 <span class="keywordtype">void</span> set_topics(<span class="keyword">const</span> std::string& top) { |
| <a name="l00159"></a>00159 topics_.clear(); |
| <a name="l00160"></a>00160 topics_.push_back(top); |
| <a name="l00161"></a>00161 } |
| <a name="l00162"></a>00162 <span class="keywordtype">void</span> set_topics(<span class="keyword">const</span> std::vector<std::string>& top) { |
| <a name="l00163"></a>00163 topics_ = top; |
| <a name="l00164"></a>00164 } |
| <a name="l00165"></a>00165 |
| <a name="l00175"></a>00175 <span class="keyword">static</span> <span class="keywordtype">void</span> on_success(<span class="keywordtype">void</span>* tokObj, MQTTAsync_successData* rsp); |
| <a name="l00185"></a>00185 <span class="keyword">static</span> <span class="keywordtype">void</span> on_failure(<span class="keywordtype">void</span>* tokObj, MQTTAsync_failureData* rsp); |
| <a name="l00190"></a>00190 <span class="keywordtype">void</span> on_success(MQTTAsync_successData* rsp); |
| <a name="l00195"></a>00195 <span class="keywordtype">void</span> on_failure(MQTTAsync_failureData* rsp); |
| <a name="l00196"></a>00196 |
| <a name="l00197"></a>00197 <span class="keyword">public</span>: |
| <a name="l00198"></a>00198 <span class="keyword">typedef</span> std::shared_ptr<token> ptr_t; |
| <a name="l00203"></a>00203 <a class="code" href="classmqtt_1_1token.html#a5d479b98ffa8efdd8e85c0d647f02a15" title="Constructs a token object.">token</a>(iasync_client& cli); |
| <a name="l00208"></a>00208 <a class="code" href="classmqtt_1_1token.html#a5d479b98ffa8efdd8e85c0d647f02a15" title="Constructs a token object.">token</a>(iasync_client& cli, MQTTAsync_token tok); |
| <a name="l00213"></a>00213 <a class="code" href="classmqtt_1_1token.html#a5d479b98ffa8efdd8e85c0d647f02a15" title="Constructs a token object.">token</a>(iasync_client& cli, <span class="keyword">const</span> std::string& topic); |
| <a name="l00218"></a>00218 <a class="code" href="classmqtt_1_1token.html#a5d479b98ffa8efdd8e85c0d647f02a15" title="Constructs a token object.">token</a>(iasync_client& cli, <span class="keyword">const</span> std::vector<std::string>& topics); |
| <a name="l00219"></a>00219 |
| <a name="l00220"></a>00220 <span class="comment">//token(const std::string& logContext);</span> |
| <a name="l00221"></a>00221 |
| <a name="l00226"></a><a class="code" href="classmqtt_1_1token.html#aad3c92d4e1be659101e118fc11daf963">00226</a> <span class="keyword">virtual</span> <a class="code" href="classmqtt_1_1iaction__listener.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">iaction_listener</a>* <a class="code" href="classmqtt_1_1token.html#aad3c92d4e1be659101e118fc11daf963" title="Return the async listener for this token.">get_action_callback</a>()<span class="keyword"> const </span>{ |
| <a name="l00227"></a>00227 <span class="comment">// TODO: Guard?</span> |
| <a name="l00228"></a>00228 <span class="keywordflow">return</span> listener_; |
| <a name="l00229"></a>00229 } |
| <a name="l00235"></a><a class="code" href="classmqtt_1_1token.html#ab23933fa6d8db2501c976b79df91d1cb">00235</a> <span class="keyword">virtual</span> <a class="code" href="classmqtt_1_1iasync__client.html" title="Enables an application to communicate with an MQTT server using non-blocking methods...">iasync_client</a>* <a class="code" href="classmqtt_1_1token.html#ab23933fa6d8db2501c976b79df91d1cb" title="Returns the MQTT client that is responsible for processing the asynchronous action...">get_client</a>()<span class="keyword"> const </span>{ |
| <a name="l00236"></a>00236 <span class="keywordflow">return</span> (<a class="code" href="classmqtt_1_1iasync__client.html" title="Enables an application to communicate with an MQTT server using non-blocking methods...">iasync_client</a>*) cli_; |
| <a name="l00237"></a>00237 } |
| <a name="l00242"></a>00242 <span class="comment">//virtual exception get_exception();</span> |
| <a name="l00248"></a><a class="code" href="classmqtt_1_1token.html#a5bab8b8df3d9f04e75e9c57a6c36e93c">00248</a> <span class="comment"></span> <span class="keyword">virtual</span> <span class="keywordtype">int</span> <a class="code" href="classmqtt_1_1token.html#a5bab8b8df3d9f04e75e9c57a6c36e93c" title="Returns an exception providing more detail if an operation failed.">get_message_id</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> int(tok_); } |
| <a name="l00253"></a><a class="code" href="classmqtt_1_1token.html#a55d8798a9def10c8f714953b696ad3e2">00253</a> <span class="keyword">virtual</span> <span class="keyword">const</span> std::vector<std::string>& <a class="code" href="classmqtt_1_1token.html#a55d8798a9def10c8f714953b696ad3e2" title="Returns the topic string(s) for the action being tracked by this token.">get_topics</a>()<span class="keyword"> const </span>{ |
| <a name="l00254"></a>00254 <span class="keywordflow">return</span> topics_; |
| <a name="l00255"></a>00255 } |
| <a name="l00259"></a><a class="code" href="classmqtt_1_1token.html#a8fb4c13db17d0861b17cbe220b0696de">00259</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span>* <a class="code" href="classmqtt_1_1token.html#a8fb4c13db17d0861b17cbe220b0696de" title="Retrieve the context associated with an action.">get_user_context</a>()<span class="keyword"> const </span>{ |
| <a name="l00260"></a>00260 guard g(lock_); |
| <a name="l00261"></a>00261 <span class="keywordflow">return</span> userContext_; |
| <a name="l00262"></a>00262 } |
| <a name="l00267"></a><a class="code" href="classmqtt_1_1token.html#a57c8c9e1f7c404a023022eba9dbcc0c2">00267</a> <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code" href="classmqtt_1_1token.html#a57c8c9e1f7c404a023022eba9dbcc0c2" title="Returns whether or not the action has finished.">is_complete</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> complete_; } |
| <a name="l00272"></a><a class="code" href="classmqtt_1_1token.html#ad916dc92354e24c17969334d84a119d6">00272</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1token.html#ad916dc92354e24c17969334d84a119d6" title="Register a listener to be notified when an action completes.">set_action_callback</a>(<a class="code" href="classmqtt_1_1iaction__listener.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">iaction_listener</a>& listener) { |
| <a name="l00273"></a>00273 guard g(lock_); |
| <a name="l00274"></a>00274 listener_ = &listener; |
| <a name="l00275"></a>00275 } |
| <a name="l00280"></a><a class="code" href="classmqtt_1_1token.html#a5faa55b04ee75f18df7b019bf443b1a0">00280</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1token.html#a5faa55b04ee75f18df7b019bf443b1a0" title="Store some context associated with an action.">set_user_context</a>(<span class="keywordtype">void</span>* userContext) { |
| <a name="l00281"></a>00281 guard g(lock_); |
| <a name="l00282"></a>00282 userContext_ = userContext; |
| <a name="l00283"></a>00283 } |
| <a name="l00288"></a>00288 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1token.html#ac4e39b9656105835e8b0eba20b3b2164" title="Blocks the current thread until the action this token is associated with has completed...">wait_for_completion</a>(); |
| <a name="l00294"></a>00294 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classmqtt_1_1token.html#ac4e39b9656105835e8b0eba20b3b2164" title="Blocks the current thread until the action this token is associated with has completed...">wait_for_completion</a>(<span class="keywordtype">long</span> timeout); |
| <a name="l00301"></a>00301 <span class="keyword">template</span> <<span class="keyword">class</span> Rep, <span class="keyword">class</span> Period> |
| <a name="l00302"></a><a class="code" href="classmqtt_1_1token.html#a688d9101d2a1370871ba9a960a4fd0c6">00302</a> <span class="keywordtype">bool</span> <a class="code" href="classmqtt_1_1token.html#a688d9101d2a1370871ba9a960a4fd0c6" title="Waits a relative amount of time for the action to complete.">wait_for_completion</a>(<span class="keyword">const</span> std::chrono::duration<Rep, Period>& relTime) { |
| <a name="l00303"></a>00303 <a class="code" href="classmqtt_1_1token.html#ac4e39b9656105835e8b0eba20b3b2164" title="Blocks the current thread until the action this token is associated with has completed...">wait_for_completion</a>((<span class="keywordtype">long</span>) std::chrono::duration_cast<std::chrono::milliseconds>(relTime).count()); |
| <a name="l00304"></a>00304 <span class="keywordflow">return</span> rc_ == 0; |
| <a name="l00305"></a>00305 } |
| <a name="l00312"></a>00312 <span class="keyword">template</span> <<span class="keyword">class</span> Clock, <span class="keyword">class</span> Duration> |
| <a name="l00313"></a><a class="code" href="classmqtt_1_1token.html#a27d4518d8962405c9f883761c0bf4e1c">00313</a> <span class="keywordtype">bool</span> <a class="code" href="classmqtt_1_1token.html#a27d4518d8962405c9f883761c0bf4e1c" title="Waits until an absolute time for the action to complete.">wait_until_completion</a>(<span class="keyword">const</span> std::chrono::time_point<Clock, Duration>& absTime) { |
| <a name="l00314"></a>00314 guard g(lock_); |
| <a name="l00315"></a>00315 <span class="keywordflow">if</span> (!cond_.wait_until(g, absTime, [<span class="keyword">this</span>]{<span class="keywordflow">return</span> complete_;})) |
| <a name="l00316"></a>00316 <span class="keywordflow">return</span> <span class="keyword">false</span>; |
| <a name="l00317"></a>00317 <span class="keywordflow">if</span> (rc_ != MQTTASYNC_SUCCESS) |
| <a name="l00318"></a>00318 <span class="keywordflow">throw</span> <a class="code" href="classmqtt_1_1exception.html" title="Provides a mechanism for tracking the completion of an asynchronous action.">exception</a>(rc_); |
| <a name="l00319"></a>00319 <span class="keywordflow">return</span> <span class="keyword">true</span>; |
| <a name="l00320"></a>00320 } |
| <a name="l00321"></a>00321 |
| <a name="l00322"></a>00322 }; |
| <a name="l00323"></a>00323 |
| <a name="l00324"></a>00324 <span class="keyword">typedef</span> token::ptr_t token_ptr; |
| <a name="l00325"></a>00325 |
| <a name="l00327"></a>00327 <span class="comment">// end namespace mqtt</span> |
| <a name="l00328"></a>00328 } |
| <a name="l00329"></a>00329 |
| <a name="l00330"></a>00330 <span class="preprocessor">#endif // __mqtt_token_h</span> |
| <a name="l00331"></a>00331 <span class="preprocessor"></span> |
| </pre></div></div> |
| <hr size="1"/><address style="text-align: right;"><small>Generated on 30 Apr 2014 for mqttpp by |
| <a href="http://www.doxygen.org/index.html"> |
| <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address> |
| </body> |
| </html> |