<?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>Create Blog</title>
	<atom:link href="http://create.tpsitulsa.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://create.tpsitulsa.com/blog</link>
	<description>the create framework blog</description>
	<lastBuildDate>Sat, 30 Jan 2010 20:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>For the Love of a Deity, Please Prove Me Wrong: IE Alpha</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/29/ie-alpha/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/29/ie-alpha/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 19:06:38 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Please Prove Me Wrong]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=317</guid>
		<description><![CDATA[Please no. Just, no. I mean, honestly, how far can IE sink? I really hope I am wrong here, but&#8230; If you check my test case, you may notice that it looks different in Internet Explorer than it does in sane browsers. What is the culprit? Simple: filter:alpha. Supposedly, making elements somewhat transparent is possible [...]]]></description>
			<content:encoded><![CDATA[<p>Please no. Just, no. I mean, honestly, how far can IE sink? I really hope I am wrong here, but&#8230;</p>

<p>If you <a href="http://create.tpsitulsa.com/static/tests/test-opacity.html">check my test case</a>, you may notice that it looks different in Internet Explorer than it does in sane browsers. What is the culprit?</p>

<p>Simple: filter:alpha.</p>

<p>Supposedly, making elements somewhat transparent is possible in <a href = "http://www.microsoft.com/windows/internet-explorer/default.aspx" rel="nofollow">everyone&#8217;s favorite browser</a>. Well, it is, but you have to use Microsoft&#8217;s proprietary &#8220;filter&#8221; CSS property.</p>

<p>Okay, fine, at least it works. Except in one case, from what I can tell: it will not apply transparency to absolutely positioned child elements of the element with the &#8220;filter&#8221; property set.</p>

<p>Why is this an issue? Let&#8217;s take the simple example of a button control (specifically, SproutCore&#8217;s SC.Button, but it should apply to other button controls as well): you have a left part (with nice rounded corners), a center part (that can be stretched), and a right part (again with the rounded corners). One part—for instance, the right part—is either positioned relatively or is the parent element itself, and simply has a background-position:right. The left and center parts, however, are positioned absolutely; something like <tt>left: 10; right: 10</tt> for the center part; <tt>right:0; width:10;</tt> for the right part (I just made these numbers up! They aren&#8217;t real!)</p>

<p>So, what happens if you try to fade the button out in Internet Explorer? Well, it looks really odd. In the above example, the right part fades out, but the left and center do not. They just stay as they were.</p>

<p>There is only one workaround that I know of: seting <tt>filter:alpha</tt> on every element. There are a few problems with this:</p>

<ul>
<li>Performance. Each setting of &#8220;opacity&#8221; will have to recursively loop through all child elements.</li>
<li>Performance * Animation fps. Yep, animating opacity in IE is plain screwed.</li>
<li>Pure messiness.</li>
<li>Long-term issues: what about when (if) Microsoft fixes the issue?</li>
</ul>

<p>Don&#8217;t you just love Internet Explorer?</p>

<p>I keep thinking I must be wrong, and there must be some nice little workaround, CSS hack, or even the simple explanation of <em>me just being plain wrong</em> (I actually hope I am).</p>

<p>Anyone know?</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/29/ie-alpha/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SproutCore Animation: SC.Animatable</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/21/sc-animatable/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/21/sc-animatable/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 04:52:34 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=284</guid>
		<description><![CDATA[In real life, things don&#8217;t just suddenly disappear from one place and appear in another. This isn&#8217;t Harry Potter1. We can&#8217;t apparate. But chances are, you know that, and don&#8217;t need to be convinced of the wonder of animation. So, instead, I will tell you about the beginnings of SproutCore&#8217;s support for animation. SproutCore&#8217;s animation [...]]]></description>
			<content:encoded><![CDATA[<p>In real life, things don&#8217;t just suddenly disappear from one place and appear in another. This isn&#8217;t Harry Potter<sup id="fnref:hp"><a href="#fn:hp" rel="footnote">1</a></sup>. We can&#8217;t apparate.</p>

<p>But chances are, you know that, and don&#8217;t need to be convinced of the wonder of animation. So, instead, I will tell you about the beginnings of SproutCore&#8217;s support for animation.</p>

<p>SproutCore&#8217;s animation support currently takes the form of a mixin for views: SC.Animatable. Here are some of the features:</p>

<ul>
<li><strong>Relatively fast.</strong> It can animate 1000 or so views with reasonable performance on a fast machine.
Oddly, the biggest performance hurdle may now be the rendering engine (unless you are on 
IE—the slow script speed will kill you first).</li>
<li><strong>Uses CSS transitions where possible.</strong> Oddly, CSS transitions do not appear to be significantly faster<sup id="fnref:performance"><a href="#fn:performance" rel="footnote">2</a></sup> than
JavaScript transitions (again, indicating the rendering engine could now be the bottleneck).</li>
<li><strong>Support for Layout.</strong> This almost goes without saying, as what animation framework would be worth anything if it didn&#8217;t 
allow you to animate the position of views?</li>
<li><strong>Support for Opacity.</strong> So you can fade things in and out.</li>
<li><strong>Support for display:none.</strong> You can add transitions to the display property, and they will simply be ignored (display gets applied immediately)
<em>except</em> in the special case of display:none, which will only apply after the specified duration. See <a href="#displaynone">display:none notes</a></li>
<li><strong><a href="http://create.tpsitulsa.com/blog/2009/12/10/sc-animate-timing/">Support for Timing Curves</a>.</strong> You can specify curves to adjust the speed of the animation, and have them work in both JavaScript transitions and CSS transitions, or optionally, CSS transitions only.</li>
</ul>

<h3>Animating with SC.Animatable</h3>

<p>It is quite simple. Here are the steps:</p>

<ul>
<li>Add &#8220;sproutcore/animation&#8221; to the required frameworks array in your Buildfile (in quotes and all).</li>
<li>Mix in SC.Animatable to your view.</li>
<li>Add transitions.</li>
<li>Change layout, opacity, or display.</li>
</ul>

<p>So, how about some code? First, let&#8217;s take a look at a Buildfile:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># The Contacts application Buildfile</span>
config <span style="color:#ff3333; font-weight:bold;">:contacts</span>, <span style="color:#ff3333; font-weight:bold;">:required</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:sproutcore</span>, :<span style="color:#996600;">&quot;sproutcore/animation&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:forms</span>, <span style="color:#ff3333; font-weight:bold;">:pomona</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>


<p>Now, how about a view?</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">myView<span style="color: #339933;">:</span> SC.<span style="color: #660066;">View</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span>SC.<span style="color: #660066;">Animatable</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// mix it in</span>
  transitions<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// and add transitions</span>
    left<span style="color: #339933;">:</span> .25<span style="color: #339933;">,</span>
    top<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> .25<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    width<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> .25<span style="color: #339933;">,</span> timing<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Animatable</span>.<span style="color: #660066;">TRANSITION_EASE_IN_OUT</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// with timing curve</span>
    height<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> .5<span style="color: #339933;">,</span> timing<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.58</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// with custom timing curve</span>
    opacity<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> .5<span style="color: #339933;">,</span> timing<span style="color: #339933;">:</span> SC.<span style="color: #660066;">Animatable</span>.<span style="color: #660066;">TRANSITION_CSS_EASE_IN_OUT</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// CSS-transition-only timing function (JavaScript gets linear)</span>
    display<span style="color: #339933;">:</span> .75 <span style="color: #006600; font-style: italic;">// a bit longer than opacity </span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>


<p>And finally, to animate!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">myView.<span style="color: #660066;">adjust</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">250</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myView.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;layout&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">250</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">340</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// style manipulation: a feature of SC.Animatable</span>
myView.<span style="color: #660066;">adjust</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;opacity&quot;</span><span style="color: #339933;">,</span> .5<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myView.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;style&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> .7<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myView.<span style="color: #660066;">adjust</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;none&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// applies after .75 seconds</span>
myView.<span style="color: #660066;">adjust</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;display&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;block&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// applies immediately</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// disable animation temporarily to change something directly</span>
myView.<span style="color: #660066;">disableAnimation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myView.<span style="color: #660066;">adjust</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;opacity&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">updateStyle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// makes invisible and applies immediately.</span>
myView.<span style="color: #660066;">enableAnimation</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myView.<span style="color: #660066;">adjust</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;opacity&quot;</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: #006600; font-style: italic;">// fades in to visibility (applying lazily).</span></pre></div></div>


<p>Pretty simple!</p>

<h3 id="displaynone">display:none notes</h3>

<p>When animating the display: none property, you should add a bit of extra time to its transition.</p>

<p>If everything was animated using JavaScript, there would be no issue. However, because SC.Animatable takes advantage of CSS transitions where possible, and CSS transitions don&#8217;t necessarily begin immediately, if you don&#8217;t add extra time to display&#8217;s transition it could set display:none before the other transitions end (in effect, cutting them short). In the most usual case—an opacity transition—the view will start to fade out and then suddenly disappear. Again, the easiest way to fix this is by simply adding a bit of time to the transition for the display property.</p>

<h3>The Problem with Callbacks</h3>

<p>Currently, SC.Animatable does not have callbacks for animation completion. In fact, unless the view is being animated using JavaScript (non-WebKit browsers and Firefox earlier than 3.6, I believe), it is even impossible to tell if a view <em>is</em> animating.</p>

<p>For JavaScript-based transitions, this should not be a terribly difficult feature to add <em>in general</em>; there is a question of, since animations are started and stopped automatically instead of explicitly by the developer, how those callbacks would be set: should it be one per property? One per the whole view?</p>

<p>For CSS transitions, however, this is trickier. There is an event fired in WebKit&#8217;s implementation, but I am unable to find any documentation on Mozilla&#8217;s support for it (or, for that matter, lack thereof).</p>

<p>Even if support was present, it may or may not be tricky to reconcile that with the actual animation library: what if one transition ends, but a new one starts immediately after? Will the event fire after the new one begins, tricking others into thinking it has already fired? It may be more reliable to &#8220;guesstimate,&#8221; and just set a timer for a callback to go off somewhere shortly after the target time (much like the way the &#8220;display&#8221; transition is implemented).</p>

<p><a href="http://create.tpsitulsa.com/static/contacts/"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/12/Screen-shot-2009-12-21-at-10.53.52-PM-300x206.png" alt="" title="Contacts Demo App" width="300" height="206" class="alignleft size-medium wp-image-312" /></a></p>

<h3>Demo</h3>

<p>It is not &#8220;officially&#8221; released by any means—it still needs fixes, including at least one animation-related one—but a fancy demo of the animation (used in FormView, another project I am working on) <a href="http://create.tpsitulsa.com/static/contacts/">can be found here</a>.</p>

<p>The code is all online too <a href="http://github.com/ialexi/Contacts/">at GitHub.</a> I am slowly making it into a full sample app for SproutCore that demonstrates animation, FormView, and Comet—and hopefully multiple back-ends (I&#8217;m working on a wacky Python one right now, and I&#8217;d like to make a node.js-based one eventually, too).</p>

<div style="clear:both"></div>

<div class="footnotes">
<hr />
<ol>

<li id="fn:hp">
<p>I had to slip in some HP reference.<sup id="fnref:footnotes"><a href="#fn:footnotes" rel="footnote">3</a></sup>&#160;<a href="#fnref:hp" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:performance">
<p>It is tricky to tell, because I cannot measure the FPS of transitions objectively. Actually, the JavaScript transitions looked <em>faster</em> than the CSS transitions.&#160;<a href="#fnref:performance" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:footnotes">
<p>I love footnotes too much. In some papers, I have filled almost half the page with footnotes. The thing about footnotes is that they allow you to add extra information (sometimes a lot of extra information) while not badly interrupting the flow of the original piece.&#160;<a href="#fnref:footnotes" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/21/sc-animatable/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Making SproutCore Objects Show in Chromium&#8217;s Heap Profiler</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/15/sc-objects-heap-profiler/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/15/sc-objects-heap-profiler/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 16:38:08 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[V8]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[chromium]]></category>
		<category><![CDATA[google chrome]]></category>
		<category><![CDATA[memory leaks]]></category>
		<category><![CDATA[profiler]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=277</guid>
		<description><![CDATA[It&#8217;s a hack. If Chromium+v8 would just support displayName, it could all be nice and clean and part of SproutCore (please go tell the Chromium folks how helpful this would be). As it is, this hack could have some SEVERE side-effects, but chances are, if you are trying to debug a memory leak, you won&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a hack. If Chromium+v8 would <a href="http://code.google.com/p/chromium/issues/detail?id=17356&#038;q=area%3DDevTools&#038;colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner%20Mstone%20OS">just support displayName</a>, it could all be nice and clean and part of SproutCore (please go tell the Chromium folks how helpful this would be).</p>

<p>As it is, this hack could have some <em>SEVERE</em> side-effects, but chances are, if you are trying to debug a memory leak, you won&#8217;t care about these side-effects because everything else will be driving you MAD (and, you&#8217;ll probably be able to find these side-effects much more easily than the leak itself).</p>

<p>It is very simple; here is how to do it for ListItemView:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">SC.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>SC.<span style="color: #660066;">ListItemView</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> SC._ListItemView<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
SC._ListItemView <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>base_type<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  base_type.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// for extra safety (should get around most potential side-effects):</span>
SC.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>SC._ListItemView<span style="color: #339933;">,</span> SC.<span style="color: #660066;">ListItemView</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
SC._ListItemView.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> SC.<span style="color: #660066;">ListItemView</span>.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span></pre></div></div>


<p>Now, ListItemViews will show up as SC._ListItemView in the heap profile. Hooray!</p>

<p>P.S. This revealed for me that the objects leaking were not SC.ListItemViews. I still have no idea what is leaking, and am giving up for now—but at least I&#8217;ll have better tools when I come back to it. :)</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/15/sc-objects-heap-profiler/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SproutCore, Todos, Django, and Comet in &lt; 100 Lines of Code</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/11/sc-todos-django-comet/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/11/sc-todos-django-comet/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 16:55:07 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=266</guid>
		<description><![CDATA[This is a continuation of the Django Todos Back-End Tutorial that adds Comet using Roots. Warning: This is not tested by anyone other than me at the moment, and might prove somewhat bumpy. Feedback is very welcome! Here is a high-level introduction of how Roots works is available here. Very little needs to be done [...]]]></description>
			<content:encoded><![CDATA[<p>This is a continuation of the <a href="https://sproutcore.pbworks.com/Todos+06-Building+with+Django">Django Todos Back-End Tutorial</a> that adds Comet using <a href="http://github.com/ialexi/dobby">Roots</a>.</p>

<p><strong>Warning: This is not tested by anyone other than me at the moment, and might prove somewhat bumpy. Feedback is very welcome!</strong></p>

<p>Here is a high-level introduction of how Roots works <a href="http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/">is available here.</a></p>

<p>Very little needs to be done to add Comet. There are three main steps on the server side:</p>

<ol>
    <li>Get Roots and Cornelius.</li>
    <li>Add views to allow clients to subscribe/connect to events.</li>
    <li>Trigger events.</li>
</ol>

<p>On the client side, there are also three major steps:</p>

<ol>
    <li>Get+set up Pomona.</li>
    <li>Subscribe to events.</li>
    <li>Receive events.</li>
</ol>

<h3>First Step</h3>

<p>You will need at least three terminal windows—one for each server. You might as well open them first.</p>

<p>You will have three directories to work out of:</p>

<ul>
    <li>Roots (or Dobby)—not much to do here :)</li>
    <li>SproutCore Project</li>
    <li>Django Project</li>
</ul>

<p>Tip: if you want to be able to open only one TextMate window (or whatever IDE you use), you can place the Django project (and, even, Roots) in a subdirectory of your SproutCore project.</p>

<h3>Server-Side</h3>

<h4>Getting Roots and Cornelius</h4>

<p>First, let&#8217;s get Roots. In the Roots terminal, navigate to the folder you&#8217;d like to keep it, and:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ git clone git://github.com/ialexi/dobby.git</pre></div></div>


<p>And now, you can start it (assuming you have the prerequisites):</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ cd dobby
$ python dobby.py
Instating Dolores High Inquisitor of Hogwarts</pre></div></div>


<p>There may be some deprecation warnings due to issues with Twisted, but they don&#8217;t cause problems. (Roots should be ported to node.js soon, and <em>node.js</em> doesn&#8217;t have these issues).</p>

<p>Now, get Cornelius. In the Django terminal, navigate to your Django project directory. Then run:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ git clone git://github.com/ialexi/cornelius.git</pre></div></div>


<p>Next, you need to add Cornelius to your Django project as an application (making it really easy to import). To do this, edit settings.py and add &#8220;cornelius&#8221; in the INSTALLED_APPS setting:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">	<span style="color: #483d8b;">'django.contrib.auth'</span>,
	<span style="color: #483d8b;">'django.contrib.contenttypes'</span>,
	<span style="color: #483d8b;">'django.contrib.sessions'</span>,
	<span style="color: #483d8b;">'django.contrib.sites'</span>,
	<span style="color: #483d8b;">'todos'</span>,
	<span style="color: #483d8b;">'cornelius'</span> <span style="color: #808080; font-style: italic;"># &lt;- SEE! :D</span></pre></div></div>


<h4>Making Views to Make Connections</h4>

<p>We can&#8217;t trust the client. As such, we do not allow the client to connect itself to any path it wants. Instead, it has to go through our Django web app. Ironically, since we&#8217;re just doing a simple application, we <em>are</em> in fact, going to allow the client to connect to whatever it wants (I&#8217;ll show how to change this, though).</p>

<p>First, you need an updated urls.py:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span>.<span style="color: black;">urls</span>.<span style="color: black;">defaults</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
urlpatterns = patterns<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>,
	<span style="color: #808080; font-style: italic;"># You don't have to do it this way, but in this example, we match</span>
	<span style="color: #808080; font-style: italic;"># optional ending slashes.</span>
	<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^tasks/?$'</span>, <span style="color: #483d8b;">&quot;todos.views.tasks&quot;</span><span style="color: black;">&#41;</span>,
	<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^task/(?P&lt;taskid&gt;[0-9]+)$'</span>, <span style="color: #483d8b;">&quot;todos.views.task&quot;</span><span style="color: black;">&#41;</span>,
&nbsp;
	<span style="color: #808080; font-style: italic;"># See here:</span>
	<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^tasks/connect/(?P&lt;uid&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>]+)$'</span>, <span style="color: #483d8b;">&quot;todos.views.connect&quot;</span><span style="color: black;">&#41;</span>,
	<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^tasks/disconnect/(?P&lt;uid&gt;[^<span style="color: #000099; font-weight: bold;">\s</span>]+)$'</span>, <span style="color: #483d8b;">&quot;todos.views.disconnect&quot;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#41;</span></pre></div></div>


<p>This makes tasks/connect/(CLIENT_ID) call our &#8220;connect&#8221; view, and tasks/disconnect/(CLIENT_ID) call our &#8220;disconnect&#8221; view.</p>

<p>Now, for the views themselves:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Import cornelius's Dudley</span>
<span style="color: #ff7700;font-weight:bold;">from</span> cornelius <span style="color: #ff7700;font-weight:bold;">import</span> dudley
&nbsp;
<span style="color: #808080; font-style: italic;"># Connect</span>
<span style="color: #ff7700;font-weight:bold;">def</span> connect<span style="color: black;">&#40;</span>request, uid<span style="color: black;">&#41;</span>:
	paths = json.<span style="color: black;">loads</span><span style="color: black;">&#40;</span>request.<span style="color: black;">raw_post_data</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Pomona sends the list of paths to connect to in a JSON array.</span>
	<span style="color: #808080; font-style: italic;"># note: paths is just a plain list of strings; you can loop through them and filter for security.</span>
	dudley.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>uid, paths<span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;"># Dudley takes a client id and a list of paths. That's it.</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> disconnect<span style="color: black;">&#40;</span>request, uid<span style="color: black;">&#41;</span>:
	paths = json.<span style="color: black;">loads</span><span style="color: black;">&#40;</span>request.<span style="color: black;">raw_post_data</span><span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;"># note: ditto regarding paths being a list of strings.</span>
	dudley.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>uid, paths<span style="color: black;">&#41;</span></pre></div></div>


<h4>Triggering Events</h4>

<p>Django has nice post-save and post-delete events we can use to trigger events (&#8220;updates&#8221; in Roots-speak).</p>

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


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Comet alerters</span>
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span>.<span style="color: black;">models</span>.<span style="color: black;">signals</span> <span style="color: #ff7700;font-weight:bold;">import</span> post_save, post_delete
<span style="color: #ff7700;font-weight:bold;">def</span> task_saved<span style="color: black;">&#40;</span>sender, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">try</span>:
		instance = kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;instance&quot;</span><span style="color: black;">&#93;</span>
		dudley.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tasks&quot;</span>, json.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>task_to_raw<span style="color: black;">&#40;</span>instance<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">except</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> task_destroyed<span style="color: black;">&#40;</span>sender, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">try</span>:
		instance = kwargs<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;instance&quot;</span><span style="color: black;">&#93;</span>
		data = task_to_raw<span style="color: black;">&#40;</span>instance<span style="color: black;">&#41;</span>
		data<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;DELETE&quot;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">True</span>
		dudley.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tasks&quot;</span>, json.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">except</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span>
post_save.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>task_saved, sender=Task<span style="color: black;">&#41;</span>
post_delete.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>task_destroyed, sender=Task<span style="color: black;">&#41;</span></pre></div></div>


<p>Note how we once again use task_to_raw.</p>

<p><strong>And now, the server-side is done!</strong></p>

<p>Of course, start it with:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ python manage.py runserver</pre></div></div>


<h3>Client Side</h3>

<h4>Getting and Setting Up Pomona</h4>

<p>In your SproutCore terminal, navigate to your SproutCore project. Then, get Pomona and put it in the &#8220;frameworks&#8221; folder:
<pre>$ git clone git://github.com/ialexi/pomona.git frameworks/pomona</pre></p>

<p>Next, just like you add the project in Django, you need to add the framework to your SproutCore project. To accomplish this, you edit the &#8220;config&#8221; line of &#8220;Buildfile&#8221; to look like this:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config <span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:required</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:sproutcore</span>, <span style="color:#ff3333; font-weight:bold;">:pomona</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>


<p>You changed it from the solitary :sproutcore, to the array [:sproutcore, :pomona].</p>

<p>Next, you need to proxy Roots&#8217; long-polling server. Just add this line at the end of Buildfile after the other two proxy lines:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">proxy <span style="color:#996600;">&quot;/comet/&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;localhost:8008&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:url</span><span style="color:#006600; font-weight:bold;">=&gt;</span><span style="color:#996600;">&quot;/&quot;</span></pre></div></div>


<h4>Subscribing to Events</h4>

<p>There are two things that have to happen to subscribe to events: create and configure an instance of the long-poller, and call connect().</p>

<p>The easiest place to do this is in an init function in our data source. Just place this code in the DataSource definition (preferably at the top).</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// DataSource has no init right now, but what if it adds one?</span>
    <span style="color: #006600; font-style: italic;">// so, call super function first.</span>
    sc_super<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #006600; font-style: italic;">// needs proxy /comet/ to dobby</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firenze</span> <span style="color: #339933;">=</span> Pomona.<span style="color: #660066;">Firenze</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>    <span style="color: #006600; font-style: italic;">// Firenze is the name of the long-poll system.</span>
      connectUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/tasks/connect/%@&quot;</span><span style="color: #339933;">,</span>        <span style="color: #006600; font-style: italic;">// %@ is replaced with the client id</span>
      disconnectUrl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/tasks/disconnect/%@&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// tell it to call this.taskReceived when a message is received on path &quot;tasks&quot;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">firenze</span>.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;tasks&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;taskReceived&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>


<p>The connectUrl/disconnectUrl parameters tell Pomona what URLs to send connect and disconnect requests to. All it does is send a JSON array of path (event) names in a JSON array). It will automatically re-send these messages if the connection with the long-polling server is interrupted and a new connection (with a new id) is created.</p>

<p>All that&#8217;s left now is to receive messages!</p>

<h4>Receiving Messages</h4>

<p>Receiving messages is simple: we just implement the &#8220;taskReceived&#8221; function we told it about earlier.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  taskReceived<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>path<span style="color: #339933;">,</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>message.<span style="color: #660066;">trim</span><span style="color: #009900;">&#40;</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: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// the first time, we receive &quot;&quot; to confirm connection</span>
    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// we sent a JSON-encoded message from Python</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>data.<span style="color: #000066; font-weight: bold;">DELETE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// We set DELETE if the record needed deleting</span>
      Todos.<span style="color: #660066;">store</span>.<span style="color: #660066;">pushDestroy</span><span style="color: #009900;">&#40;</span>Todos.<span style="color: #660066;">Task</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">guid</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #006600; font-style: italic;">// otherwise, we should just re-load that record.</span>
      Todos.<span style="color: #660066;">store</span>.<span style="color: #660066;">loadRecords</span><span style="color: #009900;">&#40;</span>Todos.<span style="color: #660066;">Task</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>data<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>


<h3>That&#8217;s All!</h3>

<p>Start sc-server, open two browsers, both pointing at <a href="http://localhost:4020/todos">http://localhost:4020/todos</a>, and hopefully, you&#8217;ll have Comet.</p>

<p>There may be some bumps—if anyone is willing to test and offer feedback, please do so!</p>

<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/11/sc-todos-django-comet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SproutCore Animation Mixin Timing Curves (and BUG FIXES!!!)</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/10/sc-animate-timing/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/10/sc-animate-timing/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 01:08:15 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=258</guid>
		<description><![CDATA[First, the comparatively boring but very good news: several bugs which made CSS transitions not always work too well have been fixed! There were a lot of little errors—including a really odd one in Safari that caused transitions to be ignored if they were initiated too soon after a style.display change—that were causing some rather [...]]]></description>
			<content:encoded><![CDATA[<p>First, the comparatively boring but very good news: several bugs which made CSS transitions not always work too well have been fixed! There were a lot of little errors—including a <em>really</em> odd one in Safari that caused transitions to be ignored if they were initiated too soon after a style.display change—that were causing some rather annoying issues, and they have now been resolved.</p>

<p>Now, the comparatively exciting:
<strong>The <a href="http://github.com/ialexi/animate/">SproutCore Animation mixin</a> now supports timing curves!
</strong></p>

<p>There are two ways to set the timing curve to use: globally and locally. The global timing curve (null by default) is used if there is no local timing curve—in essence, the global timing curve is the default timing curve (in fact, it is named defaultTimingFunction).</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">aView<span style="color: #339933;">:</span> SC.<span style="color: #660066;">LabelView</span>.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span>Animate.<span style="color: #660066;">Animatable</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    transitions<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        left<span style="color: #339933;">:</span> .25<span style="color: #339933;">,</span>
        top<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> .25<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        width<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> .25<span style="color: #339933;">,</span> timing<span style="color: #339933;">:</span> Animate.<span style="color: #660066;">TIMING_EASE_IN_OUT</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// with timing curve</span>
        height<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> duration<span style="color: #339933;">:</span> .5<span style="color: #339933;">,</span> timing<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.58</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// with custom timing curve</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>


<p>Timing curves are cubic beziér curves, as used in <a href="http://www.w3.org/TR/2009/WD-css3-transitions-20091201/">CSS Transitions</a>. They can be used as CSS-only, or, if you are willing to take a bit of a JavaScript performance hit, in both CSS and JavaScript. In fact, the JavaScript variety&#8217;s code is based on <a href="http://trac.webkit.org/browser/trunk/WebCore/platform/graphics/UnitBezier.h">WebKit&#8217;s actual code</a> for handling cubic beziér curves.</p>

<p>The pre-defined timing curves are identical to the CSS timing functions:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// CSS-only</span>
TRANSITION_NONE<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;linear&quot;</span><span style="color: #339933;">,</span>
TRANSITION_CSS_EASE<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ease&quot;</span><span style="color: #339933;">,</span>
TRANSITION_CSS_EASE_IN<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ease-in&quot;</span><span style="color: #339933;">,</span>
TRANSITION_CSS_EASE_OUT<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ease-out&quot;</span><span style="color: #339933;">,</span>
TRANSITION_CSS_EASE_OUT<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;ease-in-out&quot;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// JavaScript-enabled</span>
TRANSITION_EASE<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0.25</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.25</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
TRANSITION_LINEAR<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0.0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
TRANSITION_EASE_IN<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0.42</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
TRANSITION_EASE_OUT<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.58</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
TRANSITION_EASE_IN_OUT<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0.42</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0.58</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1.0</span><span style="color: #009900;">&#93;</span></pre></div></div>


<p>The implementation is somewhat simple; if the timing function is a string, it is set directly into the transition-timing-function CSS property—and JavaScript animations ignore it (and thus work linearly). Otherwise, a cubic-bezier() is inserted into the CSS, <em>and</em> the JavaScript implementation is set up.</p>

<p>The calculations for the cubic beziér curves involve loops, and must be calculated <em>every</em> frame—they have a significant potential to slow things down (it may be wise to have a pre-calculated option as well at some point to help alleviate this). As such, you may not want to use the JavaScript variety <em>unless</em> you are animating relatively few elements.</p>

<p>I don&#8217;t know if anyone is using the Animate mixin (aside from me) but if anyone is, I hope they enjoy this new feature!</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/10/sc-animate-timing/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Roots Deliver Nutrients</title>
		<link>http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 21:14:06 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Comet]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=245</guid>
		<description><![CDATA[Push is complex. I tried to figure it out with ActiveMQ, Orbited, and friends awhile back, but it just seemed too complicated. It made me feel like I was not very smart—obviously, some people understand this thing, but I don&#8217;t. I am a bit too prideful, and do not like feeling unintelligent. But I need [...]]]></description>
			<content:encoded><![CDATA[<p>Push is complex. I tried to figure it out with ActiveMQ, Orbited, and friends awhile back, but it just seemed too complicated. It made me feel like I was not very smart—obviously, some people understand this thing, but I don&#8217;t. I am a bit too prideful, and do not like feeling unintelligent.</p>

<p>But I need messaging.</p>

<p>I don&#8217;t care about queuing or persistence. For web applications (what I work on) messages should be delivered as quickly as possible. Any &#8220;queues&#8221; can be in-memory, and any message should not remain in a queue for more than a minute—if the client was disconnected for any duration, it would likely be better off re-downloading that receiving incremental updates.</p>

<p>So, whatever these <em>systems</em> do, they seem too complicated (I could be wrong—as I said, I have trouble understanding them).</p>

<p>My requirements:</p>

<ul>
    <li><strong>Normal Authentication</strong>. Clients should go through the &#8220;normal&#8221; web app to subscribe to events.</li>
    <li><strong>Easy</strong>. It should be very simple and easy to understand.</li>
    <li><strong>Quick</strong>. I should be able to add Comet to an app in ten to fifteen minutes, maximum.</li>
</ul>

<p>I found out that it is not only possible to write messaging servers on one&#8217;s own—it is relatively easy. So I wrote one.</p>

<p>I&#8217;ll stop the backstory here, though there is plenty more—tell me if you want any of the boring details about Dolores and the rest.</p>

<h3>Roots</h3>

<p>Roots—codenamed Dobby after the Harry Potter character—is a messaging server that has a built-in long-polling server (among several other things).</p>

<p>Roots is made up of many small message-passing pieces. They plug together, just like Lego pieces. Each piece is simple, and the connection between the pieces are even simpler: a single function, called &#8220;update.&#8221;</p>

<p>If you are interested, I&#8217;ll go over that more thoroughly in &#8220;Implementation.&#8221;</p>

<h3>How Roots is Used</h3>

<p><strong>Above All, Roots is Easy.</strong></p>

<p>It takes me about fifteen minutes to use Roots to add push to a SproutCore application (starting with a fresh clone of Roots—which, by the way, does not need any configuration for testing). It <em>should</em> only take me five minutes, but I am often a bit slow of a coder.</p>

<p><strong>There is a brief semi-tutorial <a href="http://github.com/ialexi/dobby">on GitHub</a>.</strong> The following is based on that tutorial, but has some extra background information.</p>

<p>So, a bird&#8217;s-eye view: unless you implement the server-side <em>in</em> Roots itself, you will have three components in any application: <strong>back-end</strong> (Rails, Django, etc.), <strong>front-end</strong> (SproutCore), and Roots.</p>

<ul>
    <li>Client—such as Pomona, the SproutCore framework for Roots) connects to a Roots server.</li>
    <li>Roots sends the client (Pomona) an id.</li>
    <li>
        The client calls predefined URL to connect to events.
        <ul>
            <li>The URLs are part of the back-end (Django, etc.)</li>
            <li>The ID is sent with these requests.</li>
            <li>Clients (Pomona) usually will keep track of what server-side events you are listening to and can reconnect automatically if the Dobby connection is interrupted—you just provide the client with the URLs and the events to listen to.</li>
        </ul>
    </li>
    <li>Back-end tells Roots to connect client to events—and can perform any authorization it wishes.</li>
    <li>Back-end sends updates to Roots.</li>
    <li>Client receives notifications—everyone&#8217;s Happy!!!</li>
</ul>

<h3>What You Do (for SproutCore)</h3>

<p>You don&#8217;t have to do much:</p>

<ol>
    <li>Add <a href="http://github.com/ialexi/pomona">Pomona</a> to your SproutCore project.</li>
    <li>Add a Roots connector to your server-side project:
        <ul>
        <li><a href="http://github.com/ialexi/cornelius">Cornelius for Python</a></li>
        <li>Others as I add them (I plan Ruby and PHP)</li>
        <li>Implement your own (the Python version of the better protocol—Dudley—is 45 lines counting whitespace and comments).</li>
        </ul>
    </li>
    <li>&#8220;Update&#8221; Roots (call update(path, message) from your back-end—Django has handy model post-save signals you can use).</li>
    <li>Add some URLs to your back-end that connect clients.
        <ul>
        <li>URLs accept client ID in them (/my/connect/AN_ID_HERE or /my/connect.src?id=AN_ID_HERE)</li>
        <li>URLs should accept POST of a simple JSON array of path/event names.</li>
        <li>For each path/event/whatever you want to call it, call connect(id, path) on your Roots connector.</li>
    </li>
    <li>Initialize Pomona and connect to Roots in your SproutCore Data Source.</li>
    <li>Accept incoming data!</li>
</ol>

<p>A more detailed tutorial is <a href="http://github.com/ialexi/dobby">here on GitHub</a> with the rest of Dobby (Roots).</p>

<p><del datetime="2009-12-16T22:53:44+00:00"><strong>Open Question</strong>: Would anyone like a tutorial on setting up a Django back-end for the SproutCore <a href="http://wiki.sproutcore.com/Todos%C2%A0Intro">Todos</a> tutorial, with an extra step of adding Comet at the end?</del> <a href="http://create.tpsitulsa.com/blog/2009/12/11/sc-todos-django-comet/">Done!</a></p>

<h4>Implementation</h4>

<p>Roots/Dobby is currently written in Python. I am working on a rewrite in <a href="http://nodejs.org/">node.js</a>, because node.js is <em>super cool</em>—and, secondarily, it means that some of the same code used in the server (for instance, the message dispatch code) could be used on the client.</p>

<p>Roots has many small parts that hook together in a very simple way. At Roots&#8217;s core is Thestral, a simple interface that requires implementors to do two things: have an id and implement a single method: update(sender, path, message)—where sender is an originator, path is an event name, and message is some data to send.</p>

<p>Here are some of the components:</p>

<ul>
<li><strong>Dolores</strong>: This is the main controller. It assigns IDs to all Thestral instances. It itself is a Thestral instance, and sends its updates to &#8220;delegates&#8221;.</li>
<li><strong>Pig</strong>: An &#8220;Owl,&#8221; or, really, a dispatcher; it knows who receives what message. It is often one of Dolores&#8217;s delegates. Specially-formed messages, tell it to connect certain Thestral ids with certain events—for instance, the event &#8220;::connect&#8221; with message &#8220;12345->my/data&#8221; would connect Thestral &#8220;12345&#8243; to &#8220;my/data&#8221;.</li>
<li><strong>Imperio</strong>. A somewhat pathetic text-based protocol, with accompanying receiver server (a bit like Dudley, which should be used instead). In addition to reading from a server, Imperio supports writing to anything with a write() method, and as such, is used for logging as a delegate of Dolores.</li>
<li><strong>Dudley</strong>. Dudley is not a delegate of Dolores. Rather, Dolores can be its &#8220;delegate&#8221; (or, in this case, &#8220;receiver&#8221;). Dudley is an HTTP server, and receives messages over HTTP and funnels them to a receivers (Thestral implementation), and Dolores is its default receiver.</li>
<li><strong>Firenze.</strong> Firenze, like Dudley, is an HTTP server; but instead of being a receiving server, it is a long-polling push server. It is not usually Dolores&#8217;s delegate—you don&#8217;t want every client to get every message. Instead, Firenze merely registers itself with Dolores to get an ID, and lets others register it (for instance, a client could send Dudley a message, which, through Dolores, would be received by Pig to make the connection).</li>
</ul>

<p>Above all, it is simple. The longest of these pieces is Firenze, at 280 lines of code (50 of which are intro comments I used to explain it to myself and spec it while planning).</p>

<h3>That&#8217;s All!</h3>

<p>I hope you find this helpful. I am excited about porting Roots to node.js; progress is somewhat slow, as I am quite busy (I am a student, and finals for all four classes tomorrow!)</p>

<p>What I am most looking forward to is how Pig (the dispatcher) could use the same code in both the client (Pomona) and the server—the routing is quite similar; in one case, you want to route to handlers/callbacks, in the other, to update functions on Thestral instances—pretty similar; even identical.</p>

<p>I am also looking forward to experimenting with two-way push, where all data is transferred through push—completely eradicating the expectation of a response to a request, and providing only a single receive/send code path on both client and server.</p>

<p>Anyway, I think this is all super cool. :)</p>

<p><strong><em>Thanks to Endash for the nice title. :)</em></strong>
<strong>Roots (called Dobby in development) is <a href="http://github.com/ialexi/dobby">available on GitHub.</a></strong></p>

<p><strong>P.S. I like the name Dobby, but Roots is better P.R.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/12/07/roots-deliver-nutrients/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>JavaScript Animation Benchmarks</title>
		<link>http://create.tpsitulsa.com/blog/2009/10/21/javascript-animation-benchmarks/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/10/21/javascript-animation-benchmarks/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 23:18:49 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[browsers]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=226</guid>
		<description><![CDATA[I&#8217;ve been working on a mixin to add animation to SproutCore views. The current version only works for layout properties, and does not yet work for centerX and centerY properties (they used to work, but some of the performance optimizations have made it slightly more tricky—I&#8217;ll be adding it back soon, though). I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on <a href = "http://github.com/ialexi/animate" title = "Mixin to add animation to SproutCore views">a mixin</a> to add animation to <a href = "http://sproutcore.com/">SproutCore</a> views.</p>

<p>The current version only works for layout properties, and does <em>not</em> yet work for <em>centerX</em> and <em>centerY</em> properties (they used to work, but some of the performance optimizations have made it slightly more tricky—I&#8217;ll be adding it back soon, though).</p>

<p>I decided to see how fast the code was in different browsers. The tests were done using a test application which generated a specified number of views, and then, once per second, updated a &#8220;frames per second&#8221; display. The measuring is somewhat subjective, as I have to deduce, based on consistency (or lack thereof) in the numbers, what the frame rate actually is. For the most part, they were pretty consistent, but the WebKit browsers at really low numbers of views (and really high frame rates) could be quite inconsistent at times.</p>

<p>I ran the tests on two separate machines:</p>

<ul>
    <li><strong>Mac OS X Snow Leopard on MacBook Pro 15&#8243; Core 2 Duo 2.33Gz w/2GB RAM.</strong> Lots of open programs, including iTunes. Not as scientific as would be optimal, but it <em>is</em> my work machine.</li>
    <li><strong>Windows XP on Pentium 4 3.20GHz w/2GB RAM.</strong> Only the browser was open.</li>
</ul>

<p>Browsers tested (note that my Firefox was OLD):</p>

<ul>
    <li>Safari 4.0.3 for Mac</li>
    <li>Firefox 3.5.3 for Mac</li>
    <li>Chromium 4.0.223.3 (29380) for Mac</li>
    <li>Internet Explorer 8</li>
    <li>Firefox 3.0.1 for Windows (oops. Should have upgraded first. Remind me to update numbers on Friday)</li>
    <li>Safari 4.0.3 for Windows</li>
    <li>Chrome 3.0.195.27 for Windows</li>
</ul>

<p>Here is the data:
<div id="attachment_227" class="wp-caption aligncenter" style="width: 559px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/Chart.png" alt="Data collected during testing of the different browsers" title="Data Collected" width="549" height="161" class="size-full wp-image-227" /><p class="wp-caption-text">Data collected during testing of the different browsers</p></div></p>

<p>That&#8217;s boring. Where are the pictures?</p>

<div id="attachment_228" class="wp-caption aligncenter" style="width: 480px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/All.png" alt="All Browsers" title="All Browsers" width="470" height="329" class="size-full wp-image-228" /><p class="wp-caption-text">All Browsers</p></div>

<p>Even here, you can tell that Chrome and Chromium are the best performers for small numbers of views. <strong>However, </strong> for larger numbers of views, Safari on Mac was a very clear winner, running at <strong>12 frames per second for 2000 LabelViews.</strong>.</p>

<p>Predictably, Internet Explorer was slowest. I didn&#8217;t bother to run all the tests for it.</p>

<div id="attachment_229" class="wp-caption aligncenter" style="width: 404px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/Windows.png" alt="Windows Browsers" title="Windows Browsers" width="394" height="316" class="size-full wp-image-229" /><p class="wp-caption-text">Windows Browsers</p></div>

<p>This chart shows only the Windows browsers. Again, you can tell IE is very slow. Chrome is very very fast. Safari is pretty good. My out-of-date Firefox is not always that much faster than IE; it will be interesting to see how Firefox 3.5 fares.</p>

<p>Notice the dip in performance for Safari at around 300? It briefly does worse than Firefox. It quickly levels off, though, and is second only to Chrome.</p>

<div id="attachment_230" class="wp-caption aligncenter" style="width: 394px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/Mac.png" alt="Mac Browsers" title="Mac Browsers" width="384" height="315" class="size-full wp-image-230" /><p class="wp-caption-text">Mac Browsers</p></div>

<p>And, of course, the best for last. Note how we see the same dip in performance for Safari—though it does not dip below Firefox; it would have dipped below Chromium, were it not already below it. Again, though, it leveled out faster than Chromium.</p>

<p>I always wanted to do a performance chart, but now that I have&#8230; it&#8217;s a <em>lot</em> of work (about 57 or so data points there&#8230; each measured separately).</p>

<p><strong>Question</strong>
Is the difference between Safari and Chromium completely caused by Google&#8217;s V8 JavaScript engine, or are there a few other differences?</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/10/21/javascript-animation-benchmarks/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Sprouting—Automated Spriting for SproutCore</title>
		<link>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 23:09:09 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SproutCore]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[spriting]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=203</guid>
		<description><![CDATA[Well. Spriting. Do you know what it is? While I&#8217;ve included a very brief summary, you may want to just skip that part if you already know about it, or find a better description of the technique. Also, while this article is specifically aimed at SproutCore, the concepts (and the script download) can easily be [...]]]></description>
			<content:encoded><![CDATA[<p>Well. Spriting. Do you know what it is? While I&#8217;ve included a <em>very</em> brief summary, you may want to <a href="#what-is-it">just skip that part</a> if you already know about it, or find <a href="http://www.alistapart.com/articles/sprites">a better description</a> of the technique. Also, while this article is specifically aimed at SproutCore, the concepts (and the script download) can easily be applied elsewhere.</p>

<h3>Well, here are your options:</h3>

<ol>
    <li><strong>Read the whole article.</strong> Could be amusing, if I’m having a bad writing day.</li>
    <li><a href="#skip-to-it">Skip to the download and “how to use.”</a> No, I’m not going to explain the code. Well, not here, at any rate. I’ll explain it in the code itself. Yes, of course there are comments.</li>
</ol>

<h3 id = "what-is-it">￼What is Spriting?</h3>

<p><div id="attachment_211" class="wp-caption alignleft" style="width: 110px"><img src="http://create.tpsitulsa.com/blog/wp-content/uploads/2009/10/sprites-100x300.png" alt="Example Sprited Images" title="Sprites" width="100" height="300" class="size-medium wp-image-211" /><p class="wp-caption-text">Example Sprited Images</p></div>Spriting is a technique which combines multiple small images into one large image. This can be very beneficial, because it means that, instead of downloading, say, <em>100</em> small images for all the commonly used icons in your web application, the web browser downloads <em>one</em> bigger image (or, perhaps, two or three, but you get the picture).</p>

<p>The goal of spriting is to reduce the effect of latency on your web app’s performance.</p>

<p>To your left is an example set of sprited images.</p>

<h3>Using a Script</h3>

<p>You may thing that combining several images into one larger image might be a bit time-consuming and, overall, a tad challenging, especially if you are often updating the individual images.</p>

<p>You are not alone. I feel the same way.</p>

<p>However, I do know a little about writing scripts—even if I’d like to know a lot more; I’m still only learning Python. So, there is a simple answer:</p>

<p>Write a darn script!</p>

<h3 id = "skip-to-it">The Product</h3>

<p>If you just want the script and how to use it, you’re in luck. Because there are, in fact, some comments in the script, I’ll leave the explanation of how the code works for there.</p>

<p>You can <a href="http://github.com/ialexi/Spriter">download the code</a> from GitHub.</p>

<p>The script is written in Python. It requires:
Either: Python 2.6+ or Python 2.5 with simplejson installed
PIL (Python Imaging Library)</p>

<p>Very briefly, here is what it does:</p>

<ul>
    <li>Takes a “sets” folder, which should have sub-folder <em>sets</em> of icons to be sprited.</li>
    <li>Takes an output folder, under which it will create new set folders containing CSS and image files.</li>
    <li>Combines images under a maximum size (128&#215;128 by default—see Configuration) into a sprites.png file in the output set folder.</li>
    <li>Creates CSS referencing these images using a URL template (SproutCore oriented at first—see URL)</li>
</ul>

<h3>Sets</h3>

<p>You often might have more than one set of icons. For instance, you could have a main set, and then an alternate set that you only want to load on demand. You still want that additional set to be sprited.</p>

<p>Or, perhaps, you may have images meant for repeating backgrounds, but that you still want to sprite.</p>

<p>To facilitate this, the spriting script takes, as its first argument, the location to a sets folder. You put your sets as subfolders into this folder. For instance, this could be your folder layout:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">my-sets
	common
		delete-32.png
		delete-64.png
		refresh-32.png
		refresh-64.png
		refresh-512.png
	repeat-x
		config.js       — see Configuration
		toolbar.png
		footer.png
		header.png
	odd-feature
		some-icon.png
		other-icon.png</pre></div></div>


<h3>URLs</h3>

<p>The CSS has to reference the images. Only problem: it doesn’t know where those images are.</p>

<p>Usually, you’d just be able to use paths relative to the StyleSheet. However, if you are using SproutCore, this won’t work because it moves around all the CSS and JavaScript.</p>

<p>With SproutCore, you need something more like:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">static_url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/set/sprites.png'</span><span style="color: #00AA00;">&#41;</span></pre></div></div>


<p>This is what the script does by default. Just like above. It uses a URL template of</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">static_url<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'images/{set}/{image}'</span><span style="color: #00AA00;">&#41;</span></pre></div></div>


<p>It is a string which will be passed through the Python <tt>format</tt> function, with <em>set</em> and <em>image</em> as arguments.</p>

<h3>Configuration</h3>

<p>There are a few things that are configurable. Configuration is done per-set.</p>

<p>Configuration files are JSON files, and are very simple. They are optional, and take at most four parameters:</p>

<ul>
    <li><strong>max-size</strong>: Default: 128.<br />The maximum size for sprited images. Images above this size will be put as separate images. Useful if you have some really high-res icons you don’t want to sprite, but want to keep with the set. </li>
    <li><strong>repeat</strong>: Default: “none”; can also be “x” or “y”.<br /> Determines the repeat mode. If repeat is “x,” the sprites will be laid out in rows; if “y,” the sprites will be laid out in columns. The generated CSS will include repeat-x or repeat-y, respectively.  Note that all images in a repeat-x set must be the same width, and all images in a repeat-y set must be the same height. The width or height of these images should be set using repeat-width and repeat height, which are discussed below. </li>
    <li><strong>repeat-width</strong>: Default: 32.<br />If repeat=“x,” the width of the repeat images. If your repetition does not require a pattern, you could set this to 1. </li>
    <li><strong>repeat-height</strong>: See that lovely summary of repeat-width above? Well, this is the same, but for y/height.</li>
</ul>

<h3>The CSS</h3>

<p>As you may know, SproutCore automatically includes any CSS files. So, since this generates CSS files (and, if you provide the right argument, puts them directly into a SproutCore-watched folder), the CSS is automatically added. So, how do you use it?</p>

<p>Very simply. The script generates CSS rules in this format:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.set-name</span> <span style="color: #6666ff;">.icon-name</span><span style="color: #6666ff;">.icon</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.set-name</span> <span style="color: #6666ff;">.icon-name</span><span style="color: #6666ff;">.icon</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #808080; font-style: italic;">/* CSS */</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>


<p>For example, to show &#8220;refresh-64&#8243; icon in set &#8220;common&#8221;, you&#8217;d do something like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ImageView.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;common refresh-64 icon&quot;</span> <span style="color: #006600; font-style: italic;">// using SproutCore's built-in className support for spriting</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Or, as you may have noticed from the rule above, you can accomplish some basic theming very easily by treating set names as themes:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">View.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span><span style="color: #CC0000;">256</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">256</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  classNames<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;common&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// the theme</span>
  childViews<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;styledView&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  styledView<span style="color: #339933;">:</span> ImageView.<span style="color: #660066;">design</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    layout<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> width<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span><span style="color: #339933;">,</span> height<span style="color: #339933;">:</span> <span style="color: #CC0000;">64</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    value<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;refresh-64 icon&quot;</span> <span style="color: #006600; font-style: italic;">// using SproutCore's built-in className support for spriting</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>


<p>Nice and simple, eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/10/17/sprouting%e2%80%94automated-spriting-for-sproutcore/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Running Orbited (or other Twisted Services) as a Windows Service</title>
		<link>http://create.tpsitulsa.com/blog/2009/08/04/running-orbited-or-other-twisted-services-as-a-service/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/08/04/running-orbited-or-other-twisted-services-as-a-service/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 14:47:15 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Comet]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=185</guid>
		<description><![CDATA[It took me awhile to figure out how to run Orbited and Dolores as Windows Services. Unfortunately, while I love Mac, almost every other computer at my workplace is Windows, including the servers. When deploying my web application, I can&#8217;t really just leave instructions like: &#8220;When you restart the server, please run these two scripts.&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>It took me awhile to figure out how to run <a href="http://orbited.org">Orbited</a> and <a href="http://create.tpsitulsa.com/blog/2009/08/04/dolores/">Dolores</a> as Windows Services. Unfortunately, while I love Mac, almost every other computer at my workplace is Windows, including the servers. When deploying my web application, I can&#8217;t really just leave instructions like: &#8220;When you restart the server, please run these two scripts.&#8221;</p>

<p>They need to be Windows Services.</p>

<p>Thankfully, it isn&#8217;t actually too hard. You just need a couple of Python packages (which I actually don&#8217;t remember installing, so they may have come with Twisted or Python), and then all you have to do is write a couple of very simple scripts. Here are mine:</p>

<p><strong>Orbited Service</strong></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> OrbitedManager <span style="color: #808080; font-style: italic;"># a copy of orbited.start with some very minor modifications, which I'll include after the break</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Service Utilities</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32serviceutil
<span style="color: #ff7700;font-weight:bold;">import</span> win32service
<span style="color: #ff7700;font-weight:bold;">import</span> win32event
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> WindowsService<span style="color: black;">&#40;</span>win32serviceutil.<span style="color: black;">ServiceFramework</span><span style="color: black;">&#41;</span>:
	_svc_name_ = <span style="color: #483d8b;">&quot;Orbited&quot;</span>
	_svc_display_name_ = <span style="color: #483d8b;">&quot;Orbited Server&quot;</span>
&nbsp;
	<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>, args<span style="color: black;">&#41;</span>:
		win32serviceutil.<span style="color: black;">ServiceFramework</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span> = win32event.<span style="color: black;">CreateEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcStop<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;">ReportServiceStatus</span><span style="color: black;">&#40;</span>win32service.<span style="color: black;">SERVICE_STOP_PENDING</span><span style="color: black;">&#41;</span>
		win32event.<span style="color: black;">SetEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcDoRun<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;">import</span> servicemanager
		OrbitedManager.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
			retval = win32event.<span style="color: black;">WaitForSingleObject</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> retval == win32event.<span style="color: black;">WAIT_TIMEOUT</span>:
				OrbitedManager.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">break</span>
				<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5.0</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">'__main__'</span>:
	win32serviceutil.<span style="color: black;">HandleCommandLine</span><span style="color: black;">&#40;</span>WindowsService<span style="color: black;">&#41;</span></pre></div></div>


<p><strong>Dolores Service</strong></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Dolores
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Service Utilities</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32serviceutil
<span style="color: #ff7700;font-weight:bold;">import</span> win32service
<span style="color: #ff7700;font-weight:bold;">import</span> win32event
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> WindowsService<span style="color: black;">&#40;</span>win32serviceutil.<span style="color: black;">ServiceFramework</span><span style="color: black;">&#41;</span>:
	_svc_name_ = <span style="color: #483d8b;">&quot;Dolores&quot;</span>
	_svc_display_name_ = <span style="color: #483d8b;">&quot;Dolores Server&quot;</span>
&nbsp;
	<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>, args<span style="color: black;">&#41;</span>:
		win32serviceutil.<span style="color: black;">ServiceFramework</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, args<span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span> = win32event.<span style="color: black;">CreateEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcStop<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;">ReportServiceStatus</span><span style="color: black;">&#40;</span>win32service.<span style="color: black;">SERVICE_STOP_PENDING</span><span style="color: black;">&#41;</span>
		win32event.<span style="color: black;">SetEvent</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> SvcDoRun<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;">import</span> servicemanager
		Dolores.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
			retval = win32event.<span style="color: black;">WaitForSingleObject</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hWaitStop</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> retval == win32event.<span style="color: black;">WAIT_TIMEOUT</span>:
				Dolores.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">break</span>
				<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5.0</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">'__main__'</span>:
	win32serviceutil.<span style="color: black;">HandleCommandLine</span><span style="color: black;">&#40;</span>WindowsService<span style="color: black;">&#41;</span></pre></div></div>


<p>That&#8217;s the basics, but as you may have noticed, I also changed Orbited&#8217;s start script. I did this because the original did not play nicely with threads.</p>

<p>Instead of changing it in its original location in the filesystem, I created a copy of it in the local directory (as you can see, I&#8217;m <em>not</em> importing Orbited.OrbitedManager).</p>

<p><span id="more-185"></span>
Before overwhelming you with a wall of code, I&#8217;ll explain what I changed. It is actually quite simple:</p>

<ol>
    <li>First, some imports at the top:


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> orbited
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span></pre></div></div>


</li>
    <li>Since the file is no longer &#8220;start.py&#8221; in the Orbited package directory, the static files path needs changing (just find the line and change it):


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">static_files = static.<span style="color: black;">File</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>orbited.__path__<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'orbited/static'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>



</li>
<li>And, since we&#8217;re going to start the reactor manually, we need to <strong>remove</strong> the auto-start portion:


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># REMOVE THIS PART:</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: #dc143c;">profile</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">hotshot</span>
        prof = <span style="color: #dc143c;">hotshot</span>.<span style="color: black;">Profile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;orbited.profile&quot;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;running Orbited in profile mode&quot;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;for information on profiler, see http://orbited.org/wiki/Profiler&quot;</span><span style="color: black;">&#41;</span>
        prof.<span style="color: black;">runcall</span><span style="color: black;">&#40;</span>reactor.<span style="color: black;">run</span><span style="color: black;">&#41;</span>
        prof.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>



</li>

    <li>Finally, I added a bit to the bottom, replacing the lines that call &#8220;main()&#8221;. It arranges to run Orbited (or any Twisted service) in a separate thread:


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> thread_start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># initializes the reactor</span>
	<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
	reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span>installSignalHandlers=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Crashed into Earth's Surface.&quot;</span>
&nbsp;
orbited_thread = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Starting Orbited...&quot;</span>
	orbited_thread = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=thread_start<span style="color: black;">&#41;</span>
	orbited_thread.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;In Orbit.&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop_reactor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">callFromThread</span><span style="color: black;">&#40;</span>stop_reactor<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
	start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	run = <span style="color: #008000;">True</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> run:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
			<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
			stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			run = <span style="color: #008000;">False</span></pre></div></div>



</li></ol>

<p><strong>The Raw OrbitedManager.py File</strong></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urlparse</span>
<span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> __version__ <span style="color: #ff7700;font-weight:bold;">as</span> version
<span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> config
<span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> orbited
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># NB: this is set after we load the configuration at &quot;main&quot;.</span>
logger = <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _import<span style="color: black;">&#40;</span>name<span style="color: black;">&#41;</span>:
    module_import = name.<span style="color: black;">rsplit</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #008000;">getattr</span>, name.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>, <span style="color: #008000;">__import__</span><span style="color: black;">&#40;</span>module_import<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _setup_protocols<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
    protocols = <span style="color: black;">&#91;</span>
        <span style="color: #808080; font-style: italic;">#child_path config_key  port_class_import,              factory_class_import</span>
        <span style="color: black;">&#40;</span><span style="color: #483d8b;">'tcp'</span>,     <span style="color: #483d8b;">'proxy'</span>,    <span style="color: #483d8b;">'orbited.cometsession.Port'</span>,    <span style="color: #483d8b;">'orbited.proxy.ProxyFactory'</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> child_path, config_key, port_class_import, factory_class_import <span style="color: #ff7700;font-weight:bold;">in</span> protocols:
        <span style="color: #ff7700;font-weight:bold;">if</span> config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s.enabled'</span> <span style="color: #66cc66;">%</span> config_key<span style="color: black;">&#41;</span> == <span style="color: #483d8b;">'1'</span>:
            port_class = _import<span style="color: black;">&#40;</span>port_class_import<span style="color: black;">&#41;</span>
            factory_class = _import<span style="color: black;">&#40;</span>factory_class_import<span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenWith</span><span style="color: black;">&#40;</span>port_class, factory=factory_class<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #dc143c;">resource</span>=root, childName=child_path<span style="color: black;">&#41;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s protocol active'</span> <span style="color: #66cc66;">%</span> config_key<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _setup_static<span style="color: black;">&#40;</span>root, config<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> static
    <span style="color: #ff7700;font-weight:bold;">for</span> key, val <span style="color: #ff7700;font-weight:bold;">in</span> config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[static]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">items</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> key == <span style="color: #483d8b;">'INDEX'</span>:
            key = <span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> root.<span style="color: black;">getStaticEntity</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>:
            logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cannot mount static directory with reserved name %s&quot;</span> <span style="color: #66cc66;">%</span> key<span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        root.<span style="color: black;">putChild</span><span style="color: black;">&#40;</span>key, static.<span style="color: black;">File</span><span style="color: black;">&#40;</span>val<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">import</span> twisted
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Orbited requires Twisted, which is not installed. See http://twistedmatrix.com/trac/ for installation instructions.&quot;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#################</span>
    <span style="color: #808080; font-style: italic;"># This corrects a bug in Twisted 8.2.0 for certain Python 2.6 builds on Windows</span>
    <span style="color: #808080; font-style: italic;">#   Twisted ticket: http://twistedmatrix.com/trac/ticket/3868</span>
    <span style="color: #808080; font-style: italic;">#     -mario</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">python</span> <span style="color: #ff7700;font-weight:bold;">import</span> lockfile
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
        <span style="color: #ff7700;font-weight:bold;">from</span> orbited <span style="color: #ff7700;font-weight:bold;">import</span> __path__ <span style="color: #ff7700;font-weight:bold;">as</span> orbited_path
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>orbited_path<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,<span style="color: #483d8b;">&quot;hotfixes&quot;</span>,<span style="color: #483d8b;">&quot;win32api&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">python</span> <span style="color: #ff7700;font-weight:bold;">import</span> lockfile
        lockfile.<span style="color: black;">kill</span> = <span style="color: #008000;">None</span>
    <span style="color: #808080; font-style: italic;">#################</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-c&quot;</span>,
        <span style="color: #483d8b;">&quot;--config&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;config&quot;</span>,
        default=<span style="color: #008000;">None</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;path to configuration file&quot;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-v&quot;</span>,
        <span style="color: #483d8b;">&quot;--version&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;version&quot;</span>,
        action=<span style="color: #483d8b;">&quot;store_true&quot;</span>,
        default=<span style="color: #008000;">False</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;print Orbited version&quot;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-p&quot;</span>,
        <span style="color: #483d8b;">&quot;--profile&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;profile&quot;</span>,
        action=<span style="color: #483d8b;">&quot;store_true&quot;</span>,
        default=<span style="color: #008000;">False</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;run Orbited with a profiler&quot;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span>
        <span style="color: #483d8b;">&quot;-q&quot;</span>,
        <span style="color: #483d8b;">&quot;--quickstart&quot;</span>,
        dest=<span style="color: #483d8b;">&quot;quickstart&quot;</span>,
        action=<span style="color: #483d8b;">&quot;store_true&quot;</span>,
        default=<span style="color: #008000;">False</span>,
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;run Orbited on port 8000 and MorbidQ on port 61613&quot;</span>
    <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> args:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'the &quot;orbited&quot; command does not accept positional arguments. type &quot;orbited -h&quot; for options.'</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: black;">version</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Orbited version: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>version,<span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: black;">quickstart</span>:
        config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[listen]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://:8000'</span><span style="color: black;">&#41;</span>
        config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[listen]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'stomp://:61613'</span><span style="color: black;">&#41;</span>
        config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[access]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'localhost'</span>,<span style="color: #ff4500;">61613</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'*'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Quickstarting Orbited&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #808080; font-style: italic;"># load configuration from configuration</span>
        <span style="color: #808080; font-style: italic;"># file and from command line arguments.</span>
        config.<span style="color: black;">setup</span><span style="color: black;">&#40;</span>options=options<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #dc143c;">logging</span>.<span style="color: black;">setup</span><span style="color: black;">&#40;</span>config.<span style="color: #008000;">map</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># we can now safely get loggers.</span>
    <span style="color: #ff7700;font-weight:bold;">global</span> logger<span style="color: #66cc66;">;</span> logger = <span style="color: #dc143c;">logging</span>.<span style="color: black;">get_logger</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'orbited.start'</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># NB: we need to install the reactor before using twisted.</span>
    reactor_name = config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'reactor'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> reactor_name:
        install = _import<span style="color: black;">&#40;</span><span style="color: #483d8b;">'twisted.internet.%sreactor.install'</span> <span style="color: #66cc66;">%</span> reactor_name<span style="color: black;">&#41;</span>
        install<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'using %s reactor'</span> <span style="color: #66cc66;">%</span> reactor_name<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">############</span>
    <span style="color: #808080; font-style: italic;"># This crude garbage corrects a bug in twisted</span>
    <span style="color: #808080; font-style: italic;">#   Orbited ticket: http://orbited.org/ticket/111</span>
    <span style="color: #808080; font-style: italic;">#   Twisted ticket: http://twistedmatrix.com/trac/ticket/2447</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> twisted.<span style="color: black;">web</span>.<span style="color: black;">http</span>
    twisted.<span style="color: black;">web</span>.<span style="color: black;">http</span>.<span style="color: black;">HTTPChannel</span>.<span style="color: black;">setTimeout</span> = <span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>, arg: <span style="color: #008000;">None</span>
    twisted.<span style="color: black;">web</span>.<span style="color: black;">http</span>.<span style="color: black;">HTTPChannel</span>.<span style="color: black;">resetTimeout</span> = <span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>: <span style="color: #008000;">None</span>
    <span style="color: #808080; font-style: italic;">############</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">resource</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> server
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> static
    <span style="color: #ff7700;font-weight:bold;">import</span> orbited.<span style="color: black;">system</span>
&nbsp;
    root = <span style="color: #dc143c;">resource</span>.<span style="color: black;">Resource</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    static_files = static.<span style="color: black;">File</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>orbited.__path__<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'orbited/static'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    root.<span style="color: black;">putChild</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'static'</span>, static_files<span style="color: black;">&#41;</span>
    root.<span style="color: black;">putChild</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'system'</span>, orbited.<span style="color: black;">system</span>.<span style="color: black;">SystemResource</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[test]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'stompdispatcher.enabled'</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'1'</span>:
        logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'stompdispatcher enabled'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#static_files.putChild('orbited.swf', static.File(os.path.join(os.path.dirname(__file__), 'flash', 'orbited.swf')))</span>
    <span style="color: #dc143c;">site</span> = server.<span style="color: black;">Site</span><span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
&nbsp;
    _setup_protocols<span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span>
    _setup_static<span style="color: black;">&#40;</span>root, config.<span style="color: #008000;">map</span><span style="color: black;">&#41;</span>
    start_listening<span style="color: black;">&#40;</span><span style="color: #dc143c;">site</span>, config.<span style="color: #008000;">map</span>, logger<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># switch uid and gid to configured user and group.</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">name</span> == <span style="color: #483d8b;">'posix'</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">getuid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
        <span style="color: #dc143c;">user</span> = config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'user'</span><span style="color: black;">&#41;</span>
        group = config.<span style="color: #008000;">map</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'[global]'</span><span style="color: black;">&#93;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'group'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">user</span>:
            <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">pwd</span>
            <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">grp</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                pw = <span style="color: #dc143c;">pwd</span>.<span style="color: black;">getpwnam</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span>
                uid = pw.<span style="color: black;">pw_uid</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> group:
                    gr = <span style="color: #dc143c;">grp</span>.<span style="color: black;">getgrnam</span><span style="color: black;">&#40;</span>group<span style="color: black;">&#41;</span>
                    gid = gr.<span style="color: black;">gr_gid</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    gid = pw.<span style="color: black;">pw_gid</span>
                    gr = <span style="color: #dc143c;">grp</span>.<span style="color: black;">getgrgid</span><span style="color: black;">&#40;</span>gid<span style="color: black;">&#41;</span>
                    group = gr.<span style="color: black;">gr_name</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
                logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Aborting; Unknown user or group: %s'</span> <span style="color: #66cc66;">%</span> e<span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'switching to user %s (uid=%d) and group %s (gid=%d)'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>, uid, group, gid<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">os</span>.<span style="color: black;">setgid</span><span style="color: black;">&#40;</span>gid<span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">os</span>.<span style="color: black;">setuid</span><span style="color: black;">&#40;</span>uid<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Aborting; You must define a user (and optionally a group) in the configuration file.'</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> start_listening<span style="color: black;">&#40;</span><span style="color: #dc143c;">site</span>, config, logger<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
    <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> protocol <span style="color: #ff7700;font-weight:bold;">as</span> protocol_module
    <span style="color: #808080; font-style: italic;"># allow stomp:// URIs to be parsed by urlparse</span>
    <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">uses_netloc</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;stomp&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># allow test server URIs to be parsed by urlparse</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> orbited.<span style="color: black;">servers</span> <span style="color: #ff7700;font-weight:bold;">import</span> test_servers
    <span style="color: #ff7700;font-weight:bold;">for</span> protocol <span style="color: #ff7700;font-weight:bold;">in</span> test_servers:
        <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">uses_netloc</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>protocol<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> addr <span style="color: #ff7700;font-weight:bold;">in</span> config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[listen]'</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> addr.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;stomp&quot;</span><span style="color: black;">&#41;</span>:
            stompConfig = <span style="color: #483d8b;">&quot;&quot;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">&quot; &quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> addr:
                addr, stompConfig = addr.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        url = <span style="color: #dc143c;">urlparse</span>.<span style="color: #dc143c;">urlparse</span><span style="color: black;">&#40;</span>addr<span style="color: black;">&#41;</span>
        hostname = url.<span style="color: black;">hostname</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">''</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'stomp'</span>:
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Listening stomp@%s'</span> <span style="color: #66cc66;">%</span> url.<span style="color: black;">port</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">from</span> morbid <span style="color: #ff7700;font-weight:bold;">import</span> get_stomp_factory
            morbid_instance = get_stomp_factory<span style="color: black;">&#40;</span>stompConfig<span style="color: black;">&#41;</span>
            config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'morbid_instance'</span><span style="color: black;">&#93;</span> = morbid_instance
            reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, morbid_instance, interface=hostname<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'http'</span>:
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Listening http@%s'</span> <span style="color: #66cc66;">%</span> url.<span style="color: black;">port</span><span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, <span style="color: #dc143c;">site</span>, interface=hostname<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'https'</span>:
            <span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> ssl
            crt = config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[ssl]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'crt'</span><span style="color: black;">&#93;</span>
            key = config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'[ssl]'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'key'</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                ssl_context = ssl.<span style="color: black;">DefaultOpenSSLContextFactory</span><span style="color: black;">&#40;</span>key, crt<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ImportError</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span>
            <span style="color: #ff7700;font-weight:bold;">except</span>:
                logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Error opening key or crt file: %s, %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>key, crt<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Listening https@%s (%s, %s)'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, key, crt<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenSSL</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, <span style="color: #dc143c;">site</span>, ssl_context, interface=hostname<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> url.<span style="color: black;">scheme</span> <span style="color: #ff7700;font-weight:bold;">in</span> test_servers:
            test_factory = protocol_module.<span style="color: black;">ServerFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            test_factory.<span style="color: black;">protocol</span> = test_servers<span style="color: black;">&#91;</span>url.<span style="color: black;">scheme</span><span style="color: black;">&#93;</span>
            logger.<span style="color: black;">info</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Listening %s@%s&quot;</span><span style="color: #66cc66;">%</span><span style="color: black;">&#40;</span>url.<span style="color: black;">scheme</span>, url.<span style="color: black;">port</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span>url.<span style="color: black;">port</span>, test_factory<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> url.<span style="color: black;">scheme</span> == <span style="color: #483d8b;">'monitor'</span>:
                config<span style="color: black;">&#91;</span><span style="color: #483d8b;">'globalVars'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'monitoring'</span><span style="color: black;">&#93;</span> = url.<span style="color: black;">port</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            logger.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Invalid Listen URI: %s&quot;</span> <span style="color: #66cc66;">%</span> addr<span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> thread_start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
	reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span>installSignalHandlers=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Crashed into Earth's Surface.&quot;</span>
&nbsp;
&nbsp;
orbited_thread = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Starting Orbited...&quot;</span>
	dolores_thread = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=thread_start<span style="color: black;">&#41;</span>
	dolores_thread.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;In Orbit.&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop_reactor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">callFromThread</span><span style="color: black;">&#40;</span>stop_reactor<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
	start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	run = <span style="color: #008000;">True</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> run:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
			<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
			stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			run = <span style="color: #008000;">False</span></pre></div></div>


<h3>So, What Do I Do?</h3>

<p>You should just need to put the windows service script and the OrbitedManager.py script in the same directory, and then run the windows service script from the command line. If you run it with no arguments, I <em>believe</em> it will give you an overview of available options, including the option to install.</p>

<p>I hope someone can find this helpful.</p>

<p><strong>Update: </strong> I added some clarification in the middle, discussing high-level about the replacement of Orbited&#8217;s <em>start</em> module, and a bit at the end about <em>what to do</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/08/04/running-orbited-or-other-twisted-services-as-a-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dolores &amp; Cornelius, Sitting in a Tree. Of Comet, Orbited and Python.</title>
		<link>http://create.tpsitulsa.com/blog/2009/08/04/dolores/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/08/04/dolores/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 14:27:59 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Comet]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=175</guid>
		<description><![CDATA[I&#8217;ll give you a moment to allow you to get that disturbing image out of your head. I have been working on a web application. I&#8217;ll hopefully be able to post the application soon. I needed the server to be able to send updates to the client via Comet. I&#8217;d never done any Comet before, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll give you a moment to allow you to get that disturbing image out of your head.</p>

<p>I have been working on a web application. I&#8217;ll hopefully be able to post the application soon.</p>

<p>I needed the server to be able to send updates to the client via Comet. I&#8217;d never done any Comet before, so I wasn&#8217;t sure what to do.</p>

<p>A lot of articles had examples making use of message queues and who knows what else. They were a tad overwhelming. While a message queue of some sort may eventually be a good idea, for now, all I needed to do was push a non-vital progress indicator for a job (15%, 20%, 30%, and so on) to the client so the user could see what was happening. I wanted more than one job to be supported, and the overall system should eventually allow sending updates when elements in the user&#8217;s filesystem are updated.</p>

<p>So, I&#8217;d like to be able to send:</p>


<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">UPDATE process/at/my/process/path; {progress:.5}
UPDATE file/at/my/file/path.txt</pre></div></div>


<p>In short, I&#8217;d like to send paths, along (sometimes) with little tiny messages.</p>

<p>Also, each instance of the web app should only receive updates to paths they are listening to, and they should only be <em>allowed</em> to listen to paths they actually have access to. Or, at least, that should be the case <em>once I implement permission management in the app</em> &emdash; right now, anything goes, which is fine for the moment because it is limited to test use on our local network.</p>

<p>So, I made a little script, called Dolores, which handles all of this, and another script, named Cornelius, which connects to it (incidentally, there are two Corneliuses: a Python one, and a JavaScript one).</p>

<p>I have posted it here in case anyone is interested. I used <a href = 'http://orbited.org/'>Orbited</a> in JavaScript to connect to the server. In a separate post in just a few moments, I&#8217;ll write about something a tad more useful than the Dolores &amp; Cornelius code I&#8217;m writing about here: how to run the Orbited (and, actually, Dolores) as a Windows service.</p>

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

<p><strong>Disclaimer: </strong> This is for interest only. I&#8217;m proud of it because I managed to make it, not because I actually think it is wonderful.</p>

<p>Before getting to the code, let me explain the API, which, if you have the server running, you can access manually using telnet:</p>

<p>You connect, and immediately receive:</p>


<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">I, Dolores, High Inquisitor, Hogwarts.
{A-THREAD-ID-HERE}</pre></div></div>


<p>That thread ID can then be used by a controller thread to connect you to paths.</p>

<p>Speaking of controllers, to control, you&#8217;d just make a file named &#8220;control-{THREAD-ID}&#8221; in a folder named &#8220;threads&#8221; relative to Dolores.py. Then, you&#8217;d send:</p>


<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">CONTROL</pre></div></div>


<p>Now, to connect or disconnect a thread to a path:</p>


<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">CONNECT {THREAD-ID} some/path/or/another
DISCONNECT {THREAD-ID} some/path/or/another</pre></div></div>


<p>In the web application, a django view handles the connection of a client (who sends its thread id along with the connection request). By only allowing controllers to create the connections, the client is not able to listen in to places it isn&#8217;t allowed.</p>

<p>And to send an update to a path:</p>


<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">UPDATE some/path/or/another; {some: message, may: go, right: here, format: &quot;any&quot;}</pre></div></div>


<p>Any listening thread will immediately receive:</p>


<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">UPDATE some/path/or/another; {some: message, may: go, right: here, format: &quot;any&quot;}</pre></div></div>


<p>Just as you sent it.</p>

<p>And yes, part of the reason it is named Dolores (the name of a particularly nasty character in the Harry Potter universe) is because I believe it to be a somewhat ugly solution. But unlike the HP character, it works. The HP character&emdash;let&#8217;s not go there.</p>

<p>Now, without further ado:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span>.<span style="color: black;">protocol</span> <span style="color: #ff7700;font-weight:bold;">import</span> Protocol, Factory
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">protocols</span>.<span style="color: black;">basic</span> <span style="color: #ff7700;font-weight:bold;">import</span> LineReceiver
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>, <span style="color: #dc143c;">string</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># This is Delores the High Inquisitor.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Dolores is a series of tubes. To prevent clogging, there are separate tubes,</span>
<span style="color: #808080; font-style: italic;"># rather than one big pipe everyone might get stuck in.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># A user can be connected to a tube. This allows the user to receive notifications</span>
<span style="color: #808080; font-style: italic;"># through said tubes.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># The pipe server is not made for actual COMET, believe it or not. There is no</span>
<span style="color: #808080; font-style: italic;"># persistence or anything. It is as simple as possible. Instead, we will try to</span>
<span style="color: #808080; font-style: italic;"># use Orbited to make a nice connection between Dolores and the client.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># I don't know how this will hold up performance-wise, but it is simple enough</span>
<span style="color: #808080; font-style: italic;"># that I think it will work for now.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># API:</span>
<span style="color: #808080; font-style: italic;"># There are two kinds of connections: control and client. The api </span>
<span style="color: #808080; font-style: italic;"># Creating a connection immediately results in a response with a thread id. Thread</span>
<span style="color: #808080; font-style: italic;"># ids consist of a few main parts: IPADDRESS-THREAD#-SECRET</span>
<span style="color: #808080; font-style: italic;"># Where thread# is a sequential thread number, IPADDRESS is the address of the client,</span>
<span style="color: #808080; font-style: italic;"># and SECRET is a random string of bytes made for the thread.</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Clients connecting should check first for a string starting with &quot;I,&quot;, which means they</span>
<span style="color: #808080; font-style: italic;"># successfully connected. They should then read another string with their thread id.</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#	CONTROL</span>
<span style="color: #808080; font-style: italic;">#			Attempts to make the thread a controller. This only works if a file</span>
<span style="color: #808080; font-style: italic;">#			named &quot;control-&quot; and the thread id is in the &quot;threads&quot; folder (relative</span>
<span style="color: #808080; font-style: italic;">#			to the Dolores.py file -- messy, I know)</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#	CONNECT UID PATH 			(controller only)</span>
<span style="color: #808080; font-style: italic;">#			Connects the thread with id UID to the specified path.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#	DISCONNECT UID PATH			(controller only)</span>
<span style="color: #808080; font-style: italic;">#			Disconnects the thread with id UID from the specified path.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#	UPDATE PATH MESSAGE 		(controller only)</span>
<span style="color: #808080; font-style: italic;">#			Sens a message regarding the path. Please keep the message twitter length.</span>
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;">#	CRASH UID					(controller only)</span>
<span style="color: #808080; font-style: italic;">#			Aborts a thread. Could be used for testing whole (un-optimized/cometized) reload.</span>
<span style="color: #808080; font-style: italic;">#</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Comet<span style="color: black;">&#40;</span>LineReceiver<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><span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">listening</span> = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">controls</span> = <span style="color: #008000;">False</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> connectionMade<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;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;I, Dolores, High Inquisitor, Hogwarts.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		secret = <span style="color: #483d8b;">&quot;&quot;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">32</span><span style="color: black;">&#41;</span>:
			secret += <span style="color: #dc143c;">random</span>.<span style="color: black;">choice</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span>.<span style="color: black;">ascii_letters</span> + <span style="color: #dc143c;">string</span>.<span style="color: black;">digits</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #008000;">self</span>.<span style="color: #008000;">id</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">getPeer</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">host</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;-&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">nextThreadNumber</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;-&quot;</span> + secret
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;New Thread &quot;</span> + <span style="color: #008000;">self</span>.<span style="color: #008000;">id</span>
&nbsp;
		<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">id</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">id</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">self</span><span style="color: #66cc66;">;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> control<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #808080; font-style: italic;"># if the file exists, do it.</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;threads&quot;</span>, <span style="color: #483d8b;">&quot;control-&quot;</span> + <span style="color: #008000;">self</span>.<span style="color: #008000;">id</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">controls</span> = <span style="color: #008000;">True</span>
			<span style="color: #dc143c;">os</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;threads&quot;</span>, <span style="color: #483d8b;">&quot;control-&quot;</span> + <span style="color: #008000;">self</span>.<span style="color: #008000;">id</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">controls</span> = <span style="color: #008000;">False</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> connectionLost<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, reason<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Closed Thread &quot;</span> + <span style="color: #008000;">self</span>.<span style="color: #008000;">id</span>
		<span style="color: #ff7700;font-weight:bold;">del</span> <span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: #008000;">id</span><span style="color: black;">&#93;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> listen <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">listening</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">deregister</span><span style="color: black;">&#40;</span>listen, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> listen<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">register</span><span style="color: black;">&#40;</span>path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">listening</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>path.<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;
	<span style="color: #ff7700;font-weight:bold;">def</span> ignore<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">deregister</span><span style="color: black;">&#40;</span>path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">listening</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">listening</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>path.<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;
	<span style="color: #ff7700;font-weight:bold;">def</span> lineReceived<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CONNECT&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">controls</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NOALLOW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">try</span>:
				uid, path = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">7</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
				uid = uid.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				path = path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Connect &quot;</span> + uid + <span style="color: #483d8b;">&quot; to &quot;</span> + path
&nbsp;
				<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span><span style="color: black;">&#91;</span>uid<span style="color: black;">&#93;</span>.<span style="color: black;">listen</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
				<span style="color: #008000;">self</span>.<span style="color: black;">listening</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span>:<span style="color: black;">&#93;</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>
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SUCCESS<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">except</span>:
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Failed a connect attempt.&quot;</span>
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FAIL<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">elif</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DISCONNECT&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">controls</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NOALLOW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">try</span>:
				uid, path = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot; &quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
				uid = uid.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				path = path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">if</span> uid <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span>:
					<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span><span style="color: black;">&#91;</span>uid<span style="color: black;">&#93;</span>.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
					<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SUCCESS&quot;</span><span style="color: black;">&#41;</span>
					<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">except</span>:
				<span style="color: #ff7700;font-weight:bold;">pass</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FAIL&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">elif</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CRASH&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">controls</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NOALLOW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">try</span>:
				uid = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">if</span> uid <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span>:
					<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span><span style="color: black;">&#91;</span>uid<span style="color: black;">&#93;</span>.<span style="color: black;">transport</span>.<span style="color: black;">loseConnection</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SUCCESS&quot;</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">except</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FAIL&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">elif</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;EXIT&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">loseConnection</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">elif</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;UPDATE&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">controls</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NOALLOW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">try</span>:
				pieces = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">6</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;;&quot;</span>,<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
				path = pieces<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
				message = <span style="color: #483d8b;">&quot;&quot;</span>
				<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>pieces<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:
					message = pieces<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
				<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;UPDATE &quot;</span> + path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;: &quot;</span> + message.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
				<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">message</span><span style="color: black;">&#40;</span>path.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, message.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SUCCESS&quot;</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">except</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FAIL<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">elif</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ALL&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">controls</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NOALLOW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">try</span>:
				path = data<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>:<span style="color: black;">&#93;</span>
				<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span>:
					<span style="color: #008000;">self</span>.<span style="color: black;">factory</span>.<span style="color: black;">comets</span><span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>.<span style="color: black;">sendMessage</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SUCCESS&quot;</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">except</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;FAIL&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">elif</span> data.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CONTROL&quot;</span><span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">control</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">controls</span>:
				<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;NOALLOW<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
				<span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> sendMessage<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path, message<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;UPDATE &quot;</span> + path + <span style="color: #483d8b;">&quot;;&quot;</span> + message + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Factory</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Manager<span style="color: black;">&#40;</span>Factory<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><span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">protocol</span> = Comet
		<span style="color: #008000;">self</span>.<span style="color: black;">comets</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">lookup</span> = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">threadCount</span> = <span style="color: #ff4500;">0</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> startFactory<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;">pass</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> stopFactory<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;">pass</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> register<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path, comet<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> path <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">lookup</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span> = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>comet<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> deregister<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path, comet<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> path <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">lookup</span>:
			<span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">if</span> comet <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span>:
			<span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
		<span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>comet<span style="color: black;">&#41;</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
			<span style="color: #ff7700;font-weight:bold;">del</span> <span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> message<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path, message<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> path <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">lookup</span>:
			<span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
		<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">lookup</span><span style="color: black;">&#91;</span>path<span style="color: black;">&#93;</span>:
			i.<span style="color: black;">sendMessage</span><span style="color: black;">&#40;</span>path, message<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> nextThreadNumber<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;">threadCount</span> = <span style="color: #008000;">self</span>.<span style="color: black;">threadCount</span> + <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">threadCount</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> thread_start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">global</span> dolores_reactor
	factory = Manager<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8007</span>, factory<span style="color: black;">&#41;</span>
	reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span>installSignalHandlers=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Dolores has been taken by the Centaurs. Clip clop.&quot;</span>
&nbsp;
&nbsp;
dolores_thread = <span style="color: #008000;">None</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Starting Dolores...&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">global</span> dolores_thread
	dolores_thread = <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#40;</span>target=thread_start<span style="color: black;">&#41;</span>
	dolores_thread.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Dolores has  now been appointed High Inquisitor.&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop_reactor<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">stop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	reactor.<span style="color: black;">callFromThread</span><span style="color: black;">&#40;</span>stop_reactor<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    start<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    run = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> run:
    	<span style="color: #ff7700;font-weight:bold;">try</span>:
    		<span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
    	<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
    		stop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    		run = <span style="color: #008000;">False</span></pre></div></div>


<p>And, the arguably even uglier Cornelius, with raw socket communication:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">cornelius_connection = <span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CorneliusFault<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</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>, value<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">value</span> = value
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><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;">return</span> <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">value</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _connect<span style="color: black;">&#40;</span>reconnect = <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">global</span> cornelius_connection
	<span style="color: #ff7700;font-weight:bold;">if</span> cornelius_connection <span style="color: #ff7700;font-weight:bold;">and</span> reconnect:
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			cornelius_connection.<span style="color: black;">shutdown</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span>:
			<span style="color: #ff7700;font-weight:bold;">pass</span>
		cornelius_connection = <span style="color: #008000;">None</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> cornelius_connection:
		cornelius_connection = <span style="color: #dc143c;">socket</span>.<span style="color: #dc143c;">socket</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_STREAM</span><span style="color: black;">&#41;</span>
		cornelius_connection.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;localhost&quot;</span>, <span style="color: #ff4500;">8007</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		cornelius_connection.<span style="color: black;">settimeout</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># wait one second for response if needed. Hopefully won't be that long.</span>
&nbsp;
		buffer = read<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>buffer<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">2</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> CorneliusFault<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;He's Not Back!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		verify_dolores = buffer<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> verify_dolores.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;I, Dolores, High Inquisitor, Hogwarts.&quot;</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> CorneliusFault<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Lord Thingy! Lord Thingy! Dolores, why have you turned into Lord Thingy?&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
		cornelius_thread_id = buffer<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;/threads/control-&quot;</span> + cornelius_thread_id, <span style="color: #483d8b;">&quot;w&quot;</span><span style="color: black;">&#41;</span>
		f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;you big dummy.&quot;</span><span style="color: black;">&#41;</span>
		f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		send<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CONTROL<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
		read<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> read<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">try</span>:
		buffer = cornelius_connection.<span style="color: black;">recv</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4096</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> buffer.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">except</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> send<span style="color: black;">&#40;</span>what, noconnect = <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> noconnect:
		_connect<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">global</span> cornelius_connection
	<span style="color: #ff7700;font-weight:bold;">try</span>:
		cornelius_connection.<span style="color: black;">sendall</span><span style="color: black;">&#40;</span>what + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">except</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> noconnect:
			_connect<span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
		cornelius_connection.<span style="color: black;">sendall</span><span style="color: black;">&#40;</span>what + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">try</span>:
		read<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># don't do anythin' with output yet. Just get rid of it... so it doesn't sit in some buffer</span>
	<span style="color: #ff7700;font-weight:bold;">except</span>:
		<span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span>path, message = <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>:
	send<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;UPDATE &quot;</span> + path + <span style="color: #483d8b;">&quot;; &quot;</span> + message<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> connect<span style="color: black;">&#40;</span>uid, path<span style="color: black;">&#41;</span>:
	send<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CONNECT &quot;</span> + uid + <span style="color: #483d8b;">&quot; &quot;</span> + path<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> disconnect<span style="color: black;">&#40;</span>uid, path<span style="color: black;">&#41;</span>:
	send<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;DISCONNECT &quot;</span> + uid + <span style="color: #483d8b;">&quot; &quot;</span> + path<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> crash<span style="color: black;">&#40;</span>uid<span style="color: black;">&#41;</span>:
	send<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;CRASH &quot;</span> + uid<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> log<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">global</span> cornelius_connection
	<span style="color: #ff7700;font-weight:bold;">print</span> cornelius_connection.<span style="color: black;">read_very_eager</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>


<p>Well, again, at least they work. And they are quite simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/08/04/dolores/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
