Archive for April, 2009
Renaming file extensions with bash script
A couple of weeks ago I had to rename the file extension of several view scripts from html (from my own mvc framework) to phtml (zend framework). I found, as always, bash to be my friend.
for i in * do ( file $i | grep html ) && mv $i $(echo $i | sed s/html$/phtml/) done
Would I Pass the Zend PHP5 Certification Exam?
I have just used 20 minutes on testing my php5 skills before taking the actual certification exam in the future.
As expected, by me at least, I lack some skills in arrays, string manipulation and regular expressions. I knew this. I always look up details when I use this. Database access questions are all based on PDO, which I have never used – so no surprise there either. What Web Features and Design covers, I do not know? But I am happy that I have an excellent grade in the things I use everyday.
The overall rating was however that I passed, which I find a bit strange, since 5 out of twelve subjects were failed. Well, I know which subjects I have to study before taking the actual exam.
Could MemcacheQ be Used For Cometd?
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 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 Comep since I knew nothing of writing daemons in php or the comet protocol for that mather. (I would just like to use it).
Now I am not sure if Comep is even compatible with Dojo’s dojox.cometd.subscribe – 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 – might even be me, once things quiets down, picks up the pieces and makes something great out of it.
// 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 && $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);
New Versions of Dojo and Plugd Released
I have not posted for about a week now, the main reason is that I am working on something very exciting, which will be features here in the comming days, if all works out.
Today, while reading the most recent posts on Ajaxian, I found the announcement of the Dojo 1.3 release. Read the announcement and learn about the plugd plugin.
I will be using some time to update our project, which by the way will be going into the first testphase in 13 days, to the newest version of Dojo Toolkit today.
