<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/mqtt-development-log-on-dsls-rcpp-modules-and-custom-formula-functions/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/12/17/mqtt-development-log-on-dsls-rcpp-modules-and-custom-formula-functions/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I know some folks had a bit of fun with the previous post since it exposed the fact that I left out unique MQTT client id generation from the initial 0.1.0 release of the in-development package (client ids <a rel="nofollow" href="https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.1.0/com.ibm.mq.doc/tt60310_.htm" target="_blank">need to be unique</a>).</p>
<p>There have been some serious improvements since said post and I thought a (hopefully not-too-frequent) blog-journal of the development of this particular package might be interesting/useful to some folks, especially since I’m delving into some not-oft-blogged (anywhere) topics as I use some new tricks in this particular package.</p>
<h2>Thank The Great Maker for C++</h2>
<p>I’m comfortable and not-too-shabby at wrapping C/C++ things with an R bow and I felt quite daft seeing <a rel="nofollow" href="https://github.com/eclipse/mosquitto/blob/master/lib/cpp/mosquittopp.h" target="_blank">this</a> after I had started banging on the <code>mosquitto</code> C interface. Yep, that’s right: it has a C++ interface. It’s waaaaay easier (in my experience) bridging C++ libraries since Dirk/Romain’s (et al, as I know there are <a rel="nofollow" href="https://github.com/RcppCore/Rcpp/graphs/contributors" target="_blank">many worker hands</a> involved as well) <code>Rcpp</code> has <em>so many tools</em> available to do that very thing. </p>
<blockquote>
<p><em>As an aside, if you do any work with Rcpp or want to start doing work in Rcpp, Masaki E. Tsuda’s <a rel="nofollow" href="https://teuder.github.io/rcpp4everyone_en/" target="_blank">Rcpp for Everyone</a> is an <strong>excellent</strong> tome.</em></p>
</blockquote>
<p>I hadn’t used <a rel="nofollow" href="http://jamescurran.co.nz/2017/01/eg-html/" target="_blank">Rcpp Modules</a> before (that link goes to a succinct but very helpful post by James Curran) but they make exposing C++ library functionality even easier than I had experienced before. So easy, in fact, that it made it possible to whip out an alpha version of a “domain specific language” (or a pipe-able, customized API — however you want to frame these things in your head) for the package. But, I’m getting ahead of myself.</p>
<p>The <code>mosquittopp</code> class in the <code>mosqpp</code> namespace is much like the third bowl of porridge: just right. It’s neither too low-level nor too high-level and it was well thought out enough that it only required a bit of tweaking to use as an Rcpp Module.</p>
<p>First there are more than a few <code>char *</code> parameters that needed to be <code>std::string</code>s. So, something like:</p>
<pre><code>int username_pw_set(const char *username, const char *password);</code></pre>
<p>becomes:</p>
<pre><code>int username_pw_set(std::string username, std::string password);</code></pre>
<p>in our custom wrapper class.</p>
<p>Since the whole point of the <code>mqtt</code> package is to work in R vs C[++] or any other language, the callbacks — the functions that do the work when <code>message</code>, <code>publish</code>, <code>subscribe</code>, etc. events are triggered — need to be in R. I wanted to have some default callbacks during the testing phase and they’re really straightforward to setup in <code>Rcpp</code>:</p>
<pre><code>Rcpp::Environment pkg_env = Rcpp::Environment::namespace_env(&quot;mqtt&quot;);

Rcpp::Function ccb = pkg_env[&quot;.mqtt_connect_cb&quot;];
Rcpp::Function dcb = pkg_env[&quot;.mqtt_disconnect_cb&quot;];
Rcpp::Function pcb = pkg_env[&quot;.mqtt_publish_cb&quot;];
Rcpp::Function mcb = pkg_env[&quot;.mqtt_message_cb&quot;];
Rcpp::Function scb = pkg_env[&quot;.mqtt_subscribe_cb&quot;];
Rcpp::Function ucb = pkg_env[&quot;.mqtt_unsubscribe_cb&quot;];
Rcpp::Function lcb = pkg_env[&quot;.mqtt_log_cb&quot;];
Rcpp::Function ecb = pkg_env[&quot;.mqtt_error_cb&quot;];</code></pre>
<p>The handy thing about that approach is you don’t need to export the functions (it works like the <code>:::</code> does).</p>
<p>But the kicker is the Rcpp Module syntactic sugar:</p>
<pre><code>RCPP_MODULE(MQTT) {

  using namespace Rcpp;

  class_</code></pre>