Claus Witt

software and web developer

Archive for the ‘Web development’ Category

Bash script for editing the hosts file

with one comment

I just read a post about local development in apache by Jesper Rasmussen and thought one thing was missing. I usually test my sites and applications locally with the real domain – to ensure that functionality based on the url works as expected. However this means editing the hosts file several times a day. Now I have made a small shellscript that will add and remove lines from the hosts file.

#! /bin/bash
DEFAULT_IP=127.0.0.1
IP=${3:-$DEFAULT_IP}

case "$1" in
  add)
        echo "$IP $2"  >> /etc/hosts
        ;;
  remove)
        sed -ie "\|^$IP $2\$|d" /etc/hosts
        ;;

  *)
        echo "Usage: "
		echo "hosts.sh [add|remove] [hostname] [ip]"
		echo
		echo "Ip defaults to 127.0.0.1"
		echo "Examples:"
		echo "hosts.sh add testing.com"
		echo "hosts.sh remove testing.com 192.168.1.1"
        exit 1
        ;;
esac

exit 0

As you can see the script defaults the ip to 127.0.0.1 for easy creation of local domains. Next step is to create a quicksilver (and a gnome do) plugin for easy creation without ever touching the terminal. (Even though we all love the terminal, right?)

Written by Claus Witt

November 4th, 2009 at 10:02 am

Would I Pass the Zend PHP5 Certification Exam?

without comments

I have just used 20 minutes on testing my php5 skills before taking the actual certification exam in the future.

Exam Result

Exam Result

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.

Written by Claus Witt

April 16th, 2009 at 3:17 pm

Posted in Web development

Tagged with ,

Could MemcacheQ be Used For Cometd?

without comments

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);

Written by Claus Witt

April 7th, 2009 at 10:32 pm

Posted in Web development

Tagged with , ,

New Versions of Dojo and Plugd Released

without comments

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.

Written by Claus Witt

April 1st, 2009 at 7:16 am

Posted in Web development

Tagged with , ,

New Application? Just Write The Damn Thing

without comments

I have been getting a thought into my head the last couple of days. I have been talking a lot about things surrounding web application building the last weeks. Primarily it has been about all the things that are common to most of the application ideas that I have. While reading about Scaling Twitter though, I got to thinking. I might be worrying a bit too much about things that are not even important before there are a lot of active users actually using the product on a daily basis.

Maybe it is because it is easier to talk about all the things you ought to do right, instead of just writing the damn thing. It is also a lot safer for the ego in some way. You can postpone putting your idea out there – because no mather how good an idea it is, someone is bound to be of another oppinion. I will try to not let myself be blinded by all the negative what if’s.

For us, it’s really about scaling horizontally – to that end, Rails and Ruby haven’t been stumbling blocks, compared to any other language or framework. The performance boosts associated with a “faster” language would give us a 10-20% improvement, but thanks to architectural changes that Ruby and Rails happily accommodated, Twitter is 10000% faster than it was in January.

In the near future I will be creating technical proofs of concept (because the design part is not my strong side) of several of my ideas. This way it will be easier to assemble a team, and decide what to actually do.

This way, I will also have something real to write about.

Written by Claus Witt

March 23rd, 2009 at 6:00 am

Web Application Startup – a howto

without comments

Having business ideas is not enough. As I have previously written I have a lot of them. My ideas have primarily been generated out of what I know, and as such from the needs that I either have had myself, privately or profesionally, or that I have experienced first hand from others. As such I follow the ideas of 37 Signals quite good, which states that you should build software that deals with your own problems.

I am however quite clear that there are many other factors in starting a new web-based application. Many of them are mentioned in Startups in 13 Sentences.

I especially agree with the second sentence on the list. Start up quickly! I believe this to be true, in the sense that from when you begin the project untill the beta is released, and actual users begin to use it, there should be as small an amount of time elapsed as possible. The reason, is listed in the list as well, is that when you do as 37 Signals would want you to, you could end up doing all the wrong things.

It is just like the actual development of software. Test early and test often.

Written by Claus Witt

March 18th, 2009 at 7:30 am

Posted in Business,Web development

Tagged with ,