<?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>Blog - Alex-Elliott.co.uk &#187; polymer</title>
	<atom:link href="http://alex-elliott.co.uk/blog/category/polymer/feed" rel="self" type="application/rss+xml" />
	<link>http://alex-elliott.co.uk/blog</link>
	<description>The internet home of a prospective software engineer</description>
	<lastBuildDate>Thu, 02 Feb 2012 19:05:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<image>
			<title>Blog - Alex-Elliott.co.uk</title>
			<url>http://alex-elliott.co.uk/favicon.png</url>
			<link>http://alex-elliott.co.uk/blog</link>
			<width>16</width>
			<height>16</height>
			<description>The internet home of a prospective software engineer</description>
		</image>		<item>
		<title>zBot No More</title>
		<link>http://alex-elliott.co.uk/blog/2008/12/php/zbot-no-more</link>
		<comments>http://alex-elliott.co.uk/blog/2008/12/php/zbot-no-more#comments</comments>
		<pubDate>Sat, 13 Dec 2008 16:44:39 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[polymer]]></category>
		<category><![CDATA[projects]]></category>

		<guid isPermaLink="false">http://alex-elliott.co.uk/blog/?p=43</guid>
		<description><![CDATA[The title here is more sensational than it needs to be, I&#8217;m not discontinuing the zbot 2.0 project &#8211; rather, I&#8217;ve just decided that if I want to release it publicly, then I would like a more generic parent name for the software.  The instance of the bot in irc.zymic.com will likely retain the name. [...]]]></description>
			<content:encoded><![CDATA[<p>The title here is more sensational than it needs to be, I&#8217;m not discontinuing the zbot 2.0 project &#8211; rather, I&#8217;ve just decided that if I want to release it publicly, then I would like a more generic parent name for the software.  The instance of the bot in irc.zymic.com will likely retain the name. <img src='http://alex-elliott.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>So, what&#8217;s the new name for the software?  Well, that&#8217;s possibly still in flux, but for the moment I&#8217;ve decided I might go with <strong>polymer</strong>.  A slightly nerdy nod to the fact I want to make this release inherently extensible, as much as is needed.</p>
<h2>Polymerisation</h2>
<p>Of course, with a name like that I really should elaborate on just why it&#8217;s going to be more modular and easily extensible than the previous bot.</p>
<p>There was nothing really wrong with the implementation before, and much of it has been kept constant in the new implementation.  The most notable changes are in the format used to write modules, which has been simplified somewhat &#8211; and the fact that module interaction will be made possible allowing modules to reuse functionality included in a module that is already loaded into the bot.</p>
<p>My current <acronym title="Work in Progress">WIP</acronym> draft for module layout is this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// module description up here?</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">// that would make sense.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> mod_example
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// any local variables required</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$localvar</span><span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$othervar</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/// Core and required methods:</span>
   <span style="color: #666666; font-style: italic;">// init, performs any initialisation required</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// initialisation stuff... for example:</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &lt;-- (re)loads configuration in {confdir}/{name}.conf</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">// the triggers and hooks</span>
      registerTrigger<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ping'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'respond'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      registerHook<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'passive'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// a rehash method which handles a complete config reload</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> rehash<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// rejig internals in case our config has changed</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadConfig</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// &lt;-- like this again perhaps?</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/// about() and help() will probably make an appearance though since some are</span>
   <span style="color: #666666; font-style: italic;">/// internal they need not include them.</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/// Trigger/Hook implementation</span>
   <span style="color: #666666; font-style: italic;">// Here's a trigger, triggered of course by a !ping command.</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> respond<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// note the lack of any arguments, instead the information will automatically</span>
      <span style="color: #666666; font-style: italic;">// be made available through methods/variables contained within the base</span>
      <span style="color: #666666; font-style: italic;">// class.  This simplifies format, and allows us to make triggers and hooks</span>
      <span style="color: #666666; font-style: italic;">// constant.</span>
      <span style="color: #000088;">$target</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">state</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">target</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$nick</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">state</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">caller</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// module intercommunication:</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">is_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_irc</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
         <span style="color: #000088;">$_irc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">msg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$target</span><span style="color: #339933;">,</span><span style="color: #000088;">$nick</span><span style="color: #339933;">.</span><span style="color: #0000ff;">', pong'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// And here's a hook, called on every new packet</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> passive<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// do stuff</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/// And as always, you can declare internal functions for personal use.</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> helper<span style="color: #009900;">&#40;</span><span style="color: #000088;">$arg1</span><span style="color: #339933;">,</span><span style="color: #000088;">$arg2</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// do something helpful</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This format may undergo changes as I work on it, but it&#8217;s likely to look something like the above when I&#8217;m done. <img src='http://alex-elliott.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Any suggestions/comments are very welcome, since this is going to be the interface anyone who wants to write modules will be using, so it&#8217;s important that it&#8217;s suitably intuitive.</p>
]]></content:encoded>
			<wfw:commentRss>http://alex-elliott.co.uk/blog/2008/12/php/zbot-no-more/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Setting an Agenda</title>
		<link>http://alex-elliott.co.uk/blog/2008/11/general/setting-an-agenda</link>
		<comments>http://alex-elliott.co.uk/blog/2008/11/general/setting-an-agenda#comments</comments>
		<pubDate>Sun, 30 Nov 2008 20:00:53 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[zbot]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[site]]></category>

		<guid isPermaLink="false">http://alex-elliott.co.uk/blog/?p=39</guid>
		<description><![CDATA[If you&#8217;ve checked up on the site since my last blog entry, you&#8217;ve probably noticed I have indeed started on the pages for the rest of the site.  The about and contact pages are finished, and the footer now automatically displays the four most recent blog entries.  Now comes the main bit of the work, [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve checked up on the site since my last blog entry, you&#8217;ve probably noticed I have indeed started on the pages for the rest of the site.  The about and contact pages are finished, and the footer now automatically displays the four most recent blog entries.  Now comes the main bit of the work, writing a <acronym title="Content Management System">CMS</acronym> to manage my current and completed projects.</p>
<h2>The Main Site</h2>
<p>So, what exactly is going to be one the main site?  If you&#8217;ve seen the front page you can probably mostly guess.  There will be project pages for each project I&#8217;m currently working on or have completed &#8211; and there will be one of each set as &#8220;featured&#8221; works displayed on the front page and in the blog footer (the other spot will be filled by the most recent project).</p>
<p>The project pages themselves will be written descriptions of the project: what it&#8217;s about, what it&#8217;s aiming to produce, what I want to learn from it, what&#8217;s being used to implement it.  It will also have a section for relevant blog articles, which will be automatically fetched from here by selecting all the articles with a given tag (so for the zbot2 project, I will look for a &#8220;zbot&#8221; tag on blog articles).</p>
<p>This should provide a good source page to refer people to to answer any questions about the project, and can serve as a home for any projects I decide I would like to release publicly.</p>
<h2>What About zbot?</h2>
<p>I mentioned I was hoping to start zbot v2.0 soon, I will hopefully be starting that almost directly after finishing the main site on here.  In the meantime it&#8217;s time to start some design for the structure of the program and its source files.  When the project does get started I&#8217;ll make note here, and hopefully there&#8217;ll be a working core available before too long.</p>
<h2>Other Work</h2>
<p>This site and zbot aren&#8217;t the only things I&#8217;m doing however, there&#8217;s another project I would like to write which I have not really begun looking at yet, but which requires a fair bit of research before I can start.  I&#8217;ll probably look into a few of the topics I&#8217;ll need to write it while I&#8217;m working on the other projects (this site and zbot), and will hopefully write a few articles on them to help cement my understanding and to share what I&#8217;ve found out.</p>
<p>So, hopefully we&#8217;ll see the rest of the main site taking shape over the next week or two.  But if I don&#8217;t have time to blog for a little while, don&#8217;t think I&#8217;ve stopped working, hopefully it means quite the opposite, but we&#8217;ll have to see. <img src='http://alex-elliott.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://alex-elliott.co.uk/blog/2008/11/general/setting-an-agenda/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Concurrency in PHP</title>
		<link>http://alex-elliott.co.uk/blog/2008/11/php/concurrency-in-php</link>
		<comments>http://alex-elliott.co.uk/blog/2008/11/php/concurrency-in-php#comments</comments>
		<pubDate>Thu, 13 Nov 2008 20:28:51 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[zbot]]></category>
		<category><![CDATA[concepts]]></category>
		<category><![CDATA[concurrency]]></category>

		<guid isPermaLink="false">http://alex-elliott.me/?p=14</guid>
		<description><![CDATA[One of the problems you come across when writing real-time applications in PHP is that it is in most cases a linear language, performing the tasks set before it one at a time and doesn&#8217;t contain much in the way of tools to help set up concurrent tracks of evaluation. As far as I&#8217;m aware [...]]]></description>
			<content:encoded><![CDATA[<p>One of the problems you come across when writing real-time applications in PHP is that it is in most cases a linear language, performing the tasks set before it one at a time and doesn&#8217;t contain much in the way of tools to help set up concurrent tracks of evaluation.</p>
<p>As far as I&#8217;m aware PHP doesn&#8217;t contain anything at all for controlling threads, however it does include some process control functions which allows us to use a multi-process model for concurrent programming.</p>
<h2>Introducing Process Control</h2>
<p>The functions we need come via the <a title="Documentation on the Process Control module" href="http://www.php.net/manual/en/book.pcntl.php">Process Control module</a> in PHP.  This is not included by default in the PHP Apache module (because it&#8217;s more complicated in such cases), if you want to use it for a website you will need to use PHP as a CGI module or compile mod_php with <span class="option"><em>&#8211;enable-pcntl</em>, in this case I&#8217;ll be testing using the PHP CLI binary, in which it&#8217;s included by default.</span></p>
<p>The key function that this module provides for us is <strong>pcntl_fork()</strong>.  This works much like the fork you may have come across in other languages, it creates a clone of the current process which then computes a separate code path to the parent that called it.  The important thing of course being that the clone (or child) process does its computation while the parent carries on with whatever it was doing.  This is key to parallelising our PHP applications, and allows us to create responsive applications which can still do lengthy tasks.</p>
<h2>Process Control in Use</h2>
<p>So where might this be useful?  A trivial example would be when you want to perform a task that takes a number of seconds, and you want the user to receive output from the program while it&#8217;s being computed.  A simple example of this could be the code below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is a place-holder for a hypothetical function</span>
<span style="color: #666666; font-style: italic;">// which takes a long time to compute, let's say the</span>
<span style="color: #666666; font-style: italic;">// eventual answer would be 5...</span>
<span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tmp'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// we'll also assume we know the answer isn't 0.</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Here's the fork, the process ID will be stored in $pid for the main</span>
<span style="color: #666666; font-style: italic;">// program, and it'll be 0 for the child process.</span>
<span style="color: #000088;">$pid</span> <span style="color: #339933;">=</span> pcntl_fork<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
   <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Fork failed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Our fork failed, thus our program did.</span>
<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pid</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #666666; font-style: italic;">// here's our forked process</span>
   <span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tmp'</span><span style="color: #339933;">,</span>foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Done<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// In the meantime we'll keep our parent process looping.</span>
<span style="color: #000088;">$timestamp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tmp'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$timestamp</span> <span style="color: #339933;">!=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>    <span style="color: #666666; font-style: italic;">// once a second...</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Calculating...'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// give some output</span>
   <span style="color: #000088;">$timestamp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tmp'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// output our answer from the fork.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you see we have simply made a test-case here, there&#8217;s a function with a <em>sleep(10);</em> which takes the place of any long-running function, and while it is running we inform the user that the task is underway, once a second until it is complete.  It&#8217;s quite a trivial example, but it shows that both the echo statements and the sleep were functioning in parallel when viewing the output, which is predictably as follows:</p>
<pre>bash-3.1$ php -f test.php
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Calculating...
Done
5</pre>
<p>Another example might be if you were performing a repetitive task on many targets, such as all the files in a directory.  You could loop across the directory forking off a new process which would handle each file, then back in the main loop you simply wait for all of the processes to finish and then wrap up any loose ends you might have.</p>
<h2>Conclusion</h2>
<p>So there you go, process control.  A useful tool if you need to speed up a parallelisable program/algorithm.  Of course, in most cases speed isn&#8217;t too necessary in PHP applications, so the practical applications of process control aren&#8217;t very numerous.  In cases where speed is not important, it is usually better to keep the application simple, since it means it is more likely to be correct.</p>
<p>However, if you are writing something real-time, but don&#8217;t want to bog down your main application loop &#8211; making your application unresponsive, process control comes into its own and will save the day.</p>
<h2>Concurrency in zBot</h2>
<p>As I mentioned in the previous post, concurrency is something I&#8217;ve been meaning to include in zbot for a while now.  zbot is a real-time PHP application, and as an IRC bot, a response time of over a second or two is starting to seem sluggish.  Sometimes however some responses can&#8217;t be given in this length of time because they rely on an external site, and so the bot must wait for a response before it can reply.  In such cases I would like the main loop to be free to continue watching for more easily serviced queries.  So that it could reply to those first while it is waiting.</p>
<p>It also allows me to safely parcel off largish tasks without having to worry that the bot will ping out, as the core will continue looking for PING packets.  For example, I would like to implement a news plugin which would download five or more RSS feeds and then parse them to check for new news stories.  This would be ideal for process control, as I could fork off another process which would handle the downloads and parsing while the main bot continues to function.</p>
<p>For this purpose I have written a <em>taskScheduler</em> class to which I can send jobs which are then evaluated in a forked process.  The class keeps track of all scheduled jobs, and performs a callback when the job is completed.  This might prove to be a useful separate class, so I&#8217;ll likely both include it in zbot, and separately on my site with some documentation when it&#8217;s finished.</p>
]]></content:encoded>
			<wfw:commentRss>http://alex-elliott.co.uk/blog/2008/11/php/concurrency-in-php/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A New zBot</title>
		<link>http://alex-elliott.co.uk/blog/2008/11/php/a-new-zbot</link>
		<comments>http://alex-elliott.co.uk/blog/2008/11/php/a-new-zbot#comments</comments>
		<pubDate>Thu, 13 Nov 2008 15:17:30 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[projects]]></category>
		<category><![CDATA[zbot]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[plan]]></category>

		<guid isPermaLink="false">http://alex-elliott.me/?p=11</guid>
		<description><![CDATA[I did say there was news of a project coming up, so here it is.  Version 2.0 of my PHP IRC bot, zbot (on irc.zymic.com).  It&#8217;s been out of action for a while, since it was hosted on a machine I don&#8217;t have access to at the moment (no SSH, bleh).  But there&#8217;s a (better) [...]]]></description>
			<content:encoded><![CDATA[<p>I did say there was news of a project coming up, so here it is.  Version 2.0 of my PHP IRC bot, zbot (on irc.zymic.com).  It&#8217;s been out of action for a while, since it was hosted on a machine I don&#8217;t have access to at the moment (no SSH, bleh).  But there&#8217;s a (better) alternative now, so I can start work on getting it back.</p>
<h2>Identifying the problem(s)</h2>
<p>Building a new 2.0 version of my IRC bot has been something I&#8217;ve wanted to do for a while now, and there are a few main reasons why I&#8217;ve decided that it should be a fresh start rather than an update.  These are mostly (fairly) fundamental feature issues, which should be implemented quite a long way down the source tree &#8211; and so modifying the source to patch them in would be a tedious and lengthy process which might introduce a fair few bugs into the codebase.</p>
<p>The main things I want to improve in the new version are as follows:</p>
<ul>
<li>Generally cleaner code (I might choose to release this source publicly, and I want it to be nice).</li>
<li>Improve the module system to allow the modules to intercommunicate, by doing this I can abstract even some of the &#8220;core&#8221; functionality into modules, which will allow for patches at a later date without a restart.</li>
<li>Ground-up design for a bot rehash feature, in which it reloads all settings from configuration files.</li>
<li>Simpler text-file configuration files which will be parsed and read-in.</li>
<li>Work out a system for concurrency, perhaps spawning child PHP processes to handle any tasks which might take a long time (such as fetching files online), this would free up the rest of the core loop to handle other input while these tasks are performed.</li>
<li>Additional bot features, such as automatic tracking of users in channels, dynamic topic changes (so for example, we could specify a field which would be changed by an external source, allowing us to do things like include the current stable version number of PHP in #php).</li>
<li>Web interfaces for some bot data, like current factoids, quotes, and possibly a module repository that people could submit to.</li>
</ul>
<h2>Moving forward</h2>
<p>I hope to look at building a new version soon, sorting some groundwork for things at the moment, I&#8217;ll try to keep this updated as work goes on.</p>
<p>Of course, my good buddy Ed (Bread) will probably assist as he did in the last version (he provided the initial module system and a few plugins), and maybe this time we&#8217;ll actually write some documentation so people can write their own modules easily! :p</p>
<p>Since I will be hosting this on my shiny new VPS, we have more freedom (having a web interface was not possible before), and it means we can have one central source tree (I&#8217;m thinking stable/ and testing/ with a script to update stable when testing is shown not to break things <img src='http://alex-elliott.co.uk/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ).</p>
]]></content:encoded>
			<wfw:commentRss>http://alex-elliott.co.uk/blog/2008/11/php/a-new-zbot/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

