<?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>zerocaffe.in</title>
	<atom:link href="http://zerocaffe.in/feed/" rel="self" type="application/rss+xml" />
	<link>http://zerocaffe.in</link>
	<description>Blog of Uncaffeinated Experiences on Entrepreneurship and Everything Else</description>
	<lastBuildDate>Sun, 12 Apr 2009 11:42:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Table Helper in PHP</title>
		<link>http://zerocaffe.in/2009/04/12/table-helper-in-php/</link>
		<comments>http://zerocaffe.in/2009/04/12/table-helper-in-php/#comments</comments>
		<pubDate>Sun, 12 Apr 2009 11:30:59 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[aggressive caching]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[RoR]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/?p=41</guid>
		<description><![CDATA[Custom built frameworks are always a zero-sum game. You get the freedom to build the framework completely suited to your business needs, but then you will more or less have to retread the bug ridden path of other frameworks. For our company currently, as an architect I have immense freedom to look at application from [...]]]></description>
			<content:encoded><![CDATA[<p>Custom built frameworks are always a zero-sum game. You get the freedom to build the framework completely suited to your business needs, but then you will more or less have to retread the bug ridden path of other frameworks. For our company currently, as an architect I have immense freedom to look at application from completely 'techie' viewpoint and keep building better and simpler solutions to make the lives of ouri  developers a bit simpler.</p>
<p>PHP is an amazingly simple language to build webapps in. Though personally I am in favor of Python or even Ruby to build webapplications, the amount of workforce in PHP available in the market makes it an immensely favored language.</p>
<p>Anyways, back in my work, we have this interesting problem where we have to render hundreds of analytics reports. Pull from the DB and display the results in table format. To deal with this tedium of having to write innumerable presentation layer files to deal with table rendering we use a small function to render the tables. Its a pretty simple function that formats the data in neat table cells using <code>
<td></td>
<p></code> and encloses each of the resultset in the table rows. <code><br />
<tr></tr>
<p></code>. However, because of this limitation we had the presentation layer tainting the data with the presentation tags.</p>
<p>Hence the genesis of this unit test code.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'table_helper.lib.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">################# FOR UNIT TESTING THE FUNCTION #######################
</span><span style="color: #000000; font-weight: bold;">function</span> url_logos<span style="color: #009900;">&#40;</span><span style="color: #000088;">$profile_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$profile_id</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;abcde&quot;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;vwxyz&quot;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span> <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;NONE&quot;</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'john'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'john carmack'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">75</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2008-12-1 00:0:00'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'galt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'john galt'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2008-06-06 06:06:06'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'dagny'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dagny taggart'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">180</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2008-11-00 00:0:00'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'hank'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hank rearden'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2008-06-02 06:06:06'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'roark'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'howard roark'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2008-12-1 00:0:00'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'john'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'john carmack'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">75</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2008-12-1 00:0:00'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Headers</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Alias'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Profile'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Games'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Joining Date'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Square'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Templates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'&quot;&lt;b&gt;{$data[0]}&lt;/b&gt;&quot;'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'$data[1]'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'&quot;http://www.someurl.com/profile.php?id={$data[2]}&amp;name={$data[0]}&quot;'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'&quot;&lt;a href=\&quot;http://www.someurl.com/games/played.do?num={$data[3]}\&quot;&gt;{$data[3]}&lt;/a&gt;&quot;'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'$data[4]'</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'url_logos($data[2]);'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
TableHelper<span style="color: #339933;">::</span><span style="color: #004000;">Render</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Headers</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Templates</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This led to the first version of my code for the TableHelper class which would simply use the PHP 'eval()' code the replace the Templates and fill it with data.</p>
<pre lang=php>
class TableHelper {
        /**
         * @func   Render($Headers, $Templates, $Data, $Prefix = 'data')
         * @desc   renders a table for the given data and the cell templates given.
         * @param1 $Headers a string array of table column headers
         * @param2 $Templates string of cell templates. The data bucket should be named
         *         as data in the default case. If you think of any other name be sure
         *         to pass the correct prefix to the function.
         * @param3 $Data Data to fill the templates with.
         * @param4 $Prefix Default bucket for row data is 'data'. Can be anything else.
         * @note   This function now builds a template file instead of using eval.
         */
        static function Render($Headers, $Templates, $Data, $Prefix = 'data') {
                $NumCols = count($Headers);

                echo '< table>';
                echo '< tr>';
                        foreach ($Headers as $header)
                                echo '< th>'.$header.'< /th>';
                echo '< /tr>';

                foreach ($Data as $$Prefix) {
                        echo '< tr>';
                        for ($i = 0; $i < $NumCols; $i++) {
                                        eval("\$_cell=\"$Templates[$i]\";");
                                        echo '< td>',$_cell,'';
                        }
                        echo '< /tr>';
                }
                echo '';
        }
}
</pre>
<p>As any seasoned programmer will tell you looking at the code, I was sacrificing speed (lots!) for want of better code separation by using <strong>eval()</strong>. After days of rumination I decided to use aggressive template caching in pphplace of <strong>eval().</strong> And here's the class with the caching mechanism built in...</p>
<pre lang=php>
# BEGIN Section Table Template Caching Section
define(CACHED_TEMPLATES_FOLDER, './cache/');

class TableHelper {
        /**
         * @func CachedFilePath($Id)
         * @desc The table template cached file is just named as unique id.tpl.php
         *       in the CACHED_TEMPLATES_FOLDER folder.
   */
        static function CachedFilePath($Id) {
                return CACHED_TEMPLATES_FOLDER.$Id.'.html.php';
        }

        /**
         * @func BuildTemplate($Id, $Headers, $Templates)
         * @desc Generates the php code for rendering the table and writes the
         *       table generation view code template to the cached folder.
         */
        static function BuildTemplate($Id, $Headers, $Templates) {
                assert(is_writeable(CACHED_TEMPLATES_FOLDER)) or die('Error THL001: Cache Folder has no Write Permissions.');

                $str = '';

                /* Build the Table Rendering PHP Code String */
                $str .= "< table>\n";
                $str .= "< tr>\n";
                        foreach ($Headers as $header)
                                $str .= "< th>$header< /th>\n";
                $str .= "< /tr>\n";

                $str .= '< ? php foreach ($Data as $$Prefix) { ?>';
                        $str .= "< tr>\n";
                        foreach ($Templates as $template)
                                $str .= "< td>< ?= $template ?>< /td>\n";
                        $str .= "< /tr>\n";
                $str .= "< ? php } ?>\n";
                $str .= "\n";

                $handle = fopen(TableHelper::CachedFilePath($Id), 'w');
                        fwrite($handle, $str);
                fclose($handle);

                return true;
        }
        # END Section Table Template Caching Section

        /**
         * @func   Render($Id, $Headers, $Templates, $Data, $Prefix = 'data')
         * @ver    1.2
         * @desc   renders a table for the given data and the cell templates given.
         * @param1 $Headers a string array of table column headers
         * @param2 $Templates string of cell templates. The data bucket should be named
         *         as data in the default case. If you think of any other name be sure
         *         to pass the correct prefix to the function.
         * @param3 $Data Data to fill the templates with.
         * @param4 $Prefix Default bucket for row data is 'data'. Can be anything else.
         * @note   This function now builds a template file instead of using eval.
         */
        static function Render($Id, $Headers, $Templates, $Data, $Prefix = 'data') {
                $NumCols = count($Headers);

                /* Attempt to just include the cached template file for this table */
                $cached_file_path = TableHelper::CachedFilePath($Id);
                if(!file_exists($cached_file_path))
                        TableHelper::BuildTemplate($Id, $Headers,$Templates);

                if(file_exists($cached_file_path)) {
                        include_once $cached_file_path; return;
                }

                /* If Cached File cannot be created, just do the EVAL way! */
                echo '< table>';
                echo '< tr>';
                        foreach ($Headers as $header)
                                echo '< th>'.$header.'< /th>';
                echo '< /tr>';

                foreach ($Data as $$Prefix) {
                        echo '< tr>';
                        for ($i = 0; $i < $NumCols; $i++) {
                                        eval("\$_cell=\"$Templates[$i]\";");
                                        echo '< td>',$_cell,'< /td>';
                        }
                        echo '< /tr>';   

                }
                echo '< /table>';
        }
}
</pre>
<p><strong>Catches</strong><br />
The only dependency with this code is that you have to create a Apache writeable folder referenced by the CACHED_TEMPLATES_FOLDER. After that its just fun all the way! Also note that the call to the Render in the unit testing class will now have to be supplied a unique Template Id.</p>
<pre lang=php>
TableHelper::Render($Id, $Headers, $Templates, $Data);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2009/04/12/table-helper-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux commands output in PHP</title>
		<link>http://zerocaffe.in/2008/10/04/linux-commands-output-in-php/</link>
		<comments>http://zerocaffe.in/2008/10/04/linux-commands-output-in-php/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 20:34:18 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[php snippet console output]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/?p=40</guid>
		<description><![CDATA[My friend Rahul was giving me a demo of their Web UI and a small but nifty feature caught my eye. They had a feature of being able to configure their systems using the Web UI (which is pretty cool in itself!) and the console output from the configuration command in turn would be displayed [...]]]></description>
			<content:encoded><![CDATA[<p>My friend Rahul was giving me a demo of their Web UI and a small but nifty feature caught my eye. They had a feature of being able to configure their systems using the Web UI (which is pretty cool in itself!) and the console output from the configuration command in turn would be displayed in a html iFrame. My company has a similar internal tool which does the same but then showing the output in a step-by-step manner would really be a huge help. </p>
<p>Anyways, so cooked up a quick php function for our system. Hope someone finds this snippet useful.</p>
<pre lang=php>
< ?php
/**
 * @func: Executes the command passed to it as argument and prints the
 * command console output line by line onto the html output stream hence
 * giving the illusion of having the command executing in the html window itself.
 */
function html_exec_cmd($cmd)	{
	$proc = popen("($cmd)2>&#038;1", "r");
	echo '&lt;pre&gt;';
	while(!feof($proc))	{
		$result = fgets($proc, 100);
		echo htmlspecialchars($result);
		flush();
	}
	pclose($proc);
	echo '&lt;/pre&gt;';
}

html_exec_cmd('wget yahoo.com'); # sample command.
?>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/10/04/linux-commands-output-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Erlang Ring Code</title>
		<link>http://zerocaffe.in/2008/07/10/erlang-ring-code/</link>
		<comments>http://zerocaffe.in/2008/07/10/erlang-ring-code/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 10:08:08 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[marketing]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[erlang]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/?p=39</guid>
		<description><![CDATA[Erlang Ring Benchmark Problem: In the Programming Erlang book by Joe Armstrong, we find this problem at the end of the Concurrent Programming chapter. Write a ring benchmark. Create N processes in a ring. Send a message round the ring M times so that a total of N * M messages get sent. Time how [...]]]></description>
			<content:encoded><![CDATA[<p>Erlang Ring Benchmark Problem:<br />
In the Programming Erlang book by Joe Armstrong, we find this problem at the end of the Concurrent Programming chapter.</p>
<blockquote><p>Write a ring benchmark. Create N processes in a ring. Send a message round the ring M times so that a total of N * M messages get sent. Time how long this takes for different values of N and M. Write a similar program in some other programming language you are familiar with. Compare the results. Write a blog, and publish the results on the Internet!</p></blockquote>
<p>My contribution to the already growing list of Erlang Ring Benchmark problem. However this is just the code for the Erlang, a N ring setup and M messages moving around the ring in parallel.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>ring<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>benchmark<span style="color: #014ea4;">/</span><span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% run benchmark for N processes and M messages.</span>
<span style="color: #ff3c00;">benchmark</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">N</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">M</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
        <span style="color: #45b3e6;">Pids</span> <span style="color: #014ea4;">=</span> <span style="color: #ff3c00;">makering</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
        <span style="color: #ff3c00;">for</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">M</span><span style="color: #6bb810;">,</span> <span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span>message<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Pids</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">end</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">end</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% setting up the process ring for sending</span>
<span style="color: #666666; font-style: italic;">%% messages &amp; return the Pids array</span>
<span style="color: #ff3c00;">makering</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">M</span><span style="color: #109ab8;">&#41;</span>     <span style="color: #6bb810;">-&gt;</span>
        <span style="color: #666666; font-style: italic;">%% create the processes</span>
        <span style="color: #45b3e6;">Pids</span> <span style="color: #014ea4;">=</span> <span style="color: #ff3c00;">for</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">M</span><span style="color: #6bb810;">,</span> <span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff3c00;">loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">end</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">end</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
        <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Head</span> | <span style="color: #45b3e6;">_T</span><span style="color: #109ab8;">&#93;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Pids</span><span style="color: #6bb810;">,</span>
        <span style="color: #666666; font-style: italic;">%% now setup the ring. one simple init loop to make each</span>
        <span style="color: #666666; font-style: italic;">%% each process aware of its next process in the ring</span>
        <span style="color: #666666; font-style: italic;">%% we need to pass Head head here as we want to connect</span>
        <span style="color: #666666; font-style: italic;">%% the last process back to the head.</span>
        <span style="color: #ff3c00;">connect</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Head</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Pids</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
        <span style="color: #666666; font-style: italic;">%% return the Pids</span>
        <span style="color: #45b3e6;">Pids</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% If only two elements are left in the list just point</span>
<span style="color: #666666; font-style: italic;">%% the first process to the next process. And point the</span>
<span style="color: #666666; font-style: italic;">%% the last process to the first process.</span>
<span style="color: #ff3c00;">connect</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Head</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">H</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span>   <span style="color: #6bb810;">-&gt;</span>
        <span style="color: #45b3e6;">H</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span>next<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
        <span style="color: #45b3e6;">N</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span>next<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Head</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">;</span>
<span style="color: #666666; font-style: italic;">%% Connect the head and the next element and move to the</span>
<span style="color: #666666; font-style: italic;">%% next elements</span>
<span style="color: #ff3c00;">connect</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Head</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">H</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span> | <span style="color: #45b3e6;">T</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span>       <span style="color: #6bb810;">-&gt;</span>
        <span style="color: #45b3e6;">H</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span>next<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
        <span style="color: #ff3c00;">connect</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Head</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">N</span> | <span style="color: #45b3e6;">T</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% send message from the beggining of the </span>
<span style="color: #666666; font-style: italic;">%% ring.</span>
<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Mesg</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Pids</span><span style="color: #109ab8;">&#41;</span>	<span style="color: #6bb810;">-&gt;</span>
	<span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Head</span>|T<span style="color: #109ab8;">&#93;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Pids</span><span style="color: #6bb810;">,</span>
	<span style="color: #45b3e6;">Head</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">last</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">T</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Mesg</span><span style="color: #6bb810;">,</span> <span style="color: #fa6fff;">length</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Pids</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% order a mass suicide for all process nodes	</span>
<span style="color: #ff3c00;">killall</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Pids</span><span style="color: #109ab8;">&#41;</span>	<span style="color: #6bb810;">-&gt;</span>
	<span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">foreach</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Pid</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #45b3e6;">Pid</span> <span style="color: #014ea4;">!</span> die <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Pids</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% message waiting loop. waits for message and sends </span>
<span style="color: #666666; font-style: italic;">%% it to the next process node. commits sepukku if </span>
<span style="color: #666666; font-style: italic;">%% it is ordered to die.</span>
<span style="color: #ff3c00;">loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span>	<span style="color: #6bb810;">-&gt;</span>
	<span style="color: #186895;">receive</span>
		<span style="color: #666666; font-style: italic;">%% now that we have the next pid, we move into the</span>
		<span style="color: #666666; font-style: italic;">%% the listening loop for that function. remember</span>
		<span style="color: #666666; font-style: italic;">%% the loop/1 will share the same Pid as this, so</span>
		<span style="color: #666666; font-style: italic;">%% no worries.</span>
		<span style="color: #109ab8;">&#123;</span>next<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">NextPid</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
			<span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Set next for ~p as ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">NextPid</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
			<span style="color: #ff3c00;">wait</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NextPid</span><span style="color: #109ab8;">&#41;</span>
	<span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">wait</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NextPid</span><span style="color: #109ab8;">&#41;</span>	<span style="color: #6bb810;">-&gt;</span>
	<span style="color: #186895;">receive</span>
		<span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">From</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Mesg</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">NumHops</span><span style="color: #109ab8;">&#125;</span>	<span style="color: #186895;">when</span> <span style="color: #45b3e6;">NumHops</span> <span style="color: #014ea4;">&gt;=</span> <span style="color: #ff9600;">0</span> <span style="color: #6bb810;">-&gt;</span>
			<span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;~w from ~p to ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Mesg</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">From</span><span style="color: #6bb810;">,</span> <span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
			<span style="color: #45b3e6;">NextPid</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span><span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Mesg</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">NumHops</span> <span style="color: #014ea4;">-</span> <span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
			<span style="color: #ff3c00;">wait</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NextPid</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
		die <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">void</span>
	<span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">%% custom for loops</span>
<span style="color: #ff3c00;">for</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">N</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">N</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">F</span><span style="color: #109ab8;">&#41;</span>	<span style="color: #6bb810;">-&gt;</span>	<span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">F</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">for</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">M</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">N</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">F</span><span style="color: #109ab8;">&#41;</span>	<span style="color: #6bb810;">-&gt;</span>  <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">F</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span>|<span style="color: #ff3c00;">for</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">M</span> <span style="color: #014ea4;">+</span> <span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">N</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">F</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">.</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/07/10/erlang-ring-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Moved to WP 2.5.1</title>
		<link>http://zerocaffe.in/2008/06/13/moved-to-wp-251/</link>
		<comments>http://zerocaffe.in/2008/06/13/moved-to-wp-251/#comments</comments>
		<pubDate>Fri, 13 Jun 2008 04:14:59 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[wordpress 2.5.1]]></category>
		<category><![CDATA[WP]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/?p=37</guid>
		<description><![CDATA[Finally moved the site to WordPress 2.5.1. My own personal blog has been on wordpress.com and that UI was quite inspiring. Now I stand inspired. And definitely the ease with which WP upgrades is quite awesome. However it would have been even greater if I could just upgrade by clicking on a button without even [...]]]></description>
			<content:encoded><![CDATA[<p>Finally moved the site to WordPress 2.5.1. My own personal blog has been on wordpress.com and that UI was quite inspiring. Now I stand inspired. And definitely the ease with which WP upgrades is quite awesome. However it would have been even greater if I could just upgrade by clicking on a button without even having to download, upload files!</p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/06/13/moved-to-wp-251/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Multiuser Chat using Flex and Ruby</title>
		<link>http://zerocaffe.in/2008/05/14/multiuser-chat-using-flex-and-ruby/</link>
		<comments>http://zerocaffe.in/2008/05/14/multiuser-chat-using-flex-and-ruby/#comments</comments>
		<pubDate>Wed, 14 May 2008 09:11:18 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[chat]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/2008/05/14/multiuser-chat-using-flex-and-ruby/</guid>
		<description><![CDATA[For the last few days I had been googling for some good help for coding networked flash applications for a pet project of mine. Mostly using Flex &#038; Ruby. There have been quite a few I saw - made with flash and using Java; but nothing to my personal liking. And hence I set to [...]]]></description>
			<content:encoded><![CDATA[<p>For the last few days I had been googling for some good help for coding networked flash applications for a pet project of mine. Mostly using Flex &#038; Ruby. There have been quite a few I saw - made with flash and using Java; but nothing to my personal liking. And hence I set to build a prototype of my own.</p>
<p>Being a coder and a poor (financially &#038; aesthetically &#038; skillfully <img src='http://zerocaffe.in/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ) visual designer, I am totally in love with Flex for the visual quality it brings to the flash applications i can build.</p>
<p>Ruby for the beauty it brings to the code; but then the code is only as good as the coder. And for cooking up a quick prototype, no language has been so good of course except for Perl (for me ofcourse).</p>
<p>Multiplayer - thats a tricky question. Flash cannot support P2P (peer-to-peer) applications as yet. So the best choice available is to use a client-server architecture - the good ole' sockets.</p>
<p>Hence using the code from a lot of different places and porting them from other languages, I managed to quickly cook up a Flex multiuser chat application with a ruby backend. Without further ado, into the code.</p>
<p>The server right now runs on the 9999 port of the localhost and the client connects to the same host and port. If you plan to test it on a hosting server, make sure you change the host and the port.</p>
<p><strong>Note: My wordpress code plugin is fucking up the mxml code. For this part you may just download the <a href="http://zerocaffe.in/wp-content/uploads/2008/05/flexrbchat.zip">zip file</a> and use the client.mxml.</strong></p>
<p><strong>The Flex client:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; ?xml <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :Application <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> <span style="color: #000066;">creationComplete</span>=<span style="color: #ff0000;">&quot;init()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;mx</span> :Script<span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;">&lt; !<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #009900;">			import flash.display.Sprite;</span>
&nbsp;
<span style="color: #009900;">			import flash.net.XMLSocket;</span>
<span style="color: #009900;">			import flash.events.Event;</span>
<span style="color: #009900;">			import flash.events.DataEvent;</span>
<span style="color: #009900;">			import flash.events.IOErrorEvent;</span>
<span style="color: #009900;">			import mx.events.CloseEvent;</span>
<span style="color: #009900;">			import mx.controls.Alert;</span>
&nbsp;
<span style="color: #009900;">			public var socket:XMLSocket = new XMLSocket<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">			private var host:String     = <span style="color: #ff0000;">&quot;localhost&quot;</span>;</span>
<span style="color: #009900;">			private var port:int        = 9999;</span>
&nbsp;
<span style="color: #009900;">			public function init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:void	<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">				socket.connect<span style="color: #66cc66;">&#40;</span>host, port<span style="color: #66cc66;">&#41;</span>;</span>
&nbsp;
<span style="color: #009900;">				socket.addEventListener<span style="color: #66cc66;">&#40;</span>DataEvent.DATA, onData<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">				socket.addEventListener<span style="color: #66cc66;">&#40;</span>Event.CONNECT, onConnect<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">				socket.addEventListener<span style="color: #66cc66;">&#40;</span>Event.CLOSE, onClose<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">				socket.addEventListener<span style="color: #66cc66;">&#40;</span>IOErrorEvent.IO_ERROR, onEvent<span style="color: #66cc66;">&#41;</span>;</span>
<span style="color: #009900;">			<span style="color: #66cc66;">&#125;</span></span>
&nbsp;
<span style="color: #009900;">			private function onConnect<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:void	<span style="color: #66cc66;">&#123;</span></span>
<span style="color: #009900;">				board.htmlText += <span style="color: #ff0000;">&quot;&lt;i&gt;</span></span>Client: Connected to server&quot; + &quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>&quot;;
			}
&nbsp;
			private function ReconnectHandler(event:CloseEvent):void	{
				if (event.detail == Alert.YES)
					init();
			}
&nbsp;
			private function onClose(event:Event):void	{
				board.htmlText += &quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;i<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Disconnected from server<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>&quot; + &quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>&quot;;
				Alert.show(&quot;Reconnect to the chat?&quot;, &quot;Reconnect?&quot;, (Alert.YES | Alert.NO), this, ReconnectHandler);
			}
&nbsp;
			private function onEvent(event:Event):void {
				board.htmlText += &quot;Error&quot; + &quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;br</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>&quot;;
				trace(event);
			}
&nbsp;
			private function onData(event:DataEvent):void {
				board.htmlText += event.text;
			}
&nbsp;
			private function sendMesg():void	{
				socket.send(username.text+ ': <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;font</span> <span style="color: #000066;">color</span>=<span style="color: #ff0000;">&quot;#' + cp.selectedColor.toString(16) + '&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>' + mesg.text + &quot;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/font<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>\n&quot;);
				mesg.text = &quot;&quot;;
			}
&nbsp;
		]]&gt;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :Style<span style="color: #000000; font-weight: bold;">&gt;</span></span>
		.marginstyle {
			paddingTop:12; 
			paddingBottom:12; 
			paddingLeft: 12; 
			paddingRight: 12;
		}
&nbsp;
		.spacestyle {
			paddingTop:2; 
			paddingBottom:2; 
			paddingLeft: 2; 
			paddingRight: 2;
		}
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :Panel <span style="color: #000066;">title</span>=<span style="color: #ff0000;">&quot;chatterbox&quot;</span> <span style="color: #000066;">styleName</span>=<span style="color: #ff0000;">&quot;marginstyle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :TextArea <span style="color: #000066;">editable</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;board&quot;</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;600&quot;</span> <span style="color: #000066;">color</span>=<span style="color: #ff0000;">&quot;#0000FF&quot;</span> <span style="color: #000066;">styleName</span>=<span style="color: #ff0000;">&quot;spacestyle&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
     <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;mx</span> :HBox<span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :TextArea <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">text</span>=<span style="color: #ff0000;">&quot;guest&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;50%&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :ColorPicker <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;cp&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;20&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>    
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :HBox<span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :TextArea <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mesg&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">styleName</span>=<span style="color: #ff0000;">&quot;spacestyle&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx</span> :Button <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;Send&quot;</span> <span style="color: #000066;">click</span>=<span style="color: #ff0000;">&quot;sendMesg()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>	
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><i>To compile it to a swf</i><br />
Assuming you already have the free Flex SDK just use<br />
<strong><em>$> mxmlc client.mxml</em></strong><br />
which will create a client.swf file for you.</p>
<p><b>Multiuser Ruby chat server:</b></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'socket'</span>
&nbsp;
MSGS = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;init&quot;</span>    <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Server started. Waiting for connections...&quot;</span>,
        <span style="color:#996600;">&quot;welcome&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Welcome! type EXIT to quit&quot;</span>,
        <span style="color:#996600;">&quot;byebye&quot;</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;Disconnected from server&quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
EOF     = <span style="color:#996600;">&quot;<span style="color:#000099;">\0</span>00&quot;</span>
HOST		= <span style="color:#996600;">&quot;localhost&quot;</span>
PORT		= <span style="color:#006666;">9999</span>
&nbsp;
server	= TCPServer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>HOST, PORT<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> MSGS<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;init&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;"># array to store all the active connections</span>
sessions = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">while</span> <span style="color:#006600; font-weight:bold;">&#40;</span>session = server.<span style="color:#9900CC;">accept</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># push the current session(socket) in the array</span>
  sessions <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#006600; font-weight:bold;">&lt;</span> session
  <span style="color:#008000; font-style:italic;"># initialize a new thead for each connection</span>
  <span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>session<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>local_session<span style="color:#006600; font-weight:bold;">|</span>
		local_session.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;i&gt;#{MSGS[&quot;</span>welcome<span style="color:#996600;">&quot;]}&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> EOF
&nbsp;
    <span style="color:#008000; font-style:italic;"># each time a client sends some data send it to all the connections</span>
    <span style="color:#9966CC; font-weight:bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      data = local_session.<span style="color:#CC0066; font-weight:bold;">gets</span>
&nbsp;
			<span style="color:#9966CC; font-weight:bold;">if</span> !data.<span style="color:#0000FF; font-weight:bold;">nil</span>?
				delim_pos = data.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
				name      = data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>..<span style="color:#9900CC;">delim_pos</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
				data      = data<span style="color:#006600; font-weight:bold;">&#91;</span>delim_pos<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span>..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
				<span style="color:#9966CC; font-weight:bold;">if</span> data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> == EOF
					data = data<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>..<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> 
				<span style="color:#9966CC; font-weight:bold;">elsif</span> data.<span style="color:#CC0066; font-weight:bold;">chomp</span> == <span style="color:#996600;">&quot;EXIT&quot;</span>
					sessions.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>local_session<span style="color:#006600; font-weight:bold;">&#41;</span>
					local_session.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;i&gt;#{MSGS[&quot;</span>byebye<span style="color:#996600;">&quot;]}&lt;/i&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> EOF
					local_session.<span style="color:#9900CC;">close</span>
&nbsp;
					data = <span style="color:#996600;">&quot;&lt;i&gt;#{name} disconnected from the server&lt;/i&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> EOF
				<span style="color:#9966CC; font-weight:bold;">end</span>
			<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      sessions.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>s<span style="color:#006600; font-weight:bold;">|</span> 
        <span style="color:#9966CC; font-weight:bold;">begin</span>
          s.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;&lt;b&gt;#{name}&lt;/b&gt;: #{data.chomp}&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> EOF
        <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">Errno</span>::ECONNRESET
          <span style="color:#008000; font-style:italic;"># an exception is raised, that means the connection to the client is broken</span>
          sessions.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span>s<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p><i>To Run</i><br />
User ruby chat_server.rb</p>
<p><strong>Source Code:</strong> <a href='http://zerocaffe.in/wp-content/uploads/2008/05/flexrbchat.zip' title='flexrbchat.zip'>Download here</a>. It contains a simple single user server - java &#038; ruby versions [simpleServer.java &#038; server.rb], a multiuser server [chat_server.rb] and the client [client.mxml].</p>
<p>Anyone, any doubts can leave a comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/05/14/multiuser-chat-using-flex-and-ruby/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Recursively creating a set of directories in Ruby</title>
		<link>http://zerocaffe.in/2008/04/16/recursively-creating-a-set-of-directories-in-ruby/</link>
		<comments>http://zerocaffe.in/2008/04/16/recursively-creating-a-set-of-directories-in-ruby/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 22:39:48 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/2008/04/16/recursively-creating-a-set-of-directories-in-ruby/</guid>
		<description><![CDATA[class Dir def self.mkdirs&#40;p&#41; return if File.exists? dir, file = File.split&#40;p&#41; Dir.mkdirs&#40;dir&#41; if !File.exists?&#40;dir&#41; Dir.mkdir&#40;p&#41; end end #sample usage: Dir.mkdirs('test/tmp/tempest')]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Dir</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">mkdirs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?
     dir, file = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">mkdirs</span><span style="color:#006600; font-weight:bold;">&#40;</span>dir<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> !<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>dir<span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">mkdir</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">p</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>#sample usage: Dir.mkdirs('test/tmp/tempest')</p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/04/16/recursively-creating-a-set-of-directories-in-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Business of Inertia &amp; Twittersphere experiment</title>
		<link>http://zerocaffe.in/2008/04/15/the-business-of-inertia-twittersphere-experiment/</link>
		<comments>http://zerocaffe.in/2008/04/15/the-business-of-inertia-twittersphere-experiment/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 08:23:03 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[Food For Thought]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/2008/04/15/the-business-of-inertia-twittersphere-experiment/</guid>
		<description><![CDATA[Inertia is an amazing phenomenon since Newton formulated our laws of physics for it. As is the case with any enduring physical truth, there are always economic transactions built on those physics foundations. And hence we have the M &#38;amp; A of small client-rich companies being bought out by the 800-pound gorillas hoping to use [...]]]></description>
			<content:encoded><![CDATA[<p>Inertia is an amazing phenomenon since Newton formulated our laws of physics for it. As is the case with any enduring physical truth, there are always economic transactions built on those physics foundations.</p>
<p>And hence we have the M &amp;amp; A of small client-rich companies being bought out by the 800-pound gorillas hoping to use the inertia of the clients to stick with them in the long run.</p>
<p>Inertia also has to its credit a fair-share of financial institutions that tap into its human counterpart - laziness. An amazing vice and a not-so-amazing virtue exploited ever so subtly by many institutions and the most obviously telling one being the credit card companies.</p>
<p>Sample for example, the credit cards business for individuals. It runs simply on one simple truth: that the credit card users will be lazy enough to not to pay her dues not before the due date. Its a clinchingly simple logic that trusts a basic human trait - chilling but effective.</p>
<p>Credit Card (henceforth called as CC) as instrument of finance taps into the deepest sacrosanct evil rule of personal finance.
<ul>
<li>Thou shalt not do the crime if thou can't do the time a.k.a Don't buy it if you can't afford it. CC makes it chillingly simple by providing a hand-loan for a interest free period of 30 days or so.</li>
</ul>
<p>Armed with a CC and unlimited temptations we have hundreds of well earning guys taking to the temptation and entering that unending cycle of bad financial karma.</p>
<p>Anyway now that we have proved beyond doubt that laziness or simply<br />
inertia can itself be a good business model, a new wave now sweeps the golden lands of bits and bytes which is lovingly called the internet. </p>
<p>andrewbaron has put up his twitter account for sale which at one level is a horrid sin to the community to commit and at another level is a damn interesting social internet experiment.</p>
<p>A twitter account by nature of the micro content created has a single author (content creator) and many readers (followers / consumers). A follower starts following a twitter account on account of one of the following
<ul>
<li>he is a close friend of yours (and thus trusts the '<b>author</b>')</li>
<li>loves the content produced by the author (and hence again adds the user because of the trust he places on the '<b>content</b>').</li>
<li>just follows because he wants to track the author &amp;amp; the activities (not trust but more of a voyeuristic pleasure) </li>
<li>just nothing better to do</li>
</ul>
<p>On the other hand, though a buyer faces one of the following
<ul>
<li>People who trust the author will leave.</li>
<li>People who love the content will stay as long as the content still stays original to the soul. In this case, the buyer does not have to put in effort in marketing his twitter account as long as he is good at content.</li>
<li>People who will stay for sometime to just see where it goes.</li>
</ul>
<p>Of this the buyer gains obviously from the second case but thats hoping that the inertia of followers will gain over the indignation of having sold their trust. And which we have already proven works! <img src='http://zerocaffe.in/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Either case this is a interesting social experiment to see how the trust networks and content communities work together.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/04/15/the-business-of-inertia-twittersphere-experiment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing BarcampHyderabad5 on 16th @ Google</title>
		<link>http://zerocaffe.in/2008/02/04/announcing-barcamphyderabad5-on-16th-google/</link>
		<comments>http://zerocaffe.in/2008/02/04/announcing-barcamphyderabad5-on-16th-google/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 09:37:23 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[Food For Thought]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/2008/02/04/announcing-barcamphyderabad5-on-16th-google/</guid>
		<description><![CDATA[Its that time of the year again that we folks here call the BarcampHyderabad. And Google has graciously accepted to give us some good space, free food and some goodies to let this get going. Hopefully, we will be able to sneak out some photos of the spanking new Google campus near Kondapur. The lead [...]]]></description>
			<content:encoded><![CDATA[<p>Its that time of the year again that we folks here call the BarcampHyderabad. And Google has graciously accepted to give us some good space, free food and some goodies to let this get going. Hopefully, we will be able to sneak out some photos of the spanking new Google campus near Kondapur. </p>
<p>The lead team planning out and spreading the message is quite a few this time and hopefully that reflects in the turnouts &#038; participants too.</p>
<p><img src="http://barcamp.org/f/barcamp-hyd-logo5.jpg" alt="barcamphyderabad5" /></p>
<p><strong>What about:</strong><br />
<strong><em>Technologies, demos and startups- betting on Web2.0, Social web, Semantics Web and beyond</em>!</strong><br />
Thats prolly the longest ever Barcamp topic ever! Anyways so if you are a startup guy, a been there done that guy, a research dude or a company eager to show their baby - make it a point to drop in for the party. Hopefully, yours truly too can make some presentation thats worth something.</p>
<p><strong>How to participate?</strong><br />
Go here for joining into the party &#038; participate. How about making a presentation on that spankingly awesome thing you were working on? Think we can get to know something about it? How about giving us a sneakpeak. We are aware of atleast of one startup planning to demo there. Why not you?</p>
<p>For more details and to signup for the camp, go <a href="http://barcamp.org/BarCampHyderabad5">here</a>.</p>
<p>Oh and btw <strong>Spread the word</strong>!</p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/02/04/announcing-barcamphyderabad5-on-16th-google/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hyderabad has a tech community (?)</title>
		<link>http://zerocaffe.in/2008/01/22/hyderabad-has-a-tech-community/</link>
		<comments>http://zerocaffe.in/2008/01/22/hyderabad-has-a-tech-community/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 18:54:21 +0000</pubDate>
		<dc:creator>poorna shashank</dc:creator>
				<category><![CDATA[Food For Thought]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/2008/01/22/hyderabad-has-a-tech-community/</guid>
		<description><![CDATA[Reaching out to the right folks and nurturing a community of tech crowd has been a stumbling block for the barcamps' campaigning. We did try a couple of initiatives - to create a blogroll, to create a hydtechblog, metroblogging for hyderabad - but nothing panned out as expected. Nada. Zilch. Nothing. And there was the [...]]]></description>
			<content:encoded><![CDATA[<p>Reaching out to the right folks and nurturing a community of tech crowd has been a stumbling block for the barcamps' campaigning. We did try a couple of initiatives - to create a blogroll, to create a hydtechblog, metroblogging for hyderabad - but nothing panned out as expected. Nada. Zilch. Nothing.</p>
<p>And there was the apparent conclusion that our tech community was not exactly thriving - a conclusion that ran counter to our intuition and common knowledge that there are lot of product tech companies in Hyderabad as well as some really good geek gods here.</p>
<p>Intuitioneva Jayate (Intuition alone triumphs). Introducing to you ladies and gentlemen (drum rolls please) <a href="http://www.twincling.org/">Twincling</a> - a very vibrant open source community from mana Hyderabad. Get to <a href="http://www.twincling.org/">their page</a> to know more about.</p>
<p>And if you readers, are aware of any such communities, do give us a tip about them.</p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/01/22/hyderabad-has-a-tech-community/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zeitgeist &#8211; Just Watch IT</title>
		<link>http://zerocaffe.in/2008/01/20/zeitgeist-just-watch-it/</link>
		<comments>http://zerocaffe.in/2008/01/20/zeitgeist-just-watch-it/#comments</comments>
		<pubDate>Sun, 20 Jan 2008 18:01:15 +0000</pubDate>
		<dc:creator>suman</dc:creator>
				<category><![CDATA[Food For Thought]]></category>

		<guid isPermaLink="false">http://zerocaffe.in/2008/01/20/zeitgeist-just-watch-it/</guid>
		<description><![CDATA[The documentary is not what begins out as.]]></description>
			<content:encoded><![CDATA[<p>The documentary is not what begins out as.</p>
<p><embed src="http://video.google.com/googleplayer.swf?docId=5547481422995115331&amp;hl=en" style="width: 400px; height: 326px" id="VideoPlayback" type="application/x-shockwave-flash"></embed></p>
]]></content:encoded>
			<wfw:commentRss>http://zerocaffe.in/2008/01/20/zeitgeist-just-watch-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

