<?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>Claus Witt &#187; memcacheq</title>
	<atom:link href="http://www.clauswitt.com/tag/memcacheq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clauswitt.com</link>
	<description>software and web developer</description>
	<lastBuildDate>Thu, 24 Jun 2010 20:07:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Could MemcacheQ be Used For Cometd?</title>
		<link>http://www.clauswitt.com/could-memcacheq-be-used-for-cometd/</link>
		<comments>http://www.clauswitt.com/could-memcacheq-be-used-for-cometd/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 21:32:47 +0000</pubDate>
		<dc:creator>Claus Witt</dc:creator>
				<category><![CDATA[Web development]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[memcacheq]]></category>

		<guid isPermaLink="false">http://www.clauswitt.com/?p=214</guid>
		<description><![CDATA[I have been wondering for a couple of weeks would it be possible to use MemcacheQ as a kind of backend for Cometd? The reason I find it interesting is that sending message to the queue(s) would be as easy as memcache_set($memcache_obj, $memcache_queue_name, $msg, 0, 0); In other words easy as pie. Since there are [...]]]></description>
			<content:encoded><![CDATA[<p>I have been wondering for a couple of weeks would it be possible to use <a href="http://memcachedb.org/memcacheq/">MemcacheQ</a> as a kind of backend for <a href="http://en.wikipedia.org/wiki/Comet_(programming)">Cometd</a>? The reason I find it interesting is that sending message to the queue(s) would be as easy as </p>
<pre name="code" class="php">
memcache_set($memcache_obj, $memcache_queue_name, $msg, 0, 0);
</pre>
<p>In other words easy as pie. </p>
<p>Since there are all sorts of client libraries for memcached (and thus for memcacheq as well, since it uses the same protocol). I have been tinkering a bit last weekend, with some minor tests. I based it on <a href="http://sourceforge.net/projects/comet/">Comep</a> since I knew nothing of writing daemons in php or the comet protocol for that mather. (I would just like to use it). </p>
<p>Now I am not sure if Comep is even compatible with Dojo&#8217;s dojox.cometd.subscribe &#8211; and I have not gotten around to test that either. But since I have not had the time to look further into this, I thought I would just post my ideas, and hopefully someone &#8211; might even be me, once things quiets down, picks up the pieces and makes something great out of it. </p>
<pre name="code" class="php">
// Set the ip and port we will listen on
$address = '192.168.1.3';
$port = '666';
$max_clients = 10;

/* connect to memcached server */
$memcache_obj = memcache_connect('127.0.0.1', 22201);
if(!$memcache_obj) {
	die('Memcache object is false');
}
$memcache_queue_name = 'queue1';

$children = array();
// Array that will hold client information
$clients = Array();
$receiveClients = Array();
$client_ID = array();

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
socket_getsockname($sock, $socket_address, $socket_port);

echo "COMET-Server startet on Port: ".$socket_port."\n";

// Start listening for connections
socket_listen($sock);
$lastPingTime = 0;
// Loop continuously
while (true) {
    // Setup clients listen socket for reading
    $read = array();
    $read[0] = $sock;
    for ($i = 0; $i < $max_clients; $i++) {
        if ($client[$i]['sock']  != null) $read[$i + 1] = $client[$i]['sock'] ;
    }
    // Set up a blocking call to socket_select()
    $ready = socket_select($read,$A=null,$B=null,1);

    /* if a new connection is being made add it to the client array */
    if (in_array($sock, $read)) {
        for ($i = 0; $i < $max_clients; $i++) {
            if ($client[$i]['sock'] == null) {
                $client[$i]['sock'] = socket_accept($sock);
				echo 'New client';
                break;
            } else if ($i == $max_clients - 1) {

			}
        }
        if (--$ready <= 0)
            continue;
    } // end if in_array
    $value = json_encode(memcache_get($memcache_obj, $memcache_queue_name));
	if($value &#038;&#038; $value!="false") {
    	echo 'Sending value to queue: ' . $value . "\n";
		for ($i = 0; $i < $max_clients; $i++) { // for each client
	        if (in_array($client[$i]['sock'] , $read)) {
				socket_write($client[$i]['sock'], "HTTP/1.0 200 OK\r\n\r\n");
				socket_write($client[$i]['sock'], $value . "\r\n\r\n");
	        } else {
	            // Close the socket
	            if(!is_null($client[$i]['sock'])) {
					socket_close($client[$i]['sock']);
				}
	            unset($client[$i]["sock"]);
	        }
	    }
	}

} // end while
// Close the master sockets
socket_close($sock);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.clauswitt.com/could-memcacheq-be-used-for-cometd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
