<?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>Mykola Bilokonsky</title>
	<atom:link href="http://mykola.bilokonsky.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://mykola.bilokonsky.net</link>
	<description>Independent Technologist</description>
	<lastBuildDate>Tue, 06 Mar 2012 19:43:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using IntelliJ Idea to build HTML5 apps on a Node.JS server</title>
		<link>http://mykola.bilokonsky.net/2012/02/using-intellij-idea-to-build-html5-apps-on-a-node-js-server/</link>
		<comments>http://mykola.bilokonsky.net/2012/02/using-intellij-idea-to-build-html5-apps-on-a-node-js-server/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 22:05:42 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[IntelliJ IDEA]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[intellij]]></category>
		<category><![CDATA[nodejs]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=34</guid>
		<description><![CDATA[So unless you&#8217;ve been hiding under a rock for the past few years you&#8217;ve probably heard of JetBrains and their wonderful IDE, IntelliJ IDEA. Originally a Java IDE, they&#8217;ve fleshed it out to support a variety of additional platforms. They have arguably the best Flex tooling on the market and have since added support for [...]]]></description>
			<content:encoded><![CDATA[<p>So unless you&#8217;ve been hiding under a rock for the past few years you&#8217;ve probably heard of JetBrains and their wonderful IDE, IntelliJ IDEA. Originally a Java IDE, they&#8217;ve fleshed it out to support a variety of additional platforms. They have arguably the best Flex tooling on the market and have since added support for the Web stack, Ruby, Python, etc etc etc.</p>
<p>I know not everyone is crazy about using IDEs, if it&#8217;s not your cup of tea then thanks for stopping by and you can feel free to stop reading here. If you want to learn how to set up IDEA to do fancy things with Node.JS and HTML5 development, however, keep reading! </p>
<p>(Note: I did this using IntelliJ IDEA 11. I do not have any idea how this will apply to previous or future versions of the IDE. If you have success or failure using this approach on other versions please let us know in the comments, as well as describing any tweaks you had to do to make it work right!)</p>
<p><span id="more-34"></span></p>
<p>Here&#8217;s the end goal: we want to have two separate applications, one of them a web server running on localhost in Node.JS, the other a website being served by that server. We want to be able to build, launch and debug both projects using IntelliJ IDEA, but we want to make sure that we&#8217;re not dependent upon IDEA &#8211; we want to be able to rsync our local folder to a remote server and have it simply run.</p>
<p>There are a variety of ways to make this happen, but I&#8217;m going to step you through the process I used.</p>
<h2>Step 1 &#8211; Install Node</h2>
<p><a href="http://nodejs.org/#download">Install node.js</a> on your machine. Go ahead, I&#8217;ll wait. It&#8217;s pretty quick now, if you&#8217;re on a Mac or PC you just run the installer &#8211; no more waiting for it to build etc.</p>
<p>(EDIT: In order to get code hinting from IntelliJ&#8217;s Node.JS plugin you&#8217;ll need to provide it with a path to the core node libraries. If you use the installer as I just suggested above then these libraries will not be accessible to the node plugin. In order to get code-hinting you&#8217;ll need to download the source tarball, unzip it, stick it somewhere and tell the plugin to point to that folder.)</p>
<h2>Step 2 &#8211; Install the Node.JS plugin</h2>
<p>Before we can set up our Node server we need to get the Node.js plugin for intelliJ. This is kind of really really easy, but it&#8217;s an extra step.</p>
<p>Open IntelliJ Idea and open the Plugin Manager (there should be a link in the upper-right of the default splash screen). In the popup that comes up, click &#8220;Browse Repositories&#8221; on the bottom left. Use the search box in the upper right to type &#8220;NodeJS&#8221; and you should see your options reduce to two plugins, NodeJS and NUnitJS. I&#8217;m not mucking around with testing right now, so ignore NUnitJS &#8211; right click on NodeJS and click &#8220;Download and Install&#8221;. When it finishes it&#8217;ll ask you to restart the IDE. Do so.</p>
<p>(Edit: to get code hinting there&#8217;s one more step. Once you&#8217;ve restarted your IDE you should see a green &#8220;Node.JS&#8221; Icon in your toolbar. It&#8217;ll be to the far right, after the Help icon. Click it. It just wants a path to the node.js source. If you installed via the official installer, follow my recommendation above and download the source as well. Either way, now that you have access to the source, tell IDEA to look at that source for the node.js plugin. It it works you&#8217;ll get a new folder linked in your project window that points to the source. As noted in the comments below, if you use this approach while having also used the installer it means that you will have to manually update the source every time you update your node install. I&#8217;m waiting for feedback from Jetbrains to see if there&#8217;s a better way.)</p>
<p>Now we&#8217;re ready to begin.</p>
<p>EDIT: As manners <a href="http://mykola.bilokonsky.net/2012/02/using-intellij-idea-to-build-html5-apps-on-a-node-js-server/#comment-164">points out below</a>, you also need to make sure that you enable the Remote Hosts plugin in order to debug this once we&#8217;re up and running!</p>
<h2>Step 3 &#8211; Create the Server project</h2>
<p>Open IntelliJ IDEA and click &#8220;Create New Project&#8221;. You&#8217;ll get a dialog box with some options. Give your project a name (&#8220;Node Server Project&#8221;), confirm that the location matches your desired location. Make sure that &#8220;create module&#8221; is checked and set the type to &#8220;Web Module&#8221;.</p>
<p>(This appears to be something of a compromise &#8211; technically we&#8217;d want a JavaScript module, since Node doesn&#8217;t make use of the rest, but JS is a subset of what the Web module gives us so we&#8217;ll roll with it.)</p>
<p>When your dialog box looks like this, click &#8220;finish&#8221;:<br />
<div id="attachment_35" class="wp-caption alignleft" style="width: 310px"><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.14.22-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.14.22-PM-300x247.png" alt="New Project config dialog box for a Node.JS project in IntelliJ IDEA" title="Screen Shot 2012-02-17 at 3.14.22 PM" width="300" height="247" class="size-medium wp-image-35" /></a><p class="wp-caption-text">New Project config dialog box for a Node.JS project in IntelliJ IDEA</p></div></p>
<p>So in your Project panel, right-click on your &#8220;Node Server Project&#8221; module and create a new directory, call it &#8220;server&#8221;. Then right click on that directory and create a new JavaScript file, call it &#8220;server&#8221; too. Finally, right click on your &#8220;server&#8221; folder one more time and create a folder called &#8220;www&#8221; &#8211; this is where we&#8217;ll be deploying our website later.</p>
<p>Here is an example of a very simple Node.JS server that simply serves up files from a &#8220;www&#8221; folder. Copy this and paste it into the &#8220;server.js&#8221; file that you created.</p>
<h3 style="clear:both;">server.js</h3>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> fs <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> path <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;path&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> mimeTypes <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;html&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;text/html&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;jpeg&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;image/jpeg&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;jpg&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;image/jpeg&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;png&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;image/png&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;js&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;css&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;text/css&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> server <span style="color: #339933;">=</span> http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> uri <span style="color: #339933;">=</span> url.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">url</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pathname</span><span style="color: #339933;">;</span>
&nbsp;
    uri <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;www/&quot;</span> <span style="color: #339933;">+</span> uri<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>uri.<span style="color: #660066;">charAt</span><span style="color: #009900;">&#40;</span>uri.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        uri <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;index.html&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>uri.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;..&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        response.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">403</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        response.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003366; font-weight: bold;">var</span> filename <span style="color: #339933;">=</span> path.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span>process.<span style="color: #660066;">cwd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> uri<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>Attempting to serve: &quot;</span> <span style="color: #339933;">+</span> filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    path.<span style="color: #660066;">exists</span><span style="color: #009900;">&#40;</span>filename<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>exists<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>exists<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;File not found: &quot;</span> <span style="color: #339933;">+</span> filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            response.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">404</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            response.<span style="color: #660066;">end</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Sorry, the file you requested was not found. Don't let it ruin your day! :)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> mimeType <span style="color: #339933;">=</span> mimeTypes<span style="color: #009900;">&#91;</span>path.<span style="color: #660066;">extname</span><span style="color: #009900;">&#40;</span>filename<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        response.<span style="color: #660066;">writeHead</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'Content-Type'</span><span style="color: #339933;">:</span>mimeType<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> fileStream <span style="color: #339933;">=</span> fs.<span style="color: #660066;">createReadStream</span><span style="color: #009900;">&#40;</span>filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        fileStream.<span style="color: #660066;">pipe</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8080</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now all we have to do is configure our application server. In the upper middle of your IDEA window should be a little combo box containing your run configurations. Right now it should be empty &#8211; click it and select &#8220;Edit Configurations&#8221;.</p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.28.03-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.28.03-PM.png" alt="IntelliJ IDEA Run Configurations" title="Screen Shot 2012-02-17 at 3.28.03 PM" width="444" height="108" class="alignleft size-full wp-image-36" /></a></p>
<p>A popup will appear. Press the &#8220;+&#8221; sign in the upper left and select &#8220;NodeJS&#8221;. If &#8220;NodeJS&#8221; isn&#8217;t available then you don&#8217;t have the NodeJS plugin installed &#8211; go back to Step 2 of these instructions and make sure you&#8217;ve restarted your IDE upon completion.</p>
<p>Select NodeJS and name your configuration &#8220;NodeJS Server&#8221;. The path to Node is OS specific, but I know on a Mac if you use their installer it&#8217;ll stick it into /usr/local/bin/node. If you&#8217;re on a PC or if you have a custom install just set that to point to the actual &#8220;node&#8221; executable, wherever it is.</p>
<p>Now you have to to set &#8220;Path to Node App JS File&#8221; equal to the server.js file you created above. Then set the &#8220;Working Directory&#8221; equal to the path to your &#8220;server&#8221; folder &#8211; for me, it looks like this: &#8220;/Users/mykola/temp/Node Server Project/server&#8221;</p>
<p>Once you&#8217;ve done that, your Run Configuration should look like this. If it does, hit OK:<br />
<a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.43.50-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.43.50-PM-300x244.png" alt="Server Run Configuration" title="Screen Shot 2012-02-17 at 4.43.50 PM" width="300" height="244" class="alignleft size-medium wp-image-45" /></a></p>
<p>Congratulations, you&#8217;re done! To test this, click the &#8220;Debug&#8221; button next to that combo box &#8211; not the green arrow, the one after that. That&#8217;ll launch your server in debug mode. Then open a browser window and navigate to &#8220;http://localhost:8080&#8243; and you&#8217;ll be greeted by a wonderful File Not Found! message. No worries, everything is going great!</p>
<p><br style="clear:both" /></p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.37.02-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.37.02-PM-300x114.png" alt="Set a Breakpoint in your Node.JS server using IntelliJ Idea" title="Screen Shot 2012-02-17 at 3.37.02 PM" width="300" height="114" class="alignleft size-medium wp-image-38" /></a><br />
Before we move on to the website project, take a second to play with IDEA&#8217;s debugger. Set a break point inside of your server handler &#8211; right on line 17, for instance, where &#8220;uri&#8221; gets defined. Do that by clicking on the gutter to make the red dot appear, as shown. With the breakpoint set and the server running, hit localhost:8080 again to put IDEA through it&#8217;s paces. It should hit the breakpoint at which point you can tab over to the debugger at the bottom and introspect all of the objects in scope, evaluate watch expressions, see and navigate the call stack, etc. Really, really useful and way better than just sticking random debug values into console.log(). ;)</p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.39.38-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-3.39.38-PM-300x281.png" alt="IntelliJ Debugger after a breakpoint has been hit." title="Screen Shot 2012-02-17 at 3.39.38 PM" width="300" height="281" class="alignnone size-medium wp-image-39" /></a></p>
<h2>Step 4 &#8211; Setting up your Web Application Project</h2>
<p>Now completely forget about all of that. Shut down your server and close the project, we&#8217;ll come back to it in a few minutes.</p>
<p>First, go ahead and create a brand new IntelliJ project called &#8220;MyWebsite&#8221;. For its location you can put it anywhere &#8211; we are going to be deploying the files from this project to the &#8220;www&#8221; folder in our server project, but we can automate that later so we never have to think about it. Just go ahead and create your new project with a Web module ready to go.</p>
<p>Right click on your module in the project view and add a folder called &#8220;www&#8221;. Inside of that folder, add a folder called &#8220;script&#8221; and a folder called &#8220;style&#8221;. This is a pretty simple run-of-the-mill html file structure. Inside of &#8220;www&#8221;, create an html file called &#8220;index.html&#8221;. Inside of &#8220;script&#8221;, create a javascript file called &#8220;main.js&#8221;. Inside of &#8220;style&#8221;, create a CSS file called &#8220;style.css&#8221;.</p>
<p>Here is some sample content for those files:</p>
<h3>index.html</h3>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;
        &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Sample Index&lt;/title&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;script/main.js&quot;&gt;&lt;/script&gt;
    &lt;link href=&quot;style/style.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
    &lt;h1 onclick=&quot;headerClicked()&quot;&gt;This is my Index page!&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<h3>script/main.js</h3>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> headerClicked<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Sample Alert: &quot;</span> <span style="color: #339933;">+</span> foo <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>style/style.css</h3>
<p></p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>So far so good, right? It&#8217;s a really simple website, when you click on the header text you&#8217;ll get an alert showing you a random number, blah blah blah. The point is, we want to be able to do two things:</p>
<ol>
<li>Deploy this sucker to our Node server!</li>
<li>Set breakpoints and debug our javascript in IntelliJ IDEA</li>
</ol>
<p>In order to achieve these two goals we need to do the following:</p>
<ol>
<li>Configure our remote deployment options so that these files get magically sent to our server&#8217;s /www folder</li>
<li>Build a debug configuration that&#8217;ll access these files via our server, instead of from the file system.</li>
</ol>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.13.42-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.13.42-PM-300x130.png" alt="Set up a Deployment Configuration for our Web Site" title="Screen Shot 2012-02-17 at 4.13.42 PM" width="300" height="130" class="alignleft size-medium wp-image-40" /></a>So, the first thing we need to tackle is Deployment. In IntelliJ IDEA, click on &#8220;Tools&#8221; in the menu, go down to &#8220;Deployment&#8221; and click &#8220;Configuration.&#8221;</p>
<p>In the ensuing popup we&#8217;ll need to add a deployment target, configure the Connection, then the mappings. First, click the &#8220;+&#8221; sign in the upper left. Name your Server and set its type to &#8220;Local or Mounted Folder&#8221;.</p>
<p><br style="clear:both;" /></p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.23.46-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.23.46-PM-300x223.png" alt="Deployment Config - Connection" title="Screen Shot 2012-02-17 at 4.23.46 PM" width="300" height="223" class="alignleft size-medium wp-image-42" /></a>The Connection tab tells IDEA how to connect to your deployment target, both to upload/download files and also to actually access it as a remote http endpoint. So in the Connection tab, make sure &#8220;Type&#8221; is equal to &#8220;Local or mounted folder&#8221;. Set the &#8220;Folder&#8221; path equal to the path to the folder that holds server.js that you created earlier. </p>
<p>In my case, folder is &#8220;/Users/mykola/temp/Node Server Project/server&#8221;. Set the &#8220;Web Server Root URL&#8221; to &#8220;http://localhost:8080/&#8221;. We&#8217;re using port 8080 because that&#8217;s what port we&#8217;re listening on in our server.js file.</p>
<p>When your &#8220;Connection&#8221; tab looks like this, tab over to &#8220;Mappings&#8221;.</p>
<p><br style="clear:both;" /></p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.25.37-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.25.37-PM-300x227.png" alt="Deployment Config - Mappings" title="Screen Shot 2012-02-17 at 4.25.37 PM" width="300" height="227" class="alignleft size-medium wp-image-43" /></a>The &#8220;Mappings&#8221; tab is where you map files and folders in your HTML project to files and folders in your remote endpoint. Go ahead and click the &#8220;use this server as default&#8221; button. If you added a &#8220;www&#8221; folder to the root of your project as I advised above, append &#8220;/www&#8221; to the &#8220;Local path&#8221; that&#8217;s populated by default. Set your Deployment Path on server &#8216;NodeServer&#8217; equal to &#8220;/www&#8221;, and wet your Web Path equal to simply &#8220;/&#8221;. Your &#8220;Mappings&#8221; tab should look like the attached screenshot.</p>
<p><br style="clear:both;" /></p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.29.00-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.29.00-PM-150x150.png" alt="Deployment Options" title="Screen Shot 2012-02-17 at 4.29.00 PM" width="150" height="150" class="alignleft size-thumbnail wp-image-44" /></a>Press OK, you&#8217;re almost done! Go back to the menu and select Tools > Deployment > Options. Check the boxes that say &#8220;Create Empty Directories&#8221; and &#8220;Upload changed files automatically to the default server.&#8221; Hit OK.</p>
<p>In your projects panel, Right Click your &#8220;MyWebApplication&#8221; Module and go all the way down to the Deployment item in the context menu. Select &#8220;Upload to NodeServer&#8221;. This should be the only time you ever have to do this &#8211; from now on, every time you make a change in this project it will automagically push that change to your server/www folder.</p>
<p>Let&#8217;s stop for a second and make sure everything is wired up correctly:</p>
<ol>
<li>Go to File -> Open Recent in IntelliJ and Open your Server project. It&#8217;ll ask you if you want to open it in a new window or the same window &#8211; use a New Window.</li>
<li>In the projects panel, look into your &#8220;www&#8221; folder &#8211; you should see the files from your web project. If you don&#8217;t then you did something wrong above &#8211; make sure that you deployed your files from the context menu as I outlined last paragraph!</li>
<li>If you still have a breakpoint set, go ahead and get rid of it. Just click on it in the gutter and it should go away.</li>
<li>Make sure that the run config we set up is selected, and start your server in Debug mode.</li>
<li>Point your browser to http://localhost:8080 and voila, you should see your html page!</li>
</ol>
<p>The very last step is to set up a Run Configuration for your Web project. This is very similar to the Run Configuration we set up for our server. Click on the combo box, select &#8220;Edit Configurations&#8221;, click the plus sign in the upper left and select &#8220;JavaScript Debug &#8211;> Remote&#8221;. Name your configuration &#8220;JS Debugger&#8221;, set the URL to &#8220;http://localhost:8080&#8243; and map your project to the following remoteURL: &#8220;/&#8221;. Your screen should look like this:</p>
<p><a href="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.47.46-PM.png"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2012/02/Screen-Shot-2012-02-17-at-4.47.46-PM-300x246.png" alt="HTML Run Configuration" title="Screen Shot 2012-02-17 at 4.47.46 PM" width="300" height="246" class="alignleft size-medium wp-image-46" /></a></p>
<p><br style="clear:both;" /></p>
<p>Now for the big test! Switch back to your server project and make sure it&#8217;s running. If so, then in your HTML project go ahead and press the &#8220;Debug&#8221; button &#8211; it should open your browser to your website, served up via localhost:8080. </p>
<p>(Note, if you have trouble with getting it to open in the right browser, you can go into IntelliJ&#8217;s settings and make sure that the &#8220;Web Browsers&#8221; settings are working the way you&#8217;d like. This can be highly specific so I didn&#8217;t cover it above.)  </p>
<p>While your browser is open and showing your app, click your text. You should see an Alert come up with some gibberish in it, right? Click OK and then without closing anything go back to your Web project. In script/main.js, put a breakpoint on the line that generates that alert. Tab back to your browser and click again and you&#8217;ll see that you&#8217;ve just triggered that breakpoint!</p>
<h2>Final Words</h2>
<p>Phew! Thanks for reading all of that. This can be a big pain in the ass to configure, but once it&#8217;s working the way you want it to you have a lot more options. Debugging in IntelliJ IDEA is more powerful than debugging in a browser console, and you can make updates and edits in your code on the fly. </p>
<p>If this worked for you, let me know in the comments! If it didn&#8217;t, well, I&#8217;m sure that it CAN with a bit more nudging. Feel free to ask for help in the comments and I&#8217;ll see if I can&#8217;t take a look, but with this many variables in play it probably makes sense just to tinker with it and figure out what&#8217;s breaking.</p>
<p>I hope that helped!</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2012/02/using-intellij-idea-to-build-html5-apps-on-a-node-js-server/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Apture added</title>
		<link>http://mykola.bilokonsky.net/2011/06/apture-added/</link>
		<comments>http://mykola.bilokonsky.net/2011/06/apture-added/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 18:46:18 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[apture]]></category>
		<category><![CDATA[blog]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=30</guid>
		<description><![CDATA[Just a quick note to say that I&#8217;ve added apture support to this blog. Apture is a super smart web service designed to transparently overlay context onto keywords. It&#8217;s smart enough to know what words people are interested in learning more about by keeping track of what words people highlight so that they can copy+google [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick note to say that I&#8217;ve added apture support to this blog. Apture is a super smart web service designed to transparently overlay context onto keywords. It&#8217;s smart enough to know what words people are interested in learning more about by keeping track of what words people highlight so that they can copy+google them. You&#8217;ll see Apture links scattered throughout the blog, hover over any of them for more information!</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2011/06/apture-added/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WWDC 2011 Prediction: Apple Acquires Twitter?</title>
		<link>http://mykola.bilokonsky.net/2011/06/wwdc-2011-prediction-apple-acquires-twitter/</link>
		<comments>http://mykola.bilokonsky.net/2011/06/wwdc-2011-prediction-apple-acquires-twitter/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 14:48:44 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[wwdc]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=27</guid>
		<description><![CDATA[I could be way off base here, but I suspect that Steve Jobs is about to announce the acquisition of Twitter by Apple. I don&#8217;t have access to any private info or anything, but here&#8217;s why I think this might be true. Twitter has a lot to offer Apple Twitter has been behaving strangely lately, [...]]]></description>
			<content:encoded><![CDATA[<p>I could be way off base here, but I suspect that Steve Jobs is about to announce the acquisition of Twitter by Apple. I don&#8217;t have access to any private info or anything, but here&#8217;s why I think this might be true.<br />
<span id="more-27"></span></p>
<h2>Twitter has a lot to offer Apple</h2>
<ul>
<li>Twitter has been behaving strangely lately, creating <a href="http://mashable.com/2011/06/01/twitter-photo-services/">their own media hosting service</a> and telling devs that they should <a href="http://www.technobuffalo.com/internet/social-networking/read-my-beak-no-new-twitter-clients/">think twice before investing time in the creation of new Twitter clients</a>. These represent a major change in tone &#8211; only a few short months ago, Twitter was everyone&#8217;s favorite public API. Now they seem to be fortifying. Why?</li>
<li>Apple is rumored to be integrating Twitter deeply into their next offering of iOS, which Steve Jobs will be announcing later today. As in, <a href="http://www.macrumors.com/2011/06/03/more-claims-of-deep-twitter-integration-in-ios-5/">very deeply</a>. But why would Apple take a neutral third-party and build them into the core of their super-tightly-controlled walled garden? Jobs isn&#8217;t the sort who spends a lot of time relying on other people/companies &#8211; it would surprise me, honestly, if iOS had twitter built in but Apple had no control over Twitter.</li>
<li>Finally, there&#8217;s simply the matter of the gaping hole in Apple&#8217;s offerings. They do hardware well, they do media well, they do software well &#8211; but it&#8217;s no secret that they <a href="http://www.apple.com/itunes/ping/">suck at social</a>. Social Media is not going away, so if Apple wants to be a relevant player in that field they have two choices. They can create a working, usable and popular social media tool &#8211; or they can buy one. This one just makes sense to me &#8211; especially when you realize how perfect Twitter + iPhone are for each other.</li>
</ul>
<p>It&#8217;s that last point that honestly makes me believe this to be true. Twitter is not just any social media service &#8211; it&#8217;s the one that&#8217;s been most embraced by the tech-savvy minimalist. It lacks the clutter of facebook, and it lacks the fragmentation of the geo/social space. It&#8217;s simple, it&#8217;s easy, and (as long as the servers are up) it just works &#8211; it&#8217;s practical an Apple offering already.</p>
<h2>Apple has a lot to offer Twitter</h2>
<p>The other side of the coin is, why would Twitter want to get into bed with Apple?</p>
<ul>
<li>Money.</li>
<li>Expansion &#8211; if Twitter goes native on every iOS device and every Mac, and gets promoted by Apple, their install base is going to go through the roof. They&#8217;re already THE social media company in elite tech circles &#8211; this could be just the move they need to go truly mainstream.</li>
<li>Resources &#8211; if they get built into Apple&#8217;s iCloud service, which is presumably going to have some of the most powerful (and most stable) servers currently running then we&#8217;ll see a lot less of the fail whale.</li>
</ul>
<h2>Conclusions</h2>
<p>What do you think? Am I crazy? I guess we&#8217;ll find out in a few hours!</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2011/06/wwdc-2011-prediction-apple-acquires-twitter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Brief Update</title>
		<link>http://mykola.bilokonsky.net/2011/06/brief-update/</link>
		<comments>http://mykola.bilokonsky.net/2011/06/brief-update/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 20:58:32 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=25</guid>
		<description><![CDATA[Very little in the way of blog updates the past few months, been up to my ears in a few things. Work is finally starting to slow down, though, so I&#8217;m hoping I&#8217;ll have more time to post. In the meantime, here is a brief rundown of stuff I&#8217;ve been playing with (and will hopefully [...]]]></description>
			<content:encoded><![CDATA[<p>Very little in the way of blog updates the past few months, been up to my ears in a few things. Work is finally starting to slow down, though, so I&#8217;m hoping I&#8217;ll have more time to post.</p>
<p>In the meantime, here is a brief rundown of stuff I&#8217;ve been playing with (and will hopefully soon be blogging about):</p>
<p>1) Living without an IDE &#8211; a lot of folks seem to have a great time coding all kinds of stuff using TextMate, without the clunkiness of an eclipse-based IDE. I&#8217;m looking to explore this option, and I&#8217;ll let you know what I think. This ties into the next few points, because my latest interests live outside of any IDEs -</p>
<p>2) Node.JS &#8211; if you haven&#8217;t heard about Node.JS you&#8217;ve been living under a rock. Node is server-side javascript, bringing that lovely (ech!) JS syntax to the server. Why is this cool? Because like AS3, it relies on the Observer Pattern to handle asynchronous requests. Instead of spawning a new thread and blocking, like most other server-side languages, Node uses an Event system with async callbacks to juggle lots and lots of stuff at the same time. Plus, it&#8217;s JavaScript. Say what you will about it, it&#8217;s pretty slick to be able to handle scripting, web server and front-end coding in the same language. </p>
<p>3) Using JavaScript with Socket.IO to replace Python as my go-to Arduino interface language. If I&#8217;m going to learn one stack inside and out, it seems like JavaScript is the stack to learn. If I&#8217;m doing everything else in JS, why keep Python around when I can use Node just as easily?</p>
<p>4) Back in the Flash world, a big project I&#8217;ve been working on for a few months is approaching launch-readiness. It&#8217;s my first Flex 4 application, and I have some thoughts about that. Generally speaking, I haven&#8217;t really enjoyed it &#8211; but I&#8217;m not sure yet if that&#8217;s my fault or Flex&#8217;s. Much to think about.</p>
<p>5) Finally, this summer I&#8217;ll be rolling out my first iOS application. Stay tuned for rants and thoughts on the subject.</p>
<p>As always, the best way to keep up with me is on Twitter, where you can always find me quickly as <a href="http://www.twitter.com/mykola">@mykola</a>. Drop me a line!</p>
<p>Last thing &#8211; I&#8217;ve implemented Google&#8217;s &#8220;+1&#8243; feature here. If a blog post is useful or interesting, please consider hitting the +1 button to let me and others know!</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2011/06/brief-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Public APIs: The True Battlefield of the Platform Wars (and a proposed solution)</title>
		<link>http://mykola.bilokonsky.net/2011/03/public-apis-the-true-battlefield-of-the-platform-wars-and-a-proposed-solution/</link>
		<comments>http://mykola.bilokonsky.net/2011/03/public-apis-the-true-battlefield-of-the-platform-wars-and-a-proposed-solution/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 21:37:06 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[APIHub]]></category>
		<category><![CDATA[APIs]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[lendle]]></category>
		<category><![CDATA[tweetdeck]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=23</guid>
		<description><![CDATA[A few interesting stories this week have compelled me to flesh out an idea I&#8217;ve been having for a while. Details below the fold, but here&#8217;s the gist: as the web continues to evolve, we get more and more services that deal with lots of information and these services are currently in the habit of [...]]]></description>
			<content:encoded><![CDATA[<p>A few interesting stories this week have compelled me to flesh out an idea I&#8217;ve been having for a while. Details below the fold, but here&#8217;s the gist: as the web continues to evolve, we get more and more services that deal with lots of information and these services are currently in the habit of putting out public APIs. However, these companies also control these APIs and they are able to change or even disable them at will &#8211; which is fine, legally, but creates huge problems for any other companies that are relying on those APIs.</p>
<p>As the web moves forward I can only suspect that this problem is going to become more common, not less &#8211; and so I have at least a vague hint of an idea for how we can go about solving it. I&#8217;m curious for your thoughts!</p>
<p><span id="more-23"></span></p>
<h2>The Context &#8211; Public APIs changed or revoked</h2>
<ul>
<li><a href="http://www.businessinsider.com/amazon-lendle-2011-3">Amazon kills Lendle&#8217;s API access</a> &#8211; Apropos of nothing, Amazon one day decided to shut down the API access of popular up-and-coming kindle lending service <a href="http://lendle.me">Lendle</a>. Nobody is arguing that Amazon isn&#8217;t allowed to do this &#8211; it&#8217;s their API and they are within their rights to do whatever they want with it. The problem is, where does this leave a service like Lendle? They were providing enormous utility for lots and lots of users and never violated any of Amazon&#8217;s policies.</li>
<li><a href="http://www.technobuffalo.com/blog/internet/social-networking/read-my-beak-no-new-twitter-clients/">Twitter threatens third-party client devs</a>. This also came almost entirely out of nowhere &#8211; apparently, now that Twitter has decided they want to turn a profit they&#8217;re gunning for anyone who is creating &#8220;unofficial&#8221; twitter apps &#8211; and this presumably includes things like TweetDeck, Destroy Twitter etc. Again, Twitter is within their rights &#8211; but the general consensus seems to be that this is a dick move. I&#8217;m certainly not about to launch a startup that uses Twitter&#8217;s API &#8211; even if I had been planning to, this would kill those plans</li>
</ul>
<p>You&#8217;re kidding yourself if you don&#8217;t think we&#8217;re going to be seeing more of this in the future. I&#8217;m not even going to mention Facebook, whose API has been in such a state of flux for so long that writing code against it requires phenomenal patience and deep pockets on the part of any client interested in doing so.</p>
<h2>The Complication: Companies Need This Freedom</h2>
<p>And you know what? A company should be allowed to make sweeping changes to a public API, whether these are changes that cause it to work differently than it did yesterday or changes that make it a violation of ToS for someone to continue doing something that was legal yesterday. These companies own these APIs and if we somehow insist that they never change the way they implement them then the only recourse these companies could have is to remove public APIs altogether. I totally understand that.</p>
<h2>The Consequences: Startups Suffer</h2>
<p>But then, look at companies like Lendle which take advantage of these APIs to do truly interesting, unique or useful things. I can only assume Lendle co-founder <a href="http://www.twitter.com/jcroft">Jeff Croft</a> woke up one morning and spit out his coffee as he opened his inbox.  &#8220;Oh, okay, so my company that has been getting all of this press and succeeding wildly is suddenly unable to provide the service around which it&#8217;s based. Now what?&#8221;</p>
<p>That&#8217;s enough to scare me out of putting any serious effort into a startup that relies on a third-party API. I could wake up one morning and realize that an API I had been relying on is no longer available, and now what do I tell my users?</p>
<p>Here&#8217;s where a lot of people simply say &#8220;Well, them&#8217;s the ropes. You knew the risks going in and that&#8217;s just how this works.&#8221;</p>
<p>And that&#8217;s true, but I think we can do better.</p>
<h2>What a Solution Could Look Like</h2>
<p>I&#8217;d like to see a third party emerge that serves as a sort of API Hub. This third party (let&#8217;s call them the API Hub) negotiates with anyone who offers a public API. They basically provide a service whereby this API Provider can register a &#8220;stable and permanent&#8221; API. That&#8217;s not saying that they will stop working on API updates or whatever &#8211; it just means that they will have to version them moving forward, and every version that goes through the API hub needs to be supported independently.</p>
<p>The API Hub in turn sells stable API access to developers. Why would a developer pay for API access instead of using the free public API being offered by the initial provider? They don&#8217;t have to. They are free to build their company on whatever the most recent public API offered by the provider happens to be, and for testing and for getting off the ground this is a perfectly viable strategy. But when they are ready to launch and have enough users that they want to make a stab at stability it&#8217;ll be worth their while to pay for access.</p>
<p>Would this work? Would people buy into this? I think it&#8217;s worth a shot, because money talks. If every serious twitter client out there was paying for the privilege of using a stable API then that&#8217;s going to be a nontrivial source of income for Twitter, I would think, right? Or am I being naive? And if an API Provider is being paid to provide a stable API then suddenly there&#8217;s a much greater incentive for developers to use that API, so this could provide a competitive advantage.</p>
<p>Is that enough to get someone like Twitter or Amazon to sign on? Obviously a big part of it would depend on how the contracts were structured, where the teeth are so to speak. But it&#8217;s at least worth thinking about, right?</p>
<p>I&#8217;m genuinely interested in your thoughts. Does something like this already exist? Is it working? Why/Why not? What are the flaws here? Do you provide an API? Your opinion is the one I&#8217;ve been exposed to the least, how would you feel about participating in something like this? Does it ask you to sign away too much? Is it reasonable? What would make this more compelling for you?</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2011/03/public-apis-the-true-battlefield-of-the-platform-wars-and-a-proposed-solution/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>2011 Adobe Community Professional</title>
		<link>http://mykola.bilokonsky.net/2011/01/2011-adobe-community-professional/</link>
		<comments>http://mykola.bilokonsky.net/2011/01/2011-adobe-community-professional/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 22:43:13 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=16</guid>
		<description><![CDATA[Just a quick note to say that I learned last evening that my application to become an Adobe Community Professional this year was accepted. The idea now that the resources and contacts that come along with that will help me to create more and better content, so hold me to it! :)]]></description>
			<content:encoded><![CDATA[<p>Just a quick note to say that I learned last evening that my application to become an Adobe Community Professional this year was accepted. The idea now that the resources and contacts that come along with that will help me to create more and better content, so hold me to it! :)</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2011/01/2011-adobe-community-professional/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I Love flash.utils.Dictionary, and you should too!</title>
		<link>http://mykola.bilokonsky.net/2011/01/why-i-love-flash-utils-dictionary-and-you-should-too/</link>
		<comments>http://mykola.bilokonsky.net/2011/01/why-i-love-flash-utils-dictionary-and-you-should-too/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 20:59:43 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[Caching in Flash]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[Dictionary]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flash.utils.Dictionary]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=20</guid>
		<description><![CDATA[One of the unsung heroes of readable AS3 code is the native Dictionary class. A Dictionary is just a container for key/value pairs, much like Object. In fact, a lot of people don&#8217;t realize just how useful Dictionary is. The assumption, easy to make, is that it&#8217;s better to use Object because then you don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>One of the unsung heroes of readable AS3 code is the native <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Dictionary.html">Dictionary</a> class. A Dictionary is just a container for key/value pairs, much like Object. In fact, a lot of people don&#8217;t realize just how useful Dictionary is. The assumption, easy to make, is that it&#8217;s better to use Object because then you don&#8217;t have to import anything.</p>
<p>The cool thing about Dictionary, though, is that you can use non-string keys. In fact, you can pass a reference to ANYTHING and use that as a key. Below are a few examples of how Dictionary lets you write clean, readable code. You can do all of this stuff without Dictionary, but you&#8217;re going to be writing more lines of code. I&#8217;ve got three examples below, in increasing order of abstraction. We&#8217;ll step through using interactive elements such as Buttons as Dictionary keys, then we&#8217;ll use Functions, and finally we&#8217;ll use Classes. This is really, really useful.<br />
<span id="more-20"></span></p>
<h2>Metrics and Analytics &#8211; using interactive elements in your application as keys in a Dictionary</h2>
<p>Anyone who does Flash in the advertising world will be familiar with this &#8211; the client wants every single click to be tracked, which means you need to go through and put unique tracking codes into all of your click handlers. Then the client changes their tracking mechanism and you have to go through and find all of your button handlers and change the codes, and various nightmares ensue. </p>
<p>Well, good news! Dictionary can really help you out here. Here&#8217;s a very simple example of what I mean:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/* note: passing &quot;true&quot; to the Dictionary constructor tells it to use weak keys - meaning that if the dictionary is the only reference to an object, it's available for GC. */</span>
<span style="color: #6699cc; font-weight: bold;">var</span> trackingCodes<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Dictionary</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> 
trackingCodes<span style="color: #000000;">&#91;</span>button1<span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;this is the tracking code for button 1&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
trackingCodes<span style="color: #000000;">&#91;</span>button2<span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;this is the tracking code for button 2, etc&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
button1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> buttonHandler<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
button2<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> buttonHandler<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> buttonHandler<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	TrackingManager<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">track</span><span style="color: #000000;">&#40;</span>trackingCodes<span style="color: #000000;">&#91;</span>e<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">currentTarget</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// logic here to handle click</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Really simple example, but you see what&#8217;s going on, right? The Tracking Code gets associated with the actual button instance. Any time you have a reference to the button you can get a reference to the tracking code &#8211; and e.currentTarget counts as a reference to the button. </p>
<p>If your application is more complex &#8211; for instance, a full-on MVC based web application, with lots of different views &#8211; this can still help you. Set up a Tracking class and give it a static dictionary and two static functions:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">protected</span> static <span style="color: #6699cc; font-weight: bold;">var</span> trackingCodes<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Dictionary</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> registerButton<span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000066; font-weight: bold;">,</span> t<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	trackingCodes<span style="color: #000000;">&#91;</span><span style="color: #004993;">b</span><span style="color: #000000;">&#93;</span> = t<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">track</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">b</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">DisplayObject</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	tracking <span style="color: #004993;">code</span> = trackingCodes<span style="color: #000000;">&#91;</span><span style="color: #004993;">b</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// logic here to do something with that tracking code.</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Then in your views you simply register your buttons in a constructor or init function:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">Tracking<span style="color: #000066; font-weight: bold;">.</span>registerButton<span style="color: #000000;">&#40;</span>button1<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;button 1 tracking code&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
Tracking<span style="color: #000066; font-weight: bold;">.</span>registerButton<span style="color: #000000;">&#40;</span>button2<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;button 2 tracking code&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Finally, on click to your button you just pass a reference to e.currentTarget to Tracking.track(). Simple, effective, and it allows you to organize your tracking codes in large blocks so you can easily make changes and no longer have to waste time looking for each button handler.</p>
<h2>Caching &#8211; using Functions as keys in a Dictionary</h2>
<p>This one came up recently on Twitter. Here&#8217;s the problem: you have some relatively expensive functions that only have to get called once. You want to set up a system that&#8217;ll either call your expensive function (the first time) or return a stored value from that function on every subsequent call. The good news? Since Functions are first class Citizen-Objects in Flash, you can use them as Dictionary keys. Here&#8217;s an example of what I mean:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> cache<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Dictionary</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> calculateExpensiveNumberOne<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// lots of messy code.</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> calculateExpensiveNumberTwo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// lots of messy code</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> numberOne<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span>cache<span style="color: #000000;">&#91;</span>calculateExpensiveNumberOne<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		cache<span style="color: #000000;">&#91;</span>calculateExpensiveNumberOne<span style="color: #000000;">&#93;</span> = calculateExpensiveNumberOne<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>calculateExpensiveNumberOne<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> numberTwo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span>cache<span style="color: #000000;">&#91;</span>calculateExpensiveNumberTwo<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		cache<span style="color: #000000;">&#91;</span>calculateExpensiveNumberTwo<span style="color: #000000;">&#93;</span> = calculateExpensiveNumberTwo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">return</span> cache<span style="color: #000000;">&#91;</span>calculateExpensiveNumberTwo<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Simple, right? By the way, the ability to pass references to functions around is REALLY useful. Don&#8217;t be afraid of it. This is how callbacks work, and it opens a door to the wide world of functional programming.</p>
<h3>Class Management &#8211; using Classes as keys in a Dictionary</h3>
<p>If you&#8217;ve ever implemented the Singleton pattern, the previous example probably looked a bit familiar to you. Just to drive the point home, let&#8217;s remember that Classes are also Objects in Flash. You can build, for instance, a factory class that returns singleton instances of the requisite classes using a very similar pattern to the one we used above. For instance, let&#8217;s create a SampleSingletonManager class like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> testing
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Dictionary</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SampleSingletonManager
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> static <span style="color: #6699cc; font-weight: bold;">var</span> instances<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Dictionary</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Dictionary</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> getInstance<span style="color: #000000;">&#40;</span><span style="color: #004993;">c</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Class</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span>instances<span style="color: #000000;">&#91;</span><span style="color: #004993;">c</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				instances<span style="color: #000000;">&#91;</span><span style="color: #004993;">c</span><span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">c</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">return</span> instances<span style="color: #000000;">&#91;</span><span style="color: #004993;">c</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Then, in our main class, let&#8217;s do this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> testing<span style="color: #000066; font-weight: bold;">.</span>SampleSingletonManager<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> TestThing <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> TestThing<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>SampleSingletonManager<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #004993;">Sprite</span><span style="color: #000000;">&#41;</span> == SampleSingletonManager<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #004993;">Sprite</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #009900; font-style: italic;">// returns true</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>SampleSingletonManager<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #004993;">Sprite</span><span style="color: #000000;">&#41;</span> === SampleSingletonManager<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #004993;">Sprite</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #009900; font-style: italic;">// returns true</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Again, a very simple example &#8211; obviously this isn&#8217;t enforcing any kind of singleton on your instances, and I have the return type set to Object &#8211; but I hope I&#8217;ve made my point. If you want to take this further, you can structure your singletons to require the SingletonManager to pass an instance of itself, or of a private enforce that can only come from inside SingletonManager. Then you return an Interface type that your various Singletons all implement and BAM.</p>
<p>Dictionary is incredibly useful &#8211; being able to use non-string Keys can lead to some very interesting patterns and can go a long way towards helping to write logical, readable code. There are, of course, some downsides to using Dictionary &#8211; it&#8217;s slower than Object or Array, and you can&#8217;t strictly type the keys or values the way you can with a Vector. However, I&#8217;ve used it to great effect on a wide variety of projects and I hope these simple examples get some of your wheels turning.</p>
<p>Please feel free to comment if you agree/disagree/have questions with any of the examples!</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2011/01/why-i-love-flash-utils-dictionary-and-you-should-too/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>2010 In Review</title>
		<link>http://mykola.bilokonsky.net/2010/12/2010-in-review/</link>
		<comments>http://mykola.bilokonsky.net/2010/12/2010-in-review/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 21:16:08 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Robotics]]></category>
		<category><![CDATA[VEX]]></category>
		<category><![CDATA[2010]]></category>
		<category><![CDATA[2011]]></category>
		<category><![CDATA[consulting]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[robotics]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=19</guid>
		<description><![CDATA[Just a quick post to wrap up my year. Work 2010 was incredibly exciting for me. A year ago, I was working full-time at a digital advertising agency doing Flash production. It wasn&#8217;t a bad job, but I wasn&#8217;t thrilled about agency life and in April I decided to finally take the plunge and go [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post to wrap up my year.</p>
<h2>Work</h2>
<p>2010 was incredibly exciting for me. A year ago, I was working full-time at a digital advertising agency doing Flash production. It wasn&#8217;t a bad job, but I wasn&#8217;t thrilled about agency life and in April I decided to finally take the plunge and <a href="http://www.prettysmartstudios.com">go into business for myself</a> &#8211; and honestly, I haven&#8217;t looked back. I prefer the consulting lifestyle over the salaried lifestyle in almost every way, and it&#8217;s allowed me to cast an incredibly wide net in terms of extracurricular activities and side projects. Having a <a href="http://www.twitter.com/stoltesawa">girlfriend</a> who is also a talented designer made this something of a no-brainer for us, and it&#8217;s been great to be able to collaborate on large projects working to meet our own deadlines. Some of my consulting has been done on-site, as has some of hers &#8211; but our most exciting projects are the ones we&#8217;re working on secretly. Hopefully in 2011 we&#8217;ll have some cool stuff to share!</p>
<h2>Activities</h2>
<p>As far as extracurriculars and side projects go, I took over running the <a href="http://www.columbusdigital.org"/>Columbus Digital</a> Adobe User Group, provided technical feedback on not one but two books from O&#8217;Reilly Press (<a href="http://oreilly.com/catalog/9780596805616/">The Flex 4 Cookbook</a> and <a href="http://www.adobe.com/content/dam/Adobe/en/devnet/flex/pdfs/flex4_getting_started.pdf">Getting Started with Flex 4</a>), and went to my first industry conference (<a href="http://riaunleashed.com">RIAUnleashed</a>). I also submitted a talk to 2011&#8242;s Flash and the City.</p>
<p>Additionally I&#8217;ve become much more active on Twitter (hit me up at <a href="http://www.twitter.com/mykola">@mykola</a>) and I&#8217;ve started updating this blog with almost alarming regularity (this is what, like, my fifth post this year? That&#8217;s huge for me!)</p>
<h2>Technology</h2>
<p>2010 was going to be the year I switched to an Android phone. I mean, come on &#8211; I&#8217;m a Flash developer. I&#8217;m supposed to hate Apple and Steve Jobs, especially after that Flash stunt he pulled! So in July, I put a bullet in my trusty iPhone 3G and bought an HTC Evo 4G.</p>
<p>I returned it inside of a month. I know this is a highly polarized field right now, and I don&#8217;t want to make any enemies &#8211; but I hated almost everything about the Android platform as a user. Inconsistent UI, a convoluted phone OS (&#8220;Oh, no, you should download the process manager to let you know what&#8217;s running!&#8221; &#8211; really?), a terrible app store experience. You name it, going down the list I felt like Apple simply cares more about providing a great user experience &#8211; and so I&#8217;m back on an iPhone. Maybe I&#8217;ll switch in 2011 &#8211; but honestly, Android has a lot of cleaning up to do before they can begin to compete with Apple&#8217;s user experience. </p>
<p>I also picked up an iPad this year. I like it okay, but honestly the novelty wore off quickly. I&#8217;m just not entirely clear what I&#8217;m supposed to do with it. Right now it kinda just lives in the bathroom, which is a constant source of amusement for guests. Still, I suspect I&#8217;ll buy an iPad 2 this coming year. </p>
<h2>Robotics</h2>
<p>2010 is the year I bought my first (four) Arduino microcontrollers. This has been an incredible way to spend my downtime, and I&#8217;ve learned so much I can&#8217;t stand it. Currently I&#8217;m working on developing a custom stack for robotics projects &#8211; it looks something like this:</p>
<ul>
<li>Robot Structure &#8211; <strong>VEX</strong> frame, with VEX motors and wheels etc. It&#8217;s a bit like Lego &#8211; everything is designed to fit together nicely.</li>
<li>Microcontroller &#8211; This is going to be <strong>Arduino</strong>. It doesn&#8217;t take that much effort to make the VEX components play nice with the Arduino, and the existing support out there for open-source hardware projects using the Arduino system is fantastic.</li>
<li>Communications &#8211; Right now I&#8217;m using <strong>Python</strong> to handle my serial communications, because it works so nicely. I can rig up my serial connection using USB, BlueTooth or XBee and it doesn&#8217;t make any difference &#8211; Python is able to read/write with like 2 lines of code. That said, I still haven&#8217;t resolved the difficulties I&#8217;ve been having integrating python into the final layer of my stack&#8230;</li>
<li>User Interface &#8211; <strong>Adobe AIR</strong>. This is a no-brainer &#8211; it&#8217;s incredibly easy to build amazingly detailed user interfaces using the Flash Platform, and I&#8217;m almost able to get complete integration between my Python layer and my AIR layer. I&#8217;m confident that with a bit more tweaking I&#8217;ll have it down.</li>
</ul>
<p>So yeah, that&#8217;s that. Vex -> Arduino -> Python -> AIR creates a <strong>VAPA Robotics Stack</strong>. If I can iron out the kinks, I&#8217;m going to have an incredible project to show off in a few months!</p>
<h2>Looking Forward</h2>
<p>2010 was a great year for me &#8211; I&#8217;m expecting 2011 to be even better. I&#8217;m going to keep learning, keep playing and keep doing cool stuff. Please reach out to me on Twitter of any of this sounds interesting to you, I love to talk shop! </p>
<p>In 2011, my largest focus is going to be on completing my robotics stack &#8211; my big goal for the year is to present at Adobe MAX 2011, controlling a variety of cool &#8216;bots from a Flash-built interface. It&#8217;s much easier than you probably think, and it&#8217;s incredibly satisfying! Like I said, I have a few kinks to work out &#8211; but it&#8217;s 80% of the way there, and I&#8217;d love to get other people to join in!</p>
<p>So, from me &#8211; Happy New year, and I&#8217;ll see you all in 2011!</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2010/12/2010-in-review/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Controlling a VEX motor with an Arduino Microcontroller</title>
		<link>http://mykola.bilokonsky.net/2010/12/controlling-a-vex-motor-with-an-arduino-microcontroller/</link>
		<comments>http://mykola.bilokonsky.net/2010/12/controlling-a-vex-motor-with-an-arduino-microcontroller/#comments</comments>
		<pubDate>Thu, 30 Dec 2010 21:45:55 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Robotics]]></category>
		<category><![CDATA[VEX]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[Arduino Microcontroller]]></category>
		<category><![CDATA[Control VEX with Arduino]]></category>
		<category><![CDATA[Replace VEX Microcontroller]]></category>
		<category><![CDATA[VEX Motor]]></category>
		<category><![CDATA[VEX Servo]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=17</guid>
		<description><![CDATA[I&#8217;ve been tinkering with some hardware I recently got my paws on, a VEX starter kit. Designed for education, VEX includes a whole host of equipment beyond their microcontroller &#8211; they have structural components, sensors, motors, servos etc and all of it is designed to plug into their microcontroller. Since it&#8217;s hard-wired into me to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been tinkering with some hardware I recently got my paws on, a <a href="http://www.google.com/products/catalog?q=VEX+Starter+Kit&#038;um=1&#038;ie=UTF-8&#038;cid=4939754482240867169&#038;ei=t_YcTd_-OseYnAf_rvzNDg&#038;sa=X&#038;oi=product_catalog_result&#038;ct=result&#038;resnum=1&#038;ved=0CB8Q8wIwAA#">VEX starter kit</a>. Designed for education, VEX includes a whole host of equipment beyond their microcontroller &#8211; they have structural components, sensors, motors, servos etc and all of it is designed to plug into their microcontroller.</p>
<p>Since it&#8217;s hard-wired into me to do things the hard way, I decided to see if it was possible to control VEX components with an Arduino microcontroller. A bit of googling shows that it can be done, but I didn&#8217;t find any good step-by-step guides &#8211; so I thought I&#8217;d write one. This isn&#8217;t a comprehensive guide to powering all things VEX through an Arduino &#8211; but follow along and I&#8217;ll show you how to control a VEX motor using an Arduino, which is pretty cool.<br />
<span id="more-17"></span></p>
<h2>VEX Motors are Servos!</h2>
<p>First of all: VEX motors don&#8217;t operate like standard motors &#8211; they have three wires, not just two, so you can&#8217;t just plug &#8216;em into an Ardumoto and hope for the best. Instead, you have to know that these motors are servos. The black wire is ground, the orange wire is power and the white wire is designed to carry signal. When we wire this up, we&#8217;re going to power the motor independently of the Arduino board &#8211; we&#8217;ll get to why in a bit. The important thing to note here is that you control this guy by creating a Servo object in your Arduino code, and then passing it a value. If you send &#8220;90&#8243; the motor just kinda sits there. If you pass 0 it spins one way, if you pass 180 it spins the other way &#8211; the closer to 90 you are in either direction, the slower the spin. Cool?</p>
<h2>Powering a VEX Motor to work with your Arduino board</h2>
<p>If you just wire this up to your USB-powered arduino unit you&#8217;ll be in for a big disappointment &#8211; it seems like the arduino is not capable of outputting enough power to drive the motor. Fortunately, the signal wire doesn&#8217;t need as much energy as the motor itself, so the solution is fairly simple. You can solder this too, but these instructions are for a breadboard:</p>
<div id="attachment_18" class="wp-caption alignleft" style="width: 310px"><a href="http://mykola.bilokonsky.net/wp-content/uploads/2010/12/photo.jpg"><img src="http://mykola.bilokonsky.net/wp-content/uploads/2010/12/photo-300x224.jpg" alt="Proper wiring: Black wire from 9v battery to both black wire on VEX and Arduino Ground. Red wire from battery to orange wire on VEX and Arduino VIN. White wire on VEX Motor to PWM output on Arduino board." title="VEX Motor controlled by Arduino" width="300" height="224" class="size-medium wp-image-18" /></a><p class="wp-caption-text">Proper wiring: Black wire from 9v battery to both black wire on VEX and Arduino Ground. Red wire from battery to orange wire on VEX and Arduino VIN. White wire on VEX Motor to PWM output on Arduino board.</p></div>
<ol>
<li>Get a 9v battery with one of those connectors that sits on top and has red and black wires coming out. The connectors are really cheap, it was like 3 bucks for a bag of &#8216;em at Radio Shack.</li>
<li>Connect the two battery cables to two different rows on your breadboard.</li>
<li>From the row that has your red wire, run a wire to the Arduino board&#8217;s &#8220;VIN&#8221; pin and run a wire to your motor&#8217;s Orange cable.</li>
<li>From the row that has your black wire, run a wire to the Arduino board&#8217;s &#8220;Ground&#8221; pin and run a wire to your motor&#8217;s Black cable.</li>
<li>Finally, connect the white pin on your motor to a PWM output on your Arduino board. I used Pin 5.</li>
</ol>
<p>What we&#8217;re actually doing here is powering the motor independently of the board &#8211; both the motor and the board are getting energy from the battery, but the motor is getting the full 9v from the battery rather than the 5v it would get if you were powering it from the arduino board directly. From reading the various forum threads it sounds like some people actually put a capacitor between the motor&#8217;s white wire and your Arduino&#8217;s PWM output &#8211; I haven&#8217;t had any issues doing it this way, but if you get erratic behavior you can try that.</p>
<h2>Arduino Code</h2>
<p>This is the easiest part, once you understand the rest. You&#8217;re going to use Arduino&#8217;s native &#8220;Servo&#8221; class. You attach it to your PWM pin, then you send a value. Really, really easy. Here&#8217;s a sample sketch that&#8217;ll get your motor turning:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;Servo.h&quot;</span>
&nbsp;
Servo motor<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  motor.<span style="color: #202020;">attach</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  motor.<span style="color: #202020;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Where to now?</h2>
<p>Once you have this part down you&#8217;re in a good position to take advantage of the various equipment that VEX lets you use. Stay tuned for a post soon that&#8217;ll have me driving a VEX robot around using easy-to-make Arduino components.</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2010/12/controlling-a-vex-motor-with-an-arduino-microcontroller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adobe AIR, NativeProcess and Python &#8211; Introduction</title>
		<link>http://mykola.bilokonsky.net/2010/11/adobe-air-nativeprocess-and-python-introduction/</link>
		<comments>http://mykola.bilokonsky.net/2010/11/adobe-air-nativeprocess-and-python-introduction/#comments</comments>
		<pubDate>Sun, 07 Nov 2010 19:20:08 +0000</pubDate>
		<dc:creator>mykola</dc:creator>
				<category><![CDATA[Arduino]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[MXML]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[NativeProcess]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://mykola.bilokonsky.net/?p=14</guid>
		<description><![CDATA[Right now, if you want to handle input and output between Arduino and Adobe Flash/AIR, the recommended best practice is to use a program like SerProxy to listen to your serial input, turn it into socket data and publish it to local host. Inside of Flash, then, you treat it as a socket server &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Right now, if you want to handle input and output between Arduino and Adobe Flash/AIR, the recommended best practice is to use a program like <a href="http://www.lspace.nildram.co.uk/freeware.html">SerProxy</a> to listen to your serial input, turn it into socket data and publish it to local host. Inside of Flash, then, you treat it as a socket server &#8211; you listen for socket data, and respond in kind.</p>
<p>It works, but it&#8217;s not pretty. You need to have access to the right ports, and you have to run a third-party application with its own config file to make it work. Additionally, I&#8217;ve never once been able to get it working on a mac &#8211; I know I&#8217;m not the only one, either. There&#8217;s some weird shit going on there. </p>
<p>I decided to see if it was possible to get the same results using the new <a href="http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/desktop/NativeProcess.html">NativeProcess API</a> in Adobe AIR 2. Read on for the details!</p>
<p><span id="more-14"></span></p>
<h2>Overview &#8211; Selecting a Language</h2>
<p>So, before we get started let&#8217;s define the problem as specifically as possible. We have a device, in this case an Arduino Microcontroller, that&#8217;s reading and writing to a serial port on the computer. Our challenge is to allow our Flash application, running in AIR, to interact with it. Flash, however, has no direct access to serial port data &#8211; even with the expanded AIR runtime that stuff is still sandboxed away. So we know right out of the box that we&#8217;re going to need an additional language to handle transport between the serial port and AIR.</p>
<p>My challenge is this: I&#8217;m a flash guy. I can comfortably scan other languages, but I don&#8217;t know the Java or Python or C API&#8217;s or syntaxes as well as I probably should. So I need to pick a language that&#8217;ll make this as easy as possible to implement. I first looked at the source of SerProxy to see if I could repurpose the existing code there &#8211; but it frankly scared me a little bit. C can be an intimidating thing to look at, and when there are 15 files full of one-or-two-character-long method names and system calls I have to assume that there&#8217;s going to be an easier way. The advantage is, there are no dependencies &#8211; once you have your program written, you can compile it down to an executable on any system, and that&#8217;s pretty neat. Still, though &#8211; too scary for now, I want to be up and running quickly.</p>
<p>So I looked into Java &#8211; and that made a bit more sense, but there seem to be issues with the libraries required for native serial IO and the documentation in the tutorials was a bit long. I wanted something simple. Bookmark the Java pages for future reference, but keep looking.</p>
<p>So I looked into python, and apparently serial IO can be handled with about two lines of code, both legible and logical. The downside is that the user needs to have Python installed, and in addition needs to have the PySerial library installed. This is a minor pain in the ass, but it&#8217;s not that big a deal &#8211; anyone who is going to be using my code should be able to at least install python and pyserial, fine. Plus, Python runs on any operating system as long as you have the python interpreter installed. So, Python it is. </p>
<h2>Setup</h2>
<p>Really simple. Open your AIR IDE of choice, create a new Flex project. My MXML is below, it&#8217;s all one file &#8211; paste that in there. Then right click the src folder and add a new file, call it &#8220;ThreadedSerialInterface.py&#8221; &#8211; the code that will go in here is below, as well. That&#8217;s it. The nice thing about AIR is that it will actually bundle your script in with your AIR application, so there&#8217;s no need to worry about running multiple programs the way you have to now, with SerProxy.</p>
<h2>The Python Script</h2>
<p>I know that I want to be able to write a script which is going to remain open in the background until I tell it to close. While open, this script needs to be able to do two things &#8211; accept any data coming out of the serial port and print it to stdout, and accept any data coming in from stdin and write it out to the serial port. Simple enough, but these are two distinct processes &#8211; so I googled around a bit and decided to implement these processes as two threads within my python script. Here&#8217;s the script as I currently have it, with the caveat that I am NOT a python developer and all of this is either made up as I went along or cribbed from helpful folks on <a href="http://stackoverflow.com/questions/4044293/python-noob-silly-question-works-in-python-interpreter-not-from-cli">Stack Overflow</a>. So, here&#8217;s my current script:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">sys</span> <span style="color: #ff7700;font-weight:bold;">import</span> argv, stdin
<span style="color: #ff7700;font-weight:bold;">from</span> serial <span style="color: #ff7700;font-weight:bold;">import</span> Serial
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    baudRate = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>argv<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    serialPort = argv<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
    <span style="color: #808080; font-style: italic;">#some nice default</span>
    baudRate = <span style="color: #ff4500;">9600</span>
    serialPort = argv<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#support paths with spaces. Windows maybe?</span>
serialPort = <span style="color: #483d8b;">' '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>serialPort<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> serialPort:
    exit<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Please specify a serial port&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;opening connection to %r@%i ...&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>serialPort, baudRate<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
ser = Serial<span style="color: black;">&#40;</span>serialPort, baudRate, timeout=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>    
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> readerThread<span style="color: black;">&#40;</span><span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, threadID, name<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">threadID</span> = threadID
        <span style="color: #008000;">self</span>.<span style="color: black;">name</span> = name
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
       <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
           <span style="color: #ff7700;font-weight:bold;">if</span> ser.<span style="color: black;">inWaiting</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
               <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;[R]&quot;</span> + ser.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> writerThread<span style="color: black;">&#40;</span><span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#41;</span>: 
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, threadID, name<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">threadID</span> = threadID
        <span style="color: #008000;">self</span>.<span style="color: black;">name</span> = name
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            <span style="color: #008000;">input</span> = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;[W}Give Me Input!&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;[W]You input %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">input</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            ser.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">input</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
thread1 = readerThread<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #483d8b;">&quot;readerThread&quot;</span><span style="color: black;">&#41;</span>
thread2 = writerThread<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>, <span style="color: #483d8b;">&quot;writerThread&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
thread1.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
thread2.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A few notes: I&#8217;ve appended &#8220;[R]&#8221; and &#8220;[W]&#8221; to outputs from my Reader and Writer threads, respectively. This lets me easily separate them in my AIR app, which is helpful for knowing what I&#8217;m looking at.</p>
<p>So what&#8217;s going on in this script? I instantiate an instance of pySerial&#8217;s serial class, called &#8220;ser&#8221;, and I tell it to connect to the serial port and baud rate specified as arguments when I run this script. Simple enough. Then I define two classes, readerThread and writerThread, each of which extends threading.Thread. readerThread simply loops and, if there is any output from the serial port, it prints it out. writerThread is the inverse &#8211; it loops, and waits for input from stdin each time through. It takes that input, when it gets it, and writes it to the serial port. I start both threads and I&#8217;m good to go. </p>
<p>Let&#8217;s see how we can tie this into Flash.</p>
<h2>AIR and NativeProcess</h2>
<p>This is actually a lot more painless than you might expect, once you understand what&#8217;s going on. AIR 2 has exposed the NativeProcess API, meaning that we basically have access to a command line interface directly through our AIR application. Rather than stepping through every detail, I&#8217;ll just post my code below, with comments. Right now it&#8217;s all one file:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:WindowedApplication</span> xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span> </span>
<span style="color: #000000;">					   xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span> </span>
<span style="color: #000000;">					   xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/mx&quot;</span></span>
<span style="color: #000000;">					   closing=<span style="color: #ff0000;">&quot;killProc()&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- Place non-visual elements (e.g., services, value objects) here --&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Declarations</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
&nbsp;
<span style="color: #000000;">			// create our native process</span>
<span style="color: #000000;">			private var np:NativeProcess = new NativeProcess<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #000000;">			// create our native process startup info, which will hold things like the path to python and any arguments.</span>
<span style="color: #000000;">			private var npi:NativeProcessStartupInfo = new NativeProcessStartupInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #000000;">			/**</span>
<span style="color: #000000;">			 * 	This method gets called when the user clicks the <span style="color: #ff0000;">&quot;init&quot;</span> button. It basically makes sure that your python </span>
<span style="color: #000000;">			 *  path is correct, then assembles the NativeProcess call, assigns listeners and starts the process.</span>
<span style="color: #000000;">			 */</span>
<span style="color: #000000;">			private function setup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void <span style="color: #66cc66;">&#123;</span></span>
&nbsp;
<span style="color: #000000;">				// the file we<span style="color: #ff0000;">'re actually going to be executing is python itself - the rest, my script included, gets passed in as arguments.</span>
<span style="color: #000000;">				var exec:File = File.applicationDirectory.resolvePath(pythonLocation.text);</span>
&nbsp;
<span style="color: #000000;">				// check to see if the python path resolved correctly.</span>
<span style="color: #000000;">				if (exec.exists) {</span>
<span style="color: #000000;">					initButton.enabled = false;</span>
<span style="color: #000000;">					killButton.enabled = true;</span>
<span style="color: #000000;">					npi.executable = exec; // assign &quot;exec&quot; to be the executable in our startup info.</span>
<span style="color: #000000;">					var args:Vector.&lt;String&gt;</span></span> = new Vector.<span style="color: #000000;">&lt;String<span style="color: #7400FF;">&gt;</span></span>; // create a vector to hold arguments - don't assign til the end.
					args[0] = &quot;-i&quot;; // python needs this to handle I/O correctly, details to follow.
					args[1] = File.applicationDirectory.resolvePath(&quot;ThreadedSerialInterface.py&quot;).nativePath; // this is the path to our python script, included with the AIR app.
					args[2] = serialPort.text; // this is the serial port, &quot;COM3&quot; etc on pc or &quot;/dev/tty.usbserial-XXXXXX&quot; on mac, etc.
					args[3] = baudRate.text; // this is our baud rate.
&nbsp;
					npi.arguments = args; // assign the arguments to the startup info object.
&nbsp;
&nbsp;
					// add listeners for stdout, stderr and process exit.
					np.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, stdoutHandler);
					np.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, sterrHandler);
					np.addEventListener(NativeProcessExitEvent.EXIT, exitHandler);
&nbsp;
					// start the NativeProcess
					np.start(npi);
				}
			}
&nbsp;
			/**
			 * 	this method passes a string to our native process, which passes it to the microcontroller.
			 */
			private function writeToSerial():void {
				np.standardInput.writeMultiByte(toSerial.text + &quot;\n&quot;, File.systemCharset);
				toSerial.text = &quot;&quot;;
			}
&nbsp;
			/**
			 * 	This method handles output from the NativeProcess. I have some basic parsing logic in here to separate output by thread.
			 */
			private function stdoutHandler(e:ProgressEvent):void {
				var output:String = np.standardOutput.readMultiByte(np.standardOutput.bytesAvailable, File.systemCharset);
				if (output.indexOf(&quot;[R]&quot;) &gt; -1) {
					logOutputFromReaderThread(output.slice(3));
				}
&nbsp;
				if (output.indexOf(&quot;[W]&quot;) &gt; -1) {
					logOutputFromWriterThread(output.slice(3));
				}
&nbsp;
&nbsp;
				trace(&quot;[Output from Python] &quot; + output);
&nbsp;
			}
&nbsp;
			/**
			 * 	utility function to log Reader thread output.
			 */
			private function logOutputFromReaderThread(s:String):void {
				var text:String = s += readerOut.text;
				readerOut.text = text;
			}
&nbsp;
&nbsp;
			/**
			 * 	utility function to log Writer thread output.
			 */
			private function logOutputFromWriterThread(s:String):void {
				var text:String = s += writerOut.text;
				writerOut.text = text;
			}
&nbsp;
			/**
			 * 	This handles STDERR output, right now it's just tracing.
			 */
			private function sterrHandler(e:ProgressEvent):void {
				var output:String = np.standardError.readMultiByte(np.standardError.bytesAvailable, File.systemCharset);
				trace(&quot;[Error!] &quot; + output);
			}
&nbsp;
			/**
			 * 	This fires when the native process is terminated. 
			 */
			private function exitHandler(e:NativeProcessExitEvent):void {
				trace(&quot;Native Process has terminated!&quot;);	
				initButton.enabled = true;
				killButton.enabled = false;
			}
&nbsp;
			/**
			 * 	This forces the NativeProcess to close. You must call this when you quit your program, 
			 *  or else your processes will live on.
			 * 
			 *  Notice that the Application is using this to handle the Closing event - so when you close the app,
			 *  your process is killed.
			 */
			private function killProc():void {
				trace(&quot;Killing native process&quot;);
				np.exit(true);	
			}
&nbsp;
		]]&gt;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
	<span style="color: #000000;"><span style="color: #808080; font-style: italic;">&lt;!-- MXML for layout, self-explanatory I hope --&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextInput</span> x=<span style="color: #ff0000;">&quot;105&quot;</span> y=<span style="color: #ff0000;">&quot;16&quot;</span> width=<span style="color: #ff0000;">&quot;174&quot;</span> id=<span style="color: #ff0000;">&quot;pythonLocation&quot;</span> text=<span style="color: #ff0000;">&quot;C:\\Python26\\python.exe&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;16&quot;</span> text=<span style="color: #ff0000;">&quot;Python Location&quot;</span> height=<span style="color: #ff0000;">&quot;22&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextInput</span> x=<span style="color: #ff0000;">&quot;105&quot;</span> y=<span style="color: #ff0000;">&quot;50&quot;</span> width=<span style="color: #ff0000;">&quot;174&quot;</span> id=<span style="color: #ff0000;">&quot;serialPort&quot;</span> text=<span style="color: #ff0000;">&quot;COM3&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;50&quot;</span> text=<span style="color: #ff0000;">&quot;Serial Port&quot;</span> height=<span style="color: #ff0000;">&quot;22&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextInput</span> x=<span style="color: #ff0000;">&quot;105&quot;</span> y=<span style="color: #ff0000;">&quot;80&quot;</span> width=<span style="color: #ff0000;">&quot;174&quot;</span> id=<span style="color: #ff0000;">&quot;baudRate&quot;</span> text=<span style="color: #ff0000;">&quot;9600&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;80&quot;</span> text=<span style="color: #ff0000;">&quot;Baud Rate&quot;</span> height=<span style="color: #ff0000;">&quot;22&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;initButton&quot;</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;110&quot;</span> label=<span style="color: #ff0000;">&quot;Init&quot;</span> width=<span style="color: #ff0000;">&quot;269&quot;</span> click=<span style="color: #ff0000;">&quot;setup()&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> id=<span style="color: #ff0000;">&quot;killButton&quot;</span> x=<span style="color: #ff0000;">&quot;9&quot;</span> y=<span style="color: #ff0000;">&quot;143&quot;</span> label=<span style="color: #ff0000;">&quot;Kill Process&quot;</span> width=<span style="color: #ff0000;">&quot;269&quot;</span> click=<span style="color: #ff0000;">&quot;killProc()&quot;</span> enabled=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HRule</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;172&quot;</span> width=<span style="color: #ff0000;">&quot;268&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> x=<span style="color: #ff0000;">&quot;11&quot;</span> y=<span style="color: #ff0000;">&quot;189&quot;</span> text=<span style="color: #ff0000;">&quot;Write to Serial:&quot;</span> height=<span style="color: #ff0000;">&quot;20&quot;</span> verticalAlign=<span style="color: #ff0000;">&quot;middle&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextInput</span> x=<span style="color: #ff0000;">&quot;98&quot;</span> y=<span style="color: #ff0000;">&quot;187&quot;</span> width=<span style="color: #ff0000;">&quot;174&quot;</span> id=<span style="color: #ff0000;">&quot;toSerial&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Button</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;217&quot;</span> label=<span style="color: #ff0000;">&quot;Send&quot;</span> width=<span style="color: #ff0000;">&quot;262&quot;</span> enabled=<span style="color: #ff0000;">&quot;{toSerial.text.length}&quot;</span> click=<span style="color: #ff0000;">&quot;writeToSerial()&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:HRule</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;251&quot;</span> width=<span style="color: #ff0000;">&quot;268&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextArea</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;292&quot;</span> width=<span style="color: #ff0000;">&quot;435&quot;</span> height=<span style="color: #ff0000;">&quot;299&quot;</span> id=<span style="color: #ff0000;">&quot;readerOut&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:TextArea</span> x=<span style="color: #ff0000;">&quot;453&quot;</span> y=<span style="color: #ff0000;">&quot;292&quot;</span> width=<span style="color: #ff0000;">&quot;421&quot;</span> height=<span style="color: #ff0000;">&quot;299&quot;</span> id=<span style="color: #ff0000;">&quot;writerOut&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> x=<span style="color: #ff0000;">&quot;10&quot;</span> y=<span style="color: #ff0000;">&quot;272&quot;</span> text=<span style="color: #ff0000;">&quot;Output from Reader Thread:&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:Label</span> x=<span style="color: #ff0000;">&quot;453&quot;</span> y=<span style="color: #ff0000;">&quot;272&quot;</span> text=<span style="color: #ff0000;">&quot;Output from Writer Thread:&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/s:WindowedApplication</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<h2>Results</h2>
<p>So, this ALMOST works. That is to say, it does work but it&#8217;s not quite ideal. I&#8217;m having some issues with input and output into python, and I think they&#8217;re caused by the fact that I don&#8217;t have an actual terminal window. When python is waiting for raw_input(), it outputs a prompt (&#8220;>>>&#8221;). When I print serial anything out, it&#8217;s expecting a console to be able to print to. Because we&#8217;re running this as a process from inside of AIR, there is no actual terminal for interface &#8211; which brings us to the &#8220;-i&#8221; argument I&#8217;m passing in above.</p>
<p>Now, I&#8217;m not a python developer &#8211; I&#8217;ve made that clear, right? &#8211; but from what I can tell, &#8220;-i&#8221; is telling Python to act as through there&#8217;s a terminal even though there isn&#8217;t. It&#8217;s saying &#8220;Trust me, someone is listening even if you can&#8217;t see them.&#8221; Without that argument, the script waits until it exits to output anything. Obviously that&#8217;s no good for our purposes because we want this thing to be constantly running, listening for output. So, the &#8220;-i&#8221; argument helps us out a lot &#8211; but there&#8217;s one final facet of this puzzle that&#8217;s throwing me for a loop.</p>
<p>When I run this application, it works great &#8211; except that only <em>every other</em> command I try to write to the serial port goes through. Boot it up, run it and see. Hit the init button, wait for the &#8220;Initialized, waiting for input.&#8221; response from the Reader thread, type a number into your &#8220;Write to Serial&#8221; box and hit submit. The Writer thread should respond with &#8220;You input 1&#8243;, and if your arduino unit is doing anything in response to serial input it should happen. Now type another number and hit submit &#8211; nothing happens. Type a third and hit submit, everything is fine.</p>
<p>I was confused until I ran it in debugger mode, and realized that I&#8217;m getting output from STDERR &#8211; specifically, every other time I submit serial input it returns &#8220;>>>&#8221;, a prompt, via the Error stream. I have no idea why.</p>
<p><strong><span style="color:#ff0000">See comment thread for more information &#8211; this problem was solved! Specifically, I replaced the &#8220;-i&#8221; command line option with a &#8220;-u&#8221;.</span></strong></p>
<h2>Conclusion</h2>
<p>This is obviously a work in progress, and I&#8217;m going to update this as I go. I&#8217;m writing this post today, perhaps a bit prematurely, because there&#8217;s been some noise on Twitter lately about other folks trying to do similar stuff and I thought I&#8217;d throw this out there in case it helps anyone. If you have any feedback, I&#8217;d love it in the comments below &#8211; especially any feedback on the python section. I&#8217;m not 100% understanding every part of it. </p>
<p>I think as Flash developers many of us tend to live in very small boxes (albeit boxes with lots of cool stuff inside). This project has helped me to realize just how little I understand about programming in general, and in that regard it&#8217;s been very helpful. I hope to continue learning as I go. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://mykola.bilokonsky.net/2010/11/adobe-air-nativeprocess-and-python-introduction/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

