<?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>Thomas Løcke Being Incoherent</title>
	<atom:link href="http://blogs.fsfe.org/thomaslocke/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.fsfe.org/thomaslocke</link>
	<description>Just another FSFE Fellowship Blogs site</description>
	<lastBuildDate>Sun, 05 May 2013 10:57:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using the Ada Web Server (AWS), part 2</title>
		<link>http://blogs.fsfe.org/thomaslocke/2013/03/10/using-the-ada-web-server-aws-part-2/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2013/03/10/using-the-ada-web-server-aws-part-2/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 12:38:44 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Templates_Parser]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=390</guid>
		<description><![CDATA[In the Using the Ada Web Server (AWS), part 1 article I showed you how to setup a simple Hello world! server powered by the awesomeness that is the Ada Web Server (AWS) project. In this second part I will show you how to utilize the Templates_Parser module to build your HTML and I&#8217;ll also <a href="http://blogs.fsfe.org/thomaslocke/2013/03/10/using-the-ada-web-server-aws-part-2/" title="Read ”Using the Ada Web Server (AWS), part 2”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://blogs.fsfe.org/thomaslocke/2013/02/10/using-the-ada-web-server-aws-part-1/">Using the Ada Web Server (AWS), part 1</a> article I showed you how to setup a simple Hello world! server powered by the awesomeness that is the <a href="http://libre.adacore.com/tools/aws">Ada Web Server (AWS)</a> project. In this second part I will show you how to utilize the <a href="http://docs.adacore.com/aws-docs/templates_parser.html">Templates_Parser</a> module to build your HTML and I&#8217;ll also give a very short example on how to serve a flat file to visitors.</p>
<p>If you haven&#8217;t already read part 1, I strongly suggest doing so before proceeding with this article, as we will be building upon the code from part 1 in the following. I will not waste time on how to get the server going, or how to setup a dispatcher. We will jump straight into the template fray.</p>
<p>But first things first:</p>
<pre>
$ git clone git://github.com/ThomasLocke/AWS_Tutorial_2.git
</pre>
<p>The instructions on how to compile and run the program are the same as for part 1 of the article.</p>
<p>So what exactly is the point of using templates to generate your content, instead of just building the HTML directly in the Ada code? Well there are several good reasons for using templates:</p>
<ul>
<li>With templates you don&#8217;t have to re-compile due to changes in the HTML.</li>
<li>With templates you get a very strong separation between logic and view.</li>
<li>With templates you can easily localize content by simply loading a different template file based on user preferences/IP address/whatever.</li>
<li>Your HTML people won&#8217;t need to learn a single line of Ada. They can stick to what they are good at.</li>
</ul>
<p>There are probably more good reasons, but the ones mentioned above should be more than enough to convince you that using a template system is a good idea.</p>
<p>Lets take a peek at a very simple template file:</p>
<pre>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Not found&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Not found&lt;/h1&gt;
    &lt;p&gt;Resource @_RESOURCE_@ not found&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>As you can see it looks very much like normal HTML except for the special <code>@_RESOURCE_@</code> tag (third line from below), which is what turns this otherwise plain looking HTML snippet into a proper template: <code>@_RESOURCE_@</code> is a template tag that is replaced by some value defined in the Ada code. As you might&#8217;ve guessed this template is used to generate the &#8220;404 not found&#8221; content, so lets see how the Ada code looks now that we&#8217;ve moved the HTML into a template. This is our new <code>Generate_Content</code> function from the <code>src/not_found.adb</code> file:</p>
<pre>
with AWS.Messages;
with AWS.MIME;
with AWS.Response;
with AWS.Status;
with AWS.Templates;

package body Not_Found is

   --  Stuff...

   ------------------------
   --  Generate_Content  --
   ------------------------

   function Generate_Content
     (Request : in AWS.Status.Data)
      return AWS.Response.Data
   is
      use AWS.Templates;

      Resource : constant String := AWS.Status.URI (Request);

      Translations : Translate_Set;
   begin
      Insert (Translations, Assoc ("RESOURCE", Resource));

      return AWS.Response.Build
        (Content_Type  =&gt; AWS.MIME.Text_HTML,
         Message_Body  =&gt; Parse ("templates/not_found.tmpl", Translations),
         Status_Code   =&gt; AWS.Messages.S404);
   end Generate_Content;

end Not_Found;
</pre>
<p>If you compare it to the <code>src/not_found.adb</code> file from <a href="http://blogs.fsfe.org/thomaslocke/2013/02/10/using-the-ada-web-server-aws-part-1/">part 1</a> you&#8217;ll notice that the changes really aren&#8217;t that huge, but lets start at the top: At line 5 we <code>with AWS.Templates;</code> to bring in the templates parser module. With that we now have all the templating power at out fingertips.</p>
<p>The next change happens in the declarative part of <code>Generate_Content</code> where we now have a <code>Use AWS.Templates;</code> line and a <code>Translations : Translate_Set;</code> line. A <code>Translate_Set</code> is basically a dictionary to which you add your tags and their associated values, and to see how that is done we go down two lines to the <code>Insert (Translations, Assoc ("RESOURCE", Resource));</code> line. Starting from the inside we associate the value of the <code>Resource</code> string with the template tag <code>RESOURCE</code> using the <code>Assoc</code> procedure and then we add the association to <code>Translations</code> with the <code>Insert</code> procedure.</p>
<p>Note that if you insert an association that already exists, then the existing association is overwritten by the new association.</p>
<p>In order to use the <code>Translations</code> dictionary we&#8217;ve created, we must call the <code>AWS.Templates.Parse</code> function:</p>
<pre>
function Parse
  (Filename          : String;
   Translations      : Translate_Set;
   Cached            : Boolean               := False;
   Keep_Unknown_Tags : Boolean               := False;
   Lazy_Tag          : Dyn.Lazy_Tag_Access   := Dyn.Null_Lazy_Tag;
   Cursor_Tag        : Dyn.Cursor_Tag_Access := Dyn.Null_Cursor_Tag)
   return String;
</pre>
<p>Only <code>Filename</code> and <code>Translations</code> are required, so that is exactly what we&#8217;re going to give <code>Parse</code>:</p>
<pre>
return AWS.Response.Build
  (Content_Type  =&gt; AWS.MIME.Text_HTML,
   Message_Body  =&gt; Parse ("templates/not_found.tmpl", Translations),
   Status_Code   =&gt; AWS.Messages.S404);
</pre>
<p><code>Parse</code> then goes to work, matching all the added associations to the tags found in the template file, which in our case means replacing <code>@_RESOURCE_@</code> with the value of <code>RESOURCE</code>.</p>
<p>And that is all. The 404 not found content is now fully templated.</p>
<p>Next lets take a peek at <code>Generate_Content</code> from <code>src/hello_world.adb</code> where we use a few more features from the <code>AWS.Templates</code> module:</p>
<pre>
with AWS.Messages;
with AWS.Templates;

package body Hello_World is

   --  Stuff...

   ------------------------
   --  Generate_Content  --
   ------------------------

   function Generate_Content
     (Request : in AWS.Status.Data)
      return AWS.Response.Data
   is
      use AWS.Templates;

      type Natural_List is array (0 .. 9) of Natural;
      Fibs : Natural_List;

      Browser : constant String := AWS.Status.User_Agent (Request);

      Fibonacci    : Vector_Tag;
      Position     : Vector_Tag;
      Translations : Translate_Set;
   begin
      Insert (Translations, Assoc ("BROWSER", Browser));

      for i in Fibs'Range loop
         case i is
            when 0 =&gt; Fibs (i) := 0;
            when 1 =&gt; Fibs (i) := 1;
            when others =&gt; Fibs (i) := Fibs (i - 1) + Fibs (i - 2);
         end case;

         Append (Position, i);
         Append (Fibonacci, Fibs (i));
      end loop;

      Insert (Translations, Assoc ("POSITION", Position));
      Insert (Translations, Assoc ("FIBONACCI", Fibonacci));

      return AWS.Response.Build
        (Content_Type  =&gt; AWS.MIME.Text_HTML,
         Message_Body  =&gt; Parse (Filename     =&gt; "templates/hello_world.tmpl",
                                    Translations =&gt; Translations,
                                    Cached       =&gt; True),
         Status_Code   =&gt; AWS.Messages.S200);
   end Generate_Content;

end Hello_World;
</pre>
<p>Let me start by saying that my Fibonacci implementation probably isn&#8217;t fast nor elegant, but it works and it doesn&#8217;t clutter the example with a whole bunch of code, and really the interesting part here is not how the Fibonacci sequence is generated but how it is added to the <code>Translations Translate_Set</code>. For this we have the <code>Vector_Tag</code>, which in all its simplicity allows us to build lists of values. As you can see we have two such vector tags: <code>Fibonacci</code> and <code>Position</code>. These two are populated by the <code>Append</code> calls in the <code>for loop</code>:</p>
<pre>
for i in Fibs'Range loop
   case i is
      when 0 =&gt; Fibs (i) := 0;
      when 1 =&gt; Fibs (i) := 1;
      when others =&gt; Fibs (i) := Fibs (i - 1) + Fibs (i - 2);
   end case;

   Append (Position, i);
   Append (Fibonacci, Fibs (i));
end loop;
</pre>
<p><code>Append</code> add the values of <code>i</code> and <code>Fibs (i)</code> to <code>Position</code> and </code>Fibonacci</code> respectively. In case you're wondering about what kinds of data you can append to a vector, here's the specification of all the <code>Append</code> procedures:</p>
<pre>
procedure Append (T : in out Tag; Value : String);
procedure Append (T : in out Tag; Value : Character);
procedure Append (T : in out Tag; Value : Boolean);
procedure Append (T : in out Tag; Value : Unbounded_String);
procedure Append (T : in out Tag; Value : Integer);
procedure Append (T : in out Tag; Value : Tag);
</pre>
<p>Note the last one: Yes, you can append a vector tag to a vector tag, making it possible to build multi-dimensional arrays.</p>
<p>Adding our vector tags to <code>Translations</code> is still done using the <code>Insert</code> procedure, so nothing new there.</p>
<p>Finally we generate the content with <code>Parse</code>, where it is worth noting that we've now added the <code>Cached =&gt; True</code> parameter. What this does is allow the <code>AWS.Templates</code> module to cache the template itself. If you do this the server no longer read and parse the template file on every hit. The downside is that if you make changes to the template file, you will have to restart the server for the changes to be registered.</p>
<p>Now lets see how we deal with vector tags and do some other tricks in the template file:</p>
<pre>
@@--
@@--  First we define a macro.
@@--
@@MACRO(F)@@
&lt;span style="font-style: italic;"&gt;F&lt;/span&gt;&lt;span style="font-size: 60%;"&gt;@_$1_@&lt;/span&gt;
@@END_MACRO@@
@@--
@@--  And then comes the actual HTML5 document
@@--
&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Hello world!&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Hello world!&lt;/h1&gt;
    &lt;p&gt;Browser used : @_BROWSER_@&lt;/p&gt;
    &lt;h2&gt;Fibonacci's from 0 to 9&lt;/h2&gt;
    &lt;table&gt;
      @@TABLE@@
        &lt;tr&gt;
          &lt;td&gt;@_F(@_POSITION_@)_@&lt;/td&gt;
          &lt;td&gt;@_FIBONACCI_@&lt;/td&gt;
        &lt;/tr&gt;
      @@END_TABLE@@
    &lt;/table&gt;
    &lt;p&gt;Now is @_NOW_@&lt;/p&gt;
    &lt;p&gt;Now reversed is @_REVERSE:NOW_@&lt;/p&gt;
    &lt;p&gt;Click &lt;a href="/helloworld.tmpl"&gt;here&lt;/a&gt; to see the hello_world.tmpl file.&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Lines starting with <code>@@--</code> are comments.</p>
<p>At line three we define a macro with the name <code>F</code>. When you call macros you can give them parameters which are then referenced in the macro as <code>@_$n_@</code>, where <code>n</code> corresponds to the N.th. parameter passed to the macro.</p>
<p>Moving on we add the visitors browser to the HTML using the <code>@_BROWSER_@</code>, and just below that we build the Fibonacci table, and here we encounter the <code>@@TABLE@@</code> tag. Code between the <code>@@TABLE@@</code> and <code>@@END_TABLE@@</code> tags are repeated as many times as there are values in the <code>POSITION</code> and <code>FIBONACCI</code> vectors. <code>@@TABLE@@</code> acts very much like an implicit iterator. Much fun can be had with the <code>@@TABLE@@</code> tag - this example is the very simplest way to utilize it. Check the manual for more <a href="http://docs.adacore.com/aws-docs/templates_parser.html#TABLE-statement">extensive examples</a>.</p>
<p>There's a few more tricks in this template worth mentioning. The templates parser module sports a bunch of constants and filters, one of these being <code>@_NOW_@</code> which is replaced with a time stamp in the format "YYYY-MM-DD HH:MM:SS". At the next line we reverse the contents of <code>@_NOW_@</code> using the <code>@_REVERSE:VAR_@</code> filter. There's a whole bunch of <a href="http://docs.adacore.com/aws-docs/templates_parser.html#Filters">filters</a> available and multiple filters can be applied to tags for pure awesomeness. As an added bonus you can even <a href="http://docs.adacore.com/aws-docs/templates_parser.html#User-defined-filters">create your own filters</a>.</p>
<p>And that was all I had about the templates parser module, but before I end this article I'd like to direct your attention to this new dispatcher in <code>src/handlers.adb</code>:</p>
<pre>
Dispatcher.Register (URI    =&gt; "/helloworld.tmpl",
                     Action =&gt; Hello_World.Hello_World_Template'Access);
</pre>
<p>The goal of this dispatcher is to return the contents of the <code>exe/templates/hello_world.tmpl</code> file to the user as <code>text/plain</code>. The <code>Hello_World.Hello_World_Template</code> function takes care of that:</p>
<pre>
function Hello_World_Template
  (Request : in AWS.Status.Data)
   return AWS.Response.Data
is (AWS.Response.File (AWS.MIME.Text_Plain,
                       "templates/hello_world.tmpl"));
</pre>
<p>That right there is an Ada 2012 expression function. Since all this function does is call <code>AWS.Response.File</code> we don't really need a body. Expression functions provides a shorthand to declare a function whose body consists of a single return statement. That is IMHO one very nice feature of Ada 2012.</p>
<p>And with that final piece of <code>AWS.Response.File</code> magic I will close this article. Stay tuned for part 3, where I plan on showing you a bit about how to handle HTTP request parameters using the Ada Web Server.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2013/03/10/using-the-ada-web-server-aws-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using the Ada Web Server (AWS), part 1</title>
		<link>http://blogs.fsfe.org/thomaslocke/2013/02/10/using-the-ada-web-server-aws-part-1/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2013/02/10/using-the-ada-web-server-aws-part-1/#comments</comments>
		<pubDate>Sun, 10 Feb 2013 15:27:27 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[AWS]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=348</guid>
		<description><![CDATA[The Ada Web Server (AWS) is a big library with a whole lot of functionality, far more than I can manage to write about, so what I am going to do in this first article is focus on the basic stuff: Getting a server up and registering a couple of content handlers. Before we move <a href="http://blogs.fsfe.org/thomaslocke/2013/02/10/using-the-ada-web-server-aws-part-1/" title="Read ”Using the Ada Web Server (AWS), part 1”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://libre.adacore.com/tools/aws/">Ada Web Server (AWS)</a> is a big library with a whole lot of functionality, far more than I can manage to write about, so what I am going to do in this first article is focus on the basic stuff: Getting a server up and registering a couple of content handlers.</p>
<p>Before we move on lets get two things out of the way:</p>
<ol>
<li>Make sure you have AWS installed on your system</li>
<li>Git clone the code from this article from <a href="https://github.com/ThomasLocke/AWS_Tutorial_1">here</a></li>
</ol>
<p>If you&#8217;re having trouble getting AWS up and running, you could take a look at an article I wrote a while ago about <a href="https://blogs.fsfe.org/thomaslocke/2012/01/08/ada-programming-on-slackware/">Ada Programming on Slackware</a>. Even if you&#8217;re not using Slackware there might be some tricks in there that also apply to your favorite distro. If you&#8217;re using Windows I really can&#8217;t help you, as I know very little about getting Ada and AWS up and running on that line of operating systems.</p>
<p>What is most important though, is that you grab the latest AWS from the Git repository:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">git clone</span> <span style="color: #660033;">--recursive</span> http:<span style="color: #000000; font-weight: bold;">//</span>forge.open-do.org<span style="color: #000000; font-weight: bold;">/</span>anonscm<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>aws<span style="color: #000000; font-weight: bold;">/</span>aws.git</pre></td></tr></table></div>

<p>At the time of writing the latest commit in the repository was ffc79ac63f06e44100bc69de59eb4ed9b76c41ae (Date: Tue Feb 5 18:48:37 2013 +0100), so assume that you at least need an AWS that is as new as that.</p>
<p>From now on I&#8217;m going to assume that you&#8217;ve got a sufficiently new AWS working and the tutorial code cloned from my Github repository, so lets get rocking with some AWS magic.</p>
<p>We start by building and trying the tutorial program you cloned earlier, so we know what we&#8217;re getting into:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> Ada_Web_Server_Tutorial_1<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">make</span>
$ <span style="color: #7a0874; font-weight: bold;">cd</span> exe<span style="color: #000000; font-weight: bold;">/</span>
$ .<span style="color: #000000; font-weight: bold;">/</span>aws_tutorial</pre></td></tr></table></div>

<p>Grab your browser and do as instructed; go to localhost:8080 and see what you get. Nice eh&#8217;? A sexy 404 message hosted by your very own Ada powered webserver. Whooooosh! If you don&#8217;t like the 404, try visiting localhost:8080/helloworld for something else. When you&#8217;re done being amazed click q to kill the server.</p>
<p>So how did this magic come to life? We zoom to the main program file <code>aws_tutorial.adb</code> for some enlightenment:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Config</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Default</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Server</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">with</span> Handlers;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> AWS_Tutorial <span style="color: #00007f;">is</span>
   Web_Server : AWS.<span style="color: #202020;">Server</span>.<span style="color: #202020;">HTTP</span>;
<span style="color: #00007f;">begin</span>
   Ada.<span style="color: #202020;">Text_IO</span>.<span style="color: #202020;">Put_Line</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;I'm available at localhost port&quot;</span>
      &amp; Positive'Image <span style="color: #66cc66;">&#40;</span>AWS.<span style="color: #202020;">Default</span>.<span style="color: #202020;">Server_Port</span><span style="color: #66cc66;">&#41;</span>
      &amp; <span style="color: #7f007f;">&quot;. Press q to kill me.&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   Ada.<span style="color: #202020;">Text_IO</span>.<span style="color: #202020;">Put_Line</span> <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;http://localhost:8080/helloworld&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   AWS.<span style="color: #202020;">Server</span>.<span style="color: #202020;">Start</span> <span style="color: #66cc66;">&#40;</span>Web_Server =&gt; Web_Server, 
                     Dispatcher =&gt; Handlers.<span style="color: #202020;">Get_Dispatcher</span>, 
                     Config =&gt; AWS.<span style="color: #202020;">Config</span>.<span style="color: #202020;">Get_Current</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   AWS.<span style="color: #202020;">Server</span>.<span style="color: #202020;">Wait</span> <span style="color: #66cc66;">&#40;</span>AWS.<span style="color: #202020;">Server</span>.<span style="color: #202020;">Q_Key_Pressed</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   AWS.<span style="color: #202020;">Server</span>.<span style="color: #202020;">Shutdown</span> <span style="color: #66cc66;">&#40;</span>Web_Server<span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">end</span> AWS_Tutorial;</pre></td></tr></table></div>

<p>Simplicity at work eh&#8217;? Lets stroll through the code together. </p>
<p>First we <code>with</code> a bunch of packages: <code>Ada.Text_IO</code> is for text input/output, and that is exactly what we&#8217;re using it for at line 12 where we output the port and kill instructions. After that we add three AWS packages, of which the first two are dealing with the configuration of the AWS HTTP server, specifically the default settings and loading of any external configuration files. The call to <code>AWS.Config.Get_Current</code> at line 19 searches for the files <code>aws.ini</code> and <code>progname.ini</code> (in our case aws_tutorial.ini</code>) in the same location as the executable, and if any of these are found the values set here are used to configure the <code>Web_Server</code> object that is started at line 17 by the <code>AWS.Server.Start</code> call. If you take a peek at the <code>exe/aws.ini</code> file you'll see that the only value we're setting is <code>Reuse_Address True</code>. What this does is allow us to start and stop the server without having to wait for the system to release the socket. The rest of the configuration is inherited from the <code>AWS.Default</code> package.</p>
<p>We'll ignore the call to <code>Handlers.Get_Dispatcher</code> for now and move further down to line 21 where we're abruptly stopped by the <code>AWS.Server.Wait (AWS.Server.Q_Key_Pressed)</code> call. What is this weird contraption? Well a server needs some sort of "loop" to prevent it from terminating, and this is exactly one such loop, except it isn't really a loop. What <code>Wait</code> does is hang until some specific event occurs, in our case it is waiting for someone to click the <code>q</code> key. As soon as <code>Wait</code> registers some action on the <code>q</code> key it returns and the program moves on to the final statement; the <code>AWS.Server.Shutdown (Web_Server)</code> call. I'm guessing that it is evident what this does. It cleanly shuts down the server and when it is done, the program exits.</p>
<p>And that is really all that is required to add HTTP(s) support to your Ada program.</p>
<p>But wait, how do we generate the content? Ahh, yes <em>now</em> we'll take a closer look at the <code>Handlers.Get_Dispatcher</code> call at line 18. What is this dispatcher thing we're getting? It's simple really. In order for the server to know what to do with all the requests coming in, we need to define some handlers. This is done in the homegrown <code>Handlers.Get_Dispatcher</code> function that lives a groovy life in the <code>Handlers</code> package found in the <code>src/handlers.ad[sb]</code> files. Lets take a peek at the specification file first:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Services</span>.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">URI</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">package</span> Handlers <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Get_Dispatcher
     <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Services</span>.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">URI</span>.<span style="color: #202020;">Handler</span>;
   <span style="color: #adadad; font-style: italic;">--  Return the content handlers for our server.</span>
&nbsp;
<span style="color: #00007f;">end</span> Handlers;</pre></td></tr></table></div>

<p>Not much happening here. We define the <code>Get_Dispatcher</code> function which return a <code>AWS.Services.Dispatchers.URI.Handler</code>. The name of the returned type should give away that we're dispatching HTTP request based on the URI of the requested resource. There are a bunch of other dispatchers available in AWS, but for now we'll stick to the basic URI stuff.</p>
<p>The body is a bit more interesting:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Hello_World;
<span style="color: #46aa03; font-weight:bold;">with</span> Not_Found;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">package</span> <span style="color: #46aa03; font-weight:bold;">body</span> Handlers <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">----------------------</span>
   <span style="color: #adadad; font-style: italic;">--  Get_Dispatcher  --</span>
   <span style="color: #adadad; font-style: italic;">----------------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Get_Dispatcher
     <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Services</span>.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">URI</span>.<span style="color: #202020;">Handler</span>
   <span style="color: #00007f;">is</span>
      Dispatcher : AWS.<span style="color: #202020;">Services</span>.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">URI</span>.<span style="color: #202020;">Handler</span>;
   <span style="color: #00007f;">begin</span>
      Dispatcher.<span style="color: #202020;">Register_Default_Callback</span> <span style="color: #66cc66;">&#40;</span>Action =&gt; Not_Found.<span style="color: #202020;">Callback</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #adadad; font-style: italic;">--  The default callback is going to be our 404 not found handler. If</span>
      <span style="color: #adadad; font-style: italic;">--  a requested resource isn't specifically defined here, we're going</span>
      <span style="color: #adadad; font-style: italic;">--  to return a 404 to the client.</span>
&nbsp;
      Dispatcher.<span style="color: #202020;">Register</span> <span style="color: #66cc66;">&#40;</span>URI    =&gt; <span style="color: #7f007f;">&quot;/helloworld&quot;</span>,
                           Action =&gt; Hello_World.<span style="color: #202020;">Callback</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #adadad; font-style: italic;">--  The /helloworld resource.</span>
&nbsp;
      <span style="color: #00007f;">return</span> Dispatcher;
   <span style="color: #00007f;">end</span> Get_Dispatcher;
&nbsp;
<span style="color: #00007f;">end</span> Handlers;</pre></td></tr></table></div>

<p>The two packages we <code>with</code> contains the code that generates our content. We need them here because this is the place where we register the resource handlers.</p>
<p>We declare the <code>Dispatcher</code> object as an <code>URI.Handler</code> at line 13 and then we proceed with registering two handlers: A default handler at line 15 and a specific <code>/helloworld</code> handler at line 20. Finally we return the <code>Dispatcher</code> object.</p>
<p>The default handler is the <code>Not_Found.Callback</code> function, which means that whenever a resource is requested that doesn't specifically match a registered URI, the <code>Not_Found.Callback</code> function is called. On the other hand if the <code>/helloworld</code> resource is requested, the <code>Hello_World.Callback</code> function is called.</p>
<p>This is how dispatching in AWS works. But it is not the only way. It is also possible to manually dispatch based on the string URL of a request, but that is very clumsy and ugly. You really should use the dispatcher model if you're working with AWS as it offers both convenience and some nice tools to build and maintain your resource-&gt;handler structure.</p>
<p>Moving on lets take a peek at the <code>Not_Found</code> package where we handle the 404's:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">package</span> Not_Found <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Callback
     <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>.<span style="color: #202020;">Handler</span>;
   <span style="color: #adadad; font-style: italic;">--  Return a callback for the Not_Found (404) response.</span>
&nbsp;
<span style="color: #00007f;">end</span> Not_Found;</pre></td></tr></table></div>

<p>Here we see the <code>Callback</code> function that we registered in the <code>Handlers.Get_Dispatcher</code> function. And that is really all we make available to the world in the specification of this package. Lets check out the body:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Messages</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">MIME</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Response</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Status</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">package</span> <span style="color: #46aa03; font-weight:bold;">body</span> Not_Found <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Generate_Content
     <span style="color: #66cc66;">&#40;</span>Request : <span style="color: #46aa03; font-weight:bold;">in</span> AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">Data</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Data</span>;
   <span style="color: #adadad; font-style: italic;">--  Generate the 404 response.</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">----------------</span>
   <span style="color: #adadad; font-style: italic;">--  Callback  --</span>
   <span style="color: #adadad; font-style: italic;">----------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Callback
     <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>.<span style="color: #202020;">Handler</span>
   <span style="color: #00007f;">is</span>
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>.<span style="color: #202020;">Create</span> <span style="color: #66cc66;">&#40;</span>Generate_Content'<span style="color: #46aa03; font-weight:bold;">Access</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> Callback;
&nbsp;
   <span style="color: #adadad; font-style: italic;">------------------------</span>
   <span style="color: #adadad; font-style: italic;">--  Generate_Content  --</span>
   <span style="color: #adadad; font-style: italic;">------------------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Generate_Content
     <span style="color: #66cc66;">&#40;</span>Request : <span style="color: #46aa03; font-weight:bold;">in</span> AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">Data</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Data</span>
   <span style="color: #00007f;">is</span>
      Resource : <span style="color: #46aa03; font-weight:bold;">constant</span> String := AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">URI</span> <span style="color: #66cc66;">&#40;</span>Request<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Build</span>
        <span style="color: #66cc66;">&#40;</span>Content_Type  =&gt; AWS.<span style="color: #202020;">MIME</span>.<span style="color: #202020;">Text_HTML</span>,
         Message_Body  =&gt;
           <span style="color: #7f007f;">&quot;&lt;h1&gt;Not found&lt;/h1&gt;&quot;</span> &amp;
           <span style="color: #7f007f;">&quot;&lt;p&gt;Resource '&quot;</span> &amp; Resource &amp; <span style="color: #7f007f;">&quot;' not found&lt;/p&gt;&quot;</span>,
         Status_Code   =&gt; AWS.<span style="color: #202020;">Messages</span>.<span style="color: #202020;">S404</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> Generate_Content;
&nbsp;
<span style="color: #00007f;">end</span> Not_Found;</pre></td></tr></table></div>

<p>Ahh, this is far more interesting. Starting with the <code>Callback</code> function we see that all it does is return an <code>AWS.Dispatchers.Callback.Handler</code>. This is done using the <code>Create</code> function which takes an <code>AWS.Response.Callback</code> as its single parameter. The signature of the <code>AWS.Response.Callback</code> is this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">type</span> Callback <span style="color: #00007f;">is</span> <span style="color: #46aa03; font-weight:bold;">access</span> <span style="color: #46aa03; font-weight:bold;">function</span> 
  <span style="color: #66cc66;">&#40;</span>Request : AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">Data</span><span style="color: #66cc66;">&#41;</span> <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Data</span>;</pre></td></tr></table></div>

<p>And that is exactly the signature of the <code>Generate_Content</code> function. It's marvelous how these things just come together like hand in glove. In <code>Generate_Content</code> we build the response sent to clients requesting unknown resources. This is all done in the <code>AWS.Response.Build</code> call. We define the <code>Content_Type</code> as text/html, we generate the actual content and finally we set the <code>Status_Code</code> to 404.</p>
<p>Pay special attention to line 32 where we grab the requested URI from the <code>Request</code> object. There's a lot of interesting data in this object. Take a peek at the <code>AWS.Status</code> package for more on how to entice this object to give up its secrets.</p>
<p>For the sake of completeness lets also take a peek at the <code>Hello_World</code> package:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">package</span> Hello_World <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Callback
     <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>.<span style="color: #202020;">Handler</span>;
   <span style="color: #adadad; font-style: italic;">--  Return a callback for the Hello World! response.</span>
&nbsp;
<span style="color: #00007f;">end</span> Hello_World;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Messages</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">MIME</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Response</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> AWS.<span style="color: #202020;">Status</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">package</span> <span style="color: #46aa03; font-weight:bold;">body</span> Hello_World <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Generate_Content
     <span style="color: #66cc66;">&#40;</span>Request : <span style="color: #46aa03; font-weight:bold;">in</span> AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">Data</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Data</span>;
   <span style="color: #adadad; font-style: italic;">--  Generate the Hello World! response.</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">----------------</span>
   <span style="color: #adadad; font-style: italic;">--  Callback  --</span>
   <span style="color: #adadad; font-style: italic;">----------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Callback
     <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>.<span style="color: #202020;">Handler</span>
   <span style="color: #00007f;">is</span>
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Dispatchers</span>.<span style="color: #202020;">Callback</span>.<span style="color: #202020;">Create</span> <span style="color: #66cc66;">&#40;</span>Generate_Content'<span style="color: #46aa03; font-weight:bold;">Access</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> Callback;
&nbsp;
   <span style="color: #adadad; font-style: italic;">------------------------</span>
   <span style="color: #adadad; font-style: italic;">--  Generate_Content  --</span>
   <span style="color: #adadad; font-style: italic;">------------------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Generate_Content
     <span style="color: #66cc66;">&#40;</span>Request : <span style="color: #46aa03; font-weight:bold;">in</span> AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">Data</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Data</span>
   <span style="color: #00007f;">is</span>
      Browser : <span style="color: #46aa03; font-weight:bold;">constant</span> String := AWS.<span style="color: #202020;">Status</span>.<span style="color: #202020;">User_Agent</span> <span style="color: #66cc66;">&#40;</span>Request<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">return</span> AWS.<span style="color: #202020;">Response</span>.<span style="color: #202020;">Build</span>
        <span style="color: #66cc66;">&#40;</span>Content_Type  =&gt; AWS.<span style="color: #202020;">MIME</span>.<span style="color: #202020;">Text_HTML</span>,
         Message_Body  =&gt;
           <span style="color: #7f007f;">&quot;&lt;h1&gt;Hello World!&lt;/h1&gt;&quot;</span> &amp;
           <span style="color: #7f007f;">&quot;&lt;p&gt;Browser used : &quot;</span> &amp; Browser &amp; <span style="color: #7f007f;">&quot;&lt;/p&gt;&quot;</span>,
         Status_Code   =&gt; AWS.<span style="color: #202020;">Messages</span>.<span style="color: #202020;">S200</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> Generate_Content;
&nbsp;
<span style="color: #00007f;">end</span> Hello_World;</pre></td></tr></table></div>

<p>As you can see this is very similar to <code>Not_Found</code>, which is only natural since both packages do nearly the same thing: Deliver some very simple HTML. Note that we grab the user agent from the <code>Request</code> object instead of the URI we used in the <code>Not_Found</code> handler.</p>
<p>And that is it. I hope that by now it is clear that adding HTTP support to your Ada program is <em>very</em> easy. Of course there's a lot more that can be done with AWS, and I will deal with that in coming articles. For example building the content using strings is rather lame, so in the next article I will turn my attention to using the AWS templating system for the content (<a href="http://docs.adacore.com/aws-docs/templates_parser.html">Templates_Parser</a>).</p>
<p>Oh and on a final note let me just mention the GPR file for this tutorial program, especially the very first line that says <code>with "aws";</code>. Yes, that is all you need to add to your project file for <code>gnatmake</code> to know that you want it to pull in AWS when compiling your project. It's wonderfully simple. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2013/02/10/using-the-ada-web-server-aws-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ada Programming Community @ Google+</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/12/08/ada-programming-community-google/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/12/08/ada-programming-community-google/#comments</comments>
		<pubDate>Sat, 08 Dec 2012 15:21:49 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[Community]]></category>
		<category><![CDATA[Google+]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=345</guid>
		<description><![CDATA[Google just rolled out its latest addition to the Google+ platform: Google+ Communities, and naturally we just had to have an Ada Programming Community. Hopefully the community will grow at a steady pace and become a good place for general discussion about Ada. We&#8217;ve already got two solid posts, one about the state of building <a href="http://blogs.fsfe.org/thomaslocke/2012/12/08/ada-programming-community-google/" title="Read ”Ada Programming Community @ Google+”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Google just rolled out its latest addition to the Google+ platform: <a href="http://www.google.com/+/learnmore/communities/">Google+ Communities</a>, and naturally we just had to have an <a href="https://plus.google.com/u/0/communities/102688015980369378804">Ada Programming Community</a>.</p>
<p>Hopefully the community will grow at a steady pace and become a good place for general discussion about Ada. We&#8217;ve already got two solid posts, one about the state of building Ada programs and another one with a suggestion for a tool that can improve the current situation where many Ada tools heavily depend on GNU make and all sorts of environment variables.</p>
<ul>
<li><a href="https://plus.google.com/u/0/102090077582777383295/posts/NHciyd726Xq">Why is gnu make and environment variables so prevalent in the Ada community?</a></li>
<li><a href="https://plus.google.com/u/0/102090077582777383295/posts/jPSppE8DtQK">The Ada tool</a></li>
</ul>
<p>Lively place eh?</p>
<p>If you&#8217;re interested in actual software engineering, be sure to join this new community and help make it spring to life.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/12/08/ada-programming-community-google/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MediaGoblin, an emerging free and open media publishing platform</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/10/28/mediagoblin-an-emerging-free-and-open-media-publishing-platform/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/10/28/mediagoblin-an-emerging-free-and-open-media-publishing-platform/#comments</comments>
		<pubDate>Sun, 28 Oct 2012 09:56:54 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Free Software]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[mediagoblin]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=343</guid>
		<description><![CDATA[I&#8217;ve already tried to raise awareness about GNU MediaGoblin at my Google+ stream using the #mediagoblin hashtag and as you can see there is some activity going on. But not enough. I wonder why that is. There&#8217;s a lot of open source people hanging out in the Google+ sphere, but judging from the relatively quiet <a href="http://blogs.fsfe.org/thomaslocke/2012/10/28/mediagoblin-an-emerging-free-and-open-media-publishing-platform/" title="Read ”MediaGoblin, an emerging free and open media publishing platform”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve already tried to raise awareness about <a href="http://mediagoblin.org/">GNU MediaGoblin</a> at my <a href="https://plus.google.com/u/0/112815721307813813920/posts">Google+ stream</a> using the <a href="https://plus.google.com/u/0/s/%23mediagoblin">#mediagoblin hashtag</a> and as you can see there is some activity going on. But not enough. I wonder why that is. There&#8217;s <strong>a lot</strong> of open source people hanging out in the Google+ sphere, but judging from the relatively quiet #mediagoblin hashtag, they don&#8217;t much care for having a free and open source media publishing platform.</p>
<p>It is rather sad I think.</p>
<p>I&#8217;m guessing that most of us <a href="http://fsfe.org">fsfe.org</a> people are fully aware of MediaGoblin and their current <a href="http://mediagoblin.org/pages/campaign.html">fundraising campaign</a>, but on the off chance that one of you have missed it completely, here goes: Please go support <a href="http://mediagoblin.org/pages/campaign.html">MediaGoblin</a>. Most of us can easily spare a few $, and if not then at least help spread the word. Lets do our best to make MediaGoblin a new household word. Post about it on Google+, facebook, twitter, identi.ca &#8211; wherever you hang out. You might hate social websites such as Google+ like the plague, but understand that they can also be used to spread knowledge about free and open software. It&#8217;s not all bad.</p>
<p>Lets do our best to make the <a href="https://plus.google.com/u/0/s/%23mediagoblin">#mediagoblin hashtag</a> fly!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/10/28/mediagoblin-an-emerging-free-and-open-media-publishing-platform/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding support for the ::json datatype to GNATColl</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/10/28/adding-support-for-the-json-datatype-to-gnatcoll/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/10/28/adding-support-for-the-json-datatype-to-gnatcoll/#comments</comments>
		<pubDate>Sun, 28 Oct 2012 09:14:12 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[GNATColl]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=334</guid>
		<description><![CDATA[GNATColl is a suite of reusable software components and utilities for the Ada programming language. One of the things that GNATColl can help you accomplish is interfacing with PostgreSQL and SQLite. What it cannot, as per GNATColl 2012, is handle the new ::json datatype added to version 9.2 of PostgreSQL. This is a bit of <a href="http://blogs.fsfe.org/thomaslocke/2012/10/28/adding-support-for-the-json-datatype-to-gnatcoll/" title="Read ”Adding support for the ::json datatype to GNATColl”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://libre.adacore.com/tools/gnat-component-collection/">GNATColl</a> is a suite of reusable software components and utilities for the <a href="http://en.wikibooks.org/wiki/Ada_Programming">Ada programming</a> language. One of the things that GNATColl can help you accomplish is interfacing with <a href="http://postgresql.org">PostgreSQL</a> and <a href="http://sqlite.org">SQLite</a>. What it cannot, as per GNATColl 2012, is handle the new <a href="http://www.postgresql.org/docs/9.2/static/datatype-json.html">::json datatype</a> added to version 9.2 of PostgreSQL.</p>
<p>This is a bit of a pain for those of us using Ada for web-development, especially those of us relying heavily on JSON. The current solution is to just stick with using the ::text datatype for JSON, and then manage validity in the application, which obviously is annoying given that PostgreSQL now can do all that for you.</p>
<p>So I decided to add ::json support to GNATColl, and as luck would have it, <a href="http://www.adacore.com/frontline-support/expertise/emmanuel-briot">Emmanuel Briot</a>, who appears to be the main contributor to GNATColl, from <a href="http://www.adacore.com/">AdaCore</a> did not oppose the idea. So I got cracking on the patch, and I&#8217;m now glad to announce that initial support is up and running, and it is at least working in my own projects. The patch has not been accepted into mainline GNATColl yet, and it wont be for at least another 2-3 weeks, as Emmanuel is on vacation.</p>
<p>The really nice thing about my patch is that it also takes advantage of the fact that GNATColl also handles <a href="http://blogs.fsfe.org/thomaslocke/2011/11/18/ada-with-a-side-of-json/">building JSON objects</a> so why not allow users of the GNATCOLL.SQL utilities to read ::json fields as GNATCOLL.JSON.JSON_Value objects? It looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">type</span> Cursor <span style="color: #00007f;">is</span> <span style="color: #46aa03; font-weight:bold;">new</span> GNATCOLL.<span style="color: #202020;">SQL</span>.<span style="color: #202020;">Exec</span>.<span style="color: #202020;">Forward_Cursor</span> <span style="color: #46aa03; font-weight:bold;">with</span> <span style="color: #46aa03; font-weight:bold;">null</span> <span style="color: #46aa03; font-weight:bold;">record</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">type</span> Row <span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">record</span>
      Some_JSON : GNATCOLL.<span style="color: #202020;">JSON</span>.<span style="color: #202020;">JSON_Value</span>;
   <span style="color: #00007f;">end</span> <span style="color: #46aa03; font-weight:bold;">record</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">function</span> Element
  <span style="color: #66cc66;">&#40;</span>C : <span style="color: #46aa03; font-weight:bold;">in</span> Cursor<span style="color: #66cc66;">&#41;</span>
   <span style="color: #00007f;">return</span> Row
<span style="color: #00007f;">is</span>
<span style="color: #00007f;">begin</span>
   <span style="color: #00007f;">return</span> Row'<span style="color: #66cc66;">&#40;</span>Attr_JSON =&gt; C.<span style="color: #202020;">Json_Object_Value</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">end</span> Element;
&nbsp;
A_Row : Row := C.<span style="color: #202020;">Element</span>;
<span style="color: #adadad; font-style: italic;">--  A_Row.Some_JSON is now a JSON_Value object.</span></pre></td></tr></table></div>

<p>Of vourse it is also possible to extract ::json as a string:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">type</span> Cursor <span style="color: #00007f;">is</span> <span style="color: #46aa03; font-weight:bold;">new</span> GNATCOLL.<span style="color: #202020;">SQL</span>.<span style="color: #202020;">Exec</span>.<span style="color: #202020;">Forward_Cursor</span> <span style="color: #46aa03; font-weight:bold;">with</span> <span style="color: #46aa03; font-weight:bold;">null</span> <span style="color: #46aa03; font-weight:bold;">record</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">type</span> Row <span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">record</span>
      Some_JSON   : GNATCOLL.<span style="color: #202020;">JSON</span>.<span style="color: #202020;">JSON_Value</span>;
      String_JSON : GNATCOLL.<span style="color: #202020;">JSON</span>.<span style="color: #202020;">UTF8_String</span>;
   <span style="color: #00007f;">end</span> <span style="color: #46aa03; font-weight:bold;">record</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">function</span> Element
  <span style="color: #66cc66;">&#40;</span>C : <span style="color: #46aa03; font-weight:bold;">in</span> Cursor<span style="color: #66cc66;">&#41;</span>
   <span style="color: #00007f;">return</span> Row
<span style="color: #00007f;">is</span>
<span style="color: #00007f;">begin</span>
   <span style="color: #00007f;">return</span> Row'<span style="color: #66cc66;">&#40;</span>Some_JSON   =&gt; C.<span style="color: #202020;">Json_Object_Value</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #66cc66;">&#41;</span>,
               String_JSON =&gt; C.<span style="color: #202020;">Json_String_value</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">end</span> Element;
&nbsp;
A_Row : Row := C.<span style="color: #202020;">Element</span>;
<span style="color: #adadad; font-style: italic;">--  A_Row.Some_JSON is now a JSON_Value object.</span>
<span style="color: #adadad; font-style: italic;">--  A_Row.String_JSON is now an UTF8_String.</span></pre></td></tr></table></div>

<p>Currently my patch only support writing UTF8_String JSON to the database, but I&#8217;d really like to have support for writing JSON_Value objects directly, but I&#8217;m not sure what Emmanuel will think about that idea. Time will tell I guess. I can see no reasons for not adding this, as users of the GNATCOLL.SQL interface also have GNATCOLL.JSON readily available, so no additional dependencies are being added to burden users. If you want to try this yourself, grab the latest SVN of GNATColl and my patched version:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.eu.adacore.com<span style="color: #000000; font-weight: bold;">/</span>anonsvn<span style="color: #000000; font-weight: bold;">/</span>Dev<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/</span>gps<span style="color: #000000; font-weight: bold;">/</span>gnatlib<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #c20cb9; font-weight: bold;">git clone</span> git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>ThomasLocke<span style="color: #000000; font-weight: bold;">/</span>GNATColl.git</pre></td></tr></table></div>

<p>You&#8217;ll have to do some manual mucking about with copying files from my version to the official version to make it work. The files you need to copy are these:</p>
<ul>
<li>src/postgres/with_postgres/gnatcoll-sql-postgres-builder.adb</li>
<li>src/sqlite/with_sqlite/gnatcoll-sql-sqlite-builder.adb</li>
<li>src/tools/gnatcoll_db2ada.adb</li>
<li>src/dborm.py</li>
<li>src/gnatcoll-sql-exec.adb</li>
<li>src/gnatcoll-sql-exec.ads</li>
<li>src/gnatcoll-sql-exec_private.adb</li>
<li>src/gnatcoll-sql-exec_private.ads</li>
<li>src/gnatcoll-sql-inspect.adb</li>
<li>src/gnatcoll-sql-inspect.ads</li>
<li>src/gnatcoll-sql.ads</li>
<li>src/gnatcoll-sql_impl.adb</li>
<li>src/gnatcoll-sql_impl.ads</li>
</ul>
<p>Note that the addition to dborm.py does not enable the JSON_Value stuff. For some odd reason the ORM stuff doesn&#8217;t work very well on my box (I get a bunch of Python and gnatchop errors), and since I don&#8217;t use ORM I didn&#8217;t spend an inordinate amount of time getting it fixed. If Emmanuel approves my patch, the ORM stuff will of course also be fixed.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/10/28/adding-support-for-the-json-datatype-to-gnatcoll/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8220;Hello World&#8221; GtkAda and Glade3 Tutorial</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/09/20/hello-world-gtkada-and-glade3-tutorial/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/09/20/hello-world-gtkada-and-glade3-tutorial/#comments</comments>
		<pubDate>Thu, 20 Sep 2012 07:12:44 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[Glade3]]></category>
		<category><![CDATA[GtkAda]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=329</guid>
		<description><![CDATA[Many years ago I tried my hand at Glade and PHP-Gtk (don&#8217;t ask!), and I found the entire experience to be fairly horrible. Since then I&#8217;ve steered well clear of anything to do with Gtk &#8211; in fact I&#8217;ve steered clear of GUI (Gtk/Qt) programming in general, mostly because I&#8217;ve found that if a program <a href="http://blogs.fsfe.org/thomaslocke/2012/09/20/hello-world-gtkada-and-glade3-tutorial/" title="Read ”&#8220;Hello World&#8221; GtkAda and Glade3 Tutorial”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Many years ago I tried my hand at Glade and PHP-Gtk (don&#8217;t ask!), and I found the entire experience to be fairly horrible. Since then I&#8217;ve steered well clear of anything to do with Gtk &#8211; in fact I&#8217;ve steered clear of GUI (Gtk/Qt) programming in general, mostly because I&#8217;ve found that if a program I&#8217;m building needs a GUI, it is usually much easier to just deploy it in the browser using HTML/CSS/Javascript (Argh! I really detest Javascript &#8211; thank God for the <a href="http://dartlang.org">Dart</a> project). </p>
<p>Well, that was then, and this is now.</p>
<p>Today I noticed that a new article had been added to the <a href="http://wiki.ada-dk.org">Ada-DK Wiki</a>: <a href="http://wiki.ada-dk.org/building_gui_with_glade_3">Building GUI With Glade3</a>. Naturally the short tutorial make use of <a href="http://en.wikibooks.org/wiki/Ada_Programming">Ada</a> and <a href="http://libre.adacore.com/tools/gtkada/">GtkAda</a>, so it&#8217;s right up my alley.</p>
<p>It looks absolutely doable. The code is clean and simple, and not at all as messy as my PHP-Gtk experiments those many years ago. Maybe it is time for me to give GtkAda and Glade3 a try?</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/09/20/hello-world-gtkada-and-glade3-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Yolk aligned with latest Ada Web Server update</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/09/06/yolk-aligned-with-latest-ada-web-server-update/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/09/06/yolk-aligned-with-latest-ada-web-server-update/#comments</comments>
		<pubDate>Thu, 06 Sep 2012 10:38:20 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Yolk]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=326</guid>
		<description><![CDATA[When I got back from my trip to Iceland, I did a git pull on AWS and among the ~89 patches that had gone in since my departure from Denmark, a handful added some new configuration parameters. I&#8217;ve added these new parameters to Yolk, so it now requires AWS commit 30281b522ed43782c8f9e7fd1f0fd912510b48f0 in order to work. <a href="http://blogs.fsfe.org/thomaslocke/2012/09/06/yolk-aligned-with-latest-ada-web-server-update/" title="Read ”Yolk aligned with latest Ada Web Server update”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>When I got back from my trip to Iceland, I did a git pull on <a href="http://libre.adacore.com/tools/aws/">AWS</a> and among the ~89 patches that had gone in since my departure from Denmark, a handful added some new configuration parameters. I&#8217;ve added these new parameters to <a href="https://github.com/ThomasLocke/yolk">Yolk</a>, so it now requires <a href="https://forge.open-do.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=aws/aws.git;a=commit;h=30281b522ed43782c8f9e7fd1f0fd912510b48f0">AWS commit 30281b522ed43782c8f9e7fd1f0fd912510b48f0</a> in order to work. It is no longer enough to just grab the latest official AWS package from <a href="http://libre.adacore.com">libre.adacore.com</a> &#8211; you must use the developer version of AWS.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/09/06/yolk-aligned-with-latest-ada-web-server-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Labeled exit bug in GNAT GPL 2012 confirmed</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/07/17/labeled-exit-bug-in-gnat-gpl-2012-confirmed/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/07/17/labeled-exit-bug-in-gnat-gpl-2012-confirmed/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 07:10:00 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[Ada 2012]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=318</guid>
		<description><![CDATA[While trying out the new Ada 2012 loop syntax, I discovered what appeared to be a bug in the compiler: I could not use labeled exit statements when using the new loop syntax. Observe this simple example: with Ada.Text_IO; &#160; procedure Loopy is use Ada.Text_IO; &#160; Foo : array &#40;1 .. 3&#41; of String &#40;1 <a href="http://blogs.fsfe.org/thomaslocke/2012/07/17/labeled-exit-bug-in-gnat-gpl-2012-confirmed/" title="Read ”Labeled exit bug in GNAT GPL 2012 confirmed”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>While trying out the new <a href="http://www.ada-auth.org/standards/12rat/html/Rat12-1-3-5.html">Ada 2012 loop syntax</a>, I discovered what appeared to be a bug in the compiler: I could not use labeled exit statements when using the new loop syntax. Observe this simple example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Loopy <span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
   Foo : <span style="color: #46aa03; font-weight:bold;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #46aa03; font-weight:bold;">of</span> String <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">3</span><span style="color: #66cc66;">&#41;</span>
     := <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">2</span> =&gt; <span style="color: #7f007f;">&quot;Bar&quot;</span>, <span style="color: #46aa03; font-weight:bold;">others</span> =&gt; <span style="color: #7f007f;">&quot;Foo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">begin</span>
   <span style="color: #adadad; font-style: italic;">--  Classic loop syntax</span>
   <span style="color: #00007f;">for</span> I <span style="color: #46aa03; font-weight:bold;">in</span> Foo'<span style="color: #46aa03; font-weight:bold;">Range</span> <span style="color: #00007f;">loop</span>
      Put_Line <span style="color: #66cc66;">&#40;</span>Foo <span style="color: #66cc66;">&#40;</span>I<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
&nbsp;
   <span style="color: #adadad; font-style: italic;">--  New loop syntax</span>
   <span style="color: #00007f;">for</span> K <span style="color: #46aa03; font-weight:bold;">of</span> Foo <span style="color: #00007f;">loop</span>
      Put_Line <span style="color: #66cc66;">&#40;</span>K<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
<span style="color: #00007f;">end</span> Loopy;</pre></td></tr></table></div>

<p>Compiling this with the <code>-gnat2012</code> works like a charm and output from the program is as expected:</p>
<pre>
Foo
Bar
Foo
Foo
Bar
Foo
</pre>
<p>Obviously using labeled loops/exits in this example is more or less completely pointless, but since I&#8217;m a bit of a renegade (and for the sake of the argument), I&#8217;m going to do it anyway. Lets see what happens:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Loopy <span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
   Foo : <span style="color: #46aa03; font-weight:bold;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #46aa03; font-weight:bold;">of</span> String <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">3</span><span style="color: #66cc66;">&#41;</span>
     := <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">2</span> =&gt; <span style="color: #7f007f;">&quot;Bar&quot;</span>, <span style="color: #46aa03; font-weight:bold;">others</span> =&gt; <span style="color: #7f007f;">&quot;Foo&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">begin</span>
   <span style="color: #adadad; font-style: italic;">--  Classic loop syntax</span>
   Loop_Classic : <span style="color: #00007f;">for</span> I <span style="color: #46aa03; font-weight:bold;">in</span> Foo'<span style="color: #46aa03; font-weight:bold;">Range</span> <span style="color: #00007f;">loop</span>
      <span style="color: #46aa03; font-weight:bold;">exit</span> Loop_Classic <span style="color: #46aa03; font-weight:bold;">When</span> Foo <span style="color: #66cc66;">&#40;</span>I<span style="color: #66cc66;">&#41;</span> = <span style="color: #7f007f;">&quot;Bar&quot;</span>;	  
      Put_Line <span style="color: #66cc66;">&#40;</span>Foo <span style="color: #66cc66;">&#40;</span>I<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span> Loop_Classic;
&nbsp;
   <span style="color: #adadad; font-style: italic;">--  New loop syntax</span>
   Loop_New : <span style="color: #00007f;">for</span> K <span style="color: #46aa03; font-weight:bold;">of</span> Foo <span style="color: #00007f;">loop</span>
      <span style="color: #46aa03; font-weight:bold;">exit</span> Loop_New <span style="color: #46aa03; font-weight:bold;">when</span> K = <span style="color: #7f007f;">&quot;Bar&quot;</span>;    
      Put_Line <span style="color: #66cc66;">&#40;</span>K<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span> Loop_New;
<span style="color: #00007f;">end</span> Loopy;</pre></td></tr></table></div>

<p>Awesome labels I&#8217;ve come up with eh&#8217;?</p>
<p>Compiling the above example yields this little puppy:</p>
<pre>
gcc -c -gnat2012 loopy.adb
loopy.adb:17:07: invalid loop name in exit statement
gnatmake: "loopy.adb" compilation error
</pre>
<p>Gaagle! The horror! The pain! The bug has been reported to <a href="http://adacore.com">AdaCore</a> by <a href="http://identi.ca/marcc">Marc A. Criley</a>, and according to <a href="http://identi.ca/notice/95432414">this notice</a> they&#8217;ve sorta/kinda acknowledged it.</p>
<p>So all we have to do now is wait for a fix. When I cried out on <a href="https://groups.google.com/d/msg/comp.lang.ada/RcbiaAJ0KS0/BTmkk1QYf3sJ">comp.lang.ada</a> that I wanted my labeled exit statements back, he replied with this:</p>
<blockquote><p>
GNAT GPL 2013
</p></blockquote>
<p>Hehe.. Dry fellow that Marc. But he&#8217;s probably right. I&#8217;m just going to have to patiently wait an entire year before I can actually use the new loop syntax and labeled exit statements at the same time. I can handle that. I think.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/07/17/labeled-exit-bug-in-gnat-gpl-2012-confirmed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eating Pez With Ada</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/07/16/eating-pez-with-ada/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/07/16/eating-pez-with-ada/#comments</comments>
		<pubDate>Mon, 16 Jul 2012 14:23:05 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[Concurrent]]></category>
		<category><![CDATA[protected objects]]></category>
		<category><![CDATA[Tasking]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=239</guid>
		<description><![CDATA[Back in May I wrote an article called Concurrent Ada Programming. It was meant as an introduction to the wonderful world of Ada tasking and protected objects, and while the code presented in the article most certainly did not muck about with complex constructions and mind-bending designs, it did however expose the reader to my <a href="http://blogs.fsfe.org/thomaslocke/2012/07/16/eating-pez-with-ada/" title="Read ”Eating Pez With Ada”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Back in May I wrote an article called <a href="http://blogs.fsfe.org/thomaslocke/2012/05/17/concurrent-ada-programming/">Concurrent Ada Programming</a>. It was meant as an introduction to the wonderful world of Ada tasking and protected objects, and while the code presented in the article most certainly did not muck about with complex constructions and mind-bending designs, it did however expose the reader to my rather feeble attempts at naming identifiers, as Georg Bauhaus was kind enough to point out to me:</p>
<blockquote><p>
Can I make some comments about the choice of identifiers? That’s from the perspective of a reader, totally subjective, of course. It reflects what had me a little confused at first, looking at the source (not the explanation). For example, there are: Jobs (Positive), Job (Natural), Jobs (protected), A_Job (Natural), Job as an immutable loop variable. And none of these is, well, if I may, a job (task). They are jobs’ numbers, though, as the output indicates.</p>
<p>This might all appear nitpicky, I guess. I always found good names helpful for understanding source from source, which is why…
</p></blockquote>
<p>Of course Georg is completely right, and because it did <strong>not</strong> appear nit-picky to me at all, I decided to code a completely new example, this time focusing on the beloved <a href="http://en.wikipedia.org/wiki/Pez">Pez</a> dispenser. One Pez dispenser + several children = perfect situation to flex a bit of Ada tasking muscle. I&#8217;ve tried my best to name the identifiers properly, and I do believe I&#8217;ve done a marginally better job of it than in my previous article. </p>
<p>So without further ado, lets get on with the Ada programming, and check out a basic sequential Pez dispenser example (one child, one dispenser). Note that the example make use of an Ada 2012 feature, so you must have an Ada 2012 compiler to make it work. I&#8217;ve used the <a href="http://libre.adacore.com/download/">GNAT GPL 2012</a> compiler.</p>
<p>Before you read on, please <a href="https://github.com/ThomasLocke/Pez_Dispenser">clone the Pez_Dispenser</a> project from my github account, so you have all the files available.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Task_Identification</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Sequential <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Task_Identification</span>;
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
   Task_Id : <span style="color: #46aa03; font-weight:bold;">constant</span> String := Image <span style="color: #66cc66;">&#40;</span>Current_Task<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">type</span> Reward <span style="color: #00007f;">is</span> <span style="color: #66cc66;">&#40;</span>Candy, No_Candy<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">type</span> Dispenser <span style="color: #00007f;">is</span>
      <span style="color: #46aa03; font-weight:bold;">record</span>
         Available_Candies : Natural := <span style="color: #ff0000;">20</span>;
      <span style="color: #00007f;">end</span> <span style="color: #46aa03; font-weight:bold;">record</span>;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Pop
     <span style="color: #66cc66;">&#40;</span>D : <span style="color: #46aa03; font-weight:bold;">in</span> <span style="color: #46aa03; font-weight:bold;">out</span> Dispenser<span style="color: #66cc66;">&#41;</span>
   <span style="color: #00007f;">return</span> Reward;
   <span style="color: #adadad; font-style: italic;">--  Get one of those delicious candies!</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">-----------</span>
   <span style="color: #adadad; font-style: italic;">--  Pop  --</span>
   <span style="color: #adadad; font-style: italic;">-----------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">function</span> Pop
     <span style="color: #66cc66;">&#40;</span>D : <span style="color: #46aa03; font-weight:bold;">in</span> <span style="color: #46aa03; font-weight:bold;">out</span> Dispenser<span style="color: #66cc66;">&#41;</span>
     <span style="color: #00007f;">return</span> Reward
   <span style="color: #00007f;">is</span>
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">case</span> D.<span style="color: #202020;">Available_Candies</span> <span style="color: #00007f;">is</span>
         <span style="color: #46aa03; font-weight:bold;">when</span> <span style="color: #ff0000;">0</span> =&gt;
            <span style="color: #00007f;">return</span> No_Candy;
         <span style="color: #46aa03; font-weight:bold;">when</span> <span style="color: #46aa03; font-weight:bold;">others</span> =&gt;
            D.<span style="color: #202020;">Available_Candies</span> := D.<span style="color: #202020;">Available_Candies</span> - <span style="color: #ff0000;">1</span>;
            Put_Line <span style="color: #66cc66;">&#40;</span>Task_Id &amp; <span style="color: #7f007f;">&quot; pops a candy from the dispenser.&quot;</span><span style="color: #66cc66;">&#41;</span>;
            Put_Line <span style="color: #66cc66;">&#40;</span>Natural'Image <span style="color: #66cc66;">&#40;</span>D.<span style="color: #202020;">Available_Candies</span><span style="color: #66cc66;">&#41;</span> 
                      &amp; <span style="color: #7f007f;">&quot; left in the dispenser&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #00007f;">return</span> Candy;
      <span style="color: #00007f;">end</span> <span style="color: #00007f;">case</span>;
   <span style="color: #00007f;">end</span> Pop;
&nbsp;
   Result        : Reward;
   Pez_Dispenser : Dispenser;
&nbsp;
<span style="color: #00007f;">begin</span>
&nbsp;
   A_Child_Popping_Pez : <span style="color: #00007f;">loop</span>
      Result := Pop <span style="color: #66cc66;">&#40;</span>Pez_Dispenser<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">exit</span> A_Child_Popping_Pez <span style="color: #46aa03; font-weight:bold;">when</span> Result = No_Candy;
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">delay</span> <span style="color: #ff0000;">1.0</span>; 
      <span style="color: #adadad; font-style: italic;">--  This simulates chewing. Yes, a 1 second chew is pretty darn fast!</span>
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span> A_Child_Popping_Pez;
&nbsp;
   Put_Line <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;No more candy! &quot;</span> &amp; Task_Id &amp; <span style="color: #7f007f;">&quot; runs out to play.&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #00007f;">end</span> Sequential;</pre></td></tr></table></div>

<p>Looks nice eh? I am hopeful that it reads better than the example from my previous article, and if it doesn&#8217;t, well then I think I&#8217;ll just give up on this subject.</p>
<p>But for now lets assume that the example code is good, and do a quick rundown of what is going on.</p>
<p>First we <code>with</code> the two packages <code>Ada.Task_Identification</code> and <code>Ada.Text_IO</code>. What this does is make the subprograms and types from those packages visible and available to our program, somewhat analogous to the <code>#include</code> directive used in C. After that we setup our &#8220;main&#8221; procedure and name it Sequential. In Ada you can call your main procedure whatever you like. </p>
<p>The <code>Ada.Task_Identification</code> package makes it possible for us to figure out the id of a task and <code>Ada.Text_IO</code> makes the <code>Put_Line</code> procedure available to Sequential.</p>
<p>The <code>use</code> clauses we encounter next lets us access the tools of the <code>Ada.Text_IO</code> and <code>Ada.Task_Identification</code> packages without having to prefix every type and subprogram with the package name. This is equivalent to the <code>using namespace</code> statement in C++. Note that it is fairly easy to abuse <code>use</code> clauses to a point where your code becomes almost unreadable. Another option is using renames instead, for example like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Foo <span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">package</span> IO <span style="color: #46aa03; font-weight:bold;">renames</span> Ada.<span style="color: #202020;">Text_IO</span>;
<span style="color: #00007f;">begin</span>
   IO.<span style="color: #202020;">Put_Line</span> <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;Hey!&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">end</span> Foo;</pre></td></tr></table></div>

<p>But I digress. Lets get back on track!</p>
<p>Next up we declare and initialize the <code>Task_Id</code> constant. This is basically just a string representation of the internal id for the main environment task. Both <code>Image</code> and <code>Current_Task</code> are methods found in the <code>Ada.Task_Identification</code> package. We use the <code>Task_Id</code> constant to identify the task that is currently chewing on a snack.</p>
<p>After the <code>Task_Id</code> declaration, we define the new type <code>Reward</code> as an enumeration type with the possible values <code>Candy</code> and <code>No_Candy</code>. The <code>Dispenser</code> type is a record with a single component, <code>Available_Candies</code>, which is initialized with the value 20, meaning that all new dispensers comes loaded with 20 delicious candies. Not too shabby!</p>
<p>The <code>Pop</code> function takes a <code>Dispenser</code> and returns a <code>Reward</code>. If there are still candies in the dispenser, then it decreases the number of available candies by 1. Note that it is the <code>D : in out Dispenser</code> parameter in this function that requires an Ada 2012 compiler. Older versions of Ada only allow <code>in</code> parameters when using functions.</p>
<p>Finally we declare the <code>Result</code> variable as a <code>Reward</code> and the <code>Pez_Dispenser</code> variable as a <code>Dispenser</code>.</p>
<p>And that my friends was all the declaration stuff (the part between <code>is</code> and <code>begin</code>). Next up: The body of the program (the part between <code>begin</code> and <code>end</code>).</p>
<p>The meat of the body is the <code>A_Child_Popping_Pez</code> loop, in which the first thing we do is call the <code>Pop</code> function with our <code>Pez_Dispenser</code> and put the reward in the <code>Result</code> variable. After that we exit the loop if the reward given in <code>Result</code> is <code>No_Candy</code>. If the given reward is <code>Candy</code> we proceed with some chewing in the form of the <code>delay 1.0</code> statement. This proceeds until all the candies are gone and the dispenser is empty.</p>
<p>When the loop exits we cheerfully let the world know that because there are no more candies, we might as well run out to play.</p>
<p>Let us compile and run the program:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> sequential
$ gnatmake <span style="color: #660033;">-P</span> sequential.gpr
$ <span style="color: #000000; font-weight: bold;">time</span> sequential</pre></td></tr></table></div>

<p>You should now see something like this flow by:</p>
<pre>
thomas@t420:~/Ada/Pez_Dispenser/sequential$ time sequential
main_task_000000000064C010 pops a candy from the dispenser.
 19 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 18 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 17 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 16 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 15 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 14 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 13 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 12 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 11 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 10 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 9 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 8 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 7 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 6 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 5 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 4 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 3 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 2 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 1 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
main_task_000000000064C010 pops a candy from the dispenser.
 0 left in the dispenser
main_task_000000000064C010 is chewing on a candy.
No more candy! main_task_000000000064C010 runs out to play.

real    0m20.004s
user    0m0.000s
sys     0m0.002s
</pre>
<p>As you can see it takes the <code>main_task_000000000064C010</code> (you might get a different name on your system) child 20 seconds to chew its way through all the candies in the Pez dispenser. </p>
<p>We can make this go &#8220;faster&#8221; by adding some more children and have them share the dispenser. Lets see how we can do that using tasks and a protected object.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Task_Identification</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Concurrent <span style="color: #00007f;">is</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Task_Identification</span>;
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">type</span> Reward <span style="color: #00007f;">is</span> <span style="color: #66cc66;">&#40;</span>Candy, No_Candy<span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">protected</span> Pez_Dispenser <span style="color: #00007f;">is</span>
      <span style="color: #46aa03; font-weight:bold;">procedure</span> Pop
        <span style="color: #66cc66;">&#40;</span>Result  : <span style="color: #46aa03; font-weight:bold;">out</span> Reward;
         Task_Id : <span style="color: #46aa03; font-weight:bold;">in</span>  String<span style="color: #66cc66;">&#41;</span>;
      <span style="color: #adadad; font-style: italic;">--  Get one of those delicious candies!</span>
   <span style="color: #46aa03; font-weight:bold;">private</span>
      Available_Candies : Natural := <span style="color: #ff0000;">20</span>;
   <span style="color: #00007f;">end</span> Pez_Dispenser;
&nbsp;
   <span style="color: #adadad; font-style: italic;">---------------------</span>
   <span style="color: #adadad; font-style: italic;">--  Pez_Dispenser  --</span>
   <span style="color: #adadad; font-style: italic;">---------------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">protected</span> <span style="color: #46aa03; font-weight:bold;">body</span> Pez_Dispenser <span style="color: #00007f;">is</span>
      <span style="color: #adadad; font-style: italic;">-----------</span>
      <span style="color: #adadad; font-style: italic;">--  Pop  --</span>
      <span style="color: #adadad; font-style: italic;">-----------</span>
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">procedure</span> Pop
        <span style="color: #66cc66;">&#40;</span>Result  : <span style="color: #46aa03; font-weight:bold;">out</span> Reward;
         Task_Id : <span style="color: #46aa03; font-weight:bold;">in</span>  String<span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">is</span>
      <span style="color: #00007f;">begin</span>
         <span style="color: #00007f;">case</span> Available_Candies <span style="color: #00007f;">is</span>
            <span style="color: #46aa03; font-weight:bold;">when</span> <span style="color: #ff0000;">0</span> =&gt;
               Result := No_Candy;
            <span style="color: #46aa03; font-weight:bold;">when</span> <span style="color: #46aa03; font-weight:bold;">others</span> =&gt;
               Result := Candy;
&nbsp;
               Available_Candies := Available_Candies - <span style="color: #ff0000;">1</span>;
&nbsp;
               Put_Line <span style="color: #66cc66;">&#40;</span>Task_Id &amp; <span style="color: #7f007f;">&quot; pops a candy from the dispenser.&quot;</span><span style="color: #66cc66;">&#41;</span>;
               Put_Line <span style="color: #66cc66;">&#40;</span>Natural'Image <span style="color: #66cc66;">&#40;</span>Available_Candies<span style="color: #66cc66;">&#41;</span> 
                         &amp; <span style="color: #7f007f;">&quot; left in the dispenser&quot;</span><span style="color: #66cc66;">&#41;</span>;
         <span style="color: #00007f;">end</span> <span style="color: #00007f;">case</span>;
      <span style="color: #00007f;">end</span> Pop;
   <span style="color: #00007f;">end</span> Pez_Dispenser;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">type</span> Child;
   <span style="color: #adadad; font-style: italic;">--  A Child wants candy, and when the child gets a candy it spends time</span>
   <span style="color: #adadad; font-style: italic;">--  chewing it. If there's no more candy, the child exits the &quot;pop&quot; loop</span>
   <span style="color: #adadad; font-style: italic;">--  and goes out to play in the garden.</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">--------------</span>
   <span style="color: #adadad; font-style: italic;">--  Child  --</span>
   <span style="color: #adadad; font-style: italic;">--------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">body</span> Child 
   <span style="color: #00007f;">is</span>
      Task_Id : <span style="color: #46aa03; font-weight:bold;">constant</span> String := Image <span style="color: #66cc66;">&#40;</span>Current_Task<span style="color: #66cc66;">&#41;</span>;
      Result  : Reward;
   <span style="color: #00007f;">begin</span>
      A_Child_Popping_Pez : <span style="color: #00007f;">loop</span>
         Pez_Dispenser.<span style="color: #202020;">Pop</span> <span style="color: #66cc66;">&#40;</span>Result, Task_Id<span style="color: #66cc66;">&#41;</span>;
&nbsp;
         <span style="color: #46aa03; font-weight:bold;">exit</span> A_Child_Popping_Pez <span style="color: #46aa03; font-weight:bold;">when</span> Result = No_Candy;
&nbsp;
         <span style="color: #46aa03; font-weight:bold;">delay</span> <span style="color: #ff0000;">1.0</span>; 
      <span style="color: #adadad; font-style: italic;">--  This simulates chewing. Yes, a 1 second chew is pretty darn fast!</span>
      <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span> A_Child_Popping_Pez;
&nbsp;
      Put_Line <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;No more candy! &quot;</span> &amp; Task_Id &amp; <span style="color: #7f007f;">&quot; runs out to play.&quot;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> Child;
&nbsp;
   Alice  : Child;
   Bob    : Child;
   Clair  : Child;
   Dwayne : Child;
&nbsp;
<span style="color: #00007f;">begin</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">null</span>;  <span style="color: #adadad; font-style: italic;">--  We're not using the main environment task for anything.</span>
&nbsp;
<span style="color: #00007f;">end</span> Concurrent;</pre></td></tr></table></div>

<p>Not too complicated I should say. We <code>with</code> and <code>use</code> the same packages as before, but after that there are a few new things, first of which is a protected object. In Ada these are objects that export procedures, functions and entries that enables us to interact with the encapsulated data structure. In our case we’ve set up a protected object called <code>Pez_Dispenser</code> (effectively a singleton), but we could just as well have created a protected type and then declared one of more objects to be of that type, as we did with the <code>Dispenser</code> type in the sequential example.</p>
<p>We can only operate on the data structure of a protected object via the exported subprograms and this is done under automatic mutual exclusion. A protected object will allow many concurrent readers (via exported functions), but only one writer (via exported procedures and entries).</p>
<p>A protected object is just what we need to protect our Pez dispenser from race conditions (raging kids all grabbing for the same candy!), now that we have several tasks (children) querying and updating it.</p>
<p>Lets take a closer look at the code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">protected</span> Pez_Dispenser <span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">procedure</span> Pop
     <span style="color: #66cc66;">&#40;</span>Result  : <span style="color: #46aa03; font-weight:bold;">out</span> Reward;
      Task_Id : <span style="color: #46aa03; font-weight:bold;">in</span>  String<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #adadad; font-style: italic;">--  Get one of those delicious candies!</span>
<span style="color: #46aa03; font-weight:bold;">private</span>
   Available_Candies : Natural := <span style="color: #ff0000;">20</span>;
<span style="color: #00007f;">end</span> Pez_Dispenser;
&nbsp;
<span style="color: #adadad; font-style: italic;">---------------------</span>
<span style="color: #adadad; font-style: italic;">--  Pez_Dispenser  --</span>
<span style="color: #adadad; font-style: italic;">---------------------</span>
&nbsp;
<span style="color: #46aa03; font-weight:bold;">protected</span> <span style="color: #46aa03; font-weight:bold;">body</span> Pez_Dispenser <span style="color: #00007f;">is</span>
   <span style="color: #adadad; font-style: italic;">-----------</span>
   <span style="color: #adadad; font-style: italic;">--  Pop  --</span>
   <span style="color: #adadad; font-style: italic;">-----------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">procedure</span> Pop
     <span style="color: #66cc66;">&#40;</span>Result  : <span style="color: #46aa03; font-weight:bold;">out</span> Reward;
      Task_Id : <span style="color: #46aa03; font-weight:bold;">in</span>  String<span style="color: #66cc66;">&#41;</span>
   <span style="color: #00007f;">is</span>
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">case</span> Available_Candies <span style="color: #00007f;">is</span>
         <span style="color: #46aa03; font-weight:bold;">when</span> <span style="color: #ff0000;">0</span> =&gt;
            Result := No_Candy;
         <span style="color: #46aa03; font-weight:bold;">when</span> <span style="color: #46aa03; font-weight:bold;">others</span> =&gt;
            Result := Candy;
&nbsp;
            Available_Candies := Available_Candies - <span style="color: #ff0000;">1</span>;
&nbsp;
            Put_Line <span style="color: #66cc66;">&#40;</span>Task_Id &amp; <span style="color: #7f007f;">&quot; pops a candy from the dispenser.&quot;</span><span style="color: #66cc66;">&#41;</span>;
            Put_Line <span style="color: #66cc66;">&#40;</span>Natural'Image <span style="color: #66cc66;">&#40;</span>Available_Candies<span style="color: #66cc66;">&#41;</span> 
                      &amp; <span style="color: #7f007f;">&quot; left in the dispenser&quot;</span><span style="color: #66cc66;">&#41;</span>;
      <span style="color: #00007f;">end</span> <span style="color: #00007f;">case</span>;
   <span style="color: #00007f;">end</span> Pop;
<span style="color: #00007f;">end</span> Pez_Dispenser;</pre></td></tr></table></div>

<p>First we declare our <code>Pez_Dispenser</code> protected object where we export the sole <code>Pop</code> procedure, which is the only way for us to interact with the very simple data structure consisting only of the variable <code>Available_Candies</code>.</p>
<p>In the body of <code>Pez_Dispenser</code> we find the actual implementation of the <code>Pop</code> procedure. If <code>Available_Candies</code> is greater than 0 then <code>Result</code> is set to <code>Candy</code>, <code>Available_Candies</code> is reduced by 1 and a short message about who did the popping and how many candies are left in the dispenser is written to STDOUT. If <code>Available_Candies</code> is 0 then <code>Result</code> is set to <code>No_Candy</code>.</p>
<p>With this simple system in place we’re ensured that access to <code>Available_Candies</code> is only ever granted to one single task (child) at a time. As soon as one task is done popping a Pez, access is opened up to the next task.</p>
<p>With a secure <code>Pez_Dispenser</code> in place we move on to creating the tasks that will be doing the actual eating:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">type</span> Child;
<span style="color: #adadad; font-style: italic;">--  A Child wants candy, and when the child gets a candy it spends time</span>
<span style="color: #adadad; font-style: italic;">--  chewing it. If there's no more candy, the child exits the &quot;pop&quot; loop</span>
<span style="color: #adadad; font-style: italic;">--  and goes out to play in the garden.</span>
&nbsp;
<span style="color: #adadad; font-style: italic;">--------------</span>
<span style="color: #adadad; font-style: italic;">--  Child  --</span>
<span style="color: #adadad; font-style: italic;">--------------</span>
&nbsp;
<span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">body</span> Child 
<span style="color: #00007f;">is</span>
   Task_Id : <span style="color: #46aa03; font-weight:bold;">constant</span> String := Image <span style="color: #66cc66;">&#40;</span>Current_Task<span style="color: #66cc66;">&#41;</span>;
   Result  : Reward;
<span style="color: #00007f;">begin</span>
   A_Child_Popping_Pez : <span style="color: #00007f;">loop</span>
      Pez_Dispenser.<span style="color: #202020;">Pop</span> <span style="color: #66cc66;">&#40;</span>Result, Task_Id<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">exit</span> A_Child_Popping_Pez <span style="color: #46aa03; font-weight:bold;">when</span> Result = No_Candy;
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">delay</span> <span style="color: #ff0000;">1.0</span>; 
      <span style="color: #adadad; font-style: italic;">--  This simulates chewing. Yes, a 1 second chew is pretty darn fast!</span>
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span> A_Child_Popping_Pez;
&nbsp;
   Put_Line <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;No more candy! &quot;</span> &amp; Task_Id &amp; <span style="color: #7f007f;">&quot; runs out to play.&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #00007f;">end</span> Child;</pre></td></tr></table></div>

<p>First we declare a task type called <code>Child</code>. We could’ve setup a task object, just as we did with the <code>Pez_Dispenser</code> protected object, but since we want more than one <code>Child</code> a type is the way to go. The body of the task looks a lot like the body from the Sequential program. </p>
<p>Inside the <code>Child</code> task we declare the <code>Task_Id</code> constant so we can identify each of the running <code>Child</code> tasks, and then we declare the <code>Result</code> variable to be of the type <code>Reward</code>.</p>
<p>In the body of the <code>Child</code> task we’ve got a plain named loop in which the first thing we do is try and pop a candy from the <code>Pez_Dispenser</code>. If <code>Result</code> equals <code>No_Candy</code> after having called <code>Pop</code> we immediately exit the loop and output the sad &#8220;No more candy!&#8221; message. If <code>Result</code> is <code>Candy</code> we Chew to our hearts content, again signified by the <code>delay 1.0</code> statement.</p>
<p>Last we have this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;">   Alice  : Child;
   Bob    : Child;
   Clair  : Child;
   Dwayne : Child;
&nbsp;
<span style="color: #00007f;">begin</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">null</span>;  <span style="color: #adadad; font-style: italic;">--  We're not using the main environment task for anything.</span>
&nbsp;
<span style="color: #00007f;">end</span> Concurrent;</pre></td></tr></table></div>

<p>Here we declare the children Alice, Bob, Clair and Dwayne. As soon as the program reaches this spot, four <code>Child</code> tasks are created and when we pass <code>begin</code> they spring to life. Since we already have 4 children trying to get to the candy like there’s no tomorrow, we don’t really need to add any code to the main environment task, so we simply provide a null statement. The main environment task is the master of the 4 <code>Child</code> tasks, so it wont complete until all 4 <code>Child</code> tasks have completed.</p>
<p>Lets compile and see how the program performs now:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> concurrent
$ gnatmake <span style="color: #660033;">-P</span> sequential.gpr
$ <span style="color: #000000; font-weight: bold;">time</span> sequential</pre></td></tr></table></div>

<pre>
thomas@t420:~/Ada/Pez_Dispenser/concurrent$ time concurrent
bob_000000000065D2C0 pops a candy from the dispenser.
 19 left in the dispenser
bob_000000000065D2C0 is chewing on a candy.
dwayne_0000000000663D80 pops a candy from the dispenser.
 18 left in the dispenser
dwayne_0000000000663D80 is chewing on a candy.
alice_0000000000659D60 pops a candy from the dispenser.
 17 left in the dispenser
alice_0000000000659D60 is chewing on a candy.
clair_0000000000660820 pops a candy from the dispenser.
 16 left in the dispenser
clair_0000000000660820 is chewing on a candy.
bob_000000000065D2C0 pops a candy from the dispenser.
 15 left in the dispenser
bob_000000000065D2C0 is chewing on a candy.
alice_0000000000659D60 pops a candy from the dispenser.
 14 left in the dispenser
alice_0000000000659D60 is chewing on a candy.
dwayne_0000000000663D80 pops a candy from the dispenser.
 13 left in the dispenser
dwayne_0000000000663D80 is chewing on a candy.
clair_0000000000660820 pops a candy from the dispenser.
 12 left in the dispenser
clair_0000000000660820 is chewing on a candy.
bob_000000000065D2C0 pops a candy from the dispenser.
 11 left in the dispenser
bob_000000000065D2C0 is chewing on a candy.
alice_0000000000659D60 pops a candy from the dispenser.
 10 left in the dispenser
alice_0000000000659D60 is chewing on a candy.
dwayne_0000000000663D80 pops a candy from the dispenser.
 9 left in the dispenser
dwayne_0000000000663D80 is chewing on a candy.
clair_0000000000660820 pops a candy from the dispenser.
 8 left in the dispenser
clair_0000000000660820 is chewing on a candy.
bob_000000000065D2C0 pops a candy from the dispenser.
 7 left in the dispenser
bob_000000000065D2C0 is chewing on a candy.
alice_0000000000659D60 pops a candy from the dispenser.
 6 left in the dispenser
alice_0000000000659D60 is chewing on a candy.
dwayne_0000000000663D80 pops a candy from the dispenser.
 5 left in the dispenser
dwayne_0000000000663D80 is chewing on a candy.
clair_0000000000660820 pops a candy from the dispenser.
 4 left in the dispenser
clair_0000000000660820 is chewing on a candy.
bob_000000000065D2C0 pops a candy from the dispenser.
 3 left in the dispenser
bob_000000000065D2C0 is chewing on a candy.
alice_0000000000659D60 pops a candy from the dispenser.
 2 left in the dispenser
alice_0000000000659D60 is chewing on a candy.
dwayne_0000000000663D80 pops a candy from the dispenser.
 1 left in the dispenser
dwayne_0000000000663D80 is chewing on a candy.
clair_0000000000660820 pops a candy from the dispenser.
 0 left in the dispenser
clair_0000000000660820 is chewing on a candy.
No more candy! bob_000000000065D2C0 runs out to play.
No more candy! alice_0000000000659D60 runs out to play.
No more candy! dwayne_0000000000663D80 runs out to play.
No more candy! clair_0000000000660820 runs out to play.

real    0m5.012s
user    0m0.002s
sys     0m0.001s
</pre>
<p>There you have it. With 4 children attacking our Pez dispenser it takes a mere 5 seconds to empty it. Obviously the performance gains are normally not as linear as in this simple example where chewing each candy amounts to no more than a simple <code>delay 1.0</code> statement. The performance gained from adding concurrency to a program depends on a lot of different factors.</p>
<p>The important point to get across is that adding concurrency to a program doesn&#8217;t have to be complicated. Adding concurrency to an Ada program is very simple, and the model for doing it is easy, both to understand and to work with. When you&#8217;re using Ada there&#8217;s simply no excuse for not adding concurrency to your program where it will benefit from it.</p>
<p>There is of course a whole lot more to Ada and concurrent programming than shown here. This little example has barely scratched the surface. A good place to start is the <a href="http://en.wikibooks.org/wiki/Ada_Programming/Tasking">Ada Programming Wikibook</a>.</p>
<p>You can grab the code for these examples at <a href="https://github.com/ThomasLocke/Pez_Dispenser">github.com/ThomasLocke/Pez_Dispenser</a>.</p>
<p>Finally a big thank you goes out to my good friend Dwight Scott Miller for helping me weed out the worst parts of my Danish-ified English. I really appreciate it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/07/16/eating-pez-with-ada/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Concurrent Ada Programming</title>
		<link>http://blogs.fsfe.org/thomaslocke/2012/05/17/concurrent-ada-programming/</link>
		<comments>http://blogs.fsfe.org/thomaslocke/2012/05/17/concurrent-ada-programming/#comments</comments>
		<pubDate>Thu, 17 May 2012 13:36:32 +0000</pubDate>
		<dc:creator>Thomas Løcke</dc:creator>
				<category><![CDATA[Ada Programming]]></category>
		<category><![CDATA[Ada]]></category>
		<category><![CDATA[Concurrent]]></category>
		<category><![CDATA[protected objects]]></category>
		<category><![CDATA[Tasking]]></category>

		<guid isPermaLink="false">http://blogs.fsfe.org/thomaslocke/?p=194</guid>
		<description><![CDATA[Ada&#8217;s model for doing concurrent programming is absolutely marvelous, and today I&#8217;m going to give you a small taste of it. More or less all programming languages provide tools for concurrent programming, but few do it as elegant as Ada, where the whole concept has been build into the language from it&#8217;s inception in 1983. <a href="http://blogs.fsfe.org/thomaslocke/2012/05/17/concurrent-ada-programming/" title="Read ”Concurrent Ada Programming”" class="read-more">Read more &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Ada&#8217;s model for doing concurrent programming is absolutely marvelous, and today I&#8217;m going to give you a small taste of it. More or less all programming languages provide tools for concurrent programming, but few do it as elegant as Ada, where the whole concept has been build into the language from it&#8217;s inception in 1983.</p>
<p>In Ada concurrent programming is a first class citizen, not an afterthought, and in this day and age where even the cheapest of processors have multiple cores, being able to properly harness the power of those many cores is important. A lot of programs could benefit from a bit of concurrent magic, but since concurrent programming brings with it a whole new set of problems, many programmers shy away from it. Not so with Ada programmers. We have tasks, protected objects, scheduling, guards and entries right there in front of us. In this article I&#8217;m going to show you how tasks and protected objects can be used to add concurrency to a very simple program.</p>
<p>The first thing we&#8217;re going to do is setup a small program that solves the plain problem of counting 20 times to 1_000_000_000.</p>
<p>One possible Ada solution to this problem is this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Task_Identification</span>;
<span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Sequential
<span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Task_Identification</span>;
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
   Id   : <span style="color: #46aa03; font-weight:bold;">constant</span> String := Image <span style="color: #66cc66;">&#40;</span>Current_Task<span style="color: #66cc66;">&#41;</span>;
   Jobs : Positive := <span style="color: #ff0000;">20</span>;
<span style="color: #00007f;">begin</span>
   <span style="color: #00007f;">for</span> Job <span style="color: #46aa03; font-weight:bold;">in</span> <span style="color: #46aa03; font-weight:bold;">reverse</span> <span style="color: #ff0000;">1</span> .. <span style="color: #202020;">Jobs</span> <span style="color: #00007f;">loop</span>
      <span style="color: #00007f;">for</span> K <span style="color: #46aa03; font-weight:bold;">in</span> <span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">1</span>_000_000_000 <span style="color: #00007f;">loop</span>
	 <span style="color: #46aa03; font-weight:bold;">null</span>; <span style="color: #adadad; font-style: italic;">--  This is really hard!</span>
      <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
&nbsp;
      Put_Line <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;Job&quot;</span> &amp; Positive'Image <span style="color: #66cc66;">&#40;</span>Job<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #7f007f;">&quot; done by &quot;</span> &amp; Id<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
<span style="color: #00007f;">end</span> Sequential;</pre></td></tr></table></div>

<p>Before we get into actually compiling and running Sequential, allow me to first explain what&#8217;s going on in the program. </p>
<p>First we <code>with</code> the two packages <code>Ada.Task_Identification</code> and <code>Ada.Text_IO</code>. What this does is make the subprograms and types from those packages visible and available to our program, somewhat analogous to the <code>#include</code> directive used in C. After that we setup our &#8220;main&#8221; procedure and name it Sequential. In Ada you can call your main procedure whatever you like. The <code>use</code> clauses we encounter next lets us access the tools of the <code>Ada.Text_IO</code> and <code>Ada.Task_Identification</code> packages without having to prefix every type and subprogram with the package name. This is equivalent to the <code>using namespace</code> statement in C++.</p>
<p>The <code>Ada.Task_Identification</code> package makes it possible for us to figure out the id of a running task and <code>Ada.Text_IO</code> makes the <code>Put_Line</code> procedure available to Sequential.</p>
<p>We declare and initialize two objects in the declarative part of the program (between <code>is</code> and <code>begin</code>): <code>Id</code> and <code>Jobs</code>. The <code>Id</code> constant contains a String representation of the environment task identifier. As our program only have one running task (or thread if you will), which is the main environment task, the output done in the <code>Put_Line</code> line will show the same <code>Id</code> for every job completed.</p>
<p>The <code>Jobs</code> variable is the queue of jobs, in our case 20. Note that the <code>Positive</code> type is actually a subtype of <code>Integer</code> with the range <code>1 .. Integer'Last</code>. If you try to assign a value below 1 or above <code>Integer'Last</code> then the program will raise a <code>Constraint_Error</code> exception.</p>
<p>After the <code>begin</code> keyword we&#8217;re in the actual body of the program, and the first thing we do is setup a <code>for loop</code> that&#8217;ll count down from 20 to 1, as indicated by the <code>reverse</code> keyword and the given range of <code>1 .. Jobs</code>.</p>
<p>Inside this loop we do the actual work: Counting to 1_000_000_000 for each job and then outputting a line of text indicating whenever a job has been completed and by which task id.</p>
<p>And that&#8217;s it.</p>
<p>In order to compile the program do:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">gnatmake sequential.adb</pre></td></tr></table></div>

<p>And then to execute and time:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">time</span> sequential</pre></td></tr></table></div>

<p>My laptop yields the following when executed and timed:</p>
<pre>
thomas@t420:~/Ada/Simple_Tasking time sequential
Job 20 done by main_task_000000000064A010
Job 19 done by main_task_000000000064A010
Job 18 done by main_task_000000000064A010
Job 17 done by main_task_000000000064A010
Job 16 done by main_task_000000000064A010
Job 15 done by main_task_000000000064A010
Job 14 done by main_task_000000000064A010
Job 13 done by main_task_000000000064A010
Job 12 done by main_task_000000000064A010
Job 11 done by main_task_000000000064A010
Job 10 done by main_task_000000000064A010
Job 9 done by main_task_000000000064A010
Job 8 done by main_task_000000000064A010
Job 7 done by main_task_000000000064A010
Job 6 done by main_task_000000000064A010
Job 5 done by main_task_000000000064A010
Job 4 done by main_task_000000000064A010
Job 3 done by main_task_000000000064A010
Job 2 done by main_task_000000000064A010
Job 1 done by main_task_000000000064A010

real    0m52.737s
user    0m52.636s
sys     0m0.001s
</pre>
<p>As you can see it took a good 52 seconds to finish all 20 jobs. At no point during the execution of the program did the system utilize more than one CPU core. Each job was completed in an absolute sequential manner, starting with job 20 and ending with 1.</p>
<p>Surely this is sad, considering my laptop is equipped with a CPU sporting a whooping four cores. Lets add some tasking magic to the program and see how much faster we can make it go.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;"><span style="color: #46aa03; font-weight:bold;">with</span> Ada.<span style="color: #202020;">Task_Identification</span>;
<span style="color: #46aa03; font-weight:bold;">With</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
<span style="color: #46aa03; font-weight:bold;">procedure</span> Concurrent
<span style="color: #00007f;">is</span>
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Task_Identification</span>;
   <span style="color: #46aa03; font-weight:bold;">use</span> Ada.<span style="color: #202020;">Text_IO</span>;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">protected</span> Jobs <span style="color: #00007f;">is</span>
      <span style="color: #46aa03; font-weight:bold;">procedure</span> Get 
	<span style="color: #66cc66;">&#40;</span>Job : <span style="color: #46aa03; font-weight:bold;">out</span> Natural<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #46aa03; font-weight:bold;">private</span>
      J : Natural := <span style="color: #ff0000;">20</span>;
   <span style="color: #00007f;">end</span> Jobs;
&nbsp;
   <span style="color: #adadad; font-style: italic;">------------</span>
   <span style="color: #adadad; font-style: italic;">--  Jobs  --</span>
   <span style="color: #adadad; font-style: italic;">------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">protected</span> <span style="color: #46aa03; font-weight:bold;">body</span> Jobs <span style="color: #00007f;">is</span>
      <span style="color: #adadad; font-style: italic;">-----------</span>
      <span style="color: #adadad; font-style: italic;">--  Get  --</span>
      <span style="color: #adadad; font-style: italic;">-----------</span>
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">procedure</span> Get 
	<span style="color: #66cc66;">&#40;</span>Job : <span style="color: #46aa03; font-weight:bold;">out</span> Natural<span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">is</span>
      <span style="color: #00007f;">begin</span>
	 Job := J;
         <span style="color: #00007f;">if</span> J &gt; <span style="color: #ff0000;">0</span> <span style="color: #00007f;">then</span>
            J := J - <span style="color: #ff0000;">1</span>;
         <span style="color: #00007f;">end</span> <span style="color: #00007f;">if</span>;
      <span style="color: #00007f;">end</span> Get;
   <span style="color: #00007f;">end</span> Jobs;
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">type</span> Worker;
   <span style="color: #adadad; font-style: italic;">--  The Worker gets a job, and does the hard work.</span>
   <span style="color: #adadad; font-style: italic;">--  When there are no more jobs, the Worker exits.</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">--------------</span>
   <span style="color: #adadad; font-style: italic;">--  Worker  --</span>
   <span style="color: #adadad; font-style: italic;">--------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">body</span> Worker <span style="color: #00007f;">is</span>
      A_Job : Natural;
      Id    : <span style="color: #46aa03; font-weight:bold;">constant</span> String := Image <span style="color: #66cc66;">&#40;</span>Current_Task<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">loop</span>
	 Jobs.<span style="color: #202020;">Get</span> <span style="color: #66cc66;">&#40;</span>A_Job<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	 <span style="color: #46aa03; font-weight:bold;">exit</span> <span style="color: #46aa03; font-weight:bold;">when</span> A_Job &lt; <span style="color: #ff0000;">1</span>; 
&nbsp;
	 <span style="color: #00007f;">for</span> K <span style="color: #46aa03; font-weight:bold;">in</span> <span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">1</span>_000_000_000 <span style="color: #00007f;">loop</span>
	    <span style="color: #46aa03; font-weight:bold;">null</span>; <span style="color: #adadad; font-style: italic;">--  This is really hard!</span>
	 <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
&nbsp;
	 Put_Line <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;Job&quot;</span> &amp; Natural'Image <span style="color: #66cc66;">&#40;</span>A_Job<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #7f007f;">&quot; done by &quot;</span> &amp; Id<span style="color: #66cc66;">&#41;</span>;
      <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
   <span style="color: #00007f;">end</span> Worker;
&nbsp;
   Workers : <span style="color: #46aa03; font-weight:bold;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #46aa03; font-weight:bold;">of</span> Worker;
<span style="color: #00007f;">begin</span>
   <span style="color: #46aa03; font-weight:bold;">null</span>; <span style="color: #adadad; font-style: italic;">--  We're not using the main environment task for anything.</span>
<span style="color: #00007f;">end</span> Concurrent;</pre></td></tr></table></div>

<p>Let&#8217;s dive in!</p>
<p>We <code>with</code> and <code>use</code> the same packages as before, but after that there are quite a few new things, first of which is a protected object. In Ada these are objects that export procedures, functions and entries that enables us to interact with the encapsulated data structure. In our case we&#8217;ve setup a protected object called <code>Jobs</code>, but we could just as well have created a protected type and then declared one of more objects to be of that type.</p>
<p>We can only operate on the data structure of a protected object via the exported subprograms and this is done under automatic mutual exclusion. A protected object will allow many concurrent readers (via exported functions), but only one writer (via exported procedures and entries).</p>
<p>A protected object is just what we need to protect our job queue from race conditions, now that we have several tasks querying and updating it.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;">   <span style="color: #46aa03; font-weight:bold;">protected</span> Jobs <span style="color: #00007f;">is</span>
      <span style="color: #46aa03; font-weight:bold;">procedure</span> Get 
	<span style="color: #66cc66;">&#40;</span>Job : <span style="color: #46aa03; font-weight:bold;">out</span> Natural<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #46aa03; font-weight:bold;">private</span>
      J : Natural := <span style="color: #ff0000;">20</span>;
   <span style="color: #00007f;">end</span> Jobs;
&nbsp;
   <span style="color: #adadad; font-style: italic;">------------</span>
   <span style="color: #adadad; font-style: italic;">--  Jobs  --</span>
   <span style="color: #adadad; font-style: italic;">------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">protected</span> <span style="color: #46aa03; font-weight:bold;">body</span> Jobs <span style="color: #00007f;">is</span>
      <span style="color: #adadad; font-style: italic;">-----------</span>
      <span style="color: #adadad; font-style: italic;">--  Get  --</span>
      <span style="color: #adadad; font-style: italic;">-----------</span>
&nbsp;
      <span style="color: #46aa03; font-weight:bold;">procedure</span> Get 
	<span style="color: #66cc66;">&#40;</span>Job : <span style="color: #46aa03; font-weight:bold;">out</span> Natural<span style="color: #66cc66;">&#41;</span>
      <span style="color: #00007f;">is</span>
      <span style="color: #00007f;">begin</span>
	 Job := J;
         <span style="color: #00007f;">if</span> J &gt; <span style="color: #ff0000;">0</span> <span style="color: #00007f;">then</span>
            J := J - <span style="color: #ff0000;">1</span>;
         <span style="color: #00007f;">end</span> <span style="color: #00007f;">if</span>;
      <span style="color: #00007f;">end</span> Get;
   <span style="color: #00007f;">end</span> Jobs;</pre></td></tr></table></div>

<p>First we declare our <code>Jobs</code> protected object. We export the sole <code>Get (Job : out Natural)</code> procedure, which is the only way for us to interact with the very simple data structure, consisting only of the variable <code>J</code>.</p>
<p>In the body of <code>Jobs</code> we find the actual implementation of the <code>Get</code> procedure. All it does is put the current value of <code>J</code> into the <code>Job</code> parameter and then decrease the value of <code>J</code> by one. With this simple system in place we&#8217;re ensured that access to <code>J</code> is only ever granted to one single task at the same time. Only when that task is done is the next one allowed access.</p>
<p>With a secure job queue in place we move on to creating the tasks that will be doing the actual work:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;">   <span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">type</span> Worker;
   <span style="color: #adadad; font-style: italic;">--  The Worker gets a job, and does the hard work.</span>
   <span style="color: #adadad; font-style: italic;">--  When there are no more jobs, the Worker exits.</span>
&nbsp;
   <span style="color: #adadad; font-style: italic;">--------------</span>
   <span style="color: #adadad; font-style: italic;">--  Worker  --</span>
   <span style="color: #adadad; font-style: italic;">--------------</span>
&nbsp;
   <span style="color: #46aa03; font-weight:bold;">task</span> <span style="color: #46aa03; font-weight:bold;">body</span> Worker <span style="color: #00007f;">is</span>
      A_Job : Natural;
      Id    : <span style="color: #46aa03; font-weight:bold;">constant</span> String := Image <span style="color: #66cc66;">&#40;</span>Current_Task<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #00007f;">begin</span>
      <span style="color: #00007f;">loop</span>
	 Jobs.<span style="color: #202020;">Get</span> <span style="color: #66cc66;">&#40;</span>A_Job<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	 <span style="color: #46aa03; font-weight:bold;">exit</span> <span style="color: #46aa03; font-weight:bold;">when</span> A_Job &lt; <span style="color: #ff0000;">1</span>; 
&nbsp;
	 <span style="color: #00007f;">for</span> K <span style="color: #46aa03; font-weight:bold;">in</span> <span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">1</span>_000_000_000 <span style="color: #00007f;">loop</span>
	    <span style="color: #46aa03; font-weight:bold;">null</span>; <span style="color: #adadad; font-style: italic;">--  This is really hard!</span>
	 <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
&nbsp;
	 Put_Line <span style="color: #66cc66;">&#40;</span><span style="color: #7f007f;">&quot;Job&quot;</span> &amp; Natural'Image <span style="color: #66cc66;">&#40;</span>A_Job<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #7f007f;">&quot; done by &quot;</span> &amp; Id<span style="color: #66cc66;">&#41;</span>;
      <span style="color: #00007f;">end</span> <span style="color: #00007f;">loop</span>;
   <span style="color: #00007f;">end</span> Worker;</pre></td></tr></table></div>

<p>First we declare a task type called <code>Worker</code>. We could&#8217;ve setup a task object, just as we did with the <code>Jobs</code> protected object, but since we want more than one worker a type is the way to go. The body of the task looks a lot like the body from the Sequential program, with a few minor differences. We declare the <code>A_Job</code> variable to be of the type <code>Natural</code>, which is a subtype of <code>Integer</code> with the range <code>0 .. Integer'Last</code>. The <code>Id</code> constant is exactly like the one from the first program.</p>
<p>In the body of the <code>Worker</code> task we&#8217;ve got a plain loop that starts out with putting a job into the <code>A_Job</code> variable. We then check if the job is valid in the <code>exit when...</code> line and if not we exit the loop and the task is then completed.</p>
<p>The <code>for</code> loop and the <code>Put_Line</code> code is more or less the same as before.</p>
<p>Last we have this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="ada" style="font-family:monospace;">   Workers : <span style="color: #46aa03; font-weight:bold;">array</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">1</span> .. <span style="color: #ff0000;">4</span><span style="color: #66cc66;">&#41;</span> <span style="color: #46aa03; font-weight:bold;">of</span> Worker;
<span style="color: #00007f;">begin</span>
   <span style="color: #46aa03; font-weight:bold;">null</span>; <span style="color: #adadad; font-style: italic;">--  We're not using the main environment task for anything.</span>
<span style="color: #00007f;">end</span> Concurrent;</pre></td></tr></table></div>

<p>Here we declare an array of 4 <code>Worker</code> tasks. As soon as the program reach this spot, four workers are created and when we pass <code>begin</code> they spring to life. Since we already have 4 workers counting to 1_000_000_000 like there&#8217;s no tomorrow, we don&#8217;t really need to add any code to the main environment task, so we simply add a <code>null</code> statement. The main environment task is the master of the 4 workers, so it wont complete until all 4 workers have completed.</p>
<p>Lets see how the program performs now:</p>
<pre>
thomas@t420:~/Ada/Simple_Tasking$ time concurrent
Job 20 done by workers(4)_000000000065FCC0
Job 19 done by workers(2)_0000000000659240
Job 18 done by workers(3)_000000000065C780
Job 17 done by workers(1)_0000000000655D00
Job 16 done by workers(4)_000000000065FCC0
Job 15 done by workers(2)_0000000000659240
Job 13 done by workers(1)_0000000000655D00
Job 14 done by workers(3)_000000000065C780
Job 12 done by workers(4)_000000000065FCC0
Job 10 done by workers(1)_0000000000655D00
Job 11 done by workers(2)_0000000000659240
Job 9 done by workers(3)_000000000065C780
Job 6 done by workers(2)_0000000000659240
Job 8 done by workers(4)_000000000065FCC0
Job 7 done by workers(1)_0000000000655D00
Job 5 done by workers(3)_000000000065C780
Job 4 done by workers(2)_0000000000659240
Job 3 done by workers(4)_000000000065FCC0
Job 1 done by workers(3)_000000000065C780
Job 2 done by workers(1)_0000000000655D00

real    0m18.727s
user    1m11.454s
sys     0m0.025s
</pre>
<p>YAY! It is much faster now, and all four cores on my CPU is running at 100% while the program executes. Also note how the jobs are no longer completed sequentially. You can experiment with the amount of workers by changing the size of the <code>Workers</code> array. On my box the sweet spot is firing up 20 workers at the same time. With twenty workers I&#8217;m very close to a clean 18 seconds. The second fastes result is obtained with 4 workers. Every other amount of workers I&#8217;ve tried is slower.</p>
<p>There&#8217;s of course a whole lot more to Ada and concurrent programming than shown here. This little example has barely scratched the surface. A good place to start is the <a href="http://en.wikibooks.org/wiki/Ada_Programming/Tasking">Ada Programming Wikibook</a>.</p>
<p>You can grab the code for these examples at <a href="https://github.com/ThomasLocke/Simple_Tasking">github.com/ThomasLocke/Simple_Tasking</a>.</p>
<p>Adding concurrency to an Ada program is very simple, and the model for doing it is both easy to understand and to work with. When you&#8217;re using Ada there&#8217;s simply no excuse for not adding concurrency to your program where it will benefit from it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.fsfe.org/thomaslocke/2012/05/17/concurrent-ada-programming/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
