<?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>Cake Solutions Team Blog &#187; REST</title>
	<atom:link href="http://www.cakesolutions.net/teamblogs/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cakesolutions.net/teamblogs</link>
	<description>void magic() { }</description>
	<lastBuildDate>Tue, 07 Feb 2012 12:02:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tomcat 7, Akka REST and a microcontroller</title>
		<link>http://www.cakesolutions.net/teamblogs/2011/11/16/tomcat-7-akka-rest-and-a-microcontroller/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tomcat-7-akka-rest-and-a-microcontroller</link>
		<comments>http://www.cakesolutions.net/teamblogs/2011/11/16/tomcat-7-akka-rest-and-a-microcontroller/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 15:28:21 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[Akka]]></category>
		<category><![CDATA[Akka REST]]></category>
		<category><![CDATA[mbed]]></category>
		<category><![CDATA[microcontroller]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Tomcat Akka]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=1248</guid>
		<description><![CDATA[In this post, I am going to show you a fun experiment that combines Akka and the mbed microcontroller. The µ controller runs simple REST client that makes HTTP requests to the server to allow entry to some supposedly secure &#8230; <a href="http://www.cakesolutions.net/teamblogs/2011/11/16/tomcat-7-akka-rest-and-a-microcontroller/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In this post, I am going to show you a fun experiment that combines <a href="http://akka.io">Akka</a> and the <a href="http://mbed.org">mbed</a> microcontroller.</p>
<p>The µ controller runs simple REST client that makes HTTP requests to the server to allow entry to some supposedly secure location. (Of course, this is just a trivial application; the ultimate project is to have an automatic hoover that cleans my house automatically, but allows me to control where it goes and to tell it to focus on a particular area.) Before we start looking at the code, here&#8217;s a video that shows the project in its full hardware glory!</p>
<p><iframe src="http://player.vimeo.com/video/32211023?title=0&amp;byline=0&amp;portrait=0" width="600" height="338" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe></p>
<h2>The client</h2>
<p>The client in this case is not a browser, but it is a microcontroller running a simple tourniquete-like application.</p>
<h3>The hardware</h3>
<p>The hardware is the mbed microcontroller talking to a HD 44780 two-line character LCD module, a photodiode and a few other bits and pieces. If you fancy building it yourself, Figure 1 shows the schematics.</p>
<p>Figure 1. Schematics</p>
<p><img src="http://www.cakesolutions.net/teamblogs/wp-content/uploads/2011/11/akka.png" alt="" title="akka" width="558" height="933" /></p>
<h3>The client software</h3>
<p>The client software is a C++ program that runs on the microcontroller. The code initialises the display module; initialises the network interface and then makes plain HTTP requests to the server-side code. The interface is</p>
<pre class="brush:[c]">class Main {
private:
    int count;
    DigitalOut *busyLed;
    AnalogIn *light;
    TextLCD *lcd;
    DigitalOut *goLed;
    DigitalOut *stopLed;
    char* status;
    EthernetNetIf *eth;
    HTTPClient *http;
    float ambient;
    bool noEthernet;

    void go();
    void stop();
    bool isHit();
public:
    Main();
    void run();
};

…

int main() {
    Main main;
    main.run();
}
</pre>
<p>The constructor, <code>Main::Main()</code> performs the necessary initialisation; and the code that runs the main loop is in the <code>run</code> method. The code in the <code>run</code> makes the HTTP requests (when I pass my hand over the photodiode), displays the results and execute the appropriate methods <code>stop</code> and <code>go</code>. The methods simply turn on the LEDs, but one could imagine that the methods open barriers or something similar. (Think the Tube tourniquets). To complete the picture, here is the skeleton of the <code>run</code> method:</p>
<pre class="brush:[cpp]">
void Main::run() {
    while (1) {
        lcd->locate(0, 0);
        lcd->printf("--- %.4f", ambient);

        wait(0.1);

        if (!isHit()) continue;

        (*busyLed) = 1;
        HTTPText txt;
        lcd->locate(0, 0);
        lcd->printf("*** %.4f", ambient);
        stop();

        if (noEthernet) {
            lcd->locate(0, 1);
            lcd->printf("Could not verify");
        } else {
            char request[200];
            bool isGo;
            count++;
            char message[16];
            sprintf(request, "http://192.168.1.69:8080/%d.toString", count);
            HTTPResult r = http->get(request, &#038;txt);
            if (r == HTTP_OK) {
                const char* s = txt.gets();
                isGo = s[0] == 'G';
                strcpy(message, s);
            } else {
                lcd->locate(0, 1);
                isGo = false;
                strcpy(message, "STOP (no server)");
            }
            if (isGo) go(); else stop();
            lcd->locate(0, 1);
            lcd->printf(message);
        }
        wait(5);
        ambient = (*light);
        lcd->locate(0, 1);
        lcd->printf("                ");
        (*busyLed) = 0;
        stop();
    }
}
</pre>
<p>Now that we have the client, let&#8217;s move on to the server code.</p>
<h2>The server</h2>
<p>I have implemented the server-side code using Akka; I am deploying it in Tomcat 7. Figure 2 shows the overview of all the files that make up the application.
</p>
<p>Figure 2. The key files</p>
<p><img src="http://www.cakesolutions.net/teamblogs/wp-content/uploads/2011/11/dirs1.png" alt="" title="dirs" width="338" height="298"/><br />
<br />
The key files include</p>
<ul>
<li><code>akka.conf</code> file in <code>src/main/resources</code> containing the Akka configuration</li>
<li><code>web.xml</code> in <code>src/main/webapp/WEB-INF</code> with the servlet configuration for Tomcat 7</li>
<li><code>Initializer</code> that implements the <code>ServletContextListener</code> that boots up Akka</li>
<li><code>Boot</code> that configures Akka on startup</li>
<li>Finally, <code>GatekeeperActor</code> that handles the client requests</li>
</ul>
<h3>Akka in Tomcat</h3>
<p>We now need to bootstrap Akka when we deploy it in our Tomcat 7 container; we also need to map the HTTP requests to our actor. The <code>web.xml</code> file defines the <code>listener</code> that initializes the Akka environment and the <code>servlet</code> that processes the HTTP requests.</p>
<pre class="brush:[xml]">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"&gt;

  &lt;listener&gt;
    &lt;listener-class&gt;scalad.example.akka.Initializer&lt;/listener-class&gt;
  &lt;/listener&gt;

  &lt;servlet&gt;
    &lt;servlet-name&gt;AkkaServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;akka.http.AkkaMistServlet&lt;/servlet-class&gt;
    &lt;async-supported&gt;true&lt;/async-supported&gt;
  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;AkkaServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;

&lt;/web-app&gt;
</pre>
<p>Notice, in particular that this is Servlet 3.0 web.xml (you will have no luck running the application without it)! Onwards to the <code>Initializer</code> that prepares the environment for Akka.</p>
<pre class="brush:[scala]">class Initializer extends ServletContextListener {

  lazy val loader = new AkkaLoader

  def contextDestroyed(e: ServletContextEvent) {
    loader.shutdown()
  }

  def contextInitialized(e: ServletContextEvent) {
    loader.boot(false,
      new BootableActorLoaderService with BootableRemoteActorService )
  }

}
</pre>
<p>Next, in <code>akka.conf</code> we have <code>boot = ["scalad.example.akka.Boot"]</code>, which means that Akka will turn to the <code>Boot</code> class, which will bootstrap the Akka environment.</p>
<pre class="brush:[scala]">class Boot {

  val factory = SupervisorFactory(
    SupervisorConfig(
      OneForOneStrategy(List(classOf[Exception]), 3, 100),
      Supervise(Actor.actorOf[RootEndpoint], Permanent) ::
      Supervise(Actor.actorOf[GatekeeperService], Permanent) ::
      Nil))

  factory.newInstance.start

}
</pre>
<p>We setup the <code>SupervisorFactory</code> with two actors: the <code>RootEndpoint</code>, which acts like a central registry for the HTTP requests and our <code>GatekeeperService</code>, which registers the <code>GatekeeperActor</code>. The actor is the key element in our application. It receives the HTTP requests and produces the responses:</p>
<pre class="brush:[scala]">class GatekeeperActor extends Actor {
  private val random = new Random

  def receive = {
    case get: Get =>
      get OK (if (random.nextInt() % 2 == 0) "G" else "S")
    case other: RequestMethod =>
      other OK "S"
  }
}
</pre>
<p>And yes, the actor is trivial. When it receives a HTTP GET request, it produces a text response as either <code>"G"</code> or <code>"S"</code>, at random. The client interprets these responses and performs the appropriate actions.</p>
<h2>The code</h2>
<p>This application&#8217;s source code is available as part of Scalad, but the Scalad version includes some data access code and it is at <a href="https://github.com/janm399/scalad">github.com/janm399/scalad</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2011/11/16/tomcat-7-akka-rest-and-a-microcontroller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>COBOL is fifty</title>
		<link>http://www.cakesolutions.net/teamblogs/2009/08/14/cobol-is-fifty/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cobol-is-fifty</link>
		<comments>http://www.cakesolutions.net/teamblogs/2009/08/14/cobol-is-fifty/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 14:23:18 +0000</pubDate>
		<dc:creator>Jan Machacek</dc:creator>
				<category><![CDATA[Jan's Blog]]></category>
		<category><![CDATA[COBOL]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[WS-*]]></category>

		<guid isPermaLink="false">http://www.cakesolutions.net/teamblogs/?p=427</guid>
		<description><![CDATA[Not today, of course, COBOL celebrated its fiftieth birthday in April. It is the hidden language, no one will openly admit that they have COBOL systems, yet there are millions of lines of COBOL code that no one dares to &#8230; <a href="http://www.cakesolutions.net/teamblogs/2009/08/14/cobol-is-fifty/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Not today, of course, COBOL celebrated its fiftieth birthday in April. It is the hidden language, no one will openly admit that they have COBOL systems, yet there are millions of lines of COBOL code that no one dares to throw away. The number one reason is &#8220;if it ain&#8217;t broke, don&#8217;t fix it&#8221;, closely followed by the fact that software rewrites usually end in tears. Joel Spolsky gives excellent evidence on his <a href="http://www.joelonsoftware.com/articles/fog0000000069.html">blog</a>.</p>
<h2>Why is COBOL still out there?</h2>
<p>So, how come COBOL has survived for so long? It is an old, cumbersome language; yet, it has modern compilers and runtime environments. You can compile your COBOL program on 32bit Windows, on 32 and 64bit Linux, HP-UX, Solaris, AIX, and many others.<br />
Seven years ago, I worked in <a href="http://www.ortex.cz">Ortex</a> and they (not me, I was the cool new dude) were churning out thousands of lines of COBOL code every day.<br />
I helped replace the aging Windows 3.1-style user interface with something that looked a bit more modern, but underneath the pretty UI, we were still talking to green-screen UNIX COBOL monster. And there lies the key to COBOL&#8217;s continued success. </p>
<h2>What did COBOL give us?</h2>
<p>It did not use complex binary protocols, it relied on the XML of the seventies &#8212; fixed width text messages. This simple data exchange format lends itself to easy integration with other systems. As long as your system can read and write fixed-width text, you can talk to COBOL-based systems &#8212; OK, I know I&#8217;m leaving out details, but bare with me.<br />
Fast forward to 2006 and watch the WS-* approach. Why, did the WS-* people ask, are our systems talking to each other in closed binary formats; why can&#8217;t we use an easy to read and write format? Why aren&#8217;t we using XML? It turns out that WS-* approach is still cumbersome. For example, you cannot determine what a WS-* call will do just by looking at the endpoint, you need to examine the payload. Hmmf!<br />
Enter REST. Again, we are sending structured plain text, but this time around, we&#8217;re making the most of the HTTP protocol. We use the HTTP methods properly and we give meanings to the URIs. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.cakesolutions.net/teamblogs/2009/08/14/cobol-is-fifty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

