<?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>Mon, 19 Jul 2010 10:22:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<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</dc:creator>
				<category><![CDATA[Uncategorized]]></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>1</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</dc:creator>
				<category><![CDATA[Uncategorized]]></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[maps]]></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</dc:creator>
				<category><![CDATA[Uncategorized]]></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</dc:creator>
				<category><![CDATA[Uncategorized]]></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</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/2010/04/Core-Data-Browser2.zip" title="Core Data Browser.zip">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>0</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</dc:creator>
				<category><![CDATA[Uncategorized]]></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>6</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</dc:creator>
				<category><![CDATA[Uncategorized]]></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>
		<item>
		<title>Links for October 25th through January 23rd</title>
		<link>http://atastypixel.com/blog/links-october-25th-january-23rd/</link>
		<comments>http://atastypixel.com/blog/links-october-25th-january-23rd/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 17:01:07 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[mapping]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1684</guid>
		<description><![CDATA[Links for October 25th through January 23rd: GPS Visualizer: Draw a map from a GPS data file Batch map plotter using GPS co-ordinates Map multiple locations by address Very well-implemented free batch geocoder; accepts only address data, no GPS co-ordinates, but still excellent for some applications iPhone: Custom font loading Implementing Your Own Cocoa Bindings [...]]]></description>
			<content:encoded><![CDATA[<p>Links for October 25th through January 23rd:</p>

<ul class="delicious-bookmarks">
<li><a href="http://www.gpsvisualizer.com/map_input">GPS Visualizer: Draw a map from a GPS data file</a> Batch map plotter using GPS co-ordinates</li>
<li><a href="http://www.batchgeocode.com/">Map multiple locations by address</a> Very well-implemented free batch geocoder; accepts only address data, no GPS co-ordinates, but still excellent for some applications</li>
<li><a href="http://uihacker.blogspot.com/2009/01/iphone-custom-font-loading-complete.html">iPhone: Custom font loading</a> </li>
<li><a href="http://www.tomdalling.com/cocoa/implementing-your-own-cocoa-bindings">Implementing Your Own Cocoa Bindings &laquo;  Tom Dalling</a> </li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1684" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-october-25th-january-23rd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit testing and coverage with XCode</title>
		<link>http://atastypixel.com/blog/unit-testing-and-coverage-with-xcode/</link>
		<comments>http://atastypixel.com/blog/unit-testing-and-coverage-with-xcode/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 17:52:54 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Workflow]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/11/29/unit-testing-and-coverage-with-xcode/</guid>
		<description><![CDATA[There are several great resources out there on how to incorporate unit testing into XCode projects. It&#8217;s all built into XCode now, and it&#8217;s fantastic. I just got coverage working too, thanks to a useful article at SuperMegaUltraGroovy on how to use code coverage with XCode. There were a couple of caveats that I thought [...]]]></description>
			<content:encoded><![CDATA[<p>There are several great resources out there on how to <a href="http://developer.apple.com/tools/unittest.html">incorporate</a> unit testing into XCode projects.  It&#8217;s all built into XCode now, and it&#8217;s fantastic.</p>

<p>I just got coverage working too, thanks to a useful article at SuperMegaUltraGroovy on how to use <a href="http://www.supermegaultragroovy.com/blog/2005/11/03/unit-testing-and-code-coverage-with-xcode/">code coverage with XCode</a>.  There were a couple of caveats that I thought I&#8217;d share, though.<span id="more-1685"></span>So, to repeat SMUG&#8217;s instructions:</p>

<ol>
<li>Set up <a href="http://developer.apple.com/tools/unittest.html">unit testing</a></li>
<li>Create a new build configuration (&#8216;Coverage&#8217;), duplicated from the &#8216;Debug&#8217; configuration</li>
<li>Open up build settings for the main target, make sure your new configuration is selected, and:

<ul>
<li>Enable &#8220;Generate Test Coverage Files&#8221;</li>
<li>Enable &#8220;Instrument Program Flow&#8221;</li>
<li>Add &#8220;<code>-lgcov</code>&#8221; to &#8220;Other Linker Flags&#8221;</li>
</ul></li>
</ol>

<p>SMUG proposes some commands to append to the &#8220;Run Script&#8221; build phase of the unit test target, in order to generate the coverage report and output a summary to the build results (see the original article).  I made some modifications to aid readability.  So, instead, I added the following to the &#8220;Run Script&#8221; phase of the unit test target:</p>


<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># Run gcov on the framework getting tested
if [ &quot;${CONFIGURATION}&quot; = 'Coverage' ]; then
     TARGET_NAME=&quot;My Application&quot;
     OBJ_DIR=${OBJROOT}/${TARGET_NAME}.build/${CONFIGURATION}/${TARGET_NAME}.build/Objects-normal/${CURRENT_ARCH}
     mkdir -p Coverage
     pushd Coverage
     find &quot;${OBJROOT}&quot; -name *.gcda -exec gcov -o &quot;${OBJ_DIR}&quot; {} \; 2&gt;/tmp/gconv-stderr | egrep &quot;^File|^Lines&quot; | sed -E &quot;s@File '$SRCROOT/@@;s@(\.[a-zA-Z])'@\1: @;s@Lines executed:([0-9.%]+) of ([0-9]+)@\1 (\2)@&quot; | paste -d&quot; &quot; - - | egrep -v &quot;^File '&quot; | sed -E &quot;s@^([^:]+):([^(]*)(\([^)]+\))@\2:\1\3@&quot; | sort -n | sed -E &quot;s@^([^:]+):([^(]*)(\([^)]+\))@\2:\1\3@&quot;; cat /tmp/gconv-stderr | grep -v &quot;version.*, prefer.*&quot;; rm /tmp/gconv-stderr
     popd
fi</pre></div></div>


<p>This orders the results by coverage percentage, reformats the output to be a bit terser and more readable, and suppresses some unnecessary warnings about GCC 4.0 (see below).</p>

<p>Finally, using XCode 3.2 on Snow Leopard (Mac OS X 10.6), I found that there were a few issues.  Using any SDK other than 10.6 results in build errors:</p>

<p><pre>
“<em>vproc_transaction_begin”, referenced from:
_</em>_gcov_init in libgcov.a(_gcov.o)
_vproc_transaction_begin$non_lazy_ptr in libgcov.a(_gcov.o)
“_vproc_transaction_end”, referenced from:
_gcov_exit in libgcov.a(_gcov.o)
_vproc_transaction_end$non_lazy_ptr in libgcov.a(_gcov.o)
ld: symbol(s) not found
</pre></p>

<p>The solution I found in the <a href="http://lists.apple.com/archives/xcode-users/2009/Sep/msg00066.html">Apple lists archives</a> is to use the same SDK as the OS: 10.6 (<em>Project menu, Set Active SDK, 10.6</em>).</p>

<p>After fixing this, I was getting dubious-looking results &#8211; mostly 0.00% code coverage except for a few random files.  The <a href="http://stackoverflow.com/questions/1385568/why-doesnt-gcov-report-any-lines-being-covered-by-my-unit-tests">suggestion</a> for this was to use GCC 4.0 instead of the default 4.2 (edit build settings for the main target, and probably the unit test target too, and select &#8220;GCC 4.0&#8243; under &#8220;C/C++ Compiler Version&#8221;).</p>

<p>After a clean and rebuild, I&#8217;m getting proper results.</p>

<p>Phew.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1685" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/unit-testing-and-coverage-with-xcode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Links for June 3rd through June 18th</title>
		<link>http://atastypixel.com/blog/links-june-3rd-june-18th/</link>
		<comments>http://atastypixel.com/blog/links-june-3rd-june-18th/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 00:00:26 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1631</guid>
		<description><![CDATA[Links for June 3rd through June 18th: Ten logo design tips from the field &#124; Logo Design Love 10 astonishing CSS hacks and techniques Adaptive CSS-Layouts: New Era In Fluid Layouts? effective techniques to create 100%-functional adaptive CSS-layouts WordPress Theme Development Frameworks If you build and develop WordPress themes often, you will probably be fed [...]]]></description>
			<content:encoded><![CDATA[<p>Links for June 3rd through June 18th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://www.logodesignlove.com/logo-design-tips">Ten logo design tips from the field | Logo Design Love</a> </li>
<li><a href="http://www.catswhocode.com/blog/10-astonishing-css-hacks-and-techniques">10 astonishing CSS hacks and techniques</a> </li>
<li><a href="http://www.smashingmagazine.com/2009/06/09/smart-fixes-for-fluid-layouts/">Adaptive CSS-Layouts: New Era In Fluid Layouts?</a> effective techniques to create 100%-functional adaptive CSS-layouts</li>
<li><a href="http://www.smashingmagazine.com/2009/05/27/wordpress-theme-development-frameworks/">WordPress Theme Development Frameworks</a> If you build and develop WordPress themes often, you will probably be fed up of all the repetitive code writing, the constantly checking of your mark-up and all you really want to do is focus on the design and the project-specific features. The answer is a WordPress development framework</li>
<li><a href="http://steveweller.com/articles/toolbar-icons/">How to Draw Pixel-Perfect iPhone Toolbar Icons</a> Using OmniGraffle</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1631" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-june-3rd-june-18th/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Links for May 21st through June 2nd</title>
		<link>http://atastypixel.com/blog/links-may-21st-june-2nd/</link>
		<comments>http://atastypixel.com/blog/links-may-21st-june-2nd/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 07:01:25 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Lifestyle]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1613</guid>
		<description><![CDATA[Links for May 21st through June 2nd: 49 Decent Virtual Assistant &#38; Personal Outsourcing Resources PHP: Display Adobe PSD files on a web page &#34;Any webdesigner know the PSD filetype, which is the Adobe Photoshop format. PSDs have a lot of great features, as such as layers, but they can&#8217;t being read by a browser. [...]]]></description>
			<content:encoded><![CDATA[<p>Links for May 21st through June 2nd:</p>

<ul class="delicious-bookmarks">
<li><a href="http://thegrowinglife.com/2008/04/49-decent-virtual-assistant-personal-outsourcing-resources/">49 Decent Virtual Assistant &amp; Personal Outsourcing Resources</a> </li>
<li><a href="http://www.catswhocode.com/blog/php-display-adobe-psd-files-on-a-web-page">PHP: Display Adobe PSD files on a web page</a> &quot;Any webdesigner know the PSD filetype, which is the Adobe Photoshop format. PSDs have a lot of great features, as such as layers, but they can&rsquo;t being read by a browser. Unless you use this great PHP class!&quot;</li>
<li><a href="http://www.iconfinder.net/">Iconfinder &#8211; Icon Search Made Easy</a> </li>
<li><a href="http://www.typetester.org/">Typetester &ndash; Compare fonts for the screen</a> </li>
<li><a href="http://www.kennettnet.co.uk/code/knappguide/">KNAppGuide</a> KNAppGuide is a Cocoa framework for embedding &ldquo;guides&rdquo; into your application, visually inspired by Apple Guide from the System 7 and 8 era</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1613" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-may-21st-june-2nd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Links for March 17th through April 24th</title>
		<link>http://atastypixel.com/blog/links-march-17th-april-24th/</link>
		<comments>http://atastypixel.com/blog/links-march-17th-april-24th/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 00:00:41 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1514</guid>
		<description><![CDATA[Links for March 17th through April 24th: XSD Schema XML Validator A XSD Schema validator in Java, provides lots of useful information; use it with a schema and an xml instance Three20 Three20 is a collection of iPhone UI classes, like a photo viewer, and general utilities, like an HTTP disk cache. Three20 is derived [...]]]></description>
			<content:encoded><![CDATA[<p>Links for March 17th through April 24th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://www.herongyang.com/XML-Schema/JAXP-XSD-Schema-XML-Validator-Final-Version.html">XSD Schema XML Validator</a> A XSD Schema validator in Java, provides lots of useful information; use it with a schema and an xml instance</li>
<li><a href="http://github.com/joehewitt/three20/tree/master">Three20</a> Three20 is a collection of iPhone UI classes, like a photo viewer, and general utilities, like an HTTP disk cache. Three20 is derived from the Facebook iPhone app, which is one of the most downloaded iPhone apps ever.</li>
<li><a href="http://www.webdesignerwall.com/tutorials/css-decorative-gallery/">CSS Decorative Gallery</a> &#8230;How to decorate your images and photo galleries without editing the source images. The trick is very simple. All you need is an extra &lt;span&gt; tag and apply a background image to create the overlaying effect.</li>
<li><a href="http://en.wikipedia.org/wiki/Comet_(programming)">Comet (programming) &#8211; Wikipedia, the free encyclopedia</a> Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it</li>
<li><a href="http://www.cimgf.com/2008/03/26/cocoa-tutorial-awakefromnib-vs-applicationdidfinishlaunching/">Cocoa Is My Girlfriend &raquo; Cocoa Tutorial: awakeFromNib vs applicationDidFinishLaunching</a> A very good overview of the &#39;startup&#39; procedure for objects stored in IB nib/xibs.</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1514" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-march-17th-april-24th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sneak preview of my new web framework Michaelangelo</title>
		<link>http://atastypixel.com/blog/sneak-preview-of-my-new-web-framework-michaelangelo/</link>
		<comments>http://atastypixel.com/blog/sneak-preview-of-my-new-web-framework-michaelangelo/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 14:39:22 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/03/28/sneak-preview-of-my-new-web-framework-michaelangelo/</guid>
		<description><![CDATA[I&#8217;ve been working on a new web framework which provides image theming &#8211; a little like what Elegant Grunge does with its image frames, but much more sophisticated. For example: This is a PHP framework that uses the common GD library to manipulate images. It&#8217;s main interface is a content filter &#8211; give it HTML, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a new web framework which provides image theming &#8211; a little like what <a href="http://atastypixel.com/blog/wordpress/themes/elegant-grunge">Elegant Grunge</a> does with its image frames, but much more sophisticated.</p>

<p>For example:
<img src="http://atastypixel.com/blog/wp-content/michaelangelo-images/be8dc62a1730cbd98d16f2f8dedfb147.jpg" width="449" height="245" alt="boathouse.jpg" title="boathouse.jpg" class="aligncenter sticky-tape tape-corners noframe" /></p>

<p><img src="http://atastypixel.com/blog/wp-content/michaelangelo-images/d63aad7828e8d537818261cf4dfc9927.jpg" width="471" alt="A dynamic caption" title="A dynamic caption" class="aligncenter polaroid rotation frame-title noframe" height="293" /></p>

<p><img src="http://atastypixel.com/blog/wp-content/michaelangelo-images/f6dc80edccaa4b113042bd2074f187f5.jpg" width="286" height="414" alt="thailand.jpg" title="thailand.jpg" class="alignright wood-frame noframe" /></p>

<p>This is a PHP framework that uses the common GD library to manipulate images.</p>

<p>It&#8217;s main interface is a content filter &#8211; give it HTML, containing images, and it will return the same HTML modified so that the images are now the converted versions, according to their &#8216;class&#8217; attributes, with appropriate width/height attributes, etc.  This makes it super easy to work with.</p>

<p>It stands alone, but it is also going to be a WordPress plugin (as you can see on this site, it&#8217;s already operational), a Joomla plugin, and I&#8217;m considering establishing a web service too, so those who don&#8217;t have adequate software on their server can still use it.</p>

<p><img src="http://atastypixel.com/blog/wp-content/michaelangelo-images/6815f5612dc94ea010b01cc3c0266b98.jpg" width="415" height="314" alt="IMG_1773.jpg" title="IMG_1773.jpg" class="aligncenter wood-frame noframe" /></p>

<p>It has a plugin-based architecture so anyone can add new &#8216;themes&#8217; (<em>props to my partner Katherine for that beautiful wooden frame, by the way</em>).  I&#8217;m going to also implement a simple XML-based plugin schema, and possibly an interface to it, so that it&#8217;s easy to do so.  I&#8217;m planning a &#8216;community&#8217; style directory site to host contributed styles.</p>

<p>The base frame rendering code is such that it is trivially easy to add a new &#8216;theme&#8217;.  It extracts segments from a single frame image, and handles seamless tiling to make the frame the right dimensions, so you don&#8217;t even have to worry about overlapping regions.</p>

<p>I will release it soon, after adding a little more content &#8211; keep your eyes peeled.</p>

<p>For now, check out the <a href="http://atastypixel.com/blog/wp-content/plugins/michaelangelo/Michaelangelo/Sample">Michaelangelo showcase</a>, which gives an idea of the different styles, and contains an interactive sampler to play with styles (<em>IE users should stop being IE users to view this</em>).</p>

<p>Doing my bit to beautify the web.</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1566" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/sneak-preview-of-my-new-web-framework-michaelangelo/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Align comments in Textmate</title>
		<link>http://atastypixel.com/blog/align-comments-in-textmate/</link>
		<comments>http://atastypixel.com/blog/align-comments-in-textmate/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 12:30:02 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Workflow]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/03/27/align-comments-in-textmate/</guid>
		<description><![CDATA[A Textmate command script to align comments in a block]]></description>
			<content:encoded><![CDATA[<p>Unless I&#8217;m using Objective-C, which is fantastically self-documenting, I often like to add comments to parameters to remind me of what they do.  For example:</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">imagecopy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$frame</span><span style="color: #339933;">,</span> 
          <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">-</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topRightCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topLeftCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Destination x</span>
          <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// Destination y</span>
          <span style="color: #000088;">$x</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// Source x</span>
          <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// Source y</span>
          <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topRightCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">-</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topLeftCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// Source width</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topRightCap</span><span style="color: #009900;">&#91;</span>kCapHeight<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Source height</span></pre></div></div>


<p>The only thing is, it looks terrible and is very hard to read.  I usually carefully insert spaces before the comments so they line up, but that&#8217;s really hard to maintain.</p>

<p>I use <a href="http://macromates.com/">Textmate</a>, and found an &#8216;Align Assignments&#8217; script by Chris Poirier which applies very nice formatting to a block of assignments.</p>

<p>Some trivial modifications resulted in a similar script to align comments. Hit <em>Ctrl-Option-Command-/</em>, and:</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">imagecopy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$frame</span><span style="color: #339933;">,</span> 
          <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">-</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topRightCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topLeftCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Destination x</span>
          <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>                                                                            <span style="color: #666666; font-style: italic;">// Destination y</span>
          <span style="color: #000088;">$x</span><span style="color: #339933;">,</span>                                                                           <span style="color: #666666; font-style: italic;">// Source x</span>
          <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>                                                                            <span style="color: #666666; font-style: italic;">// Source y</span>
          <span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topRightCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">-</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topLeftCap</span><span style="color: #009900;">&#91;</span>kCapWidth<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>      <span style="color: #666666; font-style: italic;">// Source width</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">topRightCap</span><span style="color: #009900;">&#91;</span>kCapHeight<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                                              <span style="color: #666666; font-style: italic;">// Source height</span></pre></div></div>


<p>Here it is:</p>

<p><a href="http://atastypixel.com/blog/wp-content/uploads/2009/03/align-commentstmcommand1.zip" title="Align Comments.tmCommand.zip">Align Comments.tmCommand.zip</a></p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1559" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/align-comments-in-textmate/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Links for March 6th through March 16th</title>
		<link>http://atastypixel.com/blog/links-march-6th-march-16th/</link>
		<comments>http://atastypixel.com/blog/links-march-6th-march-16th/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 08:00:30 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Links]]></category>
		<category><![CDATA[Travel]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/?p=1488</guid>
		<description><![CDATA[Links for March 6th through March 16th: CSS Code Snippets : 15 Wicked Tricks Some useful CSS tricks; of particular interest: cross-platform min-height, a trick to clear floats (without needing a &#39;clearing&#39; div), and page breaks 45+ New jQuery Techniques For Good User Experience Here are over 45 impressive jQuery plug-ins and techniques that have [...]]]></description>
			<content:encoded><![CDATA[<p>Links for March 6th through March 16th:</p>

<ul class="delicious-bookmarks">
<li><a href="http://devsnippets.com/reviews/css-code-snippets-15-wicked-tricks.html">CSS Code Snippets : 15 Wicked Tricks</a> Some useful CSS tricks; of particular interest: cross-platform min-height, a trick to clear floats (without needing a &#39;clearing&#39; div), and page breaks</li>
<li><a href="http://www.smashingmagazine.com/2009/01/15/45-new-jquery-techniques-for-a-good-user-experience/">45+ New jQuery Techniques For Good User Experience</a> Here are over 45 impressive jQuery plug-ins and techniques that have been recently created and that could make the development of your next website an easier and more interesting experience than the last.</li>
<li><a href="http://digital-sushi.org/entry/how-to-create-a-disk-image-installer-for-apple-mac-os-x/">How to Create a Disk Image Installer for Apple Mac OS X</a> </li>
<li><a href="http://th30z.netsons.org/2009/03/cocoa-sidebar-with-badges-take-2/">Cocoa: Sidebar with Badges, Take 2</a> &#39;Source list&#39;-esque sidebar with support for badges</li>
<li><a href="http://www.soultravelers3.com/">soultravelers3</a> A family of three from Santa Cruz, California on an epic odyssey: open-ended, years long slow trip around the world as a family adventure, unschool, spiritual journey and lifestyle.</li>

</ul>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1488" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/links-march-6th-march-16th/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Podcast interview with Dan Grigsby of Mobile Orchard on Loopy&#8217;s development</title>
		<link>http://atastypixel.com/blog/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/</link>
		<comments>http://atastypixel.com/blog/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 08:49:26 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Loopy]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://atastypixel.com/blog/2009/03/03/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/</guid>
		<description><![CDATA[Last Thursday I did an interview with Dan Grigsby from Mobile Orchard; the interview is now online. Highlights from this interview include: From UIView to OpenGL: the seven different implementations it took to finalize its unique — and Best App Ever award-nominated — UI. From audio-queues to Remote IO: the four different architectural approaches he [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://atastypixel.com/blog/wp-content/uploads/2009/03/iphone-loopy-mobileorchard.png" width="106" height="200" alt="iphone-loopy-mobileorchard.png" title="iphone-loopy-mobileorchard.png" class="alignright noframe" />Last Thursday I did an interview with Dan Grigsby from <a href="http://www.mobileorchard.com">Mobile Orchard</a>; the <a href="http://www.mobileorchard.com/interview-with-michael-tyson-creator-of-the-audio-mixing-app-loopy/">interview</a> is now online.</p>

<blockquote>
<em>Highlights from this interview include:

From UIView to OpenGL: the seven different implementations it took to finalize its unique — and Best App Ever award-nominated — UI.

From audio-queues to Remote IO: the four different architectural approaches he tried before finalizing audio subsystems.

The travails of trying to implement echo cancelation.

The business of making a living off of a $10 app</em>
</blockquote>

<p>Listen to it <a href="http://www.mobileorchard.com/interview-with-michael-tyson-creator-of-the-audio-mixing-app-loopy/">here</a>, or <a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=294369513">subscribe in iTunes</a> (30 min.)</p>
 <img src="http://atastypixel.com/blog/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=1462" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://atastypixel.com/blog/podcast-interview-with-dan-grigsby-of-mobile-orchard-on-loopys-development/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
