Tuesday, April 30, 2013

Blog closed

After 27 months I have published posts and videos at Youtube, I decided to have a new chapter of my life. I will stop to blogging at May 1st 2013. If you want to contact me, please contact me via Twitter. I would like to thanks to my readers and God for opportunity to blogging tutorials and another stuff.

Thursday, April 25, 2013

How to create a virtual interface on CentOS or Debian

I was working on my DNS server and I wanted to create a virtual interface to distinguish internal requests from external requests. The command to create a virtual interface is:

ifconfig eth0:0 199.133.189.122 netmask 255.255.255.192 up

Tuesday, April 16, 2013

Design Patterns - Observer Pattern

Introduction

A design pattern is a specific way of solving a particular problem. They are not simply about reusing code; they are more abstract and generalized than that. They are about using reusing ideas.

The Observer Pattern

Often, you'll have data in your application in your application that changes over time. Say that you have some GUI components that are required to show this data and update it when it changes. How would you handle it? One solution might be to pass the newly updated data to a method of the GUI component so that could redraw the information. A problem with this approach is remembering to do that each time the data is updated. What if it's not clear how often the data will be updated, and whether you want the GUI to update automatically when it does?

The observer pattern solves this problem by using two interfaces, Observer and Observable. As the names suggests, the Observer "watches" the Observable to see whether it changes.

Keeping with the theme of human senses, an Observer is sometimes called a Listener, but for this chapter we stick with the former name.

In its most basic implementation, the Observable can add Observers. Observable then is responsible for notifying them if anything about its state has changed, and the Observer is responsible for reacting to the change. In this example, our data is the Observable and the GUI components are the Observers. If the data changes, those changes will automatically be reflected in any GUI component that is an Observer of the data. Next figure demonstrates the observer pattern

Widgets

Continuing with the previous example, you'll use the observer pattern to handle displaying price information for some of the instruments within graphical elements on a Web page. First, you'll need to define some simple graphical components. These components will be basic HTML table structures whose functionality will be contained within an object. Such components often go under the all-purpose term of widgets.

Designing the Widgets

A graphical widget will have two responsibilities. It will need to draw its own HTML so that it can be seen on a Web page, and it will need to update the data it displays. You may have noticed (from previous figure) that an update method is defined in the Observer interface.

Each widget is an Observer. The item being observed is the data representing the instrument name and price information. The data source is Observable.

All widgets, then, should implement the Observer interface. In addition, each one should descend from an abstract class that defines some shared functionality between widget objects. This is an example of using interfaces and abstract class together. Because the update() method is the same for all widgets, it can be implemented in the abstract class. The class diagram is shown in next figure.

Now all concrete implementations of widgets will descend from the AbstractWidget class. The AbstractWidget class in the turn implements the Observer interface. Notice the update() method in the class diagram for AbstractWidget is not shown in italics. This means that the method is actually implemented at that point. The # symbol indicates that the internalData property is protected. That means subclasses of AbstractWidget have access to it. If it were private, subclasses would not be able to access it.

All text and images above is from the book:"Professional PHP5" ISBN: 0-7645-7282-2 (Lecky-Thompson et al., 2005)

How to configure NTP Server and Client on CentOS

Introduction

NTP is widely used to synchronize a computer to Internet time servers or other sources, such as a radio or satellite receiver or telephone modem service. It can also be used as a server for dependent clients. It provides accuracies typically less than a millisecond on LANs and up to a few milliseconds on WANs. Typical NTP configurations utilize multiple redundant servers and diverse network paths in order to achieve high accuracy and reliability.

Configure NTP Server

  1. yum install ntp (and type 'y' when show up two questions);
  2. vi /etc/ntp.conf
  3. Add the next statement for accept requests from another servers (DNS server, LDAP server, etc.) on your LAN:
    restrict 10.50.0.0 mask 255.255.255.0 nomodify nopeer noquery
  4. Add the next statement for public servers NTP synchronize with server's internal clock on your LAN:
    server 0.europe.pool.ntp.org
    server 1.europe.pool.ntp.org
    server 2.europe.pool.ntp.org
    server 3.europe.pool.ntp.org
  5. This command allow to set current time from indicated NTP server:
    ntpdate pool.ntp.org
  6. The same server can be defined on configuration file:
    vi /etc/ntp/step-tickers
    pool.ntp.org
  7. service ntpd start
  8. Make sure the ntpd is active from beginning Linux boot;
    chkconfig ntpd on
  9. Checking synchronization status of service NTP.
    nptq -pn
    tail -f /var/log/messages

Configure NTP Client

  1. This file allows to set server NTP to use for initial adjust clock during starting of ntpd:
    vi /etc/ntp/step-tickers
    10.50.0.254
  2. On the file /etc/ntp.conf, all you need is add server NTP's IP address to use:
    vi /etc/ntp.conf
    server 10.50.0.254
  3. Checking synchronization status of service NTP.
    nptq -pn
    tail -f /var/log/messages
    It can take long to see message of success. When I was configuring, it took about 1 hour.

Conclusion

This protocol allows to keep operating system's clock synchronized with high accurate, running at information of synchronism got from another NTP servers.

Although the configuration is relatively simple, NTP performances a very important role. The possibility to make sure that clocks from many servers are kept with right time is relevant and it is a important factor to correct of many applications. The correction of local time in operating system allows also ensure that logs recorded in operating system have correct time and that is important when I use logs to diagnostic problems of some services.

Validation Email Address: Server side vs Client side

The week ago I was programming my web application using PHP5.

There is two ways to validate email:

  1. On server side (using filter_var from PHP5)
  2. On client side (using input type email from HTML5)

PHP5 example

$email = "xpto@email.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "This ".$email." email address is considered valid.";
}

HTML5 example

<form action="file.php" method="post">
  E-mail: <input type="email" name="useremail"><br>
  <input type="submit">
</form>

In my opinion it is better validate email on client side instead server side. So don't validate email on server side because it takes a lot work to warn user that has an invalid email address. With client side to validate email, it saves a lot work to warn user that has an invalid email address.

But if you are programming a web application using OOP, it is good idea to validate email from server side because you are not using form to validate email but you are using object, for example:

class User{
  private $name, email;
 
  public function __construct($name,$email){
   $this->name=$name;
   $this->email=validate($email);
  }
  
 private function validate($email){
  $mail = "";
  if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $mail = $email;
  }else{
    echo "This $email is not valid!";
   }
  return $mail;
 }
}
$user = new User("Peter","peter@xpto.com");//valid email
$user = new User("Paul","kjfddks");//prints "This kjfddks is not valid!"