Gaku.net Uklog http://www.gaku.net/uklog/ Gaku.net Uklog Things to be done before upgrading to iOS5 http://www.gaku.net/uklog/a11750.html <p>Google Authenticator stopped working after I upgraded iphone to iOS5. It crushed right after invocations. My co-worker told me about this, so I was at least mentally prepared.</p> <p>Actually, it is pretty troublesome as you&rsquo;ll lose access to Google accounts. Before you&rsquo;re upgrading to iOS5, here&rsquo;s what you should do:</p> <ul> <li>turn-off 2-factor authenticator</li> <li>upgrade your iPhone to iOS5</li> <li>uninstall Authenticator</li> <li>install Authenticator from App Store.</li> <li>configure Authenticator</li> </ul> <p>It sounds pretty dumb, but worked.</p> Thu, 13 Oct 2011 23:19:05 -0400 2011-10-13T23:19:05-04:00 8/15/2011 http://www.gaku.net/uklog/a11745.html <p>Setup a periodic http ping program.</p> <ul> <li>currently runs under <code>screen</code>.</li> <li>writes timestamp and {0,1} value to STDOUT and it is redirected to <code>~/src/pinger/data</code>.</li> <li>writes log every 30 seconds.</li> <li>It fetches <code>http://www.yahoo.com</code>.</li> </ul> <p>TODO: - flush STDOUT every output.</p> Tue, 16 Aug 2011 03:35:12 -0400 2011-08-16T03:35:12-04:00 How iPhone app is initialized with XIB http://www.gaku.net/uklog/a11744.html <p>iPhone application starts with <code>UIApplicationMain()</code> called in <code>main()</code>.</p> <p>It does a couple of things under the hood. It instantiates a <code>UIApplication</code>. It also loads a main nib file specified in <code>.plist</code> file. In my case it is <code>MainWindow</code> in <code>.plist</code> file and it loads <code>MainWindow.xib</code> file.</p> <p>Each application works differently. In Cocoa, these application specific behaviors are implemented using <code>UIApplicationDelegate</code> instance instead of subclassing <code>UIApplication</code>. In other words, <code>UIApplication</code> and your <code>UIApplicationDelegate</code> protocol-supported class works together to implement your application&rsquo;s behavior.</p> <p>Let&rsquo;s look into <code>MainWindow.xib</code>.</p> <p>XIB file has two types of objects. They are not quite noticeable in default UI, but by expanding the view by clicking a little triangle below, it is more understandable:</p> <p><img src="/pict/xib-objects.png" /> <img src="/pict/xib-objects-list.png" /></p> <p>The objects above the line are Placeholders. They are objects that already created somewhere in a program, and XIB connects them to references in XIB.</p> <p>Other objects are instantiated by XIB. Application Delegate is one of these objects created by XIB. My Application Delegate has two outlets: <code>viewController</code> and <code>window</code>. Outlets are set up by XIB: these objects are instantiated by XIB (because they are Objects in the XIB file) and you can refer them via these member variables.</p> <pre><code>@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *viewController; </code></pre> <p>Application does the final step. It shows window and sets what to be shown on the window.</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } </code></pre> Mon, 11 Jul 2011 04:08:58 -0400 2011-07-11T04:08:58-04:00 Show branch name in bash command line prompt http://www.gaku.net/uklog/a11743.html <p>I&rsquo;m getting better with Git and I use git branches more. I often found myself working on a wrong branch especially I worked on a code longer than a day. You can easily forget which branch you were working on. I thought it would be nice to show branch in command line prompt.</p> <p>I found Jon Maddox&rsquo;s &ldquo;<a href="http://www.jonmaddox.com/2008/03/13/show-your-git-branch-name-in-your-prompt/">Show Your GIT Branch Name In Your Prompt</a>&rdquo; which does the job for me. As I don&rsquo;t use his prompt as it is, I needed to understand what it does. I&rsquo;ve discovered a few things I didn&rsquo;t know, so I recorded how I got the prompt I wanted.</p> <p>I put following code in my <code>.bashrc</code>.</p> <pre><code>function git_branch { git branch 2&gt;/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/:\1/' } PS1="\u@\h[\w\$(git_branch)]$ " </code></pre> <p>The code renders a prompt with branch name for git managed directories. It doesn&rsquo;t show branch information if it is not in a git directory.</p> <pre><code>gaku@spektr[~]$ cd ~/src/sinatra gaku@spektr[~/src/sinatra:master]$ </code></pre> <h2 id="command-bash-notation"><code>$(&lt;command&gt;)</code> bash notation.</h2> <p>The first thing to know is <code>$(&lt;command&gt;)</code> in bash. This captures the output of specified command. You can create a dynamic prompt by having this in <code>PS1</code> variable. Note that you need to escape <code>$(&lt;command&gt;)</code> with &lsquo;<code>\</code>&rsquo;. Otherwise, the command is executed immediately when the script is executed (i.e. when you log in), and it fixes the prompt content and you get a static prompt.</p> <h2 id="capturing-the-branch-name">Capturing the branch name</h2> <p>I uses <code>git_branch</code> bash function. This contains <code>git branch</code> command piped to <code>sed</code>. Jon Maddox&rsquo;s command line outputs branch name in a clever way.</p> <pre><code>git branch 2&gt;/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/:\1/' </code></pre> <p>If <code>git branch</code> command is executed outside of a git managed directory, it shows an error in <code>stderr</code>. However, this command line redirect <code>stderr</code> to <code>/dev/null</code>, so there is no output.</p> <p>If the current directory is a git managed directory, <code>git branch</code> returns something like this:</p> <pre><code> master * translation </code></pre> <p>The current branch has <code>*</code> at the beginning of the line. The <code>sed</code> command in the pipeline drops lines not starting with <code>*</code> and trim <code>*</code> and a space. The rest of the line is captured with <code>\1</code> and a &lsquo;:&rsquo; is prepended to it. So, the resulting output from <code>git_branch</code> bash function is <code>:branchname</code>.</p> <p>Output from <code>git_branch</code> is used in <code>PS1</code> to form the final command line prompt.</p> Tue, 05 Jul 2011 02:49:22 -0400 2011-07-05T02:49:22-04:00 Xcode and git http://www.gaku.net/uklog/a11741.html <p>For existing Xcode project, Xcode doesn&rsquo;t provide a UI for creating a git repository. You just need to go to the xcode directory and create git repository from a command line.</p> Fri, 24 Jun 2011 03:49:30 -0400 2011-06-24T03:49:30-04:00 6/19/2011 http://www.gaku.net/uklog/a11740.html <p>Wikipedia&rsquo;s reference.</p> <pre><code>&lt;ref&gt;{{Cite web | author = World Organization of the Scout Movement | authorlink = 世界スカウト機構 | date = | url = http://www.scout.org/en/about_scouting/facts_figures/census | title = Census (Facts &amp; Figures) - World Organization of the Scout Movement | accessdate = 2010-08-31 }}&lt;/ref&gt; </code></pre> <p><code>{{Cite web</code> requires <code>url=</code>, <code>title=</code> and <code>accessdate=</code>.</p> <h1 id="user-location">User location</h1> <p>Setting <code>showUserLocation</code> doesn&rsquo;t do anything about scrolling or zooming the map so the user location is visible. To do that, you need to create a <code>CLLocationManager</code> object and request location updates. The LocateMe sample app is a good example of how to check the accuracy of location updates.(04/11/2011).</p> <p><a href="http://stackoverflow.com/questions/2473706/how-do-i-zoom-an-mkmapview-to-the-users-current-location-without-cllocationmanage/2578459#2578459">Another solution</a> is on stackoverflow. What is KVO notification? KVO stands for Key Value Observing. It is an Observer pattern. You get notification (a method call) when your observing key&rsquo;s value changes. KVO defines a mechanism that allows objects to be notified of changes to the specified properties of other objects.</p> <p>I used the solution on stackoverflow and it worked.</p> <h2 id="location-accuracy">location accuracy</h2> <p>Now, I&rsquo;m interested in GPS accuracy. User location is <code>CLLocation</code> class instance and it has <code>holizontalAccuracy</code> property. I could get that with:</p> <pre><code>CLLocationAccuracy accuracy = myMapView.userLocation.location.holizontalAccuracy; NSLog(@"accuracy: %f", accuracy); </code></pre> <p>In a house with Wi-Fi signal, I get 100.0 (meters) as the accuracy.</p> Sun, 19 Jun 2011 18:07:46 -0400 2011-06-19T18:07:46-04:00 6/18/2011 http://www.gaku.net/uklog/a11739.html <p><code>MKPolylineView</code> inherits from <code>MKOverlayPathView</code> : <code>MKOverlayView</code> : <code>UIView</code>. <code>MKPolylineView</code> strokes the path.</p> <p><code>MKOverlayPathView</code> represents a path drawn on a map.</p> <p>Read <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/XMLParsing/XMLParsing.html">Introduction to Event-Driven XML Programming Guide for Cocoa</a></p> <ul> <li><code>NSXMLParser</code> sends messages to its delegate instead of using callbacks.</li> <li><a href="http://developer.apple.com/library/ios/#documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html">NSXMLParserDelegate protocol</a>.</li> <li><code>NSXMLParser</code> is a stream style parser.</li> <li>SeismicXML sample code seems worth looking into.</li> </ul> Sat, 18 Jun 2011 22:25:11 -0400 2011-06-18T22:25:11-04:00 RRD http://www.gaku.net/uklog/a11738.html <p>The behavior of RRD was not straightforward. By just recording a data point doesn&rsquo;t store anything (it is actually recorded, but not provided for reading), and the value looked like off (actually there&rsquo;s a legitimate reason behind this).</p> <p>I&rsquo;ve started by creating a simple rrd with following command:</p> <pre><code>$ rrdtool create test.rrd -s 10 -b 1000000000 DS:temp:GAUGE:20:U:U RRA:AVERAGE:0.5:1:10 </code></pre> <p>A span of a data is 10 second(<code>-s 10</code>). <code>DS:temp:GAUGE:20:U:U</code> defines a data source. <code>temp</code> is the identifier or the name of the data source. <code>GAUGE</code> is a data source type, and it means it records absolute value. 20 is heartbeat which I explains it later. <code>U:U</code> is min and max values. </p> <p>RRA defines a round robin archive. <code>AVERAGE</code> is a consolidation function. RRA samples one data(AVERAGE:0.5:<b>1</b>:10) and keeps last 10 samples(AVERAGE:0.5:1:<b>10</b>) of 10 seconds. Intuitively, it should remember a data point with following command, but it doesn&rsquo;t.</p> <pre><code>$ rrdtool update test.rrd 1000000001:10 $ rrdtool dump test.rrd ... &lt;database&gt; &lt;!-- 2001-09-08 18:45:10 PDT / 999999910 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:20 PDT / 999999920 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:30 PDT / 999999930 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:40 PDT / 999999940 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:50 PDT / 999999950 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:00 PDT / 999999960 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:10 PDT / 999999970 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:20 PDT / 999999980 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:30 PDT / 999999990 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:40 PDT / 1000000000 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;/database&gt; </code></pre> <p>Actually, only when a period completes, a value is provided by RRD. In other words, while a period hasn&rsquo;t completed, the value is undetermined. The period for 11,000,000,001-11,000,000,010 completes when data arrives for 11000000010 or newer. The following commands should finalize the value for 1000000010.</p> <p>(you need to re-create test.rrd)</p> <pre><code>$ rrdtool update test.rrd 1000000001:10 $ rrdtool update test.rrd 1000000010:20 $ rrdtool dump test.rrd ... &lt;database&gt; &lt;!-- 2001-09-08 18:45:20 PDT / 999999920 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:30 PDT / 999999930 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:40 PDT / 999999940 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:45:50 PDT / 999999950 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:00 PDT / 999999960 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:10 PDT / 999999970 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:20 PDT / 999999980 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:30 PDT / 999999990 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:40 PDT / 1000000000 --&gt; &lt;row&gt;&lt;v&gt; NaN &lt;/v&gt;&lt;/row&gt; &lt;!-- 2001-09-08 18:46:50 PDT / 1000000010 --&gt; &lt;row&gt;&lt;v&gt; 1.9000000000e+01 &lt;/v&gt;&lt;/row&gt; &lt;/database&gt; </code></pre> <p>However, the value for the bucket ends up with 19, not 10 nor 20 I provided. How does this happen? With a following experiment, I could explain how it works:</p> <pre><code>$ rrdtool update test.rrd 1000000001:10 $ rrdtool update test.rrd 1000000004:15 $ rrdtool update test.rrd 1000000010:20 $ rrdtool dump test.rrd ... &lt;!-- 2001-09-08 18:46:50 PDT / 1000000010 --&gt; &lt;row&gt;&lt;v&gt; 1.7500000000e+01 &lt;/v&gt; ... </code></pre> <p>Because a period is 10 seconds, there are 10 data slots which affects the outcome. Data can be visualize like this:</p> <pre><code>1000000001 OOOOOOOOOO (10) 1000000002 no data 1000000003 no data 1000000004 OOOOOOOOOOOOOOO (15) 1000000005 no data 1000000006 no data 1000000007 no data 1000000008 no data 1000000009 no data 1000000010 OOOOOOOOOOOOOOOOOOOO (20) </code></pre> <p>RRD assumes that the value of no data slot as the immediate next value. For 100000002, the value assumed is 15 of 100000004. For 100000005, the value is 20.</p> <pre><code>1000000001 OOOOOOOOOO (10) 1000000002 OOOOOOOOOOOOOOO (15) 1000000003 OOOOOOOOOOOOOOO (15) 1000000004 OOOOOOOOOOOOOOO (15) 1000000005 OOOOOOOOOOOOOOOOOOOO (20) 1000000006 OOOOOOOOOOOOOOOOOOOO (20) 1000000007 OOOOOOOOOOOOOOOOOOOO (20) 1000000008 OOOOOOOOOOOOOOOOOOOO (20) 1000000009 OOOOOOOOOOOOOOOOOOOO (20) 1000000010 OOOOOOOOOOOOOOOOOOOO (20) </code></pre> <p>The final value is computed as the average of all these values. (10+15x3+20x6)/10 = 17.5. Voila!</p> <p>The number of data points backfilled by RRD is controlled by heartbeat parameter of DS (DS:temp:GAUGE:20:U:U&rsquo;s heartbeat is 20 seconds). This 20 seconds is the number of slots that RRD is backfilling. If there are 25 seconds of interval between two data, first 5 seconds won&rsquo;t have any data. If there&rsquo;s at least one second with unknown data, the value of the slot will be unknown or nan.</p> Thu, 28 Apr 2011 02:13:39 -0400 2011-04-28T02:13:39-04:00 launchctl http://www.gaku.net/uklog/a11736.html <p><code>launchctl</code> is hard to use. XML was a failure and writing a config file in XML is not a good choice. Yet, I had to use this for starting a web server on OS X.</p> <p>nginx Startup scripts found by searching on Google seems not correct. I encounter the following error:</p> <pre><code>com.apple.launchd.peruser.502[153] (nginx): Unknown key for boolean: NetworkState </code></pre> <p>This was due to the way <code>NetworkState</code> key was used. I searched an example from <code>/System/Library/LaunchDaemons/</code> and <code>nmbd.plist</code> uses it. In fact, <code>NetworkState</code> needs to be in a dictionary.</p> <pre><code>&lt;key&gt;KeepAlive&lt;/key&gt; &lt;dict&gt; &lt;key&gt;NetworkState&lt;/key&gt; &lt;true/&gt; &lt;/dict&gt; </code></pre> Mon, 21 Mar 2011 06:16:24 -0400 2011-03-21T06:16:24-04:00 KeePassX http://www.gaku.net/uklog/a11734.html <p>I&rsquo;ve been using <a href="http://www.keepassx.org/">KeePassX</a> for managing my passwords for years, but I didn&rsquo;t know much tricks.</p> <p>By selecting an entry on KeePassX, you can use ⌘-C to copy the password to clipboard. It will stay in clipboard for short period of time (30 seconds by default. You can change it via preference).</p> Sun, 13 Feb 2011 06:42:09 -0500 2011-02-13T06:42:09-05:00