Simple PHP Echo Server

May 1st, 2013

Sometimes you just need to see what you are sending as part of your HTTP request, to make sure the wrappers and the libraries you are using are not malfunctioning.
A simple echo server is very useful for this.

Here is a very simple one, which works with Apache Webserver and it prints out the raw Body of request.

Hope it will help someone.

<?php
$headers = apache_request_headers();

echo "-- Headers --<br/>\n";
foreach ($headers as $header => $value)
{
    echo "$header: $value <br />\n";
}

echo "-- Body -- <br/>\n";
$raw_body = file_get_contents('php://input');
print_r($raw_body);

General, PHP

Written by: MajiD Fatemian  

Got bitten by Python’s default parameters?

April 17th, 2013

Python’s default parameters are very convenient to use. Python programmers usually get bitten (once) with the fact that in Python functions are first-class objects and default parameters are kind of “member data”. So this would happen:

def foo(a=[]):
    a.append(5)
    return a
>>> foo()
[5]
>>> foo()
[5, 5]
>>> foo()
[5, 5, 5]
>>> foo()
[5, 5, 5, 5]
>>> foo()

The detailed explanation of why this is happening could be found here and here.

But just keep in mind that the best way to not fall in to it, would be:

def myfunc(value=None):
    if value is None:
        value = []

Python ,

Written by: MajiD Fatemian  

Deploying digital cross-connected systems

April 10th, 2013

This is an old article which my colleagues(@jmarchadier and Stephen Watson) and I wrote for Ubi’s Engine Room Blog.

 

General

Written by: MajiD Fatemian  

My 2 Cents on HTML5 vs Flash web Games

April 3rd, 2013

I totally agree that HTML5 is a strong choice for web-based gaming in the future, but I believe that’s not the case for the present.

I attended a talk in GDC-Online where the title was:
“Creatively Bypassing the Limitations of HTML5 as a Gaming Platform”. [Available at GDCVault]

HTML5 offers variety of tools for the game development, but as you can guess from the title they are not easily achievable, you have to do some tricks to get them working, could it be for 3D, Shading, WebGL, Audio Streaming and so on and so forth.
In HTML5 era, consistency across browsers is a big concern. When it comes to the details, each browser has different implementation and yet they are doing some experiments; adding and dropping some features and APIs.

Even if we take IE out of the question, there are still some solutions working only with WebKit, or only with Firefox…

Adobe is taking a right choice and pushing both worlds, Flash and HTML5. I think they both will exist for a long time.

Adobe using Adobe AIR is now able to compile native apps for Android and iOS (and windows phone in future). This is amazing.

I have done a test using Three.js for mobile. A simple rotating 3D object, it’s really disappointing. Working on Android for 3-4 FPS and not working on iOS at all.
Instead AIR compiled mobile app of a very similar 3D rotating object, is very smooth on both Android and iOS.

To me HTML5 is very nice, lots of feature and very bright future. But still has to mature (specially as a gaming platform).

Flash is consistent across platforms, and lots of tools for development. Also something else that I wanted to point out is their new Profiling Application “Adobe Scout”, which is an amazing tool to profile your Flash App either on desktop or mobile. Not only frame by frame but also, per movie clip and you can drill down to the point to see what’s taking memory or CPU. This is a big advantage especially for game development. (http://gaming.adobe.com/technologies/scout/)

As a fan of both, I think each has their own advantages and disadvantages. Just don’t be bias and pick the right one for your needs. Don’t go for what the buzz is.

p.s: Adobe has dropped the XC APIs as a Premium Feature, and now they are available to all the developers and there is no loyalty.
“”"As of January 2013, Adobe is no longer classifying the XC APIs as a Premium Feature. The XC APIs are now available as a standard feature without requiring a separate license agreement with Adobe, nor royalties.”"”

ActionScript, General, HTML5

Written by: MajiD Fatemian  

My 2 cents on Web Back-End Frameworks

March 27th, 2013

Today a colleague of mine was asking about other’s ideas about dropping WordPress as their main framework for development. As someone who worked with CodeIgniter for a while and in a high-traffic website, I had ideas to share which I would like to share with you as well:

 

I think dropping WordPress is a good idea. It has a lot of unnecessary footprints.

Indeed CodeIgniter is a fast and lightweight framework, among all the other available ones. I used to work with it for quite a while, and I was happy with it till it was hit with lots of traffic, then it started to show that even a lightweight framework has a huge impact on performance. We took the components that were heavily loaded out of the CodeIgniter and with a few tricks performance was improved enormously.

Developing based on frameworks is fast and easy at the early phases of development, but later in the project when you want to do something which the framework is not intended for, you will find your hands tied.

You have to either extend the core, or hack it. Both, means you have to understand underlying layers of the framework, and that is time consuming.

I believe the best way is to implement your own core architecture. This way you master the core and you can tailor it to your exact needs.

Then you can use libraries/components/frameworks around it for specific features. (e.g. Django’s admin features are amazing and super-fast to setup, but you could only benefit the Admin section from Django and not the whole architecture).

YOU should be the Master of your application’s framework and not vice versa.

I heard once this beautiful saying about frameworks from “Stefan Priebsch” (@spriebsch); imagine “Ruby on Rails”, it’s on “Rails” so it’s very fast, but when “The Rail” twists, you have to twist too, you have no other choice!

That was my 2cents on back-end architecture. Here is a good article, worth reading.

Architecture, CodeIgniter, General, High Availability, load-balance

Written by: MajiD Fatemian  

MySQL Error 2003 – 10061 – Connecting to a Virtual Machine

March 15th, 2013

If you are trying to connect to your MySQL instance on a virtual machine (VirtualBox / Ubuntu) and you are getting the following error :

ERROR 2003 (HY000): Can't connect to MySQL server on 'xx.xx.xx.xx' (10061)

There are two tiny little configurations that you might be missing:

1 – User’s Permissions to access MySQL from the host machine

 

By default all the users in MySQL are considered to be connecting from the local machine, not any remote host. So you should add a new user or giving the privileges to an existing user to be able to connect from your host machine or anywhere else. Here is how to do it:

GRANT PRIVILEGES ALL ON *.* TO 'new_user'@'host_name' IDENTIFIED BY 'new_password';

You could replace “host_name” by the real machine’s name or “%” to make it accessible by any host ( watch out for the security flaws if you going for a wildcard access)

2 – MySQL configuration

 

In the my.cnf file, you should specify for MySQL which IP address to bind to. So you can open up the my.cnf ( which in my case is located in : /etc/mysql/my.cnf) and edit the following line :

bind-address = 10.xxx.xx.xx

And this is the IP address of the Guest OS.

P.S.: This is with the assumption that the Network Adapter in the Virtual Machine is set to Bridged, so the host and guest OS are having separate IP addresses.

Hope it helps.

General

Written by: MajiD Fatemian  

rePost : Performance 101

June 27th, 2012

This is a very useful post on how to improve the performance.
It covers the tuning in 4 levels :

  1. Infrastructure (Hardware) improvements
  2. Apache Settings
  3. PHP
  4. Application performance

And it has very useful tips.

Source : http://www.bootstrappingindependence.com/technology/how-to-improve-website-performance-with-drupal-php-mysql-and-apache/

General

Written by: MajiD Fatemian  

Merging Python List of Dictionaries based on specific key

April 12th, 2012

This post is completely based on a cool post on StackOverflow.

So this is the situation, you have two list which they have dictionaries in and you want to merge the dictionaries if they have the same value for one specific field.

x = [{'id':2 , 'name': 'majid'} , {'id':3 , 'name':'maral'}]
y = [{'id':2 , 'num': 22} , {'id':3 , 'num': 33}]

And you want to get the final result like this :

[{'num': 22, 'id': 2, 'name': 'majid'}, {'num': 33, 'id': 3, 'name': 'maral'}]

The function which does the job is as follow (Written by Adam in StackOverflow) :

def merge_lists(l1, l2, key):
    merged = {}
    for item in l1+l2:
        if item[key] in merged:
            merged[item[key]].update(item)
        else:
            merged[item[key]] = item
    return [val for (_, val) in merged.items()]

courses = merge_lists(user_course_score, courses, 'course_id')

Now you can test it easily :

merge_lists(x, y , 'id')

General, Python, Tips , , ,

Written by: MajiD Fatemian  

MySQL Memory Engine Index

March 12th, 2012

By default, Memory Engine uses Hash index type to index the table, which will make it pretty much unusable and very slow performer for prefix matches and range look-ups.

Changing the index type to BTree, would dramatically boost the speed and performance.

TABLE lookup
(id INT, INDEX USING BTREE (id))
ENGINE = MEMORY;

Check out this post for further information and bench marking.

General

Written by: MajiD Fatemian  

MySQL Asynchronous vs. Semi-Synchronous Replication

January 25th, 2012

When it comes to choose your MySQL replication setup you have the choice between Asynchronous replication or Semi-Synchronous replication. At the time of writing there is no fully-synchronous solution for MySQL replications.

The way these two differ is interesting and would be very useful when you are choosing your architecture.

In MySQL Manual for semi-synchronous replication it is said very well :

  • With asynchronous replication, the master writes events to its binary log and slaves request them when they are ready. There is no guarantee that any event will ever reach any slave.

     

  • With fully synchronous replication, when a master commits a transaction, all slaves also will have committed the transaction before the master returns to the session that performed the transaction. The drawback of this is that there might be a lot of delay to complete a transaction.

     

  • Semisynchronous replication falls between asynchronous and fully synchronous replication. The master waits after commit only until at least one slave has received and logged the events. It does not wait for all slaves to acknowledge receipt, and it requires only receipt, not that the events have been fully executed and committed on the slave side.

So as you can see the ideal situation in terms of data consistency and no data-loss would be the fully-synchronous solution. Which on the other hand may result in a lot of delay in the performance of the system and will make the responses slower, as you are dealing with (at least) two nested levels of transactions. (Although fully-synchronous is not available)

On a Asynchronous solution, Master writes the events in the binary log and it may happen that no Slave would pick it up after Master has crashed or any other reason.

Semi-Synchronous seems to be good and practical solution for many cases where High Availability and No Data-loss is important, but you should consider that semi-synchronous “does not provide strong guarantees against data loss“. [article]

You may ask why ? It is as simple as this, imagine Master has sent out the event and one slave has received it, then Master will commit. But on the other hand the slave could have possibly crashed or timed-out or an error happens. In this way you have received the commit on the client, while in reality data has not been committed on the Slave. [article]

Just wanted to point out the differences and ask you to be careful when you are choosing you solution for replication. Semi-Synchronous Does NOT guarantee no data-loss.

 

High Availability, MySQL, Replication, SQL , , , , ,

Written by: MajiD Fatemian