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

<channel>
	<title>A Tasty Pixel » Blog &#187; Development</title>
	<atom:link href="http://atastypixel.com/blog/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://atastypixel.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 24 Jan 2012 10:40:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Breaking the limits: Storing data bigger than 1 Mb in Google App Engine&#8217;s Datastore</title>
		<link>http://atastypixel.com/blog/breaking-the-limits-storing-data-bigger-than-1-mb-in-google-app-engines-datastore/</link>
		<comments>http://atastypixel.com/blog/breaking-the-limits-storing-data-bigger-than-1-mb-in-google-app-engines-datastore/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 18:01:30 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[App Engine]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/breaking-the-limits-storing-data-bigger-than-1-mb-in-google-app-engines-datastore/</guid>
		<description><![CDATA[Google App Engine is a fantastic platform for hosting webapps, and a great resource for iOS developers who need an online component to their products. It&#8217;s hard to believe that the service is essentially free! I&#8217;m using it with The Cartographer, but I found myself coming up against a hard limit with the datastore. You [...]]]></description>
			<content:encoded><![CDATA[<p>Google App Engine is a fantastic platform for hosting webapps, and a great resource for iOS developers who need an online component to their products.  It&#8217;s hard to believe that the service is essentially free!  I&#8217;m using it with <a href="http://cartographer-app.com">The Cartographer</a>, but I found myself coming up against a hard limit with the datastore.</p>

<p>You see, the <a href="http://code.google.com/appengine/docs/python/datastore/overview.html">datastore</a> limits entities to 1 Mb.  I&#8217;m trying to store XML data in there, and sometimes that can exceed the 1 Mb limit.</p>

<p>XML being the verbose creature that it is compresses very nicely, so it occurred to me that if I selectively compress the larger blocks, I should be able to quite easily squeeze in underneath the limit.  Sure enough, a 1.6 Mb XML block compressed into about 200 Kb.</p>

<p>App Engine makes it very easy to define custom properties on data models, so I&#8217;ve written a <code>CompressibleTextProperty</code> class that automatically compresses/decompresses properties above a certain size.  This means that there&#8217;s no performance loss for entities that are small enough to fit easily, but still enables the storage of bigger blocks of content.</p>

<p>The alternative was to break entities up into several different database entities, but this sounded like much more work, and sounded much less elegant.</p>

<p>So here&#8217;s what I came up with &#8212; it&#8217;s used the same way the other <a href="http://code.google.com/appengine/docs/python/datastore/datamodeling.html#Property_Classes_and_Types">Property types</a> are used.<span id="more-2108"></span>Download it here: <a href="http://atastypixel.com/blog/wp-content/uploads/2011/01/compressible_text_property.py_.txt" title="compressible_text_property.py.txt">compressible_text_property.py</a></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #808080; font-style: italic;"># encoding: utf-8</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
compressible_text_property.py
&nbsp;
A string property that will automatically be stored compressed if larger than a given length threshold
&nbsp;
Created by Michael Tyson on 2011-01-07.
Copyright (c) 2011 A Tasty Pixel. All rights reserved.
&nbsp;
BSD LICENSE
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">ext</span> <span style="color: #ff7700;font-weight:bold;">import</span> db
<span style="color: #ff7700;font-weight:bold;">from</span> google.<span style="color: black;">appengine</span>.<span style="color: black;">api</span> <span style="color: #ff7700;font-weight:bold;">import</span> datastore_types
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">zlib</span>
Text = datastore_types.<span style="color: black;">Text</span>
&nbsp;
LENGTH_THRESHOLD            = <span style="color: #ff4500;">500000</span> <span style="color: #808080; font-style: italic;"># Bytes</span>
EXPECTED_ZLIB_HEADER        = u<span style="color: #483d8b;">&quot;x<span style="color: #000099; font-weight: bold;">\x</span>9c&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CompressibleTextProperty<span style="color: black;">&#40;</span>db.<span style="color: black;">TextProperty</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;A string property that will automatically be stored compressed if larger than a given length threshold
&nbsp;
    This is designed to be used with textual properties that may exceed App Engine's 1MB entity size limit.
    Note that, if compressed, property will not be searchable.
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> validate<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
      <span style="color: #483d8b;">&quot;&quot;&quot;Validate text property; Nicked verbatim from TextProperty.
&nbsp;
      Returns:
        A valid value.
&nbsp;
      Raises:
        BadValueError if property is not instance of 'Text'.
      &quot;&quot;&quot;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> value <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>value, Text<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
          value = db.<span style="color: black;">Text</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">TypeError</span>, err:
          <span style="color: #ff7700;font-weight:bold;">raise</span> BadValueError<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Property %s must be convertible '</span>
                              <span style="color: #483d8b;">'to a Text instance (%s)'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span>, err<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
      value = <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>db.<span style="color: black;">TextProperty</span>, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">validate</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> value <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>value, Text<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> BadValueError<span style="color: black;">&#40;</span><span style="color: #483d8b;">'Property %s must be a Text instance'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">return</span> value
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_value_for_datastore<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, model_instance<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;For writing to the datastore: Performs compression if length is greater than the threshold&quot;&quot;&quot;</span>
        value = <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CompressibleTextProperty, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get_value_for_datastore</span><span style="color: black;">&#40;</span>model_instance<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> LENGTH_THRESHOLD <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> value.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span>EXPECTED_ZLIB_HEADER<span style="color: black;">&#41;</span>:
            value = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">zlib</span>.<span style="color: black;">compress</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'ISO-8859-1'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> Text<span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> make_value_from_datastore<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;For reading from the datastore: Decompresses if compressed data detected&quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> value <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> value.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span>EXPECTED_ZLIB_HEADER<span style="color: black;">&#41;</span>:
            value = <span style="color: #dc143c;">zlib</span>.<span style="color: black;">decompress</span><span style="color: black;">&#40;</span>value.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'ISO-8859-1'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> value
&nbsp;
    data_type = Text</pre></div></div>

 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=2108" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/breaking-the-limits-storing-data-bigger-than-1-mb-in-google-app-engines-datastore/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Easy inclusion of OpenSSL into iOS projects</title>
		<link>http://atastypixel.com/blog/easy-inclusion-of-openssl-into-iphone-app-projects/</link>
		<comments>http://atastypixel.com/blog/easy-inclusion-of-openssl-into-iphone-app-projects/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 13:02:54 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SSL]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/easy-inclusion-of-openssl-into-iphone-app-projects/</guid>
		<description><![CDATA[Oddly, iOS doesn&#8217;t provide any OpenSSL implementation at all &#8212; If you want to do anything with crypto (like checking signatures, checksumming, etc.), you have to build in the library yourself. I came across a great XCode project wrapper for OpenSSL yesterday, by Stephen Lombardo. This is an XCode project file that contains a target [...]]]></description>
			<content:encoded><![CDATA[<p>Oddly, iOS doesn&#8217;t provide any OpenSSL implementation at all &#8212; If you want to do anything with crypto (like checking signatures, checksumming, etc.), you have to build in the library yourself.</p>

<p>I came across a great <a href="https://github.com/sjlombardo/openssl-xcode">XCode project wrapper</a> for OpenSSL yesterday, by Stephen Lombardo.  This is an XCode project file that contains a target to build OpenSSL from source, and works with both Mac and iOS projects.  I made some <a href="https://github.com/michaeltyson/openssl-xcode">modifications</a> to it, in order to make it work by just dropping in the OpenSSL source tarball, without having to dirty up your source tree with the extracted OpenSSL distribution.</p>

<p>Here&#8217;s how to use it:</p>

<ol>
<li><a href="http://www.openssl.org/source/">Download the OpenSSL source</a>.</li>
<li>Put the downloaded OpenSSL source tar.gz into the same folder
as openssl.xcodeproj (I put it in <code>Library/openssl</code> within my project tree).</li>
<li>Drag the openssl.xcodeproj file into your main project tree in XCode.</li>
<li>Right-click on your project target, and add openssl.xcodeproj under &#8220;Direct
Dependencies&#8221; on the General tab.</li>
<li><p>On the Build tab for your project&#8217;s target, find the &#8220;Header Search Paths&#8221; 
option, and add the path:</p>

<blockquote>
  <p><code>$(SRCROOT)/Library/openssl/build/openssl.build/openssl/include</code></p>
</blockquote>

<p>(Assuming you&#8217;ve put openssl.xcodeproj at the path <code>Library/openssl</code> &#8212; adjust as necessary).</p></li>
<li>Expand your target&#8217;s &#8220;Link Binary With Libraries&#8221; build stage, and drag
libcrypto.a from the openssl.xcodeproj group.</li>
</ol>

<p>Then, you can just import and use as normal (<code>#import &lt;openssl/dsa.h&gt;</code>, etc).</p>

<p><a href="https://github.com/michaeltyson/openssl-xcode/zipball/master">Download it here</a></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=2100" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/easy-inclusion-of-openssl-into-iphone-app-projects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Avoid the clobber: Nest your network activity indicator updates</title>
		<link>http://atastypixel.com/blog/avoid-the-clobber-nest-your-network-activity-indicator-updates/</link>
		<comments>http://atastypixel.com/blog/avoid-the-clobber-nest-your-network-activity-indicator-updates/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 19:33:20 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/avoid-the-clobber-nest-your-network-activity-indicator-updates/</guid>
		<description><![CDATA[On the iPhone, when you are doing anything that uses the network, you&#8217;re supposed to let the user know something&#8217;s going on, via -[UIApplication setNetworkActivityIndicatorVisible:]. This takes a boolean. That&#8217;s all well and good, but if you have more than one object in your app that may do things with the network simultaneously, you&#8217;re going [...]]]></description>
			<content:encoded><![CDATA[<p>On the iPhone, when you are doing anything that uses the network, you&#8217;re supposed to let the user know something&#8217;s going on, via <code>-[UIApplication setNetworkActivityIndicatorVisible:]</code>.  This takes a boolean.</p>

<p>That&#8217;s all well and good, but if you have more than one object in your app that may do things with the network simultaneously, you&#8217;re going to clobber yourself.</p>

<p>A nice and easy solution: Maintain an activity counter and create a category on UIApplication to maintain it, and show or hide the indicator as appropriate.  Then, whenever you start doing something with the network:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> showNetworkActivityIndicator<span style="color: #002200;">&#93;</span>;</pre></div></div>


<p>&#8230;And when you&#8217;re done:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> hideNetworkActivityIndicator<span style="color: #002200;">&#93;</span>;</pre></div></div>


<p>Here&#8217;s a category that&#8217;ll do it:
<span id="more-2015"></span></p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UIApplication <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showNetworkActivityIndicator;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hideNetworkActivityIndicator;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">static</span> NSInteger __activityCount <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #a61390;">@implementation</span> UIApplication <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>showNetworkActivityIndicator <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> __activityCount <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self setNetworkActivityIndicatorVisible<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    __activityCount<span style="color: #002200;">++</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>hideNetworkActivityIndicator <span style="color: #002200;">&#123;</span>
    __activityCount<span style="color: #002200;">--</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> __activityCount <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self setNetworkActivityIndicatorVisible<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>    
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=2015" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/avoid-the-clobber-nest-your-network-activity-indicator-updates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing audio in time using Remote IO</title>
		<link>http://atastypixel.com/blog/playing-audio-in-time-using-remote-io/</link>
		<comments>http://atastypixel.com/blog/playing-audio-in-time-using-remote-io/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 20:46:38 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Loopy]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/playing-audio-in-time-using-remote-io/</guid>
		<description><![CDATA[I got an email today with a question about how to handle playback of audio in time, synchronised with a clock. My &#8216;musical notepad&#8217; app Loopy does this, and I thought I&#8217;d briefly explain how. Any app that makes use of the Remote IO audio unit framework (which is generally necessary for the kind of [...]]]></description>
			<content:encoded><![CDATA[<p>I got an email today with a question about how to handle playback of audio in time, synchronised with a clock.  My &#8216;musical notepad&#8217; app <a href="http://atastypixel.com/products/loopy">Loopy</a> does this, and I thought I&#8217;d briefly explain how.</p>

<p>Any app that makes use of the <a href="http://atastypixel.com/blog/using-remoteio-audio-unit/">Remote IO</a> audio unit framework (which is generally necessary for the kind of responsiveness required in a realtime musical app) provides audio to the hardware via a callback, which is periodically called when the hardware is ready for more.</p>

<p>The trick here is to provide the right chunk of samples in this callback for the current time position.</p>

<p>Loopy achieves this by:</p>

<h3>1. Keeping track of where in the timeline we are at the time the callback is called</h3>

<p>This is easily accomplished by keeping a record of the time the clock was started, subtracting this from the current time, and possibly performing a modulus with the tempo.  For example:</p>

<ul>
<li><code>(now - startTime) % timePerBar</code> gives the number of time units into the current bar (lets call it <code>timeIntoBar</code>).  </li>
<li><code>timeIntoBar / (timePerBar/beatsPerBar)</code> gives the number of beats into the current bar, and </li>
<li><code>timeIntoBar % (timePerBar/beatsPerBar)</code> gives us the time into the current beat.</li>
</ul>

<h3>2. Determining first if we should be playing audio at this time, and if so, which samples should be playing</h3>

<p>This involves first converting our time units from step 1 into samples.  For instance, you can convert microseconds to samples by dividing your time by <code>1000000/yourSampleRate</code>.  Aside: Of course, you can convert back from samples to time by multiplying instead of dividing.</p>

<p>Next, in the case of Loopy&#8217;s metronome, for example, we test for whether <code>samplesIntoBeat &lt; sound.lengthInSamples</code>.  If so, that means we should be playing audio.  If the sound was a loop, of course, we could be always playing.</p>

<p>The offset into the sound, in samples, is just samplesIntoBeat, in the case of the simple metronome.  In the case of a loop, you probably will be more interested in the number of samples into your loop &#8212; so instead of determining <code>(now - startTime) % timePerBar</code>, you may be interested in <code>(now - startTime) % timePerLoop</code>.</p>

<p>So, we want to return the requested number of samples starting from this offset into the sample array representing our audio.</p>

<h3>3. Returning smooth audio in time</h3>

<p>Note that if you just go returning any old set of samples, willy-nilly, you&#8217;re going to get nasty clicks and pops from discontinuities you get by not matching the start of your next buffer to the last one.</p>

<p>To ensure smoothness, Loopy keeps track of the offset of the last samples we returned, and just return the immediately following bunch of samples &#8212; unless we&#8217;re more than some threshold number of samples out of time, in which case we&#8217;ll suffer the pop in order to stay synchronised.  Actually, you can even generally avoid the pop if you smoothly blend buffers over a short time, removing any discontinuity.</p>

<h3>Final words</h3>

<p>The example above was a relatively trivial one, for a metronome sound.  For longer audio that may span multiple bars, you&#8217;ll probably want to perform a modulus by the length of your audio clip, possibly quantised to your time signature, and possibly using a per-loop time base, so you can start the loop at any point in your timeline and have it begin from the start.  This is something Loopy doesn&#8217;t currently do &#8212; Loopy will keep your loops synchronised so when you start a loop playing, it&#8217;ll play whatever part corresponds to the current timeline, not from the start of the loop. Maybe it&#8217;ll be an option in the future?</p>

<p>I wrote a little about the timing of loops in my <a href="http://atastypixel.com/blog/developing-loopy-part-2-implementation/">second article on Loopy&#8217;s implementation</a>.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1993" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/playing-audio-in-time-using-remote-io/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>iPhone debugging tip: Breaking on exceptions and reading their content</title>
		<link>http://atastypixel.com/blog/iphone-debugging-tip-breaking-on-exceptions-and-reading-their-content/</link>
		<comments>http://atastypixel.com/blog/iphone-debugging-tip-breaking-on-exceptions-and-reading-their-content/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 21:23:12 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/iphone-debugging-tip-breaking-on-exceptions-and-reading-their-content/</guid>
		<description><![CDATA[Just a quick one: This may be obvious to many devs, but it&#8217;s worth noting. One common and useful debugging technique is breaking on exceptions, so that you can see exactly where in your app&#8217;s flow a breakpoint occurs. This can be done by adding -[NSException raise] and objc_exception_throw to your breakpoints list. Once an [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick one: This may be obvious to many devs, but it&#8217;s worth noting.  One common and useful <a href="http://www.cocoadev.com/index.pl?DebuggingTechniques">debugging technique</a> is breaking on exceptions, so that you can see exactly where in your app&#8217;s flow a breakpoint occurs.</p>

<p>This can be done by adding <code>-[NSException raise]</code> and <code>objc_exception_throw</code> to your breakpoints list.</p>

<p>Once an exception happens, you can then check out the exception itself to see what went wrong.  The approach varies between platforms. If you&#8217;re in the simulator (or any Mac OS X app running on Intel), the exception will be stored in the <code>$eax</code> register.  Take a look by typing:</p>

<p><code>po $eax</code></p>

<p>If you&#8217;re on the iPhone, it&#8217;ll be <code>$r0</code>, so:</p>

<p><code>po $r0</code></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1991" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/iphone-debugging-tip-breaking-on-exceptions-and-reading-their-content/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone/Mac animation for custom classes: Property animation for more than just CALayer</title>
		<link>http://atastypixel.com/blog/key-path-based-property-animation/</link>
		<comments>http://atastypixel.com/blog/key-path-based-property-animation/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 20:38:18 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/key-path-based-property-animation/</guid>
		<description><![CDATA[An Objective-C class that provides similar functionality to CABasicAnimation, but works on any object.]]></description>
			<content:encoded><![CDATA[<p>I recently wrote a custom view &#8212; a 3D vintage-looking pull lever &#8212; that provided a continuous property to control the state.  I wanted to animated this smoothly, a-la CABasicAnimation, but couldn&#8217;t find a built-in way to do so.</p>

<p>So, I wrote a class that provides similar functionality to CABasicAnimation, but works on any object.  I thought I&#8217;d share it.</p>

<p>Features:</p>

<ul>
<li>From/to value settings (currently only supports <code>NSNumber</code> and scalar numeric types, but easily extendable)</li>
<li>Duration, delay settings</li>
<li>Timing functions: Linear, ease out, ease in, and ease in/ease out</li>
<li>Animation chaining (specify another configured animation for <code>chainedAnimation</code>, and it&#8217;ll be fired once the first animation completes)</li>
<li>Delegate notification of animation completion</li>
<li>Uses a single timer for smooth lock-step animation</li>
<li>Uses <code>CADisplayLink</code> if available, to update in sync with screen updates</li>
</ul>

<p>Use it like this:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startMyAnimation <span style="color: #002200;">&#123;</span>
  TPPropertyAnimation <span style="color: #002200;">*</span>animation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>TPPropertyAnimation propertyAnimationWithKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;state&quot;</span><span style="color: #002200;">&#93;</span>;
  animation.toValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// fromValue is taken from current value if not specified</span>
  animation.duration <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.0</span>;
  animation.timing <span style="color: #002200;">=</span> TPPropertyAnimationTimingEaseIn;
  <span style="color: #002200;">&#91;</span>animation beginWithTarget<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Make sure you also include the QuartzCore framework, used to access <code>CADisplayLink</code>, if it&#8217;s available.</p>

<p>It&#8217;s BSD-licensed.</p>

<p>Grab it here: 
<a href="http://atastypixel.com/blog/wp-content/uploads/2010/08/TPPropertyAnimation.zip" title="TPPropertyAnimation.zip">TPPropertyAnimation.zip</a></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1973" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/key-path-based-property-animation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Links for May 30th through August 8th</title>
		<link>http://atastypixel.com/blog/links-may-30th-august-8th/</link>
		<comments>http://atastypixel.com/blog/links-may-30th-august-8th/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 22:00:37 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Fonts]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1921</guid>
		<description><![CDATA[Links for May 30th through August 8th: cuf&#243;n &#8211; fonts for the people A very impressive framework that embeds any font into a website, via javascript and the canvas element. Great cross-browser support. mikeash.com: Method Replacement for Fun and Profit Method replacement and method swizzling in Objective-C. Core Data Tutorial: How To Use NSFetchedResultsController &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>Links for May 30th through August 8th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://cufon.shoqolate.com/generate/">cuf&oacute;n &#8211; fonts for the people</a> A very impressive framework that embeds any font into a website, via javascript and the canvas element. Great cross-browser support.</li>
<li><a href="http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html">mikeash.com: Method Replacement for Fun and Profit</a> Method replacement and method swizzling in Objective-C.</li>
<li><a href="http://www.raywenderlich.com/999/core-data-tutorial-how-to-use-nsfetchedresultscontroller">Core Data Tutorial: How To Use NSFetchedResultsController | Ray Wenderlich</a> </li>
<li><a href="http://github.com/akosma/TwitThis">TwitThis &ndash; Use Multiple Twitter Clients on your iPhone Application</a> The class TwitterClientManager loads a list list of supported Twitter clients is loaded from a plist file, which can be extended to support more clients in the future;<br />
Each Twitter client is represented by an instance of the TwitterClient class;<br />
The user can choose his preferred Twitter client at any time, and launch the application by a simple touch; the TwitterClientManager class stores the selected value in the user settings.</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1921" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-may-30th-august-8th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Achieve smaller app downloads by replacing large PNGs with JPEG + mask</title>
		<link>http://atastypixel.com/blog/achieve-smaller-app-downloads-by-replacing-large-pngs-with-jpeg-mask/</link>
		<comments>http://atastypixel.com/blog/achieve-smaller-app-downloads-by-replacing-large-pngs-with-jpeg-mask/#comments</comments>
		<pubDate>Sat, 07 Aug 2010 00:26:17 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Optimisation]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1967</guid>
		<description><![CDATA[I&#8217;m using a transparent overlay on top of a fairly common interface element to make it look awesome. I originally did this with a transparent PNG, until I realised the PNG in question for the iPhone 4&#8242;s Retina display was truly massive, clocking in at 1 Mb. Why we don&#8217;t have common image format with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using a transparent overlay on top of a fairly common interface element to make it look awesome.  I originally did this with a transparent PNG, until I realised the PNG in question for the iPhone 4&#8242;s Retina display was truly massive, clocking in at 1 Mb.</p>

<p>Why we don&#8217;t have common image format with both transparency and lossy compression is beyond me, but there&#8217;s a relatively easy alternative: Using a JPEG and masking it with another JPEG.</p>

<p>Based on Rodney Aiglstorfer&#8217;s solution on <a href="http://iPhoneDeveloperTips.com/cocoa/how-to-mask-an-image.html">how to mask an image</a>, I derived a category on UIImage which would apply a mask to an image.  The method required a little tweaking to work with JPEG images &#8212; the <code>CGImageCreateWithMask</code> function won&#8217;t work correctly on source images that don&#8217;t have an alpha channel, so one has to create one first, from the original.  Jean Regisser figured out the <a href="http://pastie.org/418627">solution</a> which he presents in a comment on the above article, but it needs one more addition: A check on line 37 for <code>kCGImageAlphaNoneSkipLast</code>. <em>Update: Oh, and one more &#8211; kCGImageAlphaNoneSkipFirst</em></p>

<p>So, the complete category for applying a mask to a JPEG image, to achieve the same result as using a PNG but with less download time for your users:</p>

<p><span id="more-1967"></span></p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Header</span>
&nbsp;
<span style="color: #a61390;">@interface</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageByMaskingUsingImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>maskImage;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Implementation</span>
&nbsp;
CGImageRef CopyImageAndAddAlphaChannel<span style="color: #002200;">&#40;</span>CGImageRef sourceImage<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    CGImageRef retVal <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
&nbsp;
    <span style="color: #a61390;">size_t</span> width <span style="color: #002200;">=</span> CGImageGetWidth<span style="color: #002200;">&#40;</span>sourceImage<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">size_t</span> height <span style="color: #002200;">=</span> CGImageGetHeight<span style="color: #002200;">&#40;</span>sourceImage<span style="color: #002200;">&#41;</span>;
&nbsp;
    CGColorSpaceRef colorSpace <span style="color: #002200;">=</span> CGColorSpaceCreateDeviceRGB<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    CGContextRef offscreenContext <span style="color: #002200;">=</span> CGBitmapContextCreate<span style="color: #002200;">&#40;</span><span style="color: #a61390;">NULL</span>, width, height, 
                                                          <span style="color: #2400d9;">8</span>, <span style="color: #2400d9;">0</span>, colorSpace, kCGImageAlphaPremultipliedFirst<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>offscreenContext <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        CGContextDrawImage<span style="color: #002200;">&#40;</span>offscreenContext, CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, width, height<span style="color: #002200;">&#41;</span>, sourceImage<span style="color: #002200;">&#41;</span>;
&nbsp;
        retVal <span style="color: #002200;">=</span> CGBitmapContextCreateImage<span style="color: #002200;">&#40;</span>offscreenContext<span style="color: #002200;">&#41;</span>;
        CGContextRelease<span style="color: #002200;">&#40;</span>offscreenContext<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    CGColorSpaceRelease<span style="color: #002200;">&#40;</span>colorSpace<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> retVal;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageByMaskingUsingImage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIImage <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>maskImage <span style="color: #002200;">&#123;</span>
&nbsp;
    CGImageRef maskRef <span style="color: #002200;">=</span> maskImage.CGImage; 
    CGImageRef mask <span style="color: #002200;">=</span> CGImageMaskCreate<span style="color: #002200;">&#40;</span>CGImageGetWidth<span style="color: #002200;">&#40;</span>maskRef<span style="color: #002200;">&#41;</span>,
                                        CGImageGetHeight<span style="color: #002200;">&#40;</span>maskRef<span style="color: #002200;">&#41;</span>,
                                        CGImageGetBitsPerComponent<span style="color: #002200;">&#40;</span>maskRef<span style="color: #002200;">&#41;</span>,
                                        CGImageGetBitsPerPixel<span style="color: #002200;">&#40;</span>maskRef<span style="color: #002200;">&#41;</span>,
                                        CGImageGetBytesPerRow<span style="color: #002200;">&#40;</span>maskRef<span style="color: #002200;">&#41;</span>,
                                        CGImageGetDataProvider<span style="color: #002200;">&#40;</span>maskRef<span style="color: #002200;">&#41;</span>, <span style="color: #a61390;">NULL</span>, <span style="color: #a61390;">false</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    CGImageRef source <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self CGImage<span style="color: #002200;">&#93;</span>;
&nbsp;
    NSInteger alphaInfo <span style="color: #002200;">=</span> CGImageGetAlphaInfo<span style="color: #002200;">&#40;</span>source<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> alphaInfo <span style="color: #002200;">==</span> kCGImageAlphaNone || alphaInfo <span style="color: #002200;">==</span> kCGImageAlphaNoneSkipLast || alphaInfo <span style="color: #002200;">==</span> kCGImageAlphaNoneSkipFirst <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        source <span style="color: #002200;">=</span> CopyImageAndAddAlphaChannel<span style="color: #002200;">&#40;</span>source<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    CGImageRef masked <span style="color: #002200;">=</span> CGImageCreateWithMask<span style="color: #002200;">&#40;</span>source, mask<span style="color: #002200;">&#41;</span>;
    CGImageRelease<span style="color: #002200;">&#40;</span>mask<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> source <span style="color: #002200;">!=</span> <span style="color: #002200;">&#91;</span>self CGImage<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        CGImageRelease<span style="color: #002200;">&#40;</span>source<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    UIImage <span style="color: #002200;">*</span>result;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>UIImage respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>imageWithCGImage<span style="color: #002200;">:</span>scale<span style="color: #002200;">:</span>orientation<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>masked scale<span style="color: #002200;">:</span>self.scale orientation<span style="color: #002200;">:</span>self.imageOrientation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        result <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithCGImage<span style="color: #002200;">:</span>masked<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    CGImageRelease<span style="color: #002200;">&#40;</span>masked<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> result;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<p>Note that the image mask should be another JPEG (or PNG, if you really like), without transparency, and greyscale, where black represents full opacity, and white represents full transparency.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1967" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/achieve-smaller-app-downloads-by-replacing-large-pngs-with-jpeg-mask/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Reginald RegEx explorer</title>
		<link>http://atastypixel.com/blog/reginald-regex-explorer/</link>
		<comments>http://atastypixel.com/blog/reginald-regex-explorer/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 22:28:27 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Regex]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/reginald-regex-explorer/</guid>
		<description><![CDATA[With a desperate need to debug a lengthy regular expression destined for use with the excellent RegexKitLite library, I have quickly put together a Mac OS X application. Reginald is a kindly old gentleman devoted to assisting you with those tricky regular expressions. Provide some sample input, and your regular expression, and Reginald will provide [...]]]></description>
			<content:encoded><![CDATA[<p>With a desperate need to debug a lengthy regular expression destined for use with the excellent <a href="http://regexkit.sourceforge.net/RegexKitLite/">RegexKitLite</a> library, I have quickly put together a Mac OS X application.</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/07/reg.png" width="64" height="69" alt="Reginald icon" style="float:right;" />
Reginald is a kindly old gentleman devoted to assisting you with those tricky regular expressions.</p>

<p>Provide some sample input, and your regular expression, and Reginald will provide you with colour-coded output and a list of all your matches and the corresponding capture groups for your exploration.  Select a match or capture group in the list to the right, and the corresponding text will be selected in the panel to the left.</p>

<p>Reginald is built on <a href="http://regexkit.sourceforge.net/RegexKitLite/">RegexKitLite</a>, and so uses the <a href="http://regexkit.sourceforge.net/RegexKitLite/index.html#ICUSyntax">ICU syntax</a>.</p>

<p>It will run on Mac OS X 10.6 and above.</p>

<p><a href="http://github.com/michaeltyson/Reginald/downloads">Download Reginald here</a>, or <a href="http://github.com/michaeltyson/Reginald">access the source on GitHub</a>.</p>

<p><a href="http://atastypixel.com/blog/wp-content/uploads/2010/07/201007312318.jpg" rel="lightbox[1966]"><img src="http://atastypixel.com/blog/wp-content/uploads/2010/07/201007312318-tm.jpg" width="450" height="342" alt="Reginald screenshot" class="aligncenter" /></a></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1966" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/reginald-regex-explorer/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Making UIToolbar and UINavigationBar&#8217;s background totally transparent</title>
		<link>http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/</link>
		<comments>http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 10:14:02 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1959</guid>
		<description><![CDATA[Technique to make the background of UIToolbar and UINavigationBar transparent, for custom interfaces]]></description>
			<content:encoded><![CDATA[<p>I have an upcoming iPhone application, <a href="http://atastypixel.com/products/cartographer">Cartographer</a>, that is highly stylised and requires high customisation of the interface to achieve a convincing, beautiful vintage look.  To make it work, I needed transparent toolbars and navigation bars for my UIViewController-based views.</p>

<p>The solution I came up with for this was to implement a category on UINavigationBar and UIToolbar, and overriding <code>drawRect:</code> with a method that does absolutely nothing.  Then I can place my own textures behind the bar, and they&#8217;ll be seen, instead of the default bar background.<span id="more-1959"></span></p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UINavigationBar <span style="color: #002200;">&#40;</span>TransparentAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #a61390;">@end</span>
<span style="color: #a61390;">@implementation</span> UINavigationBar <span style="color: #002200;">&#40;</span>TransparentAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Do nothing!</span>
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>


<p>For UIToolBar, if you&#8217;re using it within a UINavigationController, you&#8217;ll want to also override <code>drawLayer:inContext:</code>, as this appears to be used instead of <code>drawRect:</code> when used within a navigation controller, for some weird reason.</p>

<p>Note that this method will affect <em>all</em> bars in your app. If you only want <em>some</em> bars to be transparent, you&#8217;ll need to do a little objc-hocus-pocus.  Thanks to Mike Ash for this solution on <a href="http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html">method replacement</a> (read that article for the whys and hows).  This technique replaces the default methods as before, but keeps track of the defaults.  If you now set the <code>tintColor</code> of the bar to <code>[UIColor clearColor]</code>, the bar will have a transparent background.  Otherwise, it&#8217;ll just look the same as usual.</p>

<p>For UIToolbar (same principle for UINavigationBar):</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;objc/runtime.h&gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Keep track of default implementation</span>
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span>_origDrawRect<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">SEL</span>, CGRect<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">*</span>_origDrawLayerInContext<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span>, <span style="color: #a61390;">SEL</span>, CALayer<span style="color: #002200;">*</span>, CGContextRef<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Override for drawRect:</span>
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> OverrideDrawRect<span style="color: #002200;">&#40;</span>UIToolbar <span style="color: #002200;">*</span>self, <span style="color: #a61390;">SEL</span> _cmd, CGRect r<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tintColor<span style="color: #002200;">&#93;</span> isEqual<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Do nothing</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Call default method</span>
        _origDrawRect<span style="color: #002200;">&#40;</span>self, _cmd, r<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Override for drawLayer:inContext:</span>
<span style="color: #a61390;">static</span> <span style="color: #a61390;">void</span> OverrideDrawLayerInContext<span style="color: #002200;">&#40;</span>UIToolbar <span style="color: #002200;">*</span>self, <span style="color: #a61390;">SEL</span> _cmd, CALayer <span style="color: #002200;">*</span>layer, CGContextRef context<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self tintColor<span style="color: #002200;">&#93;</span> isEqual<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Do nothing</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Call default method</span>
        _origDrawLayerInContext<span style="color: #002200;">&#40;</span>self, _cmd, layer, context<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> UIToolbar <span style="color: #002200;">&#40;</span>TransparentAdditions<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>load <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Replace methods, keeping originals</span>
    Method origMethod <span style="color: #002200;">=</span> class_getInstanceMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    _origDrawRect <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>method_getImplementation<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>class_addMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawRect, method_getTypeEncoding<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        method_setImplementation<span style="color: #002200;">&#40;</span>origMethod, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawRect<span style="color: #002200;">&#41;</span>;
&nbsp;
    origMethod <span style="color: #002200;">=</span> class_getInstanceMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawLayer<span style="color: #002200;">:</span>inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    _origDrawLayerInContext <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>method_getImplementation<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>class_addMethod<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>drawLayer<span style="color: #002200;">:</span>inContext<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawLayerInContext, method_getTypeEncoding<span style="color: #002200;">&#40;</span>origMethod<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
        method_setImplementation<span style="color: #002200;">&#40;</span>origMethod, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">IMP</span><span style="color: #002200;">&#41;</span>OverrideDrawLayerInContext<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>


<p>You can now add background texture to the bars in a number of ways. These are two I&#8217;ve used:</p>

<ul>
<li>By adding a CALayer to <code>[bar layer]</code> &#8212; but note that UINavigationBar will try to add elements at index 0, underneath your background.  To make this work, I provided a subclassed CALayer (and overrode UINavigationBar&#8217;s <code>+layer</code> method) which only lets <em>you</em> insert layers at index 0, via a custom method, and override <code>insertLayer:atIndex:</code> method, setting index to 1 if it&#8217;s 0.  UIToolbar doesn&#8217;t require this.</li>
<li>Or, by adding a CALayer to your view layer.  Note that the view&#8217;s bounds do not cover the UINavigationBar; I had to offset the layer by the height of the bar in question (<code>navigationBarLayer.frame = CGRectMake(0, -self.navigationController.navigationBar.frame.size.height, [barImage size].width, [barImage size].height);</code>, for example), and set <code>self.view.clipsToBounds = NO</code> to allow the layer to be seen.</li>
</ul>

<p>Of course, you can also draw the texture in <code>drawRect:</code>, instead.  It&#8217;s entirely up to you.  The advantage in using a <code>CALayer</code> is that it can overlap the view boundary, for effects like drop shadows.</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/07/201007191118.jpg" width="320" height="84" alt="201007191118.jpg" class="aligncenter" /></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1959" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Links for February 25th through May 29th</title>
		<link>http://atastypixel.com/blog/links-february-25th-may-29th/</link>
		<comments>http://atastypixel.com/blog/links-february-25th-may-29th/#comments</comments>
		<pubDate>Sat, 29 May 2010 22:01:21 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Marketing]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1849</guid>
		<description><![CDATA[Links for February 25th through May 29th: Implementing iBooks page curling using a conical deformation algorithm Excellent summary of how to implement a convincing page turn animation in OpenGL Multiplottr.com &#8212; Plot, save and share multiple locations on your own customized maps. Batch plot multiple addresses gmaps.kaeding.name :: Plot multiple locations on Google Maps Enter [...]]]></description>
			<content:encoded><![CDATA[<p>Links for February 25th through May 29th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://wdnuon.blogspot.com/2010/05/implementing-ibooks-page-curling-using.html">Implementing iBooks page curling using a conical deformation algorithm</a> Excellent summary of how to implement a convincing page turn animation in OpenGL</li>
<li><a href="http://www.multiplottr.com/">Multiplottr.com &#8212; Plot, save and share multiple locations on your own customized maps.</a> Batch plot multiple addresses</li>
<li><a href="http://gmaps.kaeding.name/">gmaps.kaeding.name :: Plot multiple locations on Google Maps</a> Enter addresses, one per line, to plot all entries on a map at once</li>
<li><a href="http://www.onlinemarketingrant.com/free-iphone-app-marketing">Free iPhone App Marketing &mdash; Online and iPhone Marketing</a> Includes a useful list of review sites, blogs, etc.</li>
</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1849" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-february-25th-may-29th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Making of Talkie: Multi-interface broadcasting and multicast</title>
		<link>http://atastypixel.com/blog/the-making-of-talkie-multi-interface-broadcasting-and-multicast/</link>
		<comments>http://atastypixel.com/blog/the-making-of-talkie-multi-interface-broadcasting-and-multicast/#comments</comments>
		<pubDate>Sun, 02 May 2010 12:52:38 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Broadcast]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Multicast]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Talkie]]></category>
		<category><![CDATA[Talkie-for-Mac]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1901</guid>
		<description><![CDATA[Part 2 Talkie is my newest product, a Walkie Talkie for iPhone and Mac. In Part 1 of this series, I wrote about basic broadcasting. This works fine with one network device, but it&#8217;s worth discussing how to send through all devices, so you can communicate with others connected via, say, Ethernet and WiFi simultaneously. [...]]]></description>
			<content:encoded><![CDATA[<h2>Part 2</h2>

<p><img src="http://atastypixel.com/media/images/products/talkie/icon-medium.jpg" width="183" height="148" alt="Talkie" style="float:right;" /><a href="http://atastypixel.com/products/talkie">Talkie</a> is my newest product, a Walkie Talkie for iPhone and Mac.</p>

<p>In <a href="http://atastypixel.com/blog/2010/03/11/the-making-of-talkie-broadcasting/">Part 1</a> of this series, I wrote about basic broadcasting.  This works fine with one network device, but it&#8217;s worth discussing how to send through all devices, so you can communicate with others connected via, say, Ethernet and WiFi simultaneously.</p>

<p>So, in Part 2 I&#8217;ll write about the approach I took in Talkie for broadcasting from all network devices (a.k.a. network interfaces), so that one can communicate with others connected via WiFi, Ethernet (on a Mac), and any other network devices simultaneously.</p>

<p><span id="more-1901"></span></p>

<h2>Bind them</h2>

<p>From Part 1, we have a <a href="http://atastypixel.com/blog/2010/03/11/the-making-of-talkie-broadcasting/">working broadcast mechanism</a>, but it will only send through the default interface &#8212; whatever you&#8217;re connected to the network via.  This is often sufficient, but if you have more than one device that you communicate through, like Ethernet and WiFi, then you will find that it only works with one.</p>

<p>In order to send through <em>all</em> your connected network interfaces, we need to create one socket for each interface, and <em>bind</em> the socket to its corresponding interface.</p>

<p>Here&#8217;s how:</p>

<p>First, we need to obtain a list of all network interfaces with <code>getifaddrs</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;ifaddrs.h&gt;</span>
...
<span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>addrs;
<span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> getifaddrs<span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addrs<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now, <code>addrs</code> is a list of interfaces that we can iterate over.  We now do so, picking out those devices that support broadcasting, and that aren&#8217;t loopback or point-to-point devices &#8212; loopback is an internal interface that is provided for your computer&#8217;s inner dialogue, and point-to-point (ppp) devices include dialup interfaces, 3G modems and the like.  We can exclude those guys.</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">const</span> <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>cursor <span style="color: #002200;">=</span> addrs;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_BROADCAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// We will do some stuff in here</span>
&nbsp;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now, for each interface that meets our criteria, we create a socket (which we covered in <a href="http://atastypixel.com/blog/2010/03/11/the-making-of-talkie-broadcasting/">Part 1</a>), then <code>bind</code> the socket to the network interface, to force transmission from that particular device.  Finally, as we did in Part 1, we enable broadcasting using <code>setsockopt</code> with <code>SO_BROADCAST</code>.</p>

<p>We want to store the sockets we create in an array, so we can access them later.  If we assume a maximum number of interfaces we will support (lets call it <code>kMaxSockets</code>), we can just use an array of that length.  So, putting it together:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kMaxSockets 16</span>
...
<span style="color: #a61390;">int</span> sock_fds<span style="color: #002200;">&#91;</span>kMaxSockets<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">int</span> number_sockets <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&amp;&amp;</span> number_sockets &lt; kMaxSockets <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_BROADCAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create socket</span>
    sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Error occurred</span>
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Create address from which we want to send, and bind it</span>
    <span style="color: #a61390;">struct</span> sockaddr_in addr;
    <span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    addr.sin_family <span style="color: #002200;">=</span> AF_INET;
    addr.sin_addr <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr;
    addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> bind<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Error occurred</span>
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Enable broadcast</span>
    <span style="color: #a61390;">int</span> flag <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
    result <span style="color: #002200;">=</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, SOL_SOCKET, SO_BROADCAST, <span style="color: #002200;">&amp;</span>flag, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>flag<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Error occurred</span>
      <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    number_sockets<span style="color: #002200;">++</span>;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Finally, as before, we can setup a broadcast address to send to, and use <code>sendto</code> to broadcast, this time for each socket we created:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Initialise broadcast address</span>
<span style="color: #a61390;">struct</span> sockaddr_in addr;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> INADDR_BROADCAST;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Send through each interface</span>
<span style="color: #a61390;">int</span> i;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i&lt;number_sockets; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> sendto<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>, data, length, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Error occurred</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Note that the receive routine only needs a single socket, as we can receive on any interface when we use <code>INADDR_ANY</code>.  So, the receive routine needs no changes from the single-interface version we saw in Part 1.</p>

<p>Here&#8217;s the test app with the above modification: <a href="http://atastypixel.com/blog/wp-content/uploads/2010/05/broadcast_sample_all_interfaces.c" title="broadcast_sample_all_interfaces.c">broadcast_sample_all_interfaces.c</a></p>

<p>Again, compile by opening Terminal, and typing <code>make broadcast_sample_all_interfaces</code> or <code>cc -o broadcast_sample_all_interfaces broadcast_sample_all_interfaces.c</code>, then run it with <code>./broadcast_sample_all_interfaces "Message to send"</code> to send, or just <code>./broadcast_sample_all_interfaces</code> with no arguments to listen.</p>

<p>You may notice that multiple messages may be received: These have probably arrived via multiple network interfaces, virtual or otherwise.  It&#8217;s usually a good idea to check for duplicate messages, if this is an issue for program operation, by including a sequence number into the message &#8212; this will be discussed in Part 3.</p>

<p>It also may be a good idea to ignore your own messages, which may find their way back to you.  One way to accomplish this is to examine the source address (<code>addr</code> in the example above) and compare it with your local interface addresses (stored in <code>addrs</code>, above).  If you get a match, the message came from you, and you can drop it.</p>

<h2>Multicast</h2>

<p>Broadcast is fine when everyone on the local network is interested in what you have to say.  If this isn&#8217;t the case, though (lets face it, those Chuck Norris jokes aren&#8217;t for everyone), effort is wasted delivering to those who aren&#8217;t particularly interested.</p>

<p>Multicast works by using a specific address that one &#8216;subscribes&#8217; to in order to receive messages sent to that address.  So, it&#8217;s opt-in, allowing for better efficiency and one day, Internet-wide support for &#8216;to-many&#8217; communications.</p>

<p>Well, in theory.  Actually, multicast is still quite new, and for the most part &#8212; from what I understand &#8212; it behaves pretty much like broadcast on a local area network.  However, support can only increase, and given that many services already use it &#8212; Multicast DNS (mDNS), also known as Bonjour, being one of the most well-known examples &#8212; it seems a good idea to follow their lead.  Note also that IPv6, the successor to IP as we know it, and our saviour-to-be from our little Internet overpopulation problem (among other things), doesn&#8217;t even have broadcast provisions &#8212; the future is all multicast.</p>

<p>So, for these reasons, Talkie speaks multicast, instead of plain ol&#8217; broadcast.</p>

<p>Making use of multicast is relatively straightforward: To receive, you join a multicast group using <code>setsockopt</code> with <code>IP_ADD_MEMBERSHIP</code>, and the address of the multicast group, which is in the range 224.0.0.0-239.255.255.255 (for IPv4, of course).  To send, you just use <code>sendto</code> to transmit data to a multicast group address.</p>

<p>Using multicast on all network interfaces is just a little more complicated.  Here&#8217;s how it&#8217;s done with Talkie:</p>

<h3>Sending</h3>

<p>The send routine is very similar to the one above, using broadcast.  However, instead of using <code>bind</code> to specify the outgoing network interface and enabling broadcast, we assign a multicast interface using <code>setsockopt</code> with <code>IP_MULTICAST_IF</code>.  And, instead of transmitting to the broadcast address, we transmit to the multicast group address.</p>

<p>Again, we loop through all network interfaces.  This time, we pick out those that support multicast (<code>ifa_flags &amp; IFF_MULTICAST</code>):</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">const</span> <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>cursor <span style="color: #002200;">=</span> addrs;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_MULTICAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// We will do some stuff in here</span>
&nbsp;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>And, after creating the socket, we assign the interface:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, IPPROTO_IP, IP_MULTICAST_IF, <span style="color: #002200;">&amp;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> in_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span>  <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Finally, as a nicety, we can disable loopback so that we don&#8217;t receive our own messages.  This isn&#8217;t 100% reliable, as certain network conditions can result in the local machine still receiving its outgoing messages, but it can improve efficiency:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">u_char loop <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>number_sockets<span style="color: #002200;">&#93;</span>, IPPROTO_IP, IP_MULTICAST_LOOP, <span style="color: #002200;">&amp;</span>loop, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>loop<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now that our sockets are set up, we can prepare to send to the multicast address:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kMulticastAddress &quot;224.0.0.123&quot;</span>
...
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> inet_addr<span style="color: #002200;">&#40;</span>kMulticastAddress<span style="color: #002200;">&#41;</span>;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>And, as before, we send through each of the sockets we created:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> i;
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i&lt;number_sockets; i<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sendto<span style="color: #002200;">&#40;</span>sock_fds<span style="color: #002200;">&#91;</span>i<span style="color: #002200;">&#93;</span>, data, length, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Error occurred</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<h3>Receiving</h3>

<p>Receiving messages from a multicast group on several network interfaces is a little more involved than doing so with broadcast: We need to subscribe to the multicast group from each network interface, explicitly.  If we were to just specify no device in particular, the system would choose a single interface for us, neglecting the others.</p>

<p>Joining the multicast group for each interface takes place in a now-familiar loop:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">const</span> <span style="color: #a61390;">struct</span> ifaddrs <span style="color: #002200;">*</span>cursor <span style="color: #002200;">=</span> addrs;
<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span> cursor <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">-</span>&gt;sa_family <span style="color: #002200;">==</span> AF_INET 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_LOOPBACK<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_POINTOPOINT<span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&amp;&amp;</span>  <span style="color: #002200;">&#40;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_flags <span style="color: #002200;">&amp;</span> IFF_MULTICAST<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// We will do some stuff in here</span>
&nbsp;
  <span style="color: #002200;">&#125;</span>
  cursor <span style="color: #002200;">=</span> cursor<span style="color: #002200;">-</span>&gt;ifa_next;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>For each network device, we use the <code>IP_ADD_MEMBERSHIP</code> property with <code>setsockopt</code> to join &#8212; thereby subscribing to any messages sent to the multicast group address that reach that network interface.</p>

<p>First, we prepare the join request structure.  This provides the multicast group address, and the network interface:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> ip_mreq multicast_req;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>multicast_req, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>multicast_req<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
multicast_req.imr_multiaddr.s_addr <span style="color: #002200;">=</span> inet_addr<span style="color: #002200;">&#40;</span>kMulticastAddress<span style="color: #002200;">&#41;</span>;
multicast_req.imr_interface <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_in <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cursor<span style="color: #002200;">-</span>&gt;ifa_addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span>&gt;sin_addr;</pre></div></div>


<p>Now we use this structure to join the group:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, <span style="color: #002200;">&amp;</span>multicast_req, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>multicast_req<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now, a caveat: While it&#8217;s perfectly legal to join the same multicast group on more than one network interface, and up to 20 memberships may be added to the same socket (see <a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man4/ip.4.html">ip(4)</a>), for some reason, OS X spews &#8216;Address already in use&#8217; errors when we actually attempt it.</p>

<p>As a workaround, we can &#8216;drop&#8217; the membership first, which would normally have no effect, as we have not yet joined on this interface.  However, it enables us to perform the subsequent join, without dropping prior memberships.</p>

<p>So, before we perform the above <code>IP_ADD_MEMBERSHIP</code>, we do a <code>IP_DROP_MEMBERSHIP</code> first:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">setsockopt<span style="color: #002200;">&#40;</span>sock_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, <span style="color: #002200;">&amp;</span>multicast_req, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>multicast_req<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>This sets up our socket to receive messages sent to the multicast group that are received via any interface.</p>

<p>Here it is all put together: <a href="http://atastypixel.com/blog/wp-content/uploads/2010/05/multicast_sample.c" title="multicast_sample.c">multicast_sample.c</a></p>

<p>Compile by opening Terminal, and typing <code>make multicast_sample</code> or <code>cc -o multicast_sample multicast_sample .c</code>, then run it with <code>./multicast_sample "Message to send"</code> to send, or just <code>./multicast_sample</code> with no arguments to listen.</p>

<h2>Still to come</h2>

<p>So, now we mostly have networking covered.  There&#8217;s one obvious omission, though, for an iPhone app: Bluetooth.  In Part 3, I&#8217;ll discuss how to perform communications over Bluetooth on the iPhone, in a way that&#8217;s compatible with the above generic network communications.  I&#8217;ll also talk about how to connect to other devices automatically, without user intervention &#8212; This is one particularly popular feature of Talkie that allows it to behave more like a real walkie-talkie.</p>

<p>I promised in Part 1 that I&#8217;d talk about packet formats.   We&#8217;ve covered a lot of ground in Part 2, however, so it shall be postponed to Part 3 &#8212; I&#8217;ll discuss how to ensure you get messages in the correct order by using sequence numbers, as well as providing for versioning and a few other bits and pieces.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1901" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/the-making-of-talkie-multi-interface-broadcasting-and-multicast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Browsing Core Data databases using F-Script</title>
		<link>http://atastypixel.com/blog/browsing-core-data-databases-using-f-script/</link>
		<comments>http://atastypixel.com/blog/browsing-core-data-databases-using-f-script/#comments</comments>
		<pubDate>Sat, 10 Apr 2010 12:00:37 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[F-Script]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1887</guid>
		<description><![CDATA[F-Script's new Core Data browser lets you open up and query any Core Data database, an invaluable debugging tool. Actually opening databases is a lengthy process through.  No longer: This Applescript app provides an easy interface to quickly open a Core Data database in F-Script's object browser.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.fscript.org/">F-Script</a>, the Cocoa-based scripting environment, now provides some great tools for exploring Core Data databases.</p>

<p>I couldn&#8217;t figure out how to easily open up my databases, other than manually creating a managed object model, then a persistent store coordinator, then a managed object context on the console.  I couldn&#8217;t find any existing tools, and I wanted a quick workflow for opening up my databases, so I put together a script that prompts for the application bundle or <code>.xcdatamodel(d)</code> data model file, then prompts for the XML (<code>.xml</code>), binary (<code>.binary</code>) or SQLite (<code>.sql</code> or anything else) database file, and opens up the inspector.</p>

<p>I wrote it as an Applescript that just calls upon F-Script to evaluate the script, and saved it in an application bundle so I can pull it up quickly.</p>

<p>Here it is:</p>

<p><a href="http://atastypixel.com/blog/wp-content/uploads/2011/11/Core-Data-Browser.zip" title="Core Data Browser.zip" alt="Core Data Browser">Core Data Browser.app</a></p>

<p><a href="http://atastypixel.com/blog/wp-content/uploads/2010/04/201004101417.jpg" rel="lightbox[1887]"><img src="http://atastypixel.com/blog/wp-content/uploads/2010/04/201004101417-tm.jpg" width="400" height="194" alt="201004101417.jpg" class="aligncenter" /></a></p>

<p>It just needs the F-Script app to be available.</p>

<p>Upon opening, the managed object context is available on the console as &#8220;<code>context</code>&#8220;. So, aside from using F-Script&#8217;s object browser, you can also do things like:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">&gt; request <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSFetchRequest</span> alloc<span style="color: #002200;">&#41;</span> init
&gt; request setEntity<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEntityDescription</span> entityForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'MyEntity'</span> inManagedObjectContext<span style="color: #002200;">:</span>context<span style="color: #002200;">&#41;</span>
&gt; request setPredicate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPredicate</span> predicateWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'type = 3'</span><span style="color: #002200;">&#41;</span>
&gt; result <span style="color: #002200;">:=</span> context executeFetchRequest<span style="color: #002200;">:</span>request error<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
&gt; result
_PFArray <span style="color: #002200;">&#123;</span>&lt;NSManagedObject<span style="color: #002200;">:</span> 0x2006cf740&gt; <span style="color: #002200;">&#40;</span>entity<span style="color: #002200;">:</span> MyEntity; <span style="color: #a61390;">id</span><span style="color: #002200;">:</span> 0x20064c9e0 &lt;x<span style="color: #002200;">-</span>coredata<span style="color: #002200;">:</span><span style="color: #11740a; font-style: italic;">//BAC82A67-CC41-48C2-8A96-693B67A501D6/MyEntity/p1&gt; ; data: &lt;fault&gt;), </span>
&lt;NSManagedObject<span style="color: #002200;">:</span> 0x2006bdc80&gt; <span style="color: #002200;">&#40;</span>entity<span style="color: #002200;">:</span> MyEntity; <span style="color: #a61390;">id</span><span style="color: #002200;">:</span> 0x20064c9c0 &lt;x<span style="color: #002200;">-</span>coredata<span style="color: #002200;">:</span><span style="color: #11740a; font-style: italic;">//BAC82A67-CC41-48C2-8A96-693B67A501D6/MyEntity/p2&gt; ; data: &lt;fault&gt;), </span>
&lt;NSManagedObject<span style="color: #002200;">:</span> 0x2006bc680&gt; <span style="color: #002200;">&#40;</span>entity<span style="color: #002200;">:</span> MyEntity; <span style="color: #a61390;">id</span><span style="color: #002200;">:</span> 0x200651180 &lt;x<span style="color: #002200;">-</span>coredata<span style="color: #002200;">:</span><span style="color: #11740a; font-style: italic;">//BAC82A67-CC41-48C2-8A96-693B67A501D6/MyEntity/p3&gt; ; data: &lt;fault&gt;)</span>
...</pre></div></div>


<p><strong>Update</strong>: Now has better error reporting, and the option to load classes from a bundle.</p>

<p>For those interested, here&#8217;s the original F-Script:<span id="more-1887"></span></p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">panel <span style="color: #002200;">:=</span> <span style="color: #400080;">NSOpenPanel</span> openPanel.
panel setAllowedFileTypes<span style="color: #002200;">:</span><span style="color: #002200;">&#123;</span><span style="color: #bf1d1a;">'xcdatamodel'</span>, <span style="color: #bf1d1a;">'app'</span>, <span style="color: #bf1d1a;">'framework'</span><span style="color: #002200;">&#125;</span>;setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Data model'</span>;setMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Select a data model file or bundle containing a data model'</span>.
classesButton <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSButton</span> alloc<span style="color: #002200;">&#41;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>&lt;&gt;<span style="color: #2400d9;">0</span> extent<span style="color: #002200;">:</span><span style="color: #2400d9;">200</span>&lt;&gt;<span style="color: #2400d9;">30</span><span style="color: #002200;">&#41;</span>.
classesButton setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Load classes in bundle'</span>;setButtonType<span style="color: #002200;">:</span>NSSwitchButton.
panel setAccessoryView<span style="color: #002200;">:</span>classesButton.
model <span style="color: #002200;">:=</span> <span style="color: #a61390;">nil</span>.
<span style="color: #002200;">&#91;</span>model <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> whileTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
	error <span style="color: #002200;">:=</span> FSPointer objectPointer.
	<span style="color: #002200;">&#40;</span>panel runModal <span style="color: #002200;">=</span> NSFileHandlingPanelCancelButton<span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSException</span> exceptionWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'CancelException'</span> reason<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Cancelled'</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">raise</span> <span style="color: #002200;">&#93;</span> .
	<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>panel URL<span style="color: #002200;">&#41;</span> absoluteString<span style="color: #002200;">&#41;</span> hasSuffix<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'xcdatamodel'</span><span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
		model <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectModel</span> alloc<span style="color: #002200;">&#41;</span> initWithContentsOfURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>panel URL<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#93;</span> ifFalse<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
		bundle <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSBundle</span> bundleWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>panel URL<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>.
		<span style="color: #002200;">&#40;</span>bundle <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> ifFalse<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
			model <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectModel</span> mergedModelFromBundles<span style="color: #002200;">:</span><span style="color: #002200;">&#123;</span>bundle<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#41;</span>.
			<span style="color: #002200;">&#40;</span>model ~~ <span style="color: #a61390;">nil</span> <span style="color: #002200;">&amp;</span> <span style="color: #002200;">&#40;</span>FSBoolean booleanWithBool<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>classesButton state<span style="color: #002200;">&#41;</span> <span style="color: #002200;">=</span> NSOnState<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
				<span style="color: #002200;">&#40;</span>FSBoolean booleanWithBool<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>bundle loadAndReturnError<span style="color: #002200;">:</span>error<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> ifFalse<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
					model <span style="color: #002200;">:=</span> <span style="color: #a61390;">nil</span>.
					<span style="color: #002200;">&#40;</span><span style="color: #400080;">NSAlert</span> alertWithMessageText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Could not load bundle'</span> defaultButton<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'OK'</span> alternateButton<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> otherButton<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> informativeTextWithFormat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>error at<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> localizedDescription<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> runModal
				<span style="color: #002200;">&#93;</span>
			<span style="color: #002200;">&#93;</span>
		<span style="color: #002200;">&#93;</span>
	<span style="color: #002200;">&#93;</span>.
	<span style="color: #002200;">&#40;</span>model <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&amp;</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>error at<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSAlert</span> alertWithMessageText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Could not load model from file or bundle'</span> defaultButton<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'OK'</span> alternateButton<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> otherButton<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> informativeTextWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">''</span><span style="color: #002200;">&#41;</span> runModal <span style="color: #002200;">&#93;</span>
<span style="color: #002200;">&#93;</span>.
panel setAccessoryView<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>.
store <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSPersistentStoreCoordinator</span> alloc<span style="color: #002200;">&#41;</span> initWithManagedObjectModel<span style="color: #002200;">:</span>model<span style="color: #002200;">&#41;</span>.
panel setAllowedFileTypes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>; setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Database file'</span>;setMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Select Core Data SQLite database'</span>.
opened <span style="color: #002200;">:=</span> <span style="color: #a61390;">nil</span>.
<span style="color: #002200;">&#91;</span>opened <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> whileTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>
	<span style="color: #002200;">&#40;</span>panel runModal <span style="color: #002200;">=</span> NSFileHandlingPanelCancelButton<span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSException</span> exceptionWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'CancelException'</span> reason<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Cancelled'</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">raise</span> <span style="color: #002200;">&#93;</span> .
	type <span style="color: #002200;">:=</span> NSSQLiteStoreType.
	<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>panel URL<span style="color: #002200;">&#41;</span> absoluteString<span style="color: #002200;">&#41;</span> lowercaseString<span style="color: #002200;">&#41;</span> hasSuffix<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'xml'</span><span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>type <span style="color: #002200;">:=</span> NSXMLStoreType<span style="color: #002200;">&#93;</span>.
	<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>panel URL<span style="color: #002200;">&#41;</span> absoluteString<span style="color: #002200;">&#41;</span> lowercaseString<span style="color: #002200;">&#41;</span> hasSuffix<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'binary'</span><span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>type <span style="color: #002200;">:=</span> NSBinaryStoreType<span style="color: #002200;">&#93;</span>.
	error <span style="color: #002200;">:=</span> FSPointer objectPointer.
	opened <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span>store addPersistentStoreWithType<span style="color: #002200;">:</span>type configuration<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> URL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>panel URL<span style="color: #002200;">&#41;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> error<span style="color: #002200;">:</span>error<span style="color: #002200;">&#41;</span>.
	<span style="color: #002200;">&#40;</span>opened <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> ifTrue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSAlert</span> alertWithMessageText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'Could not load database'</span> defaultButton<span style="color: #002200;">:</span><span style="color: #bf1d1a;">'OK'</span> alternateButton<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> otherButton<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> informativeTextWithFormat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>error at<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> localizedDescription<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> runModal <span style="color: #002200;">&#93;</span>
<span style="color: #002200;">&#93;</span>.
context <span style="color: #002200;">:=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSManagedObjectContext</span> alloc<span style="color: #002200;">&#41;</span> init<span style="color: #002200;">&#41;</span>.
context setPersistentStoreCoordinator<span style="color: #002200;">:</span>store.
context inspectWithSystem<span style="color: #002200;">:</span>sys</pre></div></div>

 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1887" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/browsing-core-data-databases-using-f-script/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Easy rounded corners on UITableViewCell image view</title>
		<link>http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/</link>
		<comments>http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 19:47:07 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2010/03/15/easy-rounded-corners-on-uitableviewcell-image-view/</guid>
		<description><![CDATA[Here&#8217;s a relatively easy way to achieve rounded corners on the standard image view in a UITableViewCell: cell.imageView.layer.masksToBounds = YES; cell.imageView.layer.cornerRadius = 5.0; Set this up when you create the cell (make sure you #import &#60;QuartzCore/QuartzCore.h&#62; at the top, of course). It would appear the UIImageView control creates sublayers to display the actual image content, [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a relatively easy way to achieve rounded corners on the standard image view in a UITableViewCell:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">cell.imageView.layer.masksToBounds <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
cell.imageView.layer.cornerRadius <span style="color: #002200;">=</span> <span style="color: #2400d9;">5.0</span>;</pre></div></div>


<p>Set this up when you create the cell (make sure you <code>#import &lt;QuartzCore/QuartzCore.h&gt;</code> at the top, of course).  It would appear the UIImageView control creates  sublayers to display the actual image content, which is why we use the <code>masksToBounds</code> property to then clip any sublayers.</p>

<p>I noticed a lot of people are seeking answers to the silly behaviour of UITableView with the grouped (<code>UITableViewStyleGrouped</code>) style and images:</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/03/201003152034.jpg" width="133" height="100" alt="Images not clipped to rounded cell border" class="aligncenter" /></p>

<p>Images don&#8217;t get clipped to the rounded cell border, which looks nasty.  This technique is one way to remedy that:</p>

<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/03/201003152036.jpg" width="130" height="73" alt="Rounded borders now stay within rounded cell edge" class="aligncenter" /></p>

<p>One caveat &#8211; due to the inexplicable way the image view within the table view cell scales image content, there&#8217;s not really a simple, sensible way to provide an inset margin from the table view cell boundary to complement this rounded border effect.</p>

<p>I tried setting the <code>frame</code> property of the UIImageView itself (<code>cell.imageView.frame</code>), as well as setting the frame of the image view&#8217;s layer.  I also tried applying a scale transform to the layer, with strangely inconsistent results: Setting scale to, say, 50%, made the image view 40x40px, only a pixel or two smaller than the full size.  This may be because another entity (the table view cell?) performs scaling of the content, instead of the actual image view; given that my original image was 80&#215;80, a 50% scale would result in 40&#215;40.</p>

<p>My solution was to steer clear of that nonsense and just provide appropriately scaled images straight to the image view.  Here&#8217;s a simple category on UIImage to scale an image:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageScaledToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UIImage <span style="color: #002200;">&#40;</span>TPAdditions<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UIImage<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>imageScaledToSize<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGSize<span style="color: #002200;">&#41;</span>size <span style="color: #002200;">&#123;</span>
    UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span>size<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>self drawInRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> image;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>


<p>So, then I just do something like this in the UITableView data provider:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> account.image;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> image <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   cell.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>image imageScaledToSize<span style="color: #002200;">:</span>CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">38</span>, <span style="color: #2400d9;">38</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>


<p><img src="http://atastypixel.com/blog/wp-content/uploads/2010/03/201003152043.jpg" width="121" height="57" alt="Sorted" class="aligncenter" /></p>

<p>Sorted.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1876" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/easy-rounded-corners-on-uitableviewcell-image-view/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>The Making of Talkie: Broadcasting</title>
		<link>http://atastypixel.com/blog/the-making-of-talkie-broadcasting/</link>
		<comments>http://atastypixel.com/blog/the-making-of-talkie-broadcasting/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 00:31:59 +0000</pubDate>
		<dc:creator>Michael Tyson</dc:creator>
				<category><![CDATA[Geekspeak]]></category>
		<category><![CDATA[Broadcast]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Talkie]]></category>
		<category><![CDATA[Talkie-for-Mac]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1854</guid>
		<description><![CDATA[Part 1 Talkie is my newest product, the result of a collaboration with a good designer friend, Tim Churchward, who did the user interface. Talkie is a little different from many of the other walkie talkie applications on the App Store (aside from the fact that much of it was written by me from our [...]]]></description>
			<content:encoded><![CDATA[<h2>Part 1</h2>

<p><img src="http://atastypixel.com/media/images/products/talkie/icon-medium.jpg" width="183" height="148" alt="Talkie" style="float:right;" /><a href="http://atastypixel.com/products/talkie">Talkie</a> is my newest product, the result of a collaboration with a good designer friend, Tim Churchward, who did the user interface.</p>

<p>Talkie is a little different from many of the other walkie talkie applications on the App Store (aside from the fact that much of it was written by me from our motorhome in <a href="http://michael.tyson.id.au/2010/01/26/down-time-in-hammamet/">Tunisia</a>!), and I thought I&#8217;d write a little about some of the tech underpinning the app, and some of the choices we made.   Along the way it may get a little tutorial-esque.</p>

<ul>
<li>This first part will introduce our initial motivations, and will talk about basic broadcast communications &#8212; the broadcast communications part may be very familiar to some, in which case it may be worth skipping to the next instalment.</li>
<li>In the <a href="http://atastypixel.com/blog/the-making-of-talkie-broadcasting/">second part</a>, I&#8217;ll continue the theme of networking, and will talk about what I ended up with for Talkie&#8217;s network code after addressing a couple of things, including switching to multicast.  </li>
<li>Finally, I&#8217;ll talk audio, dual platform development, and anything else I think of along the way (Actually, I&#8217;m aching to talk about one particular upcoming feature that had me jumping up and down when I first thought of it, but for now, mum&#8217;s the word on that one.)<span id="more-1854"></span>## Inspiration</li>
</ul>

<p><img src="http://atastypixel.com/media/images/products/talkie/screen-iphone.jpg" width="187" height="400" alt="Talkie screenshot" style="float:right;" />Right from the start, we wanted a product that brought back the fun walkie talkie experience we remember from our childhoods.  I&#8217;m talking colourful plastic, whip antennas and hiding in tree-houses.   This was mostly Tim&#8217;s domain, so I shall leave it to him to discuss how he found that in the user interface.</p>

<p>It also meant stepping back from traditional voice chat, with manual call initialisation and termination and simple one-to-one calls.  We wanted to mimic a radio, so that meant broadcasting as soon as you hit &#8220;Talk&#8221; &#8212; whereupon anyone in the neighbourhood would hear you.</p>

<p>Basically, we wanted to get as close to the real thing as was practical.  This included the addition of a prominent &#8216;morse code&#8217; button, of course, as well as a &#8216;squelch&#8217; control for &#8216;fine tuning&#8217;, which simulated the static of bad reception.</p>

<h2>Going dual-platform</h2>

<p><img src="http://atastypixel.com/media/images/products/talkie-for-mac/screenshot.jpg" width="232" height="190" alt="Talkie for Mac" style="float:left;" />Soon after I started developing Talkie, I realised I wanted a version for the Mac too.</p>

<p>Having Talkie both on the iPhone and on the Mac made sense, as we envisioned that a fairly common usage pattern would involve communication between a desktop and a handheld &#8212; say, someone wandering a campus with the iPhone in their pocket, staying in touch with friends at their desks.</p>

<p>We wanted to offer good value with Talkie, which is why we made Talkie for Mac available for free, when it&#8217;s used with Talkie for iPhone.</p>

<p>One of the very convenient things about developing for both iPhone and Mac is that the platforms are so similar, porting code is usually effortless.  So, going dual-platform was an easy decision.</p>

<h2>Finding common ground</h2>

<p>The result of all of this was the need to develop or find a communication protocol and codec that would work across the iPhone and the Mac, over both Bluetooth for iPhone-iPhone communication, and Wifi, for communication between iPhones and iPhones, and iPhones and Macs.</p>

<p>Version 3.0 of the iPhone SDK introduced the GameKit framework and as part of it, <code>GKVoiceChatService</code>, which provides two person voice chat straight out of the box.  Immediately it was clear that it wouldn&#8217;t serve our purposes &#8212; two person is not broadcast.  I was also keen to provide a Mac version of Talkie, and given that GameKit is iPhone-only, it was time to go off the beaten track.</p>

<p>There are a variety of voice communication protocols out there, with varying licences and varying complexity and feature sets.  I could&#8217;ve spent a week or two researching the options and evaluating them against our need for broadcast functionality, figuring out compilation and linking on the iPhone, resolving any compatibility or performance issues that arose, etc.</p>

<p>Or, I could spend a day putting together a few big building blocks provided by Apple (and the underlying Unix system) and have an easily tweakable working solution that precisely meets our needs and provides for future features.</p>

<p>It may not have been the most scientific approach, but the core of Talkie&#8217;s functionality was up and running on our home wi-fi network within about four hours, and it was working well.</p>

<h2>Getting from a to b (and c, d, e, and f)</h2>

<p>For those unfamiliar with the ins and outs of TCP/IP (the most common communication protocol &#8212; or rather, set of protocols &#8212; between computers, and the fundamental building block of the Internet), communication between computers can be connection-oriented, or connectionless.</p>

<p>Connection-oriented (a.k.a. &#8216;reliable&#8217;) communications are most common: They&#8217;re used when you open up a website, connect to iChat, or check your email.  This is the TCP part of TCP/IP, and includes niceties like built-in guaranteed delivery via retransmission (providing your cat hasn&#8217;t eaten your network cable, of course) and packet ordering so you receive the messages in the right order.  This only works between two computers, though &#8211; you and the server.</p>

<p>Connectionless (or &#8216;unreliable&#8217;) communications are much more open &#8212; it&#8217;s basically just a spray of messages: The data isn&#8217;t carefully ushered along, it&#8217;s just spat out and left to fend for itself.  This is UDP, and is commonly used for time-sensitive applications like audio communication, network gaming, etc., where once a packet arrives late, it&#8217;s useless: No point mucking about re-sending lost data that&#8217;s going to be past its use-by date by the time it arrives.  The other thing about UDP is that, because there&#8217;s no co-operation required from the destination (to acknowledge receipt, etc.), it lends itself to one-to-many communications &#8212; broadcast.</p>

<p>Just what we&#8217;re after.</p>

<p>So, we use connectionless communications (UDP) to send messages containing the audio, and anyone who receives them unpackages and plays the audio within.</p>

<p>The basic mechanics of this are fairly simple, once you get past the arguably cryptic C syntax.</p>

<p>Here&#8217;s how it works.</p>

<h3>Sending</h3>

<p>On the transmission end, on startup, you create a socket using the <code>socket</code> call:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #6e371a;">#include &lt;netinet/in.h&gt;</span>
<span style="color: #6e371a;">#include &lt;arpa/inet.h&gt;</span>
...
<span style="color: #a61390;">int</span> sock_fd <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sock_fd &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>The socket, identified by <code>sock_fd</code>, will be created using the IPv4 domain (<code>AF_INET</code>), a &#8216;datagram&#8217; type (<code>SOCK_DFRAM</code>: that&#8217;s connectionless communication &#8212; if we wanted connection-oriented, we could put in <code>SOCK_STREAM</code> here instead), using protocol 0, which is IP.</p>

<p>Then, we enable broadcasting:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> flag <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
<span style="color: #a61390;">int</span> result <span style="color: #002200;">=</span> setsockopt<span style="color: #002200;">&#40;</span>sock_fd, SOL_SOCKET, SO_BROADCAST, <span style="color: #002200;">&amp;</span>flag, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>flag<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>That is, we set the <code>SO_BROADCAST</code> option at the <code>SOL_SOCKET</code> level to 1 (via the <code>flag</code> variable, which we pass in as a pointer), thereby requesting permission from the operating system kernel to broadcast.</p>

<p>Now we have the carrier pigeon coop at our disposal, we can start dispatching pigeons.  First, we fill out the address:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kPortNumber 1234</span>
...
<span style="color: #a61390;">struct</span> sockaddr_in addr;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> INADDR_BROADCAST;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;</pre></div></div>


<p>(Note, <code>htons</code> converts <code>kPortNumber</code> into a format that can be understood by any computer, regardless of the way it represents numbers internally.)</p>

<p>Now, send:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>dataToSend;
result <span style="color: #002200;">=</span> sendto<span style="color: #002200;">&#40;</span>sock_fd, <span style="color: #002200;">&#91;</span>data bytes<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>data length<span style="color: #002200;">&#93;</span>, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<h3>Receiving</h3>

<p>That takes care of outgoing messages.  To receive them, we want another socket:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">int</span> sock_fd <span style="color: #002200;">=</span> socket<span style="color: #002200;">&#40;</span>AF_INET, SOCK_DGRAM, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> sock_fd &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Now we specify where we want to receive messages from, by &#8216;binding&#8217; the socket:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">struct</span> sockaddr_in addr;
<span style="color: #a61390;">memset</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #2400d9;">0</span>, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
addr.sin_family <span style="color: #002200;">=</span> AF_INET;
addr.sin_addr.s_addr <span style="color: #002200;">=</span> INADDR_ANY;
addr.sin_port <span style="color: #002200;">=</span> htons<span style="color: #002200;">&#40;</span>kPortNumber<span style="color: #002200;">&#41;</span>;
result <span style="color: #002200;">=</span> bind<span style="color: #002200;">&#40;</span>sock_fd, <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> result &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>And we pass a buffer to <code>recvfrom</code> to fill up with tasty morsels of data:</p>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kBufferSize 1024</span>
...
<span style="color: #a61390;">char</span> buffer<span style="color: #002200;">&#91;</span>kBufferSize<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">int</span> addr_len <span style="color: #002200;">=</span> <span style="color: #a61390;">sizeof</span><span style="color: #002200;">&#40;</span>addr<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">int</span> bytes_received <span style="color: #002200;">=</span> recvfrom<span style="color: #002200;">&#40;</span>sock_fd, buffer, kBufferSize, <span style="color: #2400d9;">0</span>, <span style="color: #002200;">&#40;</span>struck sockaddr<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&amp;</span>addr, <span style="color: #002200;">&amp;</span>addr_len<span style="color: #002200;">&#41;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> bytes_received &lt; <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
   <span style="color: #11740a; font-style: italic;">// Error occurred</span>
<span style="color: #002200;">&#125;</span></pre></div></div>


<p>Voila, we have <code>bytes_received</code> bytes sitting in <code>buffer</code> for us to do something with.  Note also that <code>addr</code> now also contains the address of the sender.  Now we can loop and continue to receive data as it comes in, passing the data off to some other part of the application to deal with.</p>

<p>We can put it all together into a simple test app: <a href="http://atastypixel.com/blog/wp-content/uploads/2010/03/broadcast_sample.c" title="broadcast_sample.c">broadcast_sample.c</a></p>

<p>Compile it by opening up Terminal, and typing <code>make broadcast_sample</code> (or <code>cc -o broadcast_sample broadcast_sample.c</code>, if you like), then run it with <code>./broadcast_sample "Message to send"</code>, or just <code>./broadcast_sample</code> to listen.</p>

<p><pre>
$ ./broadcast_sample 
Listening...
Hello world!
</pre></p>

<p><pre>
$ ./broadcast_sample 'Hello world!'
"Hello world!" transmitted.
</pre></p>

<h2>Coming next</h2>

<p>This will work happily between computers with just one network interface. But, if you have more than one (say, wireless, and an Ethernet connection too), you&#8217;ll notice that it will only communicate through one of the interfaces.  That&#8217;s because just the default interface is used.  You have to explicitly attend to each interface, to broadcast out of each one, and listen on each one.</p>

<p>In the <a href="http://atastypixel.com/blog/the-making-of-talkie-broadcasting/">next part of this series</a>, I&#8217;ll write about how I addressed that, and about multicast, which is used in things like Bonjour (MDNS).  I&#8217;ll also write about designing packet formats.</p>

<p>Thanks for reading!</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1854" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/the-making-of-talkie-broadcasting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

