<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Gamlor » Technical Wibbly Wobbly</title>
	
	<link>http://www.gamlor.info/wordpress</link>
	<description>thoughts about programming, tv-series and other geeky stuff.</description>
	<lastBuildDate>Sun, 26 Feb 2012 02:21:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/GamlorTechnicalWibblyWobbly" /><feedburner:info uri="gamlortechnicalwibblywobbly" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>A Tiny Survey</title>
		<link>http://www.gamlor.info/wordpress/2012/02/reader-survey/</link>
		<comments>http://www.gamlor.info/wordpress/2012/02/reader-survey/#comments</comments>
		<pubDate>Sat, 25 Feb 2012 12:07:21 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[42]]></category>
		<category><![CDATA[Media Zap Oh Snap]]></category>
		<category><![CDATA[Technical Wibbly Wobbly]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2320</guid>
		<description><![CDATA[I&#8217;ve already bitched several times that I feel that my blog is a little to chaotic. Lately it has gotten worse with the addition of my simple &#8216;Learning Korean&#8217;-posts. That&#8217;s why I created a tiny tiny survey where you can tell your opinion. You can vote for multiple things and even add stuff. Or complain [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve already bitched several times that I feel that my blog is a little to chaotic. Lately it has gotten worse with the addition of my simple &#8216;Learning Korean&#8217;-posts. That&#8217;s why I created a tiny tiny survey where you can tell your opinion. You can vote for multiple things and even add stuff. Or complain in the comments below.</p>
<p>If course I may ignore your wishes anyway, cause I&#8217;m a lazy ass =). The Survey is below the stick-figures.<br />
<div id="attachment_2321" class="wp-caption aligncenter" style="width: 298px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2012/02/poll.png"><img src="http://www.gamlor.info/wordpress/wp-content/uploads/2012/02/poll-288x300.png" alt="Chaos FTW" title="poll" width="288" height="300" class="size-medium wp-image-2321" /></a><p class="wp-caption-text">Chaos FTW</p></div> </p>
<p><script type="text/javascript" charset="utf-8" src="http://static.polldaddy.com/p/5975749.js"></script><br />
<noscript><a href="http://polldaddy.com/poll/5975749/">The FUTURE</a></noscript></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2012/02/reader-survey/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Modify Annotations in Scala</title>
		<link>http://www.gamlor.info/wordpress/2012/02/modify-annotations-in-scala/</link>
		<comments>http://www.gamlor.info/wordpress/2012/02/modify-annotations-in-scala/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 20:44:24 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[software-development]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2296</guid>
		<description><![CDATA[In Scala I rarely use annotations. However when you need to interoperate with a framework which relies on annotations you need use them. So lately I wanted to store some simple case classes with JPA. As you know, JPA relies quite heavily on annotations, unless you move everything out to XML descriptors. I wanted to [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_2299" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2012/02/ScalaAnnotations.png"><img class="size-medium wp-image-2299" title="ScalaAnnotations" src="http://www.gamlor.info/wordpress/wp-content/uploads/2012/02/ScalaAnnotations-300x193.png" alt="Best Organized Kitchen Ever?" width="300" height="193" /></a><p class="wp-caption-text">Best Organized Kitchen Ever?</p></div>
<p>In Scala I rarely use annotations. However when you need to interoperate with a framework which relies on annotations you need use them. So lately I wanted to store some simple case classes with JPA. As you know, JPA relies quite heavily on annotations, unless you move everything out to XML descriptors.</p>
<p>I wanted to store something like this:</p>
<div id="gist-1878790" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@Entity</span></div><div class='line' id='LC2'><span class="k">case</span> <span class="k">class</span> <span class="nc">MyStoredData</span><span class="o">(</span><span class="n">userName</span><span class="k">:</span><span class="kt">String</span><span class="o">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">realName</span><span class="k">:</span><span class="kt">String</span><span class="o">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nd">@Id</span> <span class="n">id</span><span class="k">:</span><span class="kt">Long</span> <span class="o">=</span><span class="mi">0</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;<span class="c1">// JPA requires a parameter-free constructor</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="k">def</span> <span class="k">this</span><span class="o">()</span> <span class="k">=</span> <span class="k">this</span><span class="o">(</span><span class="s">&quot;&quot;</span><span class="o">,</span><span class="s">&quot;&quot;</span><span class="o">)</span></div><div class='line' id='LC7'><span class="o">}</span></div><div class='line' id='LC8'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1878790/67e02a8d50192323d58f5b5174c71a9d6b180487/MyStoredData.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1878790#file_my_stored_data.scala" style="float:right;margin-right:10px;color:#666">MyStoredData.scala</a>
            <a href="https://gist.github.com/1878790">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Unfortunately this results in exceptions like the one below. Basically the annotation is not placed on the ‘field’ but rather on the parameter of the constructor.</p>
<div id="gist-1878790" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>Caused by: com.versant.jpa.enhancer.exceptions.MissingIdException: missing ID annotation or mapping for info.gamlor.vjpa.model.MyStoredData</div><div class='line' id='LC2'>	at com.versant.jpa.enhancer.JpaClassEnhancer.enhance(JpaClassEnhancer.java:123)</div><div class='line' id='LC3'>	at com.versant.jpa.enhancer.JpaClassEnhancer.enhance(JpaClassEnhancer.java:50)</div><div class='line' id='LC4'>	at com.versant.jpa.enhancer.JpaClassTransformer.transform(JpaClassTransformer.java:51)</div><div class='line' id='LC5'>	at sun.instrument.TransformerManager.transform(TransformerManager.java:169)</div><div class='line' id='LC6'>	at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:365)</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1878790/df5d457f7f4c57219aab65d4eac3e25cd7919604/Exception.txt" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1878790#file_exception.txt" style="float:right;margin-right:10px;color:#666">Exception.txt</a>
            <a href="https://gist.github.com/1878790">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now you actually can specify on which elements such an annotation is added. So in this situation we can tell the Scala to ensure that the annotation is also placed on the id field:</p>
<div id="gist-1878790" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nd">@Entity</span></div><div class='line' id='LC2'><span class="k">case</span> <span class="k">class</span> <span class="nc">MyStoredData</span><span class="o">(</span><span class="n">userName</span><span class="k">:</span><span class="kt">String</span><span class="o">,</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">realName</span><span class="k">:</span><span class="kt">String</span><span class="o">,</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// ensure that the field is also annotated</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">@(</span><span class="nc">Id</span> <span class="nd">@field</span><span class="o">)</span> <span class="n">id</span><span class="k">:</span><span class="kt">Long</span> <span class="o">=</span><span class="mi">0</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC6'>&nbsp;&nbsp;<span class="c1">// JPA requires a parameter-free constructor</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="k">def</span> <span class="k">this</span><span class="o">()</span> <span class="k">=</span> <span class="k">this</span><span class="o">(</span><span class="s">&quot;&quot;</span><span class="o">,</span><span class="s">&quot;&quot;</span><span class="o">)</span></div><div class='line' id='LC8'><span class="o">}</span></div><div class='line' id='LC9'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1878790/9304fbb71c9a3d238b983ce4b03df7b5065054fa/FixedAnnotation.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1878790#file_fixed_annotation.scala" style="float:right;margin-right:10px;color:#666">FixedAnnotation.scala</a>
            <a href="https://gist.github.com/1878790">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This will fix the issue. There are more possible targets, like Scala accessors and Java Beans getter/setters for @BeanProperty annotated fields. Take a look at <a href="http://www.gamlor.info/@BeanProperty">the documentation here</a>.</p>
<p>That’s all. Nothing more to tell =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2012/02/modify-annotations-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ScalaQuery, a Small Database Library</title>
		<link>http://www.gamlor.info/wordpress/2012/02/scalaquery-a-small-database-library/</link>
		<comments>http://www.gamlor.info/wordpress/2012/02/scalaquery-a-small-database-library/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 23:14:36 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[software-development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2284</guid>
		<description><![CDATA[On this blog I usually talk about no relational databases, like RavenDB or db4o. But guess what, I still like regular relational databases. A while back I needed to read some data from a relational database in Scala. What I wanted was a small library, which removes most of the JDBC boiler plate, but not [...]]]></description>
			<content:encoded><![CDATA[<p>On this blog I usually talk about no relational databases, like RavenDB or db4o. But guess what, I still like regular relational databases.<div id="attachment_2291" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2012/02/relational-scala.png"><img src="http://www.gamlor.info/wordpress/wp-content/uploads/2012/02/relational-scala-300x182.png" alt="Relational Stuff fits Scala well" title="relational-scala" width="300" height="182" class="size-medium wp-image-2291" /></a><p class="wp-caption-text">Relational Stuff fits Scala well</p></div></p>
<p>A while back I needed to read some data from a relational database in Scala. What I wanted was a small library, which removes most of the JDBC boiler plate, but not a full blown ORM. I ended up using is <a href="http://scalaquery.org/">ScalaQuery</a>. </p>
<p>This library gives you a thin, rational layer on top of JDBC. This is how it works. First you define a &#8216;table&#8217;. This is done extending a table class and adding field definitions to it:<br />
<div id="gist-1839338" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">val</span> <span class="nc">ApiKeys</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">ExtendedTable</span><span class="o">[(</span><span class="kt">String</span>, <span class="kt">String</span><span class="o">)](</span><span class="s">&quot;googleApiKeys&quot;</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC2'>	<span class="k">def</span> <span class="n">deviceId</span> <span class="k">=</span> <span class="n">column</span><span class="o">[</span><span class="kt">String</span><span class="o">](</span><span class="s">&quot;deviceId&quot;</span><span class="o">,</span> <span class="n">O</span><span class="o">.</span><span class="nc">PrimaryKey</span><span class="o">)</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>	<span class="k">def</span> <span class="n">apiKey</span> <span class="k">=</span> <span class="n">column</span><span class="o">[</span><span class="kt">String</span><span class="o">](</span><span class="s">&quot;apiKey&quot;</span><span class="o">)</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="k">def</span> <span class="o">*</span> <span class="k">=</span> <span class="n">deviceId</span> <span class="o">~</span> <span class="n">apiKey</span></div><div class='line' id='LC7'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1839338/2c2a3091048aeece850d3612bfdb5febc45b2738/TableDefinition.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1839338#file_table_definition.scala" style="float:right;margin-right:10px;color:#666">TableDefinition.scala</a>
            <a href="https://gist.github.com/1839338">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Afterwards you need to create a database instance:<br />
<div id="gist-1839338" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">val</span> <span class="n">database</span><span class="k">:</span> <span class="kt">Database</span> <span class="o">=</span> <span class="nc">Database</span><span class="o">.</span><span class="n">forURL</span><span class="o">(</span><span class="s">&quot;jdbc:h2:~/apiKeyStore&quot;</span><span class="o">,</span> <span class="n">driver</span> <span class="k">=</span> <span class="s">&quot;org.h2.Driver&quot;</span><span class="o">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1839338/cf5c1f0b03de11dcdeae8c2a33193f6439fcd4f5/DatabaseInstance.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1839338#file_database_instance.scala" style="float:right;margin-right:10px;color:#666">DatabaseInstance.scala</a>
            <a href="https://gist.github.com/1839338">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>To do an operation we need a transaction / session. On way to create such a session is to use the withSession method. Basically all operations require a session. We can manually pass the session to the methods. Or we can declare an implicit session which is used. For example the built in thread local session:<br />
<div id="gist-1839338" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// We use the thread local session management</span></div><div class='line' id='LC2'><span class="c1">// So we import this implicit parameter which returns the current session</span></div><div class='line' id='LC3'><span class="c1">// Of course we pass any other session / use our own implicits</span></div><div class='line' id='LC4'><span class="k">import</span> <span class="nn">org.scalaquery.session.Database.threadLocalSession</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="c1">// and we do stuff in our session</span></div><div class='line' id='LC8'><span class="n">database</span> <span class="n">withSession</span> <span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;<span class="c1">// do stuff</span></div><div class='line' id='LC10'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1839338/1efa85ed4af414b762213f23cbb0af4176d69480/SessionManagement.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1839338#file_session_management.scala" style="float:right;margin-right:10px;color:#666">SessionManagement.scala</a>
            <a href="https://gist.github.com/1839338">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>For example we can create the database schema:<br />
<div id="gist-1839338" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">database</span> <span class="n">withSession</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nc">ApiKeys</span><span class="o">.</span><span class="n">ddl</span><span class="o">.</span><span class="n">create</span></div><div class='line' id='LC3'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1839338/2d1bec8910272a5a92845c54f2b38b1416ed6651/CreateSchema.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1839338#file_create_schema.scala" style="float:right;margin-right:10px;color:#666">CreateSchema.scala</a>
            <a href="https://gist.github.com/1839338">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Now to the important part: We can query and update data with our table object. The cool thing is that we just can use the Scala for-construct to do so. The library will transform our code into the appropriate SQL statement. Everything lives in the &#8216;Scala&#8217; world, no strings and casts etc. are required. Also not that some imports are required for these API features:<br />
<div id="gist-1839338" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// Imports for the query API</span></div><div class='line' id='LC2'><span class="k">import</span> <span class="nn">org.scalaquery.ql.extended.H2Driver.Implicit._</span></div><div class='line' id='LC3'><span class="k">import</span> <span class="nn">org.scalaquery.session.</span><span class="o">{</span><span class="nc">Session</span><span class="o">,</span> <span class="nc">Database</span><span class="o">}</span></div><div class='line' id='LC4'><span class="k">import</span> <span class="nn">org.scalaquery.ResultSetInvoker</span></div><div class='line' id='LC5'><span class="k">import</span> <span class="nn">org.scalaquery.simple.StaticQuery</span></div><div class='line' id='LC6'><span class="k">import</span> <span class="nn">org.scalaquery.ql.Query</span></div><div class='line' id='LC7'><br/></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'><span class="c1">// Query</span></div><div class='line' id='LC10'><span class="k">val</span> <span class="n">devicesByApiKey</span> <span class="k">=</span> <span class="k">for</span> <span class="o">{</span></div><div class='line' id='LC11'>	<span class="n">a</span> <span class="k">&lt;-</span> <span class="nc">ApiKeys</span> <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">deviceId</span><span class="o">.</span><span class="n">like</span><span class="o">(</span><span class="n">deviceId</span><span class="o">)</span></div><div class='line' id='LC12'><span class="o">}</span> <span class="k">yield</span> <span class="n">a</span><span class="o">.</span><span class="n">apiKey</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="c1">// Do stuff with the result</span></div><div class='line' id='LC15'><span class="n">devicesByApiKey</span><span class="o">.</span><span class="n">foreach</span><span class="o">(</span><span class="n">i</span><span class="o">=&gt;</span><span class="n">doStuff</span><span class="o">(</span><span class="n">i</span><span class="o">))</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1839338/0cde61a12cd6d5153eb7746986b61f03a42033e1/QueryForAKey.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1839338#file_query_for_a_key.scala" style="float:right;margin-right:10px;color:#666">QueryForAKey.scala</a>
            <a href="https://gist.github.com/1839338">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>We also can insert and update tables:<br />
<div id="gist-1839338" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="n">val</span> <span class="n">devicesByApiKey</span> <span class="p">=</span> <span class="k">for</span> <span class="p">{</span></div><div class='line' id='LC2'>	<span class="n">a</span> <span class="p">&lt;-</span> <span class="n">ApiKeys</span> <span class="k">if</span> <span class="n">a</span><span class="p">.</span><span class="n">deviceId</span><span class="p">.</span><span class="n">like</span><span class="p">(</span><span class="n">deviceId</span><span class="p">)</span></div><div class='line' id='LC3'><span class="p">}</span> <span class="k">yield</span> <span class="n">a</span><span class="p">.</span><span class="n">apiKey</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="n">devicesByApiKey</span><span class="p">.</span><span class="n">firstOption</span> <span class="n">match</span> <span class="p">{</span></div><div class='line' id='LC6'>	<span class="k">case</span> <span class="n">None</span> <span class="p">=&gt;</span> <span class="p">{</span></div><div class='line' id='LC7'>	  <span class="n">ApiKeys</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="n">deviceId</span><span class="p">,</span> <span class="n">googleKey</span><span class="p">)</span></div><div class='line' id='LC8'>	<span class="p">}</span></div><div class='line' id='LC9'>	<span class="k">case</span> <span class="nf">Some</span><span class="p">(</span><span class="n">_</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span></div><div class='line' id='LC10'>	  <span class="n">query</span><span class="p">.</span><span class="n">update</span><span class="p">(</span><span class="n">googleKey</span><span class="p">)</span></div><div class='line' id='LC11'>	<span class="p">}</span></div><div class='line' id='LC12'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1839338/c096b67e7afb6516943d17b23d8b035c37b5c91e/UpdateOrInsert.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1839338#file_update_or_insert.cs" style="float:right;margin-right:10px;color:#666">UpdateOrInsert.cs</a>
            <a href="https://gist.github.com/1839338">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<h2>My Opinion &#038; State of the Library</h2>
<p>I really like the approach of this library. It is lightweight, close the SQL-metal and yet it blends perfectly with the Scala code. It really doesn&#8217;t feel like you entering into a new domain when talking to the database. It only surfaces operations which work in SQL and doesn’t do any operations behind the scenes. For many use cases this works way better than heavy weight ORM frameworks. </p>
<p>However I have to point out that many features are not there and there doesn&#8217;t seem any active development going on. For example you cannot check if a table already exists before calling the &#8216;create schema&#8217; method. I also miss a nice method for using raw SQL in case you need a missing feature. </p>
<p>So right now I only would use it for pet projects, or when you are willing to invest your time to patch and improve things. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2012/02/scalaquery-a-small-database-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Akka 2.0 On Android (WIP)</title>
		<link>http://www.gamlor.info/wordpress/2012/01/running-akka-2-0-on-android-wip/</link>
		<comments>http://www.gamlor.info/wordpress/2012/01/running-akka-2-0-on-android-wip/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 23:12:27 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[akka-mobile]]></category>
		<category><![CDATA[software-development]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2246</guid>
		<description><![CDATA[This information here is allready quite outdated. With the current Akka 2.0 master snapshot some issues are gone, while new pitfalls introduced. I will update / create a new post when Akka 2.0 is close to the final release. Akka 2.0 is available as a preview version. Here’s a short guide how to get it [...]]]></description>
			<content:encoded><![CDATA[<p><strong>This information here is allready quite outdated. With the current Akka 2.0 master snapshot some issues are gone, while new pitfalls introduced. I will update / create a new post when Akka 2.0 is close to the final release.</strong></p>
<p>Akka 2.0 is available as a preview version. Here’s a short guide how to get it to run on Android. This pretty much work in progress. Maybe someone else has even has a better strategy. Let me know if that’s the case=). Also I didn’t do much with Akka 2.0 on Android yet, so maybe there are major issues yet to discover =).</p>
<div id="attachment_2249" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2012/01/akka20onDroid.png"><img class="size-medium wp-image-2249" title="akka20onDroid" src="http://www.gamlor.info/wordpress/wp-content/uploads/2012/01/akka20onDroid-300x226.png" alt="Let's master this Akka beast, little Android =)" width="300" height="226" /></a><p class="wp-caption-text">Let&#39;s master this Akka-beast, little Android =)</p></div>
<p>In general I just assume that you have a working Scala build and know how to include new Scala libraries. And I also assume that you are using Proguard to shrink the jar file. I use a build based on <a href="https://github.com/pboos/scala-on-android-example">this</a>. In case you are having trouble, just ask for help =).</p>
<p>First add the Akka 2.0 M3 jar-file to the project (The M2 bits won’t work). Make sure that it is in your directories of libraries which are compacted by Proguard. Unfortunately you will get tons of warnings by Proguard about classes which couldn’t be found, because they are not part of the Android platform. These are three categories of classes which are not available:</p>
<ul>
<li>‘ akka.util.JMX’: There is no JMX on Android.</li>
<li>‘sun.misc.Unsafe’: Well at first sight Android doesn’t have this implementation-detail class. Luckily it actually does have it (at least in newer versions), but it’s only available at runtime. The jars which you compile against do not have this class.</li>
<li>‘org.omg.CORBA’-stuff. The Akka library has a UUID implementation which implements some interfaces from CORBA.</li>
</ul>
<p>So it is time for some Proguard trickery to get rid of warnings.</p>
<h2>Bye Bye JMX</h2>
<p>Well there is definitely no JMX on Android. So I just excluded the JMX stuff from Akka:</p>
<div id="gist-1706858" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">## No JMX for Android</span></div><div class='line' id='LC2'><span class="err">-dontwarn</span> <span class="err">akka.util.JMX*</span></div><div class='line' id='LC3'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1706858/5a340c0919a6c6284f281c25afbb3108e6d26f00/NoJMXForYou.cfg" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1706858#file_no_jmx_for_you.cfg" style="float:right;margin-right:10px;color:#666">NoJMXForYou.cfg</a>
            <a href="https://gist.github.com/1706858">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Ignoring ‘sun.misc.Unsafe’ Warnings</h2>
<p>The next step is to configure Proguard to ignore the ‘sun.misc.Unsafe’ warnings. It is there on the device (If not, please tell me).</p>
<div id="gist-1706858" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">## Unsafe is there at runtime</span></div><div class='line' id='LC2'><span class="err">-dontwarn</span> <span class="err">sun.misc.Unsafe</span></div><div class='line' id='LC3'><span class="err">-keep</span> <span class="err">class</span> <span class="err">sun.misc.Unsafe{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="err">*</span><span class="c">;</span></div><div class='line' id='LC5'><span class="err">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1706858/48f4299d45efd0aca4a47df961f8e634c06f3070/UnsafeIsThere.cfg" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1706858#file_unsafe_is_there.cfg" style="float:right;margin-right:10px;color:#666">UnsafeIsThere.cfg</a>
            <a href="https://gist.github.com/1706858">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Fixing the CORBA Stuff</h2>
<div>
<div>
<div id="982189de-8479-41c6-9cec-ab6cfca78500">
<p>Well, just exclude the ‘com.eaio.uuid’ stuff and you should be fine. At least is looks like it when you are only using the basic functionality. (In Akka 1.2 the UUID class is central, in Akka 2.0 it has been replaced with the path system.):</p>
</div>
</div>
</div>
<div id="gist-1706858" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">## UUID helpers are not used</span></div><div class='line' id='LC2'><span class="err">-dontwarn</span> <span class="err">com.eaio.uuid.**</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1706858/2a8296993ff7435b6dfb05040145cd52013210f3/UUIDHelpersNotNeeded.cfg" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1706858#file_uuid_helpers_not_needed.cfg" style="float:right;margin-right:10px;color:#666">UUIDHelpersNotNeeded.cfg</a>
            <a href="https://gist.github.com/1706858">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Remove the FSM Forward Reference</h2>
<p>The core Akka library contains a forward reference to its finite state machine (FSM) implementation. In a usual class loader environment this doesn’t matter, since classes are lazily loaded. However Proguard is going to bitch about it. So in case you are not using the FSM library remove that reference with Proguard:</p>
<div id="gist-1706858" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">## No Finate State Machines Support needed</span></div><div class='line' id='LC2'><span class="err">-dontwarn</span> <span class="err">akka.actor.FSM</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1706858/ae19d052d7712dbb3732d09e2cea54a805bac811/FSM.cfg" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1706858#file_fsm.cfg" style="float:right;margin-right:10px;color:#666">FSM.cfg</a>
            <a href="https://gist.github.com/1706858">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Keep Important Classes</h2>
<p>Also we need to keep some classes which are loaded at runtime. Maybe there are more required for some features, just watch for class loader errors. These are the ones you need for sure:</p>
<div id="gist-1706858" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">## Stuff referenced at runtime</span></div><div class='line' id='LC2'><span class="err">-keep</span> <span class="err">public</span> <span class="err">class</span> <span class="err">akka.actor.LocalActorRefProvider</span> <span class="err">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;<span class="err">public</span> <span class="err">&lt;init&gt;(...)</span><span class="c">;</span></div><div class='line' id='LC4'><span class="err">}</span></div><div class='line' id='LC5'><span class="err">-keep</span> <span class="err">class</span> <span class="err">akka.event.Logging*</span></div><div class='line' id='LC6'><span class="err">-keep</span> <span class="err">class</span> <span class="err">akka.event.Logging$LogExt{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="err">*</span><span class="c">;</span></div><div class='line' id='LC8'><span class="err">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1706858/65184b9b60e52c265146b956c90ded944122f945/KeepStuff.cfg" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1706858#file_keep_stuff.cfg" style="float:right;margin-right:10px;color:#666">KeepStuff.cfg</a>
            <a href="https://gist.github.com/1706858">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Work Around the Reference Configuration Issue?</h2>
<p>I’m not sure what exactly the issue is here and probably I’m doing something wrong. At least with my setup Akka cannot find its internal reference.conf resource file. Looks like resources are only loaded from the *.apk file? Anyway, for now I just copied the reference.conf file from the Akka source code to my project (Or do it with a build task). Then everything runs.</p>
<h2>Conclusion</h2>
<p>Yes, it is a little hairy to get Akka 2.0 to run on Android. I certainly will work a little more on it. And in case it gets too bad I’ll do a shallow fork of Akka <img src='http://www.gamlor.info/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2012/01/running-akka-2-0-on-android-wip/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Annoying Void in C# / Java / alike</title>
		<link>http://www.gamlor.info/wordpress/2012/01/annoying-void-in-c-java-alike/</link>
		<comments>http://www.gamlor.info/wordpress/2012/01/annoying-void-in-c-java-alike/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 23:26:27 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2191</guid>
		<description><![CDATA[One thing which regularly annoys me in C# and Java is the void type. Basically the void type is not a subtype of object, which makes it giant. Now when you write something which accepts a function as an argument you need two version: A version which accepts a function which returns something and a [...]]]></description>
			<content:encoded><![CDATA[<p>One thing which regularly annoys me in C# and Java is the void type. Basically the void type is not a subtype of object, which makes it giant. Now when you write something which accepts a function as an argument you need two version: A version which accepts a function which returns something and a version which accepts a ‘void-returning’-function (the Action delegates in C#).</p>
<div id="attachment_2195" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2012/01/void-oh-void.png"><img class="size-medium wp-image-2195" title="void-oh-void" src="http://www.gamlor.info/wordpress/wp-content/uploads/2012/01/void-oh-void-300x187.png" alt="Void Is Not Cool Enough" width="300" height="187" /></a><p class="wp-caption-text">Void Is Not Cool Enough </p></div>
<p>In my code I often end up doing something like this:</p>
<div id="gist-1597380" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">class</span> <span class="nc">Example</span></div><div class='line' id='LC2'><span class="p">{</span></div><div class='line' id='LC3'>	<span class="c1">// Stuff like transactions, run in contexts, binding methods etc etc.</span></div><div class='line' id='LC4'>	<span class="k">public</span> <span class="n">T</span> <span class="n">WithSomeContext</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;(</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span> <span class="n">runInContext</span><span class="p">)</span></div><div class='line' id='LC5'>	<span class="p">{</span></div><div class='line' id='LC6'>		<span class="c1">// do magic and stuff</span></div><div class='line' id='LC7'>		<span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">runInContext</span><span class="p">();</span></div><div class='line' id='LC8'>		<span class="c1">// do other stuff</span></div><div class='line' id='LC9'>		<span class="k">return</span> <span class="n">result</span><span class="p">;</span></div><div class='line' id='LC10'>	<span class="p">}</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>	<span class="c1">// Doublicate to also accept Void returns</span></div><div class='line' id='LC13'>	<span class="k">public</span> <span class="k">void</span> <span class="nf">WithSomeContext</span><span class="p">(</span><span class="n">Action</span> <span class="n">runInContext</span><span class="p">)</span></div><div class='line' id='LC14'>	<span class="p">{</span></div><div class='line' id='LC15'>		<span class="n">WithSomeContext</span><span class="p">&lt;</span><span class="kt">object</span><span class="p">&gt;(()</span> <span class="p">=&gt;</span></div><div class='line' id='LC16'>			<span class="p">{</span></div><div class='line' id='LC17'>				<span class="n">runInContext</span><span class="p">();</span></div><div class='line' id='LC18'>				<span class="k">return</span> <span class="k">null</span><span class="p">;</span></div><div class='line' id='LC19'>			<span class="p">});</span></div><div class='line' id='LC20'>	<span class="p">}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>	<span class="k">public</span> <span class="k">void</span> <span class="nf">UseIt</span><span class="p">()</span></div><div class='line' id='LC23'>	<span class="p">{</span></div><div class='line' id='LC24'>		<span class="n">var</span> <span class="n">valueOne</span> <span class="p">=</span> <span class="m">1</span><span class="p">;</span></div><div class='line' id='LC25'>		<span class="n">var</span> <span class="n">valueTwo</span> <span class="p">=</span> <span class="m">2</span><span class="p">;</span></div><div class='line' id='LC26'>		<span class="c1">// with return value</span></div><div class='line' id='LC27'>		<span class="n">var</span> <span class="n">sum</span> <span class="p">=</span> <span class="n">WithSomeContext</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="n">valueOne</span> <span class="p">+</span> <span class="n">valueTwo</span><span class="p">);</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>		<span class="n">WithSomeContext</span><span class="p">(()</span> <span class="p">=&gt;</span> <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">&quot;Hi-There&quot;</span><span class="p">));</span></div><div class='line' id='LC30'>	<span class="p">}</span></div><div class='line' id='LC31'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1597380/2c7baf9becb16465b3109433f82e7965b942a407/InCSharp.cs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1597380#file_in_c_sharp.cs" style="float:right;margin-right:10px;color:#666">InCSharp.cs</a>
            <a href="https://gist.github.com/1597380">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>That’s just boilerplate which I really don’t need. In many other languages, for example Scala, this is handled more elegantly. The void-type (often called Unit) is a regular type. And void/unit values just represent ‘void/unit‘:</p>
<div id="gist-1597380" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">class</span> <span class="nc">Example</span><span class="o">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="k">def</span> <span class="n">withContext</span><span class="o">[</span><span class="kt">T</span><span class="o">](</span><span class="n">runInContext</span><span class="k">:</span><span class="o">()=&gt;</span><span class="n">T</span><span class="o">)</span><span class="k">:</span><span class="kt">T</span> <span class="o">={</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// do magic and stuff</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">val</span> <span class="n">result</span> <span class="k">=</span> <span class="n">runInContext</span><span class="o">();</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// do other stuff</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">result</span><span class="o">;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC9'>&nbsp;&nbsp;</div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="c1">// no need for another method. </span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="c1">// Our withContext also can accept &#39;void&#39;-returns aka Unit </span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>&nbsp;&nbsp;<span class="k">def</span> <span class="n">useIt</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">val</span> <span class="n">valueOne</span> <span class="k">=</span> <span class="mi">1</span><span class="o">;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">val</span> <span class="n">valueTwo</span> <span class="k">=</span> <span class="mi">2</span><span class="o">;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">var</span> <span class="n">sum</span> <span class="k">=</span> <span class="n">withContext</span><span class="o">(()</span> <span class="k">=&gt;</span> <span class="n">valueOne</span> <span class="o">+</span> <span class="n">valueTwo</span><span class="o">);</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">withContext</span><span class="o">(()</span> <span class="k">=&gt;</span> <span class="n">println</span><span class="o">(</span><span class="s">&quot;Hi-There&quot;</span><span class="o">));</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC22'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1597380/bef752273b24b5861e717cb26f9f34d2654c2bbd/InScala.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1597380#file_in_scala.scala" style="float:right;margin-right:10px;color:#666">InScala.scala</a>
            <a href="https://gist.github.com/1597380">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Yes, I know, it’s not the end of the (programming) world. There are tons of other issues which lead to more boiler-plate. Anyhow, it is such a little thing and still so god damn annoying. God help us that no new programming includes the same little weakness =).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2012/01/annoying-void-in-c-java-alike/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yeah, Akka 2.0</title>
		<link>http://www.gamlor.info/wordpress/2011/12/yeah-akka-20/</link>
		<comments>http://www.gamlor.info/wordpress/2011/12/yeah-akka-20/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 17:21:13 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[akka-mobile]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2148</guid>
		<description><![CDATA[The first pre release bits of Akka 2.0 have been released. Unfortunately I haven’t looked all that deep into it yet. However, what I’ve seen really impresses me. It fixes nearly all major issues which Akka 1.2 and feels way more mature in its overall design. It feels like it can be the back-bone of [...]]]></description>
			<content:encoded><![CDATA[<p>The first pre release bits of Akka 2.0 have been released. Unfortunately I haven’t looked all that deep into it yet. However, what I’ve seen really impresses me. It fixes nearly all major issues which Akka 1.2 and feels way more mature in its overall design.</p>
<p>It feels like it can be the back-bone of an application, even when you use it to organizing big chunks of distinct functionality. When you start a project / experiment now start with Akka 2.0! It’s a much better experience than 1.2!</p>
<div id="attachment_2154" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/12/akka-20.png"><img class="size-medium wp-image-2154" title="akka-20" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/12/akka-20-300x132.png" alt="Akka 2.0" width="300" height="132" /></a><p class="wp-caption-text">Akka 2.0 </p></div>
<h2>What I Really Like About it</h2>
<ul>
<li>No god damn Singleton anymore. In Akka 1.2 the configuration and the remoting instance where hard wired Singletons. That made it really annoying to set up elaborate test cases, shut down the system cleanly etc. In Akka 2.0 you can start up as many ‘ActorSystem’-instances as you want, each one with its own configuration, setup etc.</li>
<li>Supervision is mandatory and easier to configure. In Akka 1.2 you can create actors without any parent and supervision. This basically also meant that errors just ‘disappeared’ and you only noticed stuff the log. In Akka 2.0 a supervision hierarchy is enforced. Error are past up to the appropriate supervisor. If you don’t specify anything the error basically will end up at the top level actor system.</li>
<li>The path-system. Now actors are basically addressed by file-system like paths. This is handy to get to the right actors. It’s also handy for error-logs: Instead of a cryptic UUID you get a ‘human-readable’ path.</li>
<li>Streamlined API. Nearly all API’s have been improved and streamlined.</li>
</ul>
<div id="attachment_2155" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/12/supervision-in-akka.png"><img class="size-medium wp-image-2155" title="supervision-in-akka" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/12/supervision-in-akka-300x209.png" alt="Supervision Mandatory" width="300" height="209" /></a><p class="wp-caption-text">Supervision Mandatory</p></div>
<h2>What I Don’t Like About It</h2>
<ul>
<li>Many, many breaking changes in the API, behavior etc. Of course all these fundamental improvements are not possible without breaking the API. However, porting stuff to new API’s is just no fun work.</li>
<li>The core jar is larger. Not a big deal for most cases, except for Android. Anyway, I run everything through Proguard anyway. And I believe that the internals got more efficient. =)</li>
</ul>
<div id="attachment_2156" class="wp-caption aligncenter" style="width: 259px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/12/larger-akka-20.png"><img class="size-medium wp-image-2156" title="larger-akka-20" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/12/larger-akka-20-249x300.png" alt="Akka 2.0 is a tiny bit larger (and a lot stronger)" width="249" height="300" /></a><p class="wp-caption-text">Akka 2.0 is a tiny bit larger (and a lot stronger)</p></div>
<h2>Akka-Mobile and Akka 2.0</h2>
<p>In general I’m stabilizing my Akka-Mobile code right now and fix some catastrophic bugs. After that I certainly will move everything to 2.0. I’m not planning to keep it on the 1.2/1.3 release. This is the plan:</p>
<ul>
<li>Stabilize stuff.</li>
<li>Move to Akka 2.0</li>
<li>Write documentation and a tiny demo App</li>
<li>Keep on improving the thing =)</li>
</ul>
<p>Ok, this was kind of a lazy ass blog-post. For more information go the <a href="http://blog.typesafe.com/akka-20-pre-release-milestone-1">Akka-blog</a> &amp; <a href="http://akka.io/docs/akka/snapshot/index.html">Snapshot</a>-documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2011/12/yeah-akka-20/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Akka-Mobile: First Code Snippets</title>
		<link>http://www.gamlor.info/wordpress/2011/11/akka-mobile-first-code-snippet/</link>
		<comments>http://www.gamlor.info/wordpress/2011/11/akka-mobile-first-code-snippet/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 21:52:53 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[akka-mobile]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2113</guid>
		<description><![CDATA[So far I’ve only talked about the idea / conceptof the Akka-Mobile. This time I show some small code examples =). Of course the current implementation is a uncompleted prototype. The implementation itself throws not implemented exceptions around every corner or has other unknown behavior. The API is nowhere near completion or final stage. Anyhow [...]]]></description>
			<content:encoded><![CDATA[<p>So far I’ve only talked about <a href="http://www.gamlor.info/wordpress/2011/11/akka-mobile-unreliable-connections-and-push-messages/">the idea / concept</a>of the Akka-Mobile. This time I show some small code examples =). Of course the current implementation is a uncompleted prototype. The implementation itself throws not implemented exceptions around every corner or has other unknown behavior. The API is nowhere near completion or final stage. Anyhow I just want show how it ‘feels’ like and throw in a few comments. Also keep in mind that Akka 2.0 is not that far away, and I will almost certainly move to it sooner or later.</p>
<div id="attachment_2122" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/code-drop.png"><img class="size-medium wp-image-2122" title="First look at code" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/code-drop-300x90.png" alt="First Look at Code" width="300" height="90" /></a><p class="wp-caption-text">First Look at Code</p></div>
<h2>The Server</h2>
<p>First let’s start a server. This is easily done by calling NettyRemoteServer.start() which starts up the server and listens to the specified port. Don’t confuse this with the regular Akka 1.2 remote actors. The mobile actors will use their own server-implementation. That’s also quite intentional: We want to expose only certain ‘service’ actors to our mobile clients and keep the rest internal to our cluster / internal infrastructure.</p>
<p>After that we can register regular actor to the given server instance. These actors then are reachable by the mobile clients under the given id. For example a ultra simple actor which just echoes the request:</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">val</span> <span class="n">chatServer</span> <span class="k">=</span> <span class="nc">NettyRemoteServer</span><span class="o">.</span><span class="n">start</span><span class="o">(</span><span class="mi">2552</span><span class="o">);</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="n">chatServer</span><span class="o">.</span><span class="n">register</span><span class="o">(</span><span class="s">&quot;chat-service&quot;</span><span class="o">,</span> <span class="nc">Actor</span><span class="o">.</span><span class="n">actorOf</span><span class="o">[</span><span class="kt">ChatServerActor</span><span class="o">])</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="k">class</span> <span class="nc">ChatServerActor</span> <span class="k">extends</span> <span class="nc">Actor</span><span class="o">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="k">protected</span> <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="n">msg</span> <span class="o">=&gt;{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">self</span><span class="o">.</span><span class="n">reply</span><span class="o">(</span><span class="s">&quot;Answer for: &quot;</span><span class="o">+</span><span class="n">msg</span><span class="o">)</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC12'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/5f3e8eb7d1157e842fc000121d00cced1dad7a1f/TheServer.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_the_server.scala" style="float:right;margin-right:10px;color:#666">TheServer.scala</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Well that’s not very exiting. It’s nearly identical to the regular Akka 1.2 remote actor.</p>
<h2>On The Android Device</h2>
<p>Now let’s move on to the Android device. The first thing we need to do is to <a href="https://github.com/pboos/scala-on-android-example#readme">get Scala running</a> =). Then we start with a normal ‘main’ activity. Optionally we can use the trait ‘ActivityActor’ which turns our activity into an actor. Any message send to the activity will be dispatched on the ‘activity’ thread, so that we can update the UI etc. Like this:</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">class</span> <span class="nc">AkkaDroidActivity</span> <span class="k">extends</span> <span class="nc">Activity</span> <span class="k">with</span> <span class="nc">ActivityActor</span> <span class="o">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="k">override</span> <span class="k">def</span> <span class="n">onCreate</span><span class="o">(</span><span class="n">savedInstanceState</span><span class="k">:</span> <span class="kt">Bundle</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">super</span><span class="o">.</span><span class="n">onCreate</span><span class="o">(</span><span class="n">savedInstanceState</span><span class="o">)</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">setContentView</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="n">layout</span><span class="o">.</span><span class="n">main</span><span class="o">)</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">actorOf</span><span class="o">[</span><span class="kt">DoSomeWork</span><span class="o">].</span><span class="n">start</span><span class="o">()</span></div><div class='line' id='LC8'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="k">protected</span> <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nc">WorkDone</span><span class="o">(</span><span class="n">happend</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="o">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">val</span> <span class="n">inputTextBox</span> <span class="k">=</span> <span class="n">findViewById</span><span class="o">(</span><span class="n">R</span><span class="o">.</span><span class="n">id</span><span class="o">.</span><span class="n">enterMsgBox</span><span class="o">).</span><span class="n">asInstanceOf</span><span class="o">[</span><span class="kt">EditText</span><span class="o">]</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">inputTextBox</span><span class="o">.</span><span class="n">setText</span><span class="o">(</span><span class="s">&quot;happend&quot;</span><span class="o">)</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC15'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC16'><span class="o">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="k">case</span> <span class="k">class</span> <span class="nc">WorkDone</span><span class="o">(</span><span class="n">result</span><span class="k">:</span><span class="kt">String</span><span class="o">)</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'><span class="k">class</span> <span class="nc">DoSomeWork</span> <span class="k">extends</span> <span class="nc">Actor</span><span class="o">{</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="k">protected</span> <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="s">&quot;Start&quot;</span> <span class="o">=&gt;{</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">val</span> <span class="n">result</span> <span class="k">=</span> <span class="n">workForAWhile</span><span class="o">()</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">self</span><span class="o">.</span><span class="n">reply</span><span class="o">(</span><span class="n">result</span><span class="o">)</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC27'>&nbsp;&nbsp;<span class="o">}</span> </div><div class='line' id='LC28'>&nbsp;&nbsp;</div><div class='line' id='LC29'>&nbsp;&nbsp;<span class="k">private</span> <span class="k">def</span> <span class="n">workForAWhile</span><span class="o">()</span> <span class="k">:</span><span class="kt">String</span> <span class="o">={</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// work work </span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="s">&quot;work-done&quot;</span></div><div class='line' id='LC32'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC33'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/d05d95f580f8c4f20bc1a313b94b541ad47a09a3/ActivityAsActor.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_activity_as_actor.scala" style="float:right;margin-right:10px;color:#666">ActivityAsActor.scala</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h3>Remote Actors</h3>
<p>For remote actors we need a client instance, usually one per application. When creating such an instance we need to pass in a Android context, which allows the remote implementation to get to Android resources. The other arguments are optional or loaded from the configuration.  I’ve put my remote instance in the global application object which is then registered in the Android manifest.</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// Registred in the AndroidManifest as our application object</span></div><div class='line' id='LC2'><span class="k">class</span> <span class="nc">MyApplication</span> <span class="k">extends</span> <span class="n">android</span><span class="o">.</span><span class="n">app</span><span class="o">.</span><span class="nc">Application</span> <span class="o">{</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="k">lazy</span> <span class="k">val</span> <span class="n">remote</span> <span class="k">=</span> <span class="nc">MobileRemoteClient</span><span class="o">.</span><span class="n">createClient</span><span class="o">(</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nc">AndroidDevice</span><span class="o">(</span><span class="k">this</span><span class="o">))</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/704d155461d09dc9b79e9ee781fbea2a96028c01/TheRemoteInterface.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_the_remote_interface.scala" style="float:right;margin-right:10px;color:#666">TheRemoteInterface.scala</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In the akka.conf we specify the host, port and preferably the Logcat logger:</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>akka {</div><div class='line' id='LC2'>&nbsp;&nbsp;event-handlers = [&quot;akka.mobile.android.LogcatLogger&quot;]</div><div class='line' id='LC3'>&nbsp;&nbsp;event-handler-level = &quot;DEBUG&quot; # Options: ERROR, WARNING, INFO, DEBUG</div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;mobile{</div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;client{</div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host = &quot;our.server.host&quot;</div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port = 2552</div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</div><div class='line' id='LC10'>&nbsp;&nbsp;}</div><div class='line' id='LC11'>}</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/879374aef87d255d0f16a614fb6caecea977e614/akka.conf" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_akka.conf" style="float:right;margin-right:10px;color:#666">akka.conf</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>After that we can get references to actors on the server by name and send messages to them. So we now can communicate with the actors on the server. Nice =)</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">class</span> <span class="nc">ChatClientActor</span> <span class="k">extends</span> <span class="nc">Actor</span><span class="o">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>&nbsp;&nbsp;<span class="k">protected</span> <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nc">SentToServer</span><span class="o">(</span><span class="n">msg</span><span class="o">)</span> <span class="o">=&gt;{</span></div><div class='line' id='LC5'>	  <span class="k">val</span> <span class="n">chatService</span> <span class="k">=</span> <span class="nc">MyApplication</span><span class="o">.</span><span class="n">remote</span><span class="o">.</span><span class="n">actorFor</span><span class="o">(</span><span class="s">&quot;chat-service&quot;</span><span class="o">)</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">chatService</span> <span class="o">!</span> <span class="n">msg</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="nc">MessageFromServer</span><span class="o">(</span><span class="n">msg</span><span class="o">)</span> <span class="o">=&gt;{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// well, show the message or do what is needed	</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="o">}</span> </div><div class='line' id='LC13'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/ea8ef4c8a5a3fc7e3aec0d909cb0aee3cff330d6/ChatClientActor.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_chat_client_actor.scala" style="float:right;margin-right:10px;color:#666">ChatClientActor.scala</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>‘Push’-Messages</h2>
<p>So far so good, we’ve connected from the Android device to the server, got a reference to it and started communicating with it.</p>
<p>But what if the server wants to initiate the communication with a client? To do that we register an actor on our remote instace! The server will later be able to contact this client-actor by its name:</p>
<p>The first thing we need is an ‘address’ for a client. Since mobile devices are on the move, they don&#8217;t permanent IP address. Akka mobile provides a ‘clientId&#8217; as a replacement. You can get that id in multiple way. For example when a server-side actor is communicatong with a Android device the self-reference will contain this client id. With the trait ServiceActor you get a nice accessor:</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">class</span> <span class="nc">TalksToMobileDevices</span> <span class="k">extends</span> <span class="nc">Actor</span> <span class="k">with</span> <span class="nc">ServiceActor</span><span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="k">protected</span> <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">case</span> <span class="n">msg</span> <span class="o">=&gt;{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">self</span><span class="o">.</span><span class="n">reply</span><span class="o">(</span><span class="s">&quot;Answer for: &quot;</span><span class="o">+</span><span class="n">msg</span><span class="o">)</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">val</span> <span class="n">clientId</span> <span class="k">=</span> <span class="k">this</span><span class="o">.</span><span class="n">clientId</span><span class="o">;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="o">(</span><span class="n">clientId</span><span class="o">.</span><span class="n">isDefined</span><span class="o">){</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// store it in database. So that you later can talk to this client</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC11'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/8f801c24faac263a9e72e22bb244e11c29f0c157/GetClientID.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_get_client_id.scala" style="float:right;margin-right:10px;color:#666">GetClientID.scala</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>As soon as we have a client id  we can get an reference for an actor running on the device and start sending messages to it.</p>
<div id="gist-1387130" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">val</span> <span class="n">clientId</span> <span class="k">=</span> <span class="c1">// clientId. Like from the database</span></div><div class='line' id='LC2'><span class="k">val</span> <span class="n">serviceOnPhone</span> <span class="k">=</span> <span class="n">chatServer</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="n">clientId</span><span class="o">,</span> <span class="s">&quot;notifications&quot;</span><span class="o">)</span></div><div class='line' id='LC3'><span class="n">serviceOnPhone</span> <span class="o">!</span> <span class="s">&quot;Hi Phone&quot;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1387130/face3031918ebd7023b2d6046b6a964856e82136/TalkToServiceOnPhone.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1387130#file_talk_to_service_on_phone.scala" style="float:right;margin-right:10px;color:#666">TalkToServiceOnPhone.scala</a>
            <a href="https://gist.github.com/1387130">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<div id="attachment_2123" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/c2md-support.png"><img class="size-full wp-image-2123" title="Pushing Messages to the Client" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/c2md-support.png" alt="Pushing Messages to the Client" width="500" height="466" /></a><p class="wp-caption-text">Pushing Messages to the Client</p></div>
<h2>But Wait, What if the Device is Offline?</h2>
<p>Now the stuff I&#8217;ve showed so far only works as long as the Phone has a connection to the Server. What if the Phone is offline? Or the application isn&#8217;t running. Well here&#8217;s where the C2MD integration comes in. Akka mobile can deliver messages via C2MD. Here&#8217;s a short overview how it works.</p>
<h3>Preparation on the Server</h3>
<p>First we need to configure the Server to support C2MD: We need to add two things: The C2MD authentication key for talking to the C2MD servers and a database backend. The database backend is required to store the registration ids of all devices.</p>
<p>So in the akka.conf configuration we add:</p>
<div id="gist-1428024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>akka {</div><div class='line' id='LC2'>&nbsp;&nbsp;event-handlers = [&quot;akka.mobile.android.LogcatLogger&quot;]</div><div class='line' id='LC3'>&nbsp;&nbsp;event-handler-level = &quot;DEBUG&quot; # Options: ERROR, WARNING, INFO, DEBUG</div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>&nbsp;&nbsp;mobile{</div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;client{</div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host = &quot;our.server.host&quot;</div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;port = 2552</div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c2md{</div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email=&quot;the-c2md-email@thecompany&quot;</div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register-mode = &quot;AUTO&quot; # Possible values: MANUAL, AUTO, default is MANUAL</div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</div><div class='line' id='LC14'>&nbsp;&nbsp;}</div><div class='line' id='LC15'>}</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1428024/e726a78c7a45873eee25e4f6d1b4943eea6e2676/akka-conf-on-client.conf" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1428024#file_akka_conf_on_client.conf" style="float:right;margin-right:10px;color:#666">akka-conf-on-client.conf</a>
            <a href="https://gist.github.com/1428024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>And we start the server with a given database :</p>
<div id="gist-1428024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">val</span> <span class="n">chatServer</span> <span class="k">=</span> <span class="nc">NettyRemoteServer</span><span class="o">.</span><span class="n">start</span><span class="o">(</span><span class="n">port</span><span class="o">=</span><span class="mi">2552</span><span class="o">,</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">database</span> <span class="k">=</span> <span class="nc">Some</span><span class="o">(</span><span class="k">new</span> <span class="n">H2Database</span><span class="o">(</span><span class="s">&quot;jdbc:h2:~/apiKeyStore&quot;</span><span class="o">)));</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1428024/f38f63033876a91b6ceb3617734d0795e9f22c60/ServerWithDB.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1428024#file_server_with_db.scala" style="float:right;margin-right:10px;color:#666">ServerWithDB.scala</a>
            <a href="https://gist.github.com/1428024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h3>Preparation on the Client</h3>
<p>On the client we also update the akka.conf. We add the email for which the C2MD service is registered:</p>
<div id="gist-1428024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>akka {</div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;mobile{</div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c2md{</div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key=&quot;Your Application Key for C2MD&quot;</div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;}</div><div class='line' id='LC8'>}</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1428024/9cac4dc0b3b79c06dc355c108706bf7c641ed727/akka-conf-on-server.conf" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1428024#file_akka_conf_on_server.conf" style="float:right;margin-right:10px;color:#666">akka-conf-on-server.conf</a>
            <a href="https://gist.github.com/1428024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Additionally we need a broad cast receiver which handles the C2MD intents. We inherit the &#8216;C2MDReceiver&#8217; -trait and implement the &#8216;remoteClient&#8217; method. In that method we return our instance of the akka-mobile remote client which our application is using. In this example I&#8217;ve that instance  in the application object and get it from there (via ugly cast, don&#8217;t hit me).</p>
<div id="gist-1428024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">class</span> <span class="nc">DispatchToAkka</span> <span class="k">extends</span> <span class="n">C2MDReceiver</span> <span class="o">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="k">def</span> <span class="n">remoteClient</span><span class="o">(</span><span class="n">context</span><span class="k">:</span> <span class="kt">Context</span><span class="o">)</span> <span class="k">=</span> <span class="o">{</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">context</span><span class="o">.</span><span class="n">getApplicationContext</span><span class="o">.</span><span class="n">asInstanceOf</span><span class="o">[</span><span class="kt">MyApplication</span><span class="o">].</span><span class="n">remote</span><span class="o">;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC5'><span class="o">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1428024/579c3a2b0c3f04be7365688767dbf8f64373f97b/BroadcastReceiver.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1428024#file_broadcast_receiver.scala" style="float:right;margin-right:10px;color:#666">BroadcastReceiver.scala</a>
            <a href="https://gist.github.com/1428024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>In the Android Manifest you need to get the permissions for C2MD and register the broadcast receiver previously created:</p>
<div id="gist-1428024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;manifest</span> <span class="na">xmlns:android=</span><span class="s">&quot;http://schemas.android.com/apk/res/android&quot;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">package=</span><span class="s">&quot;info.gamlor.akkamobile&quot;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">android:versionCode=</span><span class="s">&quot;1&quot;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">android:versionName=</span><span class="s">&quot;1.0&quot;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">android:debuggable=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;uses-sdk</span> <span class="na">android:minSdkVersion=</span><span class="s">&quot;10&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;permission</span> <span class="na">android:name=</span><span class="s">&quot;info.gamlor.akkamobile.permission.C2D_MESSAGE&quot;</span> <span class="na">android:protectionLevel=</span><span class="s">&quot;signature&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;uses-permission</span> <span class="na">android:name=</span><span class="s">&quot;info.gamlor.akkamobile.permission.C2D_MESSAGE&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;uses-permission</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">android:name=</span><span class="s">&quot;android.permission.INTERNET&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;uses-permission</span> <span class="na">android:name=</span><span class="s">&quot;com.google.android.c2dm.permission.RECEIVE&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;application</span> <span class="na">android:name=</span><span class="s">&quot;.MyApplication&quot;</span> <span class="na">android:label=</span><span class="s">&quot;@string/app_name&quot;</span> <span class="na">android:icon=</span><span class="s">&quot;@drawable/icon&quot;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">android:debuggable=</span><span class="s">&quot;true&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c">&lt;!-- Activities etc --&gt;</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c">&lt;!-- Our Broadcast receiver for integration C2MD with akka-mobile --&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;receiver</span> <span class="na">android:name=</span><span class="s">&quot;.DispatchToAkka&quot;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">android:permission=</span><span class="s">&quot;com.google.android.c2dm.permission.SEND&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;intent-filter&gt;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;action</span> <span class="na">android:name=</span><span class="s">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;category</span> <span class="na">android:name=</span><span class="s">&quot;info.gamlor.akkamobile&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/intent-filter&gt;</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;intent-filter&gt;</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;action</span> <span class="na">android:name=</span><span class="s">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;category</span> <span class="na">android:name=</span><span class="s">&quot;info.gamlor.akkamobile&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/intent-filter&gt;</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/receiver&gt;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/application&gt;</span></div><div class='line' id='LC32'><span class="nt">&lt;/manifest&gt;</span> </div><div class='line' id='LC33'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1428024/c6ad1c8348ad20aac6e159a039003dde0a6b87cc/AndroidManifest.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1428024#file_android_manifest.xml" style="float:right;margin-right:10px;color:#666">AndroidManifest.xml</a>
            <a href="https://gist.github.com/1428024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>After that akka mobile is ready to use C2MD. It will register the application automatically and report its registration-id to out server as soon as the remote-instance is created. Or you can optionally disable the auto-registration and use &#8216;requestC2MDRegistration()&#8217; to start the C2MD registration at a certain point in time.</p>
<h3>Sending a Message over C2MD</h3>
<p>Now messages are still not sent with C2MD if no connection is available. The reason is that akka-mobile wants to prevent that we send hundreds of messages via C2MD by accident. We should be very careful and only sent important notifications over C2MD. There are two ways to achieve that.</p>
<p>One is to use the marker-trait  &#8217;SentThroughC2MDIfNoConnectionIsAvailable&#8217;  in your message. If a message implements this trait it will be delivered via C2MD if no connection is found.</p>
<div id="gist-1428024" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="k">case</span> <span class="k">class</span> <span class="nc">ImportantNotification</span><span class="o">(</span><span class="n">msg</span><span class="k">:</span><span class="kt">String</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">SentThroughC2MDIfNoConnectionIsAvailable</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1428024/d10d3b3259a5d7b5e2dac9a5a98d2329d98aa528/ForceC2MD.scala" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1428024#file_force_c2_md.scala" style="float:right;margin-right:10px;color:#666">ForceC2MD.scala</a>
            <a href="https://gist.github.com/1428024">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>The other possibility is by using a &#8216;error&#8217;-handler which falls back on C2MD if no connection can be established. However I explain error-handlers next time, since they are quite central to the whole communication stack.</p>
<h2>Stuff Still To Implement for a Proper Prototype</h2>
<p>So, that the first look at how akka-mobile will work. There still tons of things to do. Also very basic stuff:</p>
<ul>
<li>Improve stability, test-suite and build-process.</li>
<li>Better integration with Android, especially the connection-manager. Maybe also with power management.</li>
<li>&#8216;Session&#8217; actors on the server</li>
<li>Better serialization story. Currently only Java Serialization is supported. Java serialization always makes me nervous.</li>
<li>Error-Handler API has to improve, a lot. More about that next time.</li>
<li>Do some basic performance analysis.</li>
<li>Finally start on &#8216;cool&#8217; features&#8230;.</li>
</ul>
<p>Otherwise I&#8217;ve tons of other features I want to tackle, but those above are really needed to get to a more stable state.</p>
<div id="attachment_2124" class="wp-caption aligncenter" style="width: 567px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/todo-akka-mobile.png"><img class="size-full wp-image-2124" title="todo-akka-mobile" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/todo-akka-mobile.png" alt="Lot's of Things to Do" width="557" height="400" /></a><p class="wp-caption-text">Lot&#39;s of Things to Do</p></div>
<h2>Conclusion and Next Time</h2>
<p>So we&#8217;ve seen how we can setup the akka mobile remote actors. It allows us to have nice device to server communication between actors. It also allows us to fall back on C2MD messages when a client doesn&#8217;t have a connection to a server.</p>
<p>Next time I&#8217;m going to talk about error-handlers. These are responsible for managing connection loses etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2011/11/akka-mobile-first-code-snippet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Akka-Mobile: Unreliable Connections, Push-Messages</title>
		<link>http://www.gamlor.info/wordpress/2011/11/akka-mobile-unreliable-connections-and-push-messages/</link>
		<comments>http://www.gamlor.info/wordpress/2011/11/akka-mobile-unreliable-connections-and-push-messages/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 18:02:06 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[akka-mobile]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2079</guid>
		<description><![CDATA[Oh, it has been a while since I’ve talked about my pet project =). This time I’m talking about the fundamental issues I want to tackle in the project. Client-Server Oriented Remote Actors The regular Akka remote actors in Akka 1.2 have an almost ‘symmetric’ implementation. To get the full ‘send’, ‘reply’ behavior you have [...]]]></description>
			<content:encoded><![CDATA[<p>Oh, it has been a while since <a href="http://www.gamlor.info/wordpress/2011/10/akka-mobile-the-idea">I’ve talked about my pet project</a> =). This time I’m talking about the fundamental issues I want to tackle in the project.</p>
<h2>Client-Server Oriented Remote Actors</h2>
<p>The regular Akka remote actors in Akka 1.2 have an almost ‘symmetric’ implementation. To get the full ‘send’, ‘reply’ behavior you have to start up <a href="http://stackoverflow.com/questions/7674468/reply-is-not-transmitted-back-to-the-client-actor">a full Netty server on the ‘client’ and the ‘server’</a>. And with the upcoming Akka 2.0 clustering this won’t change that much.</p>
<p>In a mobile-device scenario this is bit different. We have a clear distinction: A small, lightweight application which is the client and a server (-cluster) which runs the service. The client should be small and lightweight and usually maintains one connection to the server. The server can be large, but needs to maintain many connections to many devices.</p>
<p>Furthermore a connection is only in one way, from the client to the server. The server cannot directly contact a client. The client has almost certainly a dynamic, no public IP which changes a lot: Walking away from your Wi-Fi at home to a mobile network to another Wi-Fi hotspot. For an implementation that means the client needs to able to reconnect on network changes back to the server. And the server needs to use the latest connection when he wants to reply to a client.</p>
<div id="attachment_2080" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/asymmetric.png"><img class="size-full wp-image-2080" title="asymmetric" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/asymmetric.png" alt="Bad Company for the Server" width="600" height="358" /></a><p class="wp-caption-text">Bad Company for the Server</p></div>
<p>&nbsp;</p>
<h2>Unreliable Connections</h2>
<p>The implementation needs to deal somehow with unreliable connections. I want to have a ‘supervisior’ hierarchy in place for this. For example the first layer just tries to reconnect immediately; the second layer waits a little to reconnect. The next layer it stores the message away for later or escalates the issue to the user etc. This should be configurable of course.</p>
<h2>Push-Notifications/Message from the Server</h2>
<p>One thing I want have in Akka-Mobile is ‘push-notification’ support. Basically I want be able to send a message to a mobile device and let the device react to it. The server maintains a database of known mobile devices. When he wants to notify a mobile device he grabs an Actor reference which represents that device and sends the message to the client. Then the implementation looks up if there’s an active connection available to that device. If that’s the case, it just sends the message to the device. Otherwise it will fall back to some other push-notification mechanism, for example the <a href="http://code.google.com/android/c2dm/">Android Cloud to Device Messaging Framework (C2MD)</a>.</p>
<h2>Behavior Overview</h2>
<p>Here&#8217;s a little sketch how it should work during active communication:</p>
<div id="attachment_2082" class="wp-caption aligncenter" style="width: 610px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/communication.png"><img class="size-full wp-image-2082" title="Communication With Akka Mobile" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/communication.png" alt="Communication With Akka Mobile" width="600" height="714" /></a><p class="wp-caption-text">Communication With Akka Mobile</p></div>
<ol>
<li>The Mobile Device / Client connects to the server, gets a reference to a remote actor, for example the actor &#8220;John&#8221; and sends a message to it.</li>
<li>The server associates TCP connection with the device id (here &#8220;Green Devil&#8221;) and the forwards the message to the right actor.</li>
<li>The actor does its thing. The actor can send as many messages back to the &#8216;sender&#8217;. It also can pass the actor reference to other actors and those actors can answer.</li>
<li>When a message has to go back to the mobile device the server implementation looks up if there still a connection to the mobile device&#8230;</li>
<li>&#8230;and sends the answers to the right actor on the mobile device.</li>
</ol>
<p>Now what happens when there&#8217;s no connection from the mobile device to the server? That&#8217;s when the C2MD service comes in. Then a message is send via that service to tell the mobile device that there are answers available on the server. Of course the has to be some kind of configuration which types of messages are important enough to cause this behavior. So the behaviors changes at step 4:</p>
<div id="attachment_2083" class="wp-caption aligncenter" style="width: 592px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/c2md.png"><img class="size-full wp-image-2083" title="Cloud To Mobile Device Integration" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/11/c2md.png" alt="Cloud To Mobile Device Integration" width="582" height="1077" /></a><p class="wp-caption-text">Cloud To Mobile Device Integration</p></div>
<ol>
<li value="4">The server detects that there is no active connection to the client. So he has to fall back to alternatives.</li>
<li>He could issue a C2MD request to notify the client that there&#8217;s stuff available for him.</li>
<li>Let&#8217;s Google do its job</li>
<li>The client reconnects to get the answers/messages</li>
</ol>
<h2>Conclusion</h2>
<p>Akka Mobile has to deal with the changing and unreliable network topography. That&#8217;s means tons of tedious implementation details and tons of work for me.</p>
<p>Anyway, next time I will show code and talk less =)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2011/11/akka-mobile-unreliable-connections-and-push-messages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Running Scala on Android</title>
		<link>http://www.gamlor.info/wordpress/2011/10/running-scala-on-android/</link>
		<comments>http://www.gamlor.info/wordpress/2011/10/running-scala-on-android/#comments</comments>
		<pubDate>Tue, 18 Oct 2011 19:32:27 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[akka-mobile]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[software-development]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2040</guid>
		<description><![CDATA[For my little pet project Akka-Mobile I need to run Scala on Android. In theory this is dead simple. However in practice there are some issues. When you simply add the Scala runtime library to an Android project you will get an error during dexing. The reason is that the Scala runtime library is just [...]]]></description>
			<content:encoded><![CDATA[<p>For my little pet project <a href="http://www.gamlor.info/wordpress/2011/10/akka-mobile-the-idea/">Akka-Mobile</a> I need to run Scala on Android. In theory this is dead simple. However in practice there are some issues. When you simply add the Scala runtime library to an Android project you will get an error during dexing. The reason is that the Scala runtime library is just too large. The way to get around this issue is to remove the class-files which aren’t required by your application. This is usually done with <a href="http://proguard.sourceforge.net/">Proguard</a>.</p>
<div id="attachment_2047" class="wp-caption aligncenter" style="width: 268px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/android-scala.png"><img class="size-medium wp-image-2047" title="android-scala" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/android-scala-258x300.png" alt="Scala on Android" width="258" height="300" /></a><p class="wp-caption-text">Scala on Android</p></div>
<h2>The Build Script</h2>
<p>So you need to include Proguard in your build script. There are different ways to do it. I tried several existing build scripts from different people with mixed results. Finally I’ve settled with a script from <a href="http://lamp.epfl.ch/~michelou/android/scala-to-android.html">here</a>. This site contains a description and steps you need to take.</p>
<p>Basically you can download the <a href="http://lampsvn.epfl.ch/svn-repos/scala/android-examples/trunk/android-sdk/HelloActivity/build-scala.xml">build-scala.xml</a> and the <a href="http://lampsvn.epfl.ch/svn-repos/scala/android-examples/trunk/android-sdk/HelloActivity/proguard-template.cfg">proguard-template</a> from that website. Then you add this task to the default build.xml, right below the ‘setup’-tag:</p>
<div id="gist-1295985" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;setup</span> <span class="nt">/&gt;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="nt">&lt;import</span> <span class="na">file=</span><span class="s">&quot;build-scala.xml&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="c">&lt;!-- Converts this project&#39;s .class files into .dex files --&gt;</span></div><div class='line' id='LC6'><span class="nt">&lt;target</span> <span class="na">name=</span><span class="s">&quot;-post-compile&quot;</span> <span class="na">depends=</span><span class="s">&quot;-post-compile-scala&quot;</span> <span class="nt">/&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1295985/e394058d6892275a8e05e12e484a10cb1dda9e61/build.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1295985#file_build.xml" style="float:right;margin-right:10px;color:#666">build.xml</a>
            <a href="https://gist.github.com/1295985">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Of course you need to set the right paths in the local.properties file on your dev machines. For example on mine:</p>
<div id="gist-1295985" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="na">sdk.dir</span><span class="o">=</span><span class="s">C:\\progs\\android\\sdk</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="na">scala.dir</span><span class="o">=</span><span class="s">C:\\progs\\scala</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1295985/41eb398d426bdcaa595872b78e66d3d477aed0f8/local.properties" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1295985#file_local.properties" style="float:right;margin-right:10px;color:#666">local.properties</a>
            <a href="https://gist.github.com/1295985">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Note that Proguard doesn’t like spaces and other special characters in file paths. I had trouble initially because of that. You either can ensure that everything is properly escaped or that you put everything in an appropriate location.</p>
<p>I also added a newer version Proguard to my project, because the version shipped with Android had some bugs. So in the build.properties I have:</p>
<div id="gist-1295985" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c"># Current proguard location for this project</span></div><div class='line' id='LC2'><span class="na">proguard.dir</span><span class="o">=</span><span class="s">./tools/proguard/</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1295985/34797e23c2fabe7c7fa9650b305875957f96e8a5/build.properties" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1295985#file_build.properties" style="float:right;margin-right:10px;color:#666">build.properties</a>
            <a href="https://gist.github.com/1295985">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>That’s all. Of course I’ve done some project specific modifications to the original build file. Anyway, when you follow the instructions of the original page you <a href="http://lamp.epfl.ch/~michelou/android/scala-to-android.html">should be fine</a>.</p>
<h2>The IDE Setup</h2>
<p>Of course the build script is only half the story. I want to use my IDE to edit and debug the code. I’m an IntelliJ user, so this is my IntelliJ setup. (I should check out Eclipse’s Scala support, I’ve heard that they put a lot of effort into it).</p>
<p>First create (or import) the project like a regular Android project. Then also add the Scala facetted to the project. Now in theory you can compile and run that project. Unfortunately the dexing step will fail due to the large Scala runtime.</p>
<div id="attachment_2042" class="wp-caption aligncenter" style="width: 471px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/intellij-project-setup.png"><img class="size-full wp-image-2042" title="intellij-project-setup" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/intellij-project-setup.png" alt="Android and Scala Facette" width="461" height="224" /></a><p class="wp-caption-text">Android and Scala Facette</p></div>
<p>&nbsp;</p>
<p>Basically you need to replace the build step with your costume build step. I ‘unchecked’ the ‘make’ step in the run configuration and added an Ant step. Like this:</p>
<div id="attachment_2043" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/intellij-run-config.png"><img class="size-medium wp-image-2043" title="intellij-run-config" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/intellij-run-config-300x190.png" alt="Run Configuration" width="300" height="190" /></a><p class="wp-caption-text">Run Configuration</p></div>
<p>&nbsp;</p>
<p>You can run any Ant target which builds the .apk for you. The only thing you need to ensure is that IntelliJ finds the apk-file. I created a special ‘debug-install’ target which compiles the project and copies the apk to the location where IntelliJ usually puts it. An alternative would be to configure IntelliJ to use your location. Here’s the Ant-Task for my IDE debugging:</p>
<div id="gist-1295985" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;target</span> <span class="na">name=</span><span class="s">&quot;debug-install&quot;</span> <span class="na">depends=</span><span class="s">&quot;debug&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC2'>	<span class="nt">&lt;copy</span> <span class="na">file=</span><span class="s">&quot;./bin/AkkaDroidApp-debug.apk&quot;</span></div><div class='line' id='LC3'>		<span class="na">tofile=</span><span class="s">&quot;./out/production/AkkaDroidApp/AkkaDroidApp.apk&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC4'><span class="nt">&lt;/target&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1295985/4227bf06d9640b03ace3bafd4912bbf933876504/build-2.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1295985#file_build_2.xml" style="float:right;margin-right:10px;color:#666">build-2.xml</a>
            <a href="https://gist.github.com/1295985">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>After these steps everything should run in IntelliJ. When you use Run / Debug the Ant script will build the apk and IntelliJ will load it into the Emulator / Phone. Afterwards it just attaches to the process like normal. Everything should work, like the debugger etc.<br />
<a name="update40"></a><br />
<h2>Update For Android 4.0 SDK</h2>
<p>With the 4.0 SDK the standard Android build file has changed, so that the original script above doesn&#8217;t seem to work anymore. You will get an error telling you that the build script is outdated. Here&#8217;s what I&#8217;ve done to get it to run:</p>
<ul>
<li>I followed the instructions of the error message. I deleted the existing build.xml and then upgraded the project with: &#8216;android update project &#8211;path ./&#8217;. (You need the android tools path in your PATH variable. Or use the complete path to the &#8216;android&#8217; command of your SDK).</li>
<li>Then I re.added my Scala tasks in the build.xml:<br />
<div id="gist-1299279" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">&lt;!-- version-tag: 1 --&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;import</span> <span class="na">file=</span><span class="s">&quot;${sdk.dir}/tools/ant/build.xml&quot;</span> <span class="nt">/&gt;</span>	</div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="nt">&lt;import</span> <span class="na">file=</span><span class="s">&quot;build-scala.xml&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="nt">&lt;target</span> <span class="na">name=</span><span class="s">&quot;-post-compile&quot;</span> <span class="na">depends=</span><span class="s">&quot;-post-compile-scala&quot;</span> <span class="nt">/&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1299279/e0365dcd3bcb6c7be480ba8909b6dc8aa74d6796/build.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1299279#file_build.xml" style="float:right;margin-right:10px;color:#666">build.xml</a>
            <a href="https://gist.github.com/1299279">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</li>
<li>Unfortunately the &#8216;build-scala.xml&#8217; file still had issues with the new SDK. I fixed two things. First the &#8216;compile-scala&#8217; task new depends on &#8216;-compile&#8217; instead of &#8216;compile&#8217;. I also removed the &#8216;&lt;src refid=&#8221;project.libraries.src&#8221;/&gt;&#8217; entry. Looks like this property isn&#8217;t there anymore, but it wasn&#8217;t used anyway in my project. The compile-scala task look now like this:<br />
<div id="gist-1299279" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;target</span> <span class="na">name=</span><span class="s">&quot;compile-scala&quot;</span> <span class="na">depends=</span><span class="s">&quot;-compile&quot;</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">if=</span><span class="s">&quot;contains.scala.sources&quot;</span> <span class="na">unless=</span><span class="s">&quot;do.not.compile&quot;</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">description=</span><span class="s">&quot;Compiles project&#39;s .scala files into .class files&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;condition</span> <span class="na">property=</span><span class="s">&quot;logging&quot;</span> <span class="na">value=</span><span class="s">&quot;verbose&quot;</span> <span class="na">else=</span><span class="s">&quot;none&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;istrue</span> <span class="na">value=</span><span class="s">&quot;${verbose}&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/condition&gt;</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;echo</span> <span class="na">message=</span><span class="s">&quot;${scalac.banner}&quot;</span> <span class="na">level=</span><span class="s">&quot;info&quot;</span> <span class="na">taskname=</span><span class="s">&quot;scalac&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;scalac</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">destdir=</span><span class="s">&quot;${out.classes.absolute.dir}&quot;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">bootclasspathref=</span><span class="s">&quot;android.target.classpath&quot;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">logging=</span><span class="s">&quot;${logging}&quot;</span> <span class="na">addparams=</span><span class="s">&quot;${scalac.addparams}&quot;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">classpath=</span><span class="s">&quot;${extensible.classpath}&quot;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="na">classpathref=</span><span class="s">&quot;jar.libs.ref&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;src</span> <span class="na">path=</span><span class="s">&quot;${source.absolute.dir}&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;src</span> <span class="na">path=</span><span class="s">&quot;${gen.absolute.dir}&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;classpath&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;pathelement</span> <span class="na">location=</span><span class="s">&quot;${scala-library.jar}&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;fileset</span> <span class="na">dir=</span><span class="s">&quot;${akka-lib-dir}&quot;</span> <span class="na">includes=</span><span class="s">&quot;*.jar&quot;</span><span class="nt">&gt;&lt;/fileset&gt;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;pathelement</span> <span class="na">location=</span><span class="s">&quot;&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c">&lt;!-- include compiled resources --&gt;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;pathelement</span> <span class="na">location=</span><span class="s">&quot;${out.classes.absolute.dir}&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;fileset</span> <span class="na">dir=</span><span class="s">&quot;${extensible.libs.classpath}&quot;</span> <span class="na">includes=</span><span class="s">&quot;*.jar&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/classpath&gt;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/scalac&gt;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/target&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1299279/5982ceef638ac596131449fedec9894ef4125cf0/build-scala.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1299279#file_build_scala.xml" style="float:right;margin-right:10px;color:#666">build-scala.xml</a>
            <a href="https://gist.github.com/1299279">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</li>
<li>I added the property &#8216;manifest.package&#8217; which holds name of the top package. Looks like this property isn&#8217;t defined anymore by the standard build. This property is used for Progard to keep those classes in your project. So for my project the &#8216;ant.properties&#8217; file now looks like this:<br />
<div id="gist-1299279" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="na">scalac.addparams</span><span class="o">=</span><span class="s">-deprecation</span></div><div class='line' id='LC2'><span class="na">proguard.dir</span><span class="o">=</span><span class="s">./tools/proguard</span></div><div class='line' id='LC3'><span class="na">manifest.package</span><span class="o">=</span><span class="s">info.gamlor.akkamobile</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1299279/c626d919d3dc6d23cdd83cfe3f1c4df14b190d21/ant.properties" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1299279#file_ant.properties" style="float:right;margin-right:10px;color:#666">ant.properties</a>
            <a href="https://gist.github.com/1299279">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</li>
<li>The last change I&#8217;ve done is in the &#8216;-post-compile-scala&#8217; task. Here the &#8216;build.mode.debug&#8217; and &#8216;build.mode.release&#8217; flags are no longer set. Instead the &#8216;build.target&#8217; property contains the build type. So I&#8217;ve changed that in the &#8216;-post-compile-scala&#8217; task:<div id="gist-1299279" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;target</span> <span class="na">name=</span><span class="s">&quot;-post-compile-scala&quot;</span> <span class="na">depends=</span><span class="s">&quot;compile-scala&quot;</span> <span class="na">unless=</span><span class="s">&quot;do.not.compile&quot;</span></div><div class='line' id='LC2'>		<span class="na">description=</span><span class="s">&quot;Shrink project class files and Scala library into one .jar file&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC3'>	<span class="nt">&lt;condition</span> <span class="na">property=</span><span class="s">&quot;debug.mode&quot;</span> <span class="na">value=</span><span class="s">&quot;&quot;</span> <span class="na">else=</span><span class="s">&quot;#DEBUG#&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC4'>		 <span class="nt">&lt;equals</span> <span class="na">arg1=</span><span class="s">&quot;${build.target}&quot;</span> <span class="na">arg2=</span><span class="s">&quot;debug&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC5'>	<span class="nt">&lt;/condition&gt;</span></div><div class='line' id='LC6'>	<span class="nt">&lt;condition</span> <span class="na">property=</span><span class="s">&quot;release.mode&quot;</span> <span class="na">value=</span><span class="s">&quot;&quot;</span> <span class="na">else=</span><span class="s">&quot;#RELEASE#&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC7'>		 <span class="nt">&lt;equals</span> <span class="na">arg1=</span><span class="s">&quot;${build.target}&quot;</span> <span class="na">arg2=</span><span class="s">&quot;release&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC8'>	<span class="nt">&lt;/condition&gt;</span></div><div class='line' id='LC9'>	<span class="c">&lt;!-- priority is given to local ProGuard configuration if present --&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1299279/4abd93117dc1abf877ed5dc3000c9301f38ce17e/build-scala-second-change.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1299279#file_build_scala_second_change.xml" style="float:right;margin-right:10px;color:#666">build-scala-second-change.xml</a>
            <a href="https://gist.github.com/1299279">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</li>
</ul>
<h2>Second &#038; Third Update</h2>
<p>First I&#8217;ve fixed another thing above so that debugging works with the Android 4.0 SDK. Then I&#8217;ve created an example project which should work with the Android 4.0 SDK and incorporates all my changes as explained above. Then Patrick Boos improved that even further. Thanks a lot. You can find the <a href="https://github.com/pboos/scala-on-android-example#readme">example project on GitHub (https://github.com/pboos/scala-on-android-example#readme)</a> . Don&#8217;t forget to fix the local.properties for your environment. And remember that Proguard doesn&#8217;t like spaces in paths. My build file won&#8217;t work with spaces in file paths right now =(. </p>
<h2>Conclusion</h2>
<p>That’s it for now. If you know a better way, have tips or problems: Tell me =).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2011/10/running-scala-on-android/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Akka-Mobile: The Idea</title>
		<link>http://www.gamlor.info/wordpress/2011/10/akka-mobile-the-idea/</link>
		<comments>http://www.gamlor.info/wordpress/2011/10/akka-mobile-the-idea/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 21:26:56 +0000</pubDate>
		<dc:creator>gamlerhart</dc:creator>
				<category><![CDATA[akka-mobile]]></category>
		<category><![CDATA[akka]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.gamlor.info/wordpress/?p=2016</guid>
		<description><![CDATA[What is Akka-Mobile? Right now it’s an idea. It’s my semester term project at the University of Applied Science in Rapperswil. In this blog post I give a short overview of the idea. What is Akka? Akka is a framework for Scala (and also Java) to bring Erlang style actors (and tons of other things) [...]]]></description>
			<content:encoded><![CDATA[<p>What is Akka-Mobile? Right now it’s an idea. It’s my semester term project at the University of Applied Science in Rapperswil. In this blog post I give a short overview of the idea.</p>
<h2>What is Akka?</h2>
<p><a href="http://akka.io">Akka</a> is a framework for Scala (and also Java) to bring Erlang style actors (and tons of other things) to the JVM. In the <a href="http://en.wikipedia.org/wiki/Actor_model">actor model</a> we have actors which communicate through message passing with each other. Each actor can react to messages, do some computation and sent messages to other actors which he knows. The message passing it the only way actors can communicate, there not shared state or other communication channels.</p>
<h2>Why Actors?</h2>
<p>I just cite from the Akka website:</p>
<blockquote><p>“We believe that writing correct concurrent, fault-tolerant and scalable applications is too hard. Most of the time it&#8217;s because we are using the wrong tools and the wrong level of abstraction.  Akka is here to change that.  Using the Actor Model together with Software Transactional Memory we raise the abstraction level and provide a better platform to build correct concurrent and scalable applications.”</p></blockquote>
<p>And:</p>
<blockquote><p>“For fault-tolerance we adopt the &#8220;Let it crash&#8221; / &#8220;Embrace failure&#8221; model which have been used with great success in the telecom industry to build applications that self-heal, systems that never stop.”</p></blockquote>
<h2>Akka Mobile?</h2>
<p>The Actor Model works well for distributed, ‘event’-oriented and fault tolerant environments. Where do we have such systems? Yes, in the mobile space! Think about all the phones our there running tons of applications. Many of these applications communicate with a central service or even between devices. Think about all the applications on your phone which have ‘real-time’ communication features? Chat apps, social apps like Facebook &amp; Twitter, GPS apps which incorporates the latest traffic information etc. And all these applications have to deal with network faults, message delivery and coordination.</p>
<p>So I think the Actor model is a good fit for these kinds of ‘real time’ applications on the phone. That’s why I want to get the Actor goodness on the phone.</p>
<div id="attachment_2018" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/actors-phones.png"><img class="size-medium wp-image-2018" title="actors-phones" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/actors-phones-300x137.png" alt="Actors and Phones Don't Get Along (Yet)" width="300" height="137" /></a><p class="wp-caption-text">Actors and Phones Don&#39;t Get Along (Yet)</p></div>
<h2>30’000 Feet Overview</h2>
<p>Ok, the basic idea is very simple. We simply bring the actors to the phone. These actors can communication locally or with a remote service. On the phone we can send messages to some actors running on the Server. On the server we can access each phone like an actor and send messages to it.</p>
<p>For example on the phone we simply can send a message to the server like this:</p>
<p>theServiceOnTheServer ! “Hi”</p>
<p>And on the server we can react to this and reply:</p>
<p>onMessage “Hi” =&gt;  reply(“Hi there”)</p>
<p>These messages are then delivered to each mailbox of the actors running on the server and phone. There the actors then can read and reacht to these messages. The framework handles the communication, failures, etc. of the messages. Of course it won’t be a silver bullet and you still need to deal with errors. However with an Actor model dealing with errors is easier than with more traditional models (like exceptions).</p>
<div id="attachment_2019" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/actor-communication.png"><img class="size-medium wp-image-2019" title="actor-communication" src="http://www.gamlor.info/wordpress/wp-content/uploads/2011/10/actor-communication-300x134.png" alt="Actor Communication" width="300" height="134" /></a><p class="wp-caption-text">Actor Communication</p></div>
<p>&nbsp;</p>
<h2>Tons of Details</h2>
<p>While the actor model provides are very simple model, there are tons of details which need to go into the implementation. How do deal with connection losses and offline devices? How are communication errors exposed to the actors? And of course additions for the mobile space should also be considered. For example messages which are only delivered when the device is actively used and other messages which are always delivered.</p>
<h2>Back To Akka</h2>
<p>Now of course I could go with a green field approach and implement everything from scratch, but that would be a lot of wasted effort. Akka already has solid implemented Actors including remote communications. However Akka is intended for servers and clusters. The current remote implementation isn’t really intended to be used on mobile devices. So my goal it the change that and bring parts of the Akka goodness to the mobile space.</p>
<h2>Conclusion &amp; Next Time</h2>
<p>Ok, I want bring Akka actors to the mobile space. The motivation for this is that the actor model is a good fit for complex distributed computing, like in the mobile space.</p>
<p>Anyway, I will go into more details in future posts. Next time I will explain how to get Scala running on Android.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gamlor.info/wordpress/2011/10/akka-mobile-the-idea/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 9.536 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-02-26 04:15:04 -->

