Currently showing entries with the tag: open source

page 1 of 1
1 

Book Review: Programming Erlang

October 23, 2007 • 8:54PM • permalink
As you may know from reading my previous blog entries, I've recently been trying to mix up the books in my reading queue by exploring the benefits of a few new programming languages. Recently a friend of mine told me a few things about functional programming, concurrency and Erlang which inspired me to check it out.

Joe Armstrong, one of the creators of the Erlang language, has recently published a new book on the topic and its truly a fascinating read.

One of the biggest complaints I hear from other developers about any introductory level book about a new programming language is the lack of useful programs that can be created upon completion of the book. What I mean is that you may be able to do the normal "Hello World", Fibonacci sequence output, etc. - but you probably won't be able to do anything really useful. This isn't the case with Armstrong's book.

The main ideas of the book and of Erlang (free for most environments at http://www.erlang.org) in general is concurrency or the simultaneous execution of code. While C and its variants offer multi-threading, it is still essentially executing sequential code and hence subject to deadlocks, race-conditions, etc. Erlang enforces some strict rules from the get-go to support concurrency and allow for true simultaneous execution that is free from not only race-conditions and deadlocks, but from semaphores, mutexes and locks as well.

In order to do that, Erlang turns computer science on its head (from a sequential programming point-of-view) which Armstrong is quick to point out at every turn. For example, variables are only called as such because it makes things easier. The fact is they can't actually vary and an Exception is thrown if you even try.

On that same note, the equal sign is not the assignment operator like it usually is in programming, it is instead used to perform pattern matching. An example would probably work best to explain it:


1>X = 5.
5

2>X = 5.
5

3>X = 3.
=ERROR REPORT==== 16-Oct-2007::21:26:12 ===
Error in process <0.30.0> with exit value: {{badmatch,3},[{erl_eval,expr,3}]}

** exited: {{badmatch,3},[{erl_eval,expr,3}]} **



As I stated before, the = is not the assignment operator, it is instead used for pattern matching. An additional caveat though, when used with an uninitialized variable (which usually start with Capital letters), the variable is assigned that value. In line 1 above, we match the variable X against the literal value 5. Since X is uninitialized at the time we match it against the literal 5, it then takes on that value.

This is why, when we repeat the action in the second line, it returns the value 5 (indicating a match). Consequently, when we hit the third line, the shell throws an exception since we're matching X against 3, when it has already taken on the value 5.

You may wonder what the value of such an operator is, but when you dive into server programming, you'll see that it can be used (among other things) to direct functionality within network protocols. By matching against certain patterns, you can essentially code mini-conditional statements to perform various actions upon receipt of certain data. It seems complicated, but it's really not - since this is a Book Review and not an Introduction to Erlang, so I'll leave the explanation to Armstrong...

Within 75 pages, I had gained an enthusiasm for Erlang that was apparently infectious and it has been embraced by a few other developers in my Department (including Jon over at Rusty Razor Blade). While we're still not 100% sure it will be able to support the traffic load and perform fast enough, we're motivated enough to try. Armstrong makes it easy to learn from example too, since the book contains Erlang source to create a server for almost any major network protocol or project you could think of including IRC, SHOUTcast, a simple Error logger, a SQL Server and a Web Server.

We've discussed a plethora of different ideas we could run on our "new Erlang Framework", some more ambitious than others. We're convinced we could rewrite memcached in a few hundred lines of code, including all the features of the original - a few that are missing and commonly implemented in other cache systems - as well as a few of our own custom design. We figure if we can even get 80% of the throughput of our current memcached implementation than it will be worth our trouble. Especially if it enables us to build a Erlang Framework to support any scalable idea we can come up with.

So if the idea of scalable server applications, functional programming or Erlang in general seems interesting, I highly suggest you check out Programming Erlang.


How to Create Dynamic Blocks in Drupal

October 14, 2007 • 10:21PM • permalink
One of my clients uses the Drupal CMS Framework, which requires me to code custom modules and create custom blocks. Drupal (and PHP in general) make a lot of things very easy, but in some cases (Drupal in particular) make the simple things VERY difficult. I've come across quite a few cases where I needed to create dynamic blocks of content that can be stored and retrieved. At first I thought Drupal would make it impossible, but the solution is actually quite simple.

For this example, I'm going to build a simple Google AdSense module that will allow the creation of an infinite number of blocks.


The Assumptions

1) This module was built using Drupal 4.7. I'm not familiar enough with other Drupal environments to assure that this will work, but if anybody tries testing it on other Drupal versions - please let me know so I can update this blog entry!

2) I'm going to assume that the reader has built a basic Drupal module before and does not need a basic explanation of hooks.

3) I'm going to assume that there was previously a variable_set('google_adsense_code', 'pub-XXXXXXXXXXXX-XXXX') call in some form that will allow the site administrator to set their personal AdSense code.


The Database

Now, let's setup a basic database schema for this module. (Note, this was executed on MySql 5.0.22)


CREATE TABLE IF NOT EXISTS gadsense_blocks (
   bid INT AUTO_INCREMENT PRIMARY KEY,
   width INT,
   height INT,
   type VARCHAR(32)
);



The above would either be executed directly on the database server or embedded in a Drupal module .install file to create the table the first time the module is installed.

The above table is very simple with an auto-incrementing bid, the width and height of our target ad, and a simple text field we'll use to store whether our ad is a text or image ad (or both).


The Core Module Hooks

As I said above, I'm going to assume the reader has built a basic Drupal module before and can understand the below without explanation.


function gadsense_help($section = 'admin/help#gadsense') {
   switch ($section) {
   case 'admin/modules#description':
      return t('Creates an area for Google AdSense support.');
   }
}


function gadsense_perm() {
   return array('administer adsense');
}


function gadsense_menu($may_cache) {
   $items = array();

   $items[] = array(
      'path' => 'admin/settings/gadsense/list',
      'title' => t('Google AdSense Ads'),
      'callback' => 'list_ads_page',
      'access' => user_access('administer adsense'),
      'type' => MENU_CALLBACK,
   );

   $items[] = array(
      'path' => 'admin/settings/gadsense/add',
      'title' => t('Google AdSense Ads'),
      'callback' => 'create_ads_page',
      'access' => user_access('administer adsense'),
      'type' => MENU_CALLBACK,
   );

   return $items;
}




The Administrator Pages

Now that we've got our core module created, we can get to work! We're going to create two pages for basic administration. The first page, the main admin page, will list all the blocks we've created so far and offer a link to create a new one.


function list_ads_page() {
   $result = db_query('SELECT * FROM {gadsense_blocks} ORDER BY width + " x " + height ASC');
   $rows = array();
   while ($field = db_fetch_object($result)) {
      $rows[] = array($field->bid, $field->width, $field->height, $field->type);
   }
   if (count($rows) == 0) {
      $rows[] = array(array('data' => t('No ad blocks have been defined.'), 'colspan' => '6'));
   }

   $header = array(t('id'), t('width'), t('height'), t('type'));

   $output = theme('table', $header, $rows);
   $output .= '<br /><br />';
   $output .= l("Add a New Block", "admin/settings/gadsense/add");

   return $output;
}



This will form the page that you see here (by default):



The second page will perform the first half of our Drupal "magic". We're going to build the page to create our blocks. First we need to draw the form. Again, I'm going to assume you're competent with Drupal module building and just supply the code:


function create_ads_page() {
   $form = array();

   $sizes = array('120x600', '160x600', '728x90', '468x60', '300x250');
   $types = array('text', 'image', 'text/image');

   $form['ad_size'] = array('#type' => 'select', '#title' => 'Ad size', '#options' => $sizes);
   $form['ad_type'] = array('#type' => 'select', '#title' => 'Ad type', '#options' => $types);

   $form['submit'] = array('#type' => 'submit', '#value' => 'Submit');

   return drupal_get_form('create_ad', $form);
}



Which will show the following page:




When submitted, the following code is executed.


function create_ad_submit($form_id, $form_values) {
   $ad_sizes = array('120x600', '160x600', '728x90', '468x60', '300x250');
   $width = 0;
   $height = 0;
   $type = '';

   //check for valid sizes
   foreach ($ad_sizes as $key => $value) {
      if ($form_values['ad_size'] == $key) {
         $dimensions = explode('x', $value);
         $width = $dimensions[0];
         $height = $dimensions[1];
      }
   }

   if (intval($form_values['ad_type']) <= 2) {
      $types = array('text', 'image', 'text/image');
      $type = $types[$form_values['ad_type']];
   }

   if ($width == 0 || $height == 0 || $type == '') {
      drupal_not_found();
   }
   else {
      db_query("
      INSERT INTO {gadsense_blocks}
         (width, height, type)
      VALUES
         (%d, %d, '%s')",
      $width, $height, $type);

      drupal_set_message('Ad block created');
      drupal_goto('admin/settings/gadsense/list');
   }
}



After splitting the ad size selection into $width and $height variables and grabbing the ad $type, we insert a new record into our gadsense_blocks table. As shown above, this will create give it a unique bid value, which we'll reference later.

I'll create a few different blocks, and now our main gadsense page will be populated. Like this:



Now, the last thing we need to do is implement the block hook for both the list and view operations.


function gadsense_block($op='list', $delta=0) {
   if ($op == "list") {
      $result = db_query('
   SELECT
      *
   FROM {gadsense_blocks}
   ORDER BY width + " x " + height ASC');

      $rows = array();
      while ($field = db_fetch_object($result)) {
         $block[$field->bid]["info"] = t('Google AdSense - '.$field->width.'x'.$field->height);
      }

      return $block;
   }
   else if ($op == 'view') {
      $result = db_fetch_object(db_query('
   SELECT
      *
   FROM {gadsense_blocks}
   WHERE bid = %d',
   $delta));

      if ($result->bid > 0) {
         $block['subject'] = 'Advertisement';
         $block['content'] =
   '
   <script>
   <!--
      google_ad_client = "'.variable_get('google_adsense_code', '').'";
      google_ad_width = "'.$result->width.'";
      google_ad_height = "'.$result->height.'";
      google_ad_format = "'.$result->width.'x'.$result->height.'_as";
      google_ad_type = "'.$result->type.'";
   //-->
   </script>
   <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
   </script>
   ';

    }

   if ($block)
     return $block;
   }
}



In the list operation, we select the gadsense blocks from out of the database and use them to populate the global block list. This is the "magic" behind our module.

Normally modules use hardcoded integers for indexes in the block hook, but as long as they are unique, Drupal couldn't care less what the index values actually are. Since we use the auto incrementing primary key column from our MySql database, we know the values will be unique.

Now, our /admin/block list will contain blocks that mirror our gadsense list:



The other operation we implement in our block hook is the view operation. This is what draws the block content - in our case, a Google AdSense ad.

The unique bid value that we used to index our block in the list operation is handed to us in the view operation. Then we just lookup the information about the ad's width, height and type and output the javascript that is required to call Google's script.

So, if we check the box to add the 468x60 ad block that we created, we'll see that it appears like this:




The Enhancements...

The finished version of the module that I created has numerous enhancements that you can easily add yourself, such as the ability to edit the block settings, delete a block, clone a block, change the AdSense tracking code, add an AdSense channel, etc.

Additionally, you can tie in to any setting that Google offers on the AdSense platform. Specifically, my module offers a color wheel to set the text color, border color, and other layout specific features.


The Google AdSense module is just one simple use for this concept. Dynamic blocks can be used in millions of situations such as allowing users to create their own blocks for display or share with their friends, index the blocks using the current date and show Holiday specific blocks, automatically import affiliate links and create dynamic blocks and many others. Once you start experimenting with it, I'm sure you'll come up with all sorts of unique ideas!





page 1 of 1
1 




Tags

script generation PHP interface SQL Server performance AlternativeNicheNetwork book review mathematics interview Generic Method Python Linux Win32 API VB programming languages Regular Expressions launch Microsoft Windows Mikomicon development programming utility concurrency network job technology dotnet ASP c plus plus operator software parsing SQL Adam Visual Basic Google syntax CosplayWar Windows internals math testing module hack driver Flash anime expert internals open source enhancements