<?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 &#187; Web</title>
	<atom:link href="http://create.tpsitulsa.com/blog/category/web/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>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>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>
		<item>
		<title>Dojo, Profiling, and Function Names</title>
		<link>http://create.tpsitulsa.com/blog/2009/06/23/dojo-profiling-and-function-names/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/06/23/dojo-profiling-and-function-names/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 18:06:59 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Bespin]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=156</guid>
		<description><![CDATA[Since we are using Bespin for a new web project we are working on, it is only natural that we use the Dojo Toolkit as well. But we have had some issues with dojo.declare(). In short, when using dojo.declare(), all functions defined in a class are anonymous functions. It looks something like this: Nasty. Too [...]]]></description>
			<content:encoded><![CDATA[<p>Since we are using <a title="Bespin Code Editor" href="http://bespin.mozilla.org/">Bespin</a> for a new web project we are working on, it is only natural that we use the <a title="Dojo Toolkit" href="http://dojotoolkit.org/">Dojo Toolkit</a> as well. But we have had some issues with <tt>dojo.declare()</tt>.</p>

<p>In short, when using <tt>dojo.declare()</tt>, all functions defined in a class are anonymous functions. It looks something like this:</p>

<div class="wp-caption aligncenter" style="width: 667px"><a href="http://www.flickr.com/photos/ialexi/3653978709/"><img title="Uh... What? There are too many anonymous functions." src="http://farm4.static.flickr.com/3395/3653978709_346b7059d6_o.png" alt="Uh... What?" width="657" height="391" /></a><p class="wp-caption-text">Uh... What?</p></div>

<p>Nasty. Too many &#8220;anonymous&#8221; functions.</p>

<p>I recalled a write-up about how a new feature had been added to WebKit (and, by extension, Safari 4): <a href="http://www.alertdebugging.com/2009/04/29/building-a-better-javascript-profiler-with-webkit/">functions can have display names</a> just for this purpose.  That would be perfect, except that I&#8217;d have to go to each function and add a <tt>"this.functionName.displayName = 'somename'"</tt>, or something like that.</p>

<p>Then I realized that all of the classes are declared through one function: <tt>dojo.declare()</tt>. So, with a quick modification, I added these lines near the beginning of the function (you can find it in dojo.js.uncompressed.js, if you search for <tt>dojo.declare = function</tt>):</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// preprocess props, updating functions</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> props<span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> props<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span>
			props<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">displayName</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span></pre></div></div>


<p>And now, that profiler output looks much better:</p>

<div class="wp-caption aligncenter" style="width: 654px"><a href="http://www.flickr.com/photos/ialexi/3653978733/in/photostream/"><img title="Much nicer displayName" src="http://farm4.static.flickr.com/3385/3653978733_0d5ee6c03e_o.png" alt="Much nicer output with displayName" width="644" height="387" /></a><p class="wp-caption-text">Much nicer output with displayName</p></div>

<p>And that&#8217;s it.</p>

<h3>Side Note</h3>

<p>I tried first to implement it by wrapping dojo.declare kind of like this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">olddeclare</span> <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">declare</span><span style="color: #339933;">;</span>
dojo.<span style="color: #660066;">declare</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> base<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//stuff here</span>
    dojo.<span style="color: #660066;">olddeclare</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> base props<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>It didn&#8217;t work, I think because of something to do with the arguments.callee and a few other things. So, I settled for modifying the original function.</p>

<h3>Update</h3>

<p>A thought just occurred to me: you can display the class name quite easily too. Just change the line that sets the display name in the above example to:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">props<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">displayName</span> <span style="color: #339933;">=</span> className <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;::&quot;</span> <span style="color: #339933;">+</span> i<span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/06/23/dojo-profiling-and-function-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Notes on Bespin&#8217;s Model and Cursor Positions</title>
		<link>http://create.tpsitulsa.com/blog/2009/06/17/notes-on-bespins-model-and-cursor-positions/</link>
		<comments>http://create.tpsitulsa.com/blog/2009/06/17/notes-on-bespins-model-and-cursor-positions/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 19:43:40 +0000</pubDate>
		<dc:creator>Alex Iskander</dc:creator>
				<category><![CDATA[Bespin]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://create.tpsitulsa.com/blog/?p=153</guid>
		<description><![CDATA[I am writing this so that I will remember (as I have forgotten some of the things here before). However, since others may find it useful, and since I&#8217;d really like any double-checking I can get, I am publishing it. In a web-based Create Framework related project, we are using Mozilla&#8217;s wonderful Bespin text editor. [...]]]></description>
			<content:encoded><![CDATA[<p>I am writing this so that I will remember (as I have forgotten some of the things here before). However, since others may find it useful, and since I&#8217;d really like any double-checking I can get, I am publishing it.</p>

<p>In a web-based Create Framework related project, we are using Mozilla&#8217;s wonderful <a href="https://bespin.mozilla.com/">Bespin</a> text editor. In the process of using it, we sometimes need to modify the editor in some way, perhaps by adding new features, perhaps by finding and fixing bugs.</p>

<p>The following is technical in nature.</p>

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

<p>In Bespin, characters are arranged in rows and columns. Any character may be referred to with a row number and a column number. This set of two numbers is referred to as a position.</p>

<p>Bespin&#8217;s editor has two significant parts which each have their own kind of position: the editor, and the model. This may seem silly at first: why should the editor&#8217;s position at row 5 column 4 be any different than the model&#8217;s?</p>

<p>There are a few different reasons for this, and the reasons could grow. It boils down to one concept: the editor, in order to enhance the usability, does not show characters exactly as they are in the model. For instance, a single tab in the model will usually be transformed into more than one space in the editor (for instance, four spaces). In this case, column 4 is the column immediately following a tab at the beginning of the line in the editor &mdash; the model position would be (5, 1), pointing to the column immediately following the tab character.</p>

<p>Also, code folding hides some lines of code, so row 4 could actually have been lines 4 through 30, but collapsed to take just one line. In this case, the model position for (5, 4) would be (31, 1).</p>

<p>The positions in the editor are called cursor positions (as they are possible positions of the cursor in the editor), and the positions in the model are called model positions.</p>

<p>As detailed in Gordon Hemsley&#8217;s <a href="https://wiki.mozilla.org/Labs/Bespin/DeveloperGuide/Tabs">guide on working with tabs</a>, there are a few helper functions. There are two that are most important:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">cursorManager.<span style="color: #660066;">getCursorPosition</span><span style="color: #009900;">&#40;</span>modelPosition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
cursorManager.<span style="color: #660066;">getModelPosition</span><span style="color: #009900;">&#40;</span>cursorPosition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>As you might expect by looking at their names and arguments, they convert cursor positions to model positions and vice-versa. There are two proxies to these functions in the <em>editor</em> component as well:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">editor.<span style="color: #660066;">getCursorPos</span><span style="color: #009900;">&#40;</span>modelPosition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//note the lack of &quot;ition&quot;</span>
editor.<span style="color: #660066;">getModelPos</span><span style="color: #009900;">&#40;</span>cursorPosition<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>


<p>So what positions are used where? The model, naturally, always uses model positions. The editor always uses cursor positions. Actions (where most of the actual editing takes place) have convert between the two.</p>

<p>For instance, the action insertCharacter (in <em>actions.js</em>), used whenever you type in a letter (with extra comments inserted marking the conversions):</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    insertCharacter<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>args<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><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">editor</span>.<span style="color: #660066;">readonly</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">editor</span>.<span style="color: #660066;">selection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">deleteSelectionAndInsertCharacter</span><span style="color: #009900;">&#40;</span>args<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;">// SEE HERE:</span>
            <span style="color: #006600; font-style: italic;">// the &quot;pos&quot; argument given to insertCharacter is, of course, the current cursor position.</span>
            <span style="color: #006600; font-style: italic;">// so, we convert it to a model position and hand it off to the model.</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">model</span>.<span style="color: #660066;">insertCharacters</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cursorManager</span>.<span style="color: #660066;">getModelPosition</span><span style="color: #009900;">&#40;</span>args.<span style="color: #660066;">pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> args.<span style="color: #660066;">newchar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cursorManager</span>.<span style="color: #660066;">moveRight</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">repaint</span><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;">// undo/redo</span>
            args.<span style="color: #660066;">action</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;insertCharacter&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> redoOperation <span style="color: #339933;">=</span> args<span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> undoArgs <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> action<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;deleteCharacter&quot;</span><span style="color: #339933;">,</span> pos<span style="color: #339933;">:</span> bespin.<span style="color: #660066;">editor</span>.<span style="color: #660066;">utils</span>.<span style="color: #660066;">copyPos</span><span style="color: #009900;">&#40;</span>args.<span style="color: #660066;">pos</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> queued<span style="color: #339933;">:</span> args.<span style="color: #660066;">queued</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> undoOperation <span style="color: #339933;">=</span> undoArgs<span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">editor</span>.<span style="color: #660066;">undoManager</span>.<span style="color: #660066;">addUndoOperation</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> bespin.<span style="color: #660066;">editor</span>.<span style="color: #660066;">UndoItem</span><span style="color: #009900;">&#40;</span>undoOperation<span style="color: #339933;">,</span> redoOperation<span style="color: #009900;">&#41;</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>

]]></content:encoded>
			<wfw:commentRss>http://create.tpsitulsa.com/blog/2009/06/17/notes-on-bespins-model-and-cursor-positions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
