<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.freelan.org &#187; &#187; CppCms</title>
	<atom:link href="https://blog.freelan.org/tag/cppcms/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.freelan.org</link>
	<description>De l&#039;informatique, des octets et des poneys.</description>
	<lastBuildDate>Fri, 04 Apr 2014 17:34:59 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Développement Web en C++</title>
		<link>https://blog.freelan.org/2012/11/11/developpement-web-en-c/</link>
		<comments>https://blog.freelan.org/2012/11/11/developpement-web-en-c/#comments</comments>
		<pubDate>Sun, 11 Nov 2012 13:13:17 +0000</pubDate>
		<dc:creator><![CDATA[Damien Buhl]]></dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Développement]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[CppCms]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.freelan.org/?p=457</guid>
		<description><![CDATA[En attendant MeetingCpp j&#8217;ai commencé cet article, car j&#8217;étais étonné qu&#8217;il n&#8217;y ait aucune session prévue au sujet du développement web en C++. Je le finalise ce soir en rentrant de cette magnifique conférence, afin de me donner le temps de faire de mes nombreuses notes de véritables articles sur les sujets abordés et discutés. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>En attendant MeetingCpp j&#8217;ai commencé cet article, car j&#8217;étais étonné qu&#8217;il n&#8217;y ait aucune session prévue au sujet du développement web en C++. Je le finalise ce soir en rentrant de cette magnifique conférence, afin de me donner le temps de faire de mes nombreuses notes de véritables articles sur les sujets abordés et discutés.</p>
<p>En effet on ne trouve pas des masses de tutoriaux, ni de solutions pour le développement web en C++, du fait de la prédominances des langages tels que Php, Spring, Django, Ruby on Rails etc. dans ce domaine.</p>
<p>J&#8217;aimerai présenter dans cet article une des solutions existantes en tant que framework de développement web, permettant de parvenir très rapidement à de bons résultats, comme on peut y être habitué avec CodeIgniter, Zend, Django ou Ruby on Rails, mais avec un gain de performance.</p>
<h1>Pourquoi du développement web en C++ ?</h1>
<p>Avant de rentrer dans des détails d&#8217;implémentation, il est possible de se poser la question pourquoi du dévéloppement web en C++ ? En effet avec la pléthore de langages et de frameworks existants destinés à ce problème, on pourrait penser que cela n&#8217;a pas d&#8217;autre intérêt que la préférence de ce langage.</p>
<p>En réalité, il y a trois raisons qui peuvent mener à choisir de développer une application web ou un site internet en C++ :</p>
<ul>
<li>Hautes performances &amp; haute disponibilité : Benchmark <a href="http://cppcms.com/wikipp/en/page/benchmarks_all">CppCms vs Java, C#, PHP</a></li>
<li>Réduction du nombre de serveurs, ayant pour conséquence la réduction des coûts et de l&#8217;impact sur l&#8217;environnement : <a href="https://developers.facebook.com/blog/post/2010/02/02/hiphop-for-php--move-fast/">HipHop for Php: Move Fast</a></li>
<li>Ajout d&#8217;une interface web (html ou web service) pour une base C++ existante</li>
</ul>
<h1>CppCms parmi d&#8217;autres solutions</h1>
<p>J&#8217;ai eu le loisir de tester et de comparer différentes solutions (<em>i.e.</em> POCO HttpServer, CppCms, FastCgiQt, WebToolkit, QtWebApp, inetd&#8230;) et bien qu&#8217;il y ait différents avantages et inconvénients pour chaque solution, la librairie la plus flexible et la plus riche qu&#8217;il m&#8217;ait été donnée de trouver est CppCms.</p>
<p>CppCms est bel et bien un framework et non un système de gestion de contenu comme son nom souhaiterait l&#8217;indiquer, il est très semblable au framework Django en Python ou encore à CodeIgniter en PHP, n&#8217;ayant pas grand chose à envier à ces derniers en matières de fonctionnalités disponibles (<em>i.e.</em> Moteur de template statique/dynamique, systèmes de cache, json, jsonrpc, url mapping, génération/validation de forms HTML, modules multilingues/multiencodage, validation des entrées, fonctions cryptographiques/hash, gestion des sessions, gestion des vues, logging, accès aux bases de données avec cppdb, connexion avec apache/lighttpd/nginx via SCGI/FCGI ou HTTP&#8230;)</p>
<p>En addition le code et la documentation de CppCms est très claire, à tel point que <a href="http://www.boost.org/doc/libs/1_52_0/libs/locale/doc/html/index.html">Boost.Locale</a> qui provient de CppCms a été accepté très rapidement.</p>
<h1>Boost ou booster ?</h1>
<p>CppCms peut laisser un peu sceptique au premier abord, car ce dernier contient une bibliothèque : booster, qui correspond à une petite partie de boost sur lequel le framework se base en addition à des extensions. Il est donc possible d&#8217;utiliser boost ou directement booster dans le code client ou de mélanger les deux sans que cela ne pose problème. Il y a des parties de booster qui font double emploi avec boost tel que <em>booster::shared_ptr</em> qu&#8217;il m&#8217;est impossible de justifier, mais d&#8217;autres qui sont très justifiées tel que <em>booster::aio</em> et qui se différencie de <em>Boost.Asio</em> par différents points abordés plus après.</p>
<h1>CppCms : Télécharger &amp; Installer</h1>
<p>Je pars du principe que vous êtes dans un environnement unix, même si les étapes sont semblables sous windows.</p>
<ul>
<li>Récupérez le code source : <a href="http://sourceforge.net/projects/cppcms/files/">http://sourceforge.net/projects/cppcms/files/</a></li>
<li>Récupérez quelques dépendences :<br />
<pre class="crayon-plain-tag">apt-get install libpcre3-dev zlib1g-dev libgcrypt11-dev libicu-dev python</pre></li>
<li>Compilez :<br />
<pre class="crayon-plain-tag">tar xf cppcms-1.0.2.tar.bz2</pre><br />
<pre class="crayon-plain-tag">cd cppcms-1.0.2/</pre><br />
<pre class="crayon-plain-tag">mkdir build</pre><br />
<pre class="crayon-plain-tag">cd build/</pre><br />
<pre class="crayon-plain-tag">cmake ..</pre><br />
<pre class="crayon-plain-tag">make -jX</pre> (Où X correspond à votre nombre de coeurs processeur + 1)<br />
<pre class="crayon-plain-tag">sudo make install</pre></li>
</ul>
<h1>Hello World</h1>
<p>Nous allons pour ce premier article visant à présenter CppCms, implémenter l&#8217;infatigable &#8220;Hello World&#8221;.</p>
<p>Un design pattern que l&#8217;on retrouve de nos jours dans la quasi-totalité des frameworks webs est le <strong>MVC</strong>, utile dans le cas de sites webs, il permet de séparer les données ou la logique (<em>i.e.</em> le <strong>M</strong>odèle) de l&#8217;interface graphique (<em>i.e.</em>la <strong>V</strong>ue) à l&#8217;aide d&#8217;un <em>glue code</em> traitant les interactions utilisateurs (<em>i.e.</em> le <strong>C</strong>ontrôleur).</p>
<p>Bien que pour ce premier &#8220;Hello World&#8221; nous n&#8217;allons pas utiliser cette organisation, CppCms ne fait pas exception et met à disposition le concept de contrôleur à l&#8217;aide de la classe mère <em>cppcms::application</em>, le modèle avec <em>cppcms::base_content</em> et les vues à l&#8217;aide du compilateur de fichier <em>.tmpl</em> : <em>cppcms_tmpl_cc</em> qui permet de mélanger C++ et le format de sortie : JSON, HTML&#8230;</p>
<h2>Une cppcms::application synchrone simple</h2>
<p>CppCms propose différents modèles de programmation, celui auquel un développeur web connaissant PHP, Django, Ruby on Rails ou Java est habitué est le modèle synchrone usant différents threads afin de traiter les requêtes reçues. C&#8217;est très simple à comprendre et scalable verticalement comme horizontalement via les systèmes de load balancing proposés avec FCGI ou SCGI (<em>e.g.</em> lighttpd mod_fcgi permet de définir plusieurs processus de gestion des requêtes, locaux ou distants).</p>
<p>Ici nous allons utiliser le serveur web intégré à CppCms en créant un petit projet organisé de la façon suivante:</p><pre class="crayon-plain-tag">hello_world/
        config.js
        CMakeLists.txt
        src/
            CMakeLists.txt
            main.cpp
            SomeController.hpp</pre><p>hello_world/config.js :</p><pre class="crayon-plain-tag">{
	&quot;service&quot; : {
		&quot;api&quot; : &quot;http&quot;,
        &quot;port&quot; : 8080,
        &quot;worker_threads&quot; : 10
	},
	&quot;http&quot; : {
		&quot;script&quot; : &quot;/&quot;
	}
}</pre><p>hello_world/CMakeLists.txt :</p><pre class="crayon-plain-tag">cmake_minimum_required (VERSION 2.8.8)
project (hello_world)

# We want binaries be compiled at the root of build folder
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# We want always all warnings but no sign comparison ones. :D
set (CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -Wall -Wno-sign-compare&quot;)

# Find booster
find_library(LIB_BOOSTER booster)
find_path(INC_BOOSTER booster/config.h)
include_directories(${INC_BOOSTER})

# Find cppcms
find_library(LIB_CPPCMS cppcms)
find_path(INC_CPPCMS cppcms/config.h)
include_directories(${INC_CPPCMS})

add_subdirectory(src)</pre><p>Et dans le dossier src, le CMakeLists qui actuellement produit l&#8217;executable, hello_world/src/CMakeLists.txt</p><pre class="crayon-plain-tag"># Build our application
include_directories(${CMAKE_CURRENT_LIST_DIR})
file(GLOB_RECURSE HELLO_WORLD_SOURCE_FILES *.cpp)

add_executable(hello_world ${HELLO_WORLD_SOURCE_FILES})
target_link_libraries(hello_world
    ${LIB_BOOSTER}
    ${LIB_CPPCMS})</pre><p></p>
<p>Et enfin le code de notre application, hello_world/src/SomeController.hpp :</p>
<div class="cpp" style="font-size:12px;font-family:monospace;color: #006;border: 1px solid #d0d0d0;background-color: #f0f0f0">
<ol>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #339900">#ifndef SOMECONTROLLER_HPP</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #339900">#define SOMECONTROLLER_HPP</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #339900">#include &lt;cppcms/application.h&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #339900">#include &lt;cppcms/http_response.h&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #339900">#include &lt;cppcms/url_dispatcher.h&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #0000ff">class</span> SomeController <span style="color: #008080">:</span> <span style="color: #0000ff">public</span> cppcms<span style="color: #008080">::</span><span style="color: #007788">application</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; <span style="color: #0000ff">public</span><span style="color: #008080">:</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; SomeController<span style="color: #008000">&#040;</span>cppcms<span style="color: #008080">::</span><span style="color: #007788">service</span> <span style="color: #000040">&amp;</span>srv<span style="color: #008000">&#041;</span> <span style="color: #008080">:</span> cppcms<span style="color: #008080">::</span><span style="color: #007788">application</span><span style="color: #008000">&#040;</span>srv<span style="color: #008000">&#041;</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatcher<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">assign</span><span style="color: #008000">&#040;</span><span style="color: #FF0000">&quot;/hello&quot;</span>,<span style="color: #000040">&amp;</span>SomeController<span style="color: #008080">::</span><span style="color: #007788">hello</span>,<span style="color: #0000dd">this</span><span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatcher<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">assign</span><span style="color: #008000">&#040;</span><span style="color: #FF0000">&quot;/bye&quot;</span>,<span style="color: #000040">&amp;</span>SomeController<span style="color: #008080">::</span><span style="color: #007788">bye</span>,<span style="color: #0000dd">this</span><span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dispatcher<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">assign</span><span style="color: #008000">&#040;</span><span style="color: #FF0000">&quot;.*&quot;</span>,<span style="color: #000040">&amp;</span>SomeController<span style="color: #008080">::</span><span style="color: #007788">redirect</span>,<span style="color: #0000dd">this</span><span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000">&#125;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff">void</span> redirect<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">set_redirect_header</span><span style="color: #008000">&#040;</span><span style="color: #FF0000">&quot;/hello&quot;</span><span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000">&#125;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff">void</span> hello<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">out</span><span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span> <span style="color: #000080">&lt;&lt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;html&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;body&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot; &lt;h1&gt;Hello World&lt;/h1&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;/body&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;/html&gt;<span style="color: #000099;font-weight: bold">\n</span>&quot;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000">&#125;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff">void</span> bye<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response<span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">out</span><span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span> <span style="color: #000080">&lt;&lt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;html&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;body&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot; &lt;h1&gt;Bye&lt;/h1&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;/body&gt;&quot;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000">&quot;&lt;/html&gt;<span style="color: #000099;font-weight: bold">\n</span>&quot;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000">&#125;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #008000">&#125;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top;color: #000020"><span style="color: #339900">#endif</span></div>
</li>
</ol>
</div>
<p></p>
<p>Afin de démarrer le service web et monter notre application à une url particulière il nous faut un point d&#8217;entrée, hello_world/src/main.cpp :</p>
<div class="cpp" style="font-size:12px;font-family:monospace;color: #006;border: 1px solid #d0d0d0;background-color: #f0f0f0">
<ol>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top"><span style="color: #339900">#include &lt;cppcms/applications_pool.h&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top"><span style="color: #339900">#include &lt;cppcms/service.h&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top"><span style="color: #339900">#include &lt;iostream&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top"><span style="color: #339900">#include &lt;SomeController.hpp&gt;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top"><span style="color: #0000ff">int</span> main<span style="color: #008000">&#040;</span><span style="color: #0000ff">int</span> argc, <span style="color: #0000ff">char</span> <span style="color: #000040">**</span>argv<span style="color: #008000">&#041;</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; <span style="color: #0000ff">try</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; &nbsp; &nbsp; cppcms<span style="color: #008080">::</span><span style="color: #007788">service</span> srv<span style="color: #008000">&#040;</span>argc,argv<span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; &nbsp; &nbsp; srv.<span style="color: #007788">applications_pool</span><span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span>.<span style="color: #007788">mount</span><span style="color: #008000">&#040;</span>cppcms<span style="color: #008080">::</span><span style="color: #007788">applications_factory</span><span style="color: #000080">&lt;</span>SomeController<span style="color: #000080">&gt;</span><span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span><span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; &nbsp; &nbsp; srv.<span style="color: #007788">run</span><span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; <span style="color: #008000">&#125;</span> <span style="color: #0000ff">catch</span><span style="color: #008000">&#040;</span>std<span style="color: #008080">::</span><span style="color: #007788">exception</span> <span style="color: #0000ff">const</span> <span style="color: #000040">&amp;</span>e<span style="color: #008000">&#041;</span> <span style="color: #008000">&#123;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080">::</span><span style="color: #0000dd">cerr</span> <span style="color: #000080">&lt;&lt;</span> e.<span style="color: #007788">what</span><span style="color: #008000">&#040;</span><span style="color: #008000">&#041;</span> <span style="color: #000080">&lt;&lt;</span> std<span style="color: #008080">::</span><span style="color: #007788">endl</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; <span style="color: #008000">&#125;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp;</div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top">&nbsp; &nbsp; <span style="color: #0000ff">return</span> <span style="color: #0000dd">0</span><span style="color: #008080">;</span></div>
</li>
<li>
<div style="font: normal normal 1em/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top"><span style="color: #008000">&#125;</span></div>
</li>
</ol>
</div>
<p></p>
<h3>Configuration et mount()</h3>
<p>Ici l&#8217;instance de <em>cppcms::service</em> que nous créons au sein de la fonction <em>int main(int argc, char **argv)</em> de l&#8217;application va justement définir sa configuration en fonction du fichier config.js que l&#8217;on passe à l&#8217;application. Ce fichier respecte un format JSON avec la possibilité en plus de commenter des lignes à l&#8217;aide de <em>//</em>.</p>
<p>Il est possible de configurer à l&#8217;aide de la clé <em>&#8220;service&#8221;</em> la façon dont CppCms doit servir des requêtes. Pour cet exemple nous resterons sur la configuration présente, mais pour une mise en production un petit changement vers fcgi ou scgi sera nécessaire (<em>c.f.</em> <a href="http://cppcms.com/wikipp/en/page/cppcms_1x_tut_web_server_config">Configurer CppCms avec des serveurs webs</a>).</p>
<p>Avec cette configuration, CppCms instancie un serveur http utilisant le nombre de threads donnés par <em>worker_threads</em> pour traiter les requêtes, il utilise ainsi la fabrique : <em>cppcms::applications_factory()</em> pour créer SomeController une fois par worker thread qui traite ce type de requêtes. La méthode <em>mount()</em> permet de définir sous quel chemin racine la cppcms::application sera servie, sans second paramètre, <em>mount()</em> utilise SomeController pour répondre à n&#8217;importe quelle requête entrante.</p>
<p>L&#8217;appel un peu plus loin à <em>srv.run()</em> lance le serveur HTTP ou FCGI/SCGI selon la configuration et démarre une event loop qui distribue les différentes requêtes au threads sur la base du reactor pattern (<em>i.e.</em> Il est possible de configurer quel backend utiliser via la clé <em>&#8220;reactor&#8221;</em>, avec l&#8217;une des valeurs suivantes : <em>&#8220;default&#8221;, &#8220;select&#8221;, &#8220;poll&#8221;, &#8220;epoll&#8221;, &#8220;devpoll&#8221;, &#8220;kqueue&#8221;</em>, cependant cela n&#8217;a rien de nécessaire sur les plateformes communes parce que default choisi toujours la meilleure option pour la plateforme en cours. Vous noterez que l&#8217;option <em>iocp</em> est manquante, cela résulte dans l&#8217;utilisation de select() sur windows).</p>
<p>Plutôt que des threads il est possible d&#8217;utiliser des processus pre-forkés en spécifiant la clé : <em>worker_processes</em>. Cela a deux principaux avantages : un crash d&#8217;une des instance de <em>SomeController</em> n&#8217;affecte que l&#8217;unique requête servie par ce dernier et sous la plupart des systèmes d&#8217;exploitations, au dessus un certain nombre de thread la gestion de ces derniers devient si complexe que leur coût en performance devient exponentiel (<em>c.f.</em> <a href="http://bulk.fefe.de/scalable-networking.pdf">Scalable Network Programming, Or: The Quest For A Good Web Server (That Survives Slashdot)</a>).</p>
<p>Cependant en restant sous un nombre réaliste de threads, la rapidité sera meilleure qu&#8217;avec fork, tout dépends de combien de clients simultanés doivent pouvoir être supportés et de la longueur du traitement de chaque requête, dans cet exemple nous n&#8217;utilisons que 10 threads.</p>
<p>Pour exécuter cet exemple sur votre pc, il vous suffit de taper les commandes suivantes :</p>
<ul>
<li><pre class="crayon-plain-tag">cd hello_world/</pre></li>
<li><pre class="crayon-plain-tag">mkdir build</pre></li>
<li><pre class="crayon-plain-tag">cd build/</pre></li>
<li><pre class="crayon-plain-tag">cmake .. &amp;&amp; make</pre></li>
<li><pre class="crayon-plain-tag">./hello_world -c ../config.js</pre></li>
<li>Puis dans votre navigateur : <a href="http://127.0.0.1:8080/">http://127.0.0.1:8080/</a> et <a href="http://127.0.0.1:8080/bye">http://127.0.0.1:8080/bye</a></li>
</ul>
<h3>Assignement d&#8217;url</h3>
<p>Comme il est possible de le voir dans le fichier SomeController.hpp lors de l&#8217;instanciation de ce dernier il est possible de définir quelle méthode doit être appellée à quelle instance à l&#8217;aide du header : <em>cppcms/url_dispatcher.h</em>.</p>
<p>Ce dernier ne se limite pas à cette utilisation simpliste, en effet le premier paramètre est une expression régulière, il est ainsi possible de mapper des éléments capturés de l&#8217;url en tant que paramètre de la méthode à appeller, de telle sorte que l&#8217;on pourrait écrire pour une méthode avec la signature <em>SomeController::getArticle(string categoryName, string articleId)</em>:<br />
<pre class="crayon-plain-tag">dispatcher().assign("/getArticle/(s+)/(d+)", &amp;SomeController::getArticle,1,2);</pre><br />
<br />
Ainsi la méthode chargée de traiter la requête n&#8217;est appellé que si le format de l&#8217;url correspond. Cela n&#8217;empêche pas pour autant d&#8217;accèder directement à l&#8217;url complète ou aux paramètres GET, POST etc. à l&#8217;aide de la méthode <em>request()</em> retournant un objet de type cppcms::http::request représentant la requête.</p>
<h3>Ecriture de la réponse</h3>
<p>Comme il est possible de le voir dans les méthodes <em>redirect()</em>, <em>hello()</em> &amp; <em>bye</em> on manipule la réponse http à l&#8217;aide de la méthode <em>response()</em> qui retourne un object de type cppcms::http::response et qui possède une API très riche pour manipuler les réponses, avec <a href="http://cppcms.com/cppcms_ref/latest/classcppcms_1_1http_1_1response.html">des méthodes confortables</a>. Vous trouverez également une méthode <em>io_mode()</em> qui peut être définie sur <em>normal</em>, <em>nogzip</em>, <em>raw</em>, <em>asynchronous</em> ou <em>asynchronous_raw</em>.</p>
<h1>De nombreuses autres fonctionallités à découvrir</h1>
<p>Comme vous avez pu le constater il y a différents modes d&#8217;entrées/sorties possibles dans cppcms. Un mode très intéressant, mais peut-être le moins commun pour les personnes ne connaissant pas boost::asio est le mode asynchrone.</p>
<p>Chaque thread qui fait de l&#8217;entrée/sortie a la problèmatique de devoir attendre sur l&#8217;achèvement d&#8217;une entrée ou d&#8217;une sortie pour pouvoir continuer les traitements. On peut résoudre ce problème d&#8217;entrées/sorties bloquants les thread la majorité du temps, en délégant le transfert des données aux drivers et au matériel. Laissant ces derniers effectuer les entrées/sorties lorsque c&#8217;est actuellement le moment ou possible, et répondre ainsi à d&#8217;autres entrées/sorties avec le même thread. Cela décuple de façon très importante le nombre de requêtes pouvant être traitées avec le même nombre de threads, cela implique cependant plus de complexité et nécessite de gérer soit-même le nombre de connexions par thread, afin de rendre l&#8217;application scalable.</p>
<p>Pour ceux connaissant boost::asio il y a cependant certaines différences dans la façon avec laquelle CppCms implémente les mêmes principes avec sa librairie booster::aio. En effet <em>io_service::run()</em> ne peut être appellée par plusieurs threads à la fois et laisse tourner sa boucle d&#8217;évènement jusqu&#8217;à ce que<em> stop()</em> soit appellée et non pas lorsqu&#8217;il n&#8217;y a plus de tâches postées.</p>
<p>booster::aio à l&#8217;instar de boost::asio n&#8217;utilise pas iocp sur Windows, parce qu&#8217;il utilise toujours le Reactor pattern. Un autre point est qu&#8217;il y a moins de fonctionnalités que dans boost::asio et est orienté objet plutôt que concepts.</p>
<p>Je posterai sous peu un article expliquant cela plus en détails et présenterai dans quels cas avec CppCms les applications webs asynchrones sont intéressantes, cela ne ferait aucun sens pour notre hello_world, mais plutôt dans le cas d&#8217;implémentation de server-push ou de long-polling à l&#8217;aide de la classe : <em>cppcms::http::context</em> permettant de conserver une requête en cours sur le serveur et de la servir partiellement sans bloquer un thread par utilisateur, à l&#8217;aide de fonctions telles qu&#8217;<em>async_flush_output(handler)</em>.</p>
<h1>Conclusion</h1>
<p>Développer des applications webs en C++ n&#8217;est plus un problème et a différents avantages, je n&#8217;ai présenté ici que la base la plus simple de ce framework, mais il y a de nombreuses fonctionnalités qui méritent d&#8217;être connues comme le support de Json::Rpc, le compilateur de vues/templates, les mécanismes de cache intégrés, les classes de sérialisation/désérialisation, la gestion des sessions, des formulaires et de l&#8217;internationalisation.</p>
<p>Si vous souhaitez en savoir plus n&#8217;hésitez pas à visiter les exemples et la documentation officielle : <a href="http://cppcms.com/wikipp/en/page/cppcms_1x">CppCMS 1.x.x — Stable</a>. Je serai également ravi de vous répondre ou de vous aider.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.freelan.org/2012/11/11/developpement-web-en-c/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
