How to build and install FreeSWITCH 1.6 on Debian 8 Jessie

FreeSWITCH is an opensource telephony soft switch created in 2006. As per official wiki  page,

It is a scalable open source cross-platform telephony platform designed to route and interconnect popular communication protocols using audio, video, text or any other form of media.

Sounds good. right ?

We are using Debian for this tutorial as it is very stable & mature linux distribution and FreeSWITCH core developers’ choice-of-distribution .
You can read more about FreeSWITCH on there wiki page.

Now lets cut a crap & start an action, assuming you  already have working Debian 8 OS.

Build & Install FreeSWITCH

There are different ways to install FreeSWITCH. In this tutorial, we will see how to install it from source.

  1. First update your Debian box & install curl & git.

  2. Add FreeSWITCH GPG key to APT sources keyring.

  3. Add FreeSWITCH repository to APT sources.

  4. Once again update your system.

  5. Now lets first install FreeSWITCH dependencies.

  6. Though above step takes care of most of dependencies, few still remains required to compile mod_fsv. So install them as,

  7. Grab source code of FreeSWITCH as follows,

  8. Now lets compile FreeSWITCH source for version 1.6

  9. Now lets compile sounds

  10. Lets create simlinks to required binaries to access them from anywhere.

     

Set Owner & Permissions

Starting FreeSWITCH service on boot automatically

To start FreeSWITCH after each boot automatically we need to set up init script. Init script is script used by init system to manipulate services. Debian 8 is now migrated to systemd init system, we will add systemd unit file.

Copy following content to ‘/lib/systemd/system/freeswitch.service’

Now execute following commands in your shell

Start FreeSWITCH

Now we are all set. Lets start to hack FreeSWITCH.

 

Notes

  1. If something goes wrong & you try compilation again by ‘make clean’, sometimes you get errors regarding ‘spandsp’. To resolve them try to clean using
    ‘git clean -fdx’. For more info check this ticket – https://freeswitch.org/jira/browse/FS-6405

 

How to write port-forwarding program using Twisted

Recently I was faced with an issue where a long running process is listening on loop back IP (127.0.0.1) on port 8080 on one of our servers and client programs on other machines are trying to access it on server’s local IP 10.91.20.66.  We ended up at this situation when we have updated server configuration and restarted the server program and forgot to change IP binding info in config file from loop back to local IP. Server got busy with it’s work, with lots of customer’s connections already, by the time we have discovered that some services of  server are not accessible to client programs on other machines. So, the dummy’s guide to fixing it by changing config and restarting the server program is not an option as we can’t risk to disconnect existing customers. So, hot patching is the only option until we can restart the program at next scheduled down time.

I could have fixed this in couple of ways either by adding few lines to iptables configuration or by writing simple socket program in python. The task is to forward data coming in on local IP port 8080 to loop back IP (127.0.0.1) port 8080 and send replies back to source address. Forwarding one socket data to other socket is pretty trivial using Python’s socket library and Twisted made it even more trivial, so I went with the following solution using Twisted.

That’s it. Now, all I needed to do is to run this program by the following command

This simple program is made possible by the heavy lifting done by twisted library. Interested folks can look under hood at twisted’s portforward.py module.

How to Fix G729a CODEC NEGOTIATION ERROR in FreeSWITCH

Are you facing issue of failing calls that are having G729a codec with 488 response? This article tells you how to fix that issue. When using G729 codec in FreeSWITCH if it receives following SDP in INVITE packet, that call is going to fail with 488 Incompatible Destination printing the error message mod_sofia.c:2226 CODEC NEGOTIATION ERROR.  SDP:

FreeSWITCH is not the offending party here, but the device that is sending G729 as G729a. The correct encoding name assigned to this codec by IANA is G729 not G729a.

To fix interop quirks like these FreeSWITCH has implemented some options that can be enabled. They are prefixed with NDLB means No Device Left Behind. The particular option that helps us with the current issue is

If you set this option to true in sofia profile as shown below, FreeSWITCH will be more forgiving to devices that are using non standard IANA codec names in SDP.

Note:- The situation described in this article has commercial G729 module loaded in FreeSWITCH

Installing SoftEther VPN server on Ubuntu

This tutorial is to show you how to install a SoftEther VPN on Ubuntu 12.04 & Ubuntu 14.04.
After installing a fresh copy of Ubuntu or your machine, run following commands to update the packages.

 

After updating add SoftEther PPA:

 

Update package list again & install softether package.

After installing run following commands to check for any errors.

Select option 1.  And press Enter for the rest. Then run command check.

 

2015-12-25 19_51_20-Clipboard

 

If you get output same as above image, then you have successfully installed SoftEther VPN server on your machine.

 

Introduction to Erlang part 1

Erlang is a functional programming language. If you have ever worked with imperative languages, statements such as i++ may be normal to you; in functional programming they are not allowed. In fact, changing the value of any variable is strictly forbidden.

We will start with installing Erlang.

Installation:

In Erlang, you can test most of your stuff in an emulator; it will run your scripts
and  it will also let you edit stuff live. To start,  open a terminal and then type $ erl. If you’ve set up everything fine, you should see text like this:

Shell

Shell commands:

If you type in i then c, Erlang should stop the currently running code and bring you back to a responsive shell. J will give you a list of processes running (a star after a number indicates this is the job you are currently running), which you can then interrupt with i followed by the number. If you use k , you will kill the shell as it is instead of just interrupting it. Press s to start a new one.

Now we will go through some basics

In the Erlang shell, expressions have to be terminated with a period followed by whitespace(line break, a space etc.), otherwise they won’t be executed. You can separate expressions with commas, but only the result of the last one will be shown (the others are still executed). This is certainly unusual syntax for most people and it comes from the days Erlang was implemented directly in Prolog, a logic programming language.

Here is example,

 

Erlang doesn’t care if you enter floating point numbers or integers: both types are supported when dealing with arithmetic.Integers and floating values are pretty much the only types of data Erlang’s mathematical operators will handle transparently for you.

Note that we can use several operators in a single expression, and mathematical operations obey the normal precedence rules.

If you want to express integers in other bases than base 10, just enter the number as Base#Value (given Base is in the range 2..36):


 

Invariable Variables

Doing arithmetic is alright, but you won’t go far without being able to store results somewhere. For that, we’ll use variables. If you have read the intro to this book, you’ll know that variables can’t be variable in functional programming. The basic behavior of variables can be demonstrated with these 7 expressions (note that variables begin with an uppercase letter):

The first thing these commands tell us is that you can assign a value to a variable exactly once; then you can ‘pretend’ to assign a value to a variable if it’s the same value it already has. If it’s different, Erlang will complain. It’s a correct observation, but the explanation is a bit more complex and depends on the = operator. The = operator (not the variables) has the role of comparing values and complaining if they’re different. If they’re the same, it returns the value:

IMPORTANT POINTS TO NOTE:

Data Types:

1) Terms: A piece of data of any data type is called a term.

2) Number: There are two types of numeric literals, integers and floats. Besides the conventional notation, there are two Erlang-specific notations:

$char ASCII value or unicode code-point of the character char.

base#value Integer with the base base, that must be an integer in the range 2..36. In Erlang 5.2/OTP R9B and earlier versions, the allowed range is 2..16.

Examples:

3) Atom

Examples:

4) Bit Strings and Binaries

A bit string is used to store an area of untyped memory. Bit strings are expressed using the bit syntax. Bit strings that consist of a number of bits that are evenly divisible by eight, are called binaries.

Examples:

5) Reference

A reference is a term that is unique in an Erlang runtime system, created by calling make_ref/0.

6) Fun

A fun is a functional object. Funs make it possible to create an anonymous function and pass the function itself — not its name — as argument to other functions.

Example:

7) Port Identifier

A port identifier identifies an Erlang port. open_port/2, which is used to create ports, returns a value of this data type.

8) Pid

A process identifier, pid, identifies a process. The following BIFs, which are used to create processes, return values of this data type:

spawn/1,2,3,4 spawn_link/1,2,3,4 spawn_opt/4

Example:

9) Tuple

A tuple is a compound data type with a fixed number of terms: Each term Term in the tuple is called an element. The number of elements is said to be the size of the tuple. There exists a number of BIFs to manipulate tuples.

Examples:

10) Map

A map is a compound data type with a variable number of key-value associations: ex: {Key1=>Value1,…,KeyN=>ValueN}

Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.

Examples:

11) List

A list is a compound data type with a variable number of terms. [Term1,…,TermN] Each term Term in the list is called an element. The number of elements is said to be the length of the list.

Formally, a list is either the empty list [] or consists of a head (first element) and a tail (remainder of the list). The tail is also a list. The latter can be expressed as [H|T]. The notation [Term1,…,TermN] above is equivalent with the list [Term1|[…|[TermN|[]]]].

A list where the tail is a list is sometimes called a proper list. It is allowed to have a list where the tail is not a list, for example, [a|b]. However, this type of list is of little practical use.

Examples:

13) Record A record is a data structure for storing a fixed number of elements. It has named fields and is similar to a struct in C. However, a record is not a true data type. Instead, record expressions are translated to tuple expressions during compilation. Therefore, record expressions are not understood by the shell unless special actions are taken.

14) Boolean

There is no Boolean data type in Erlang. Instead the atoms true and false are used to denote Boolean values.

Examples:

15) Type converstions


 

 

How to integrate Celery into Django project

What is Celery?

Celery is a distributed task queue that allows us to execute jobs in background. This article explains how to set up Celery with Django to perform a background task.

Advantages:

  • Large or small, Celery makes scheduling such periodic tasks easy.
  • You never want end users to have to wait unnecessarily for pages to load or actions to complete. If a long process is part of your application’s workflow, you can use Celery to execute that process in the background, as resources become available, so that your application can continue to respond to client requests.

Celery uses brokers to pass messages between a Django Project and the Celery workers. We will use Redis as the message broker.

Installation

Before diving into Celery, follow the below setup points

Create a new virtualenv ‘venv’ using following command:

To activate the environment use command:

Install django and create a django project ‘myproject’. Make sure to activate a virtualenv, create a requirements.txt file and run the migrations. Then fire up the server and navigate to http://localhost:8000/ in your browser. You should see the familiar “Congratulations on your first Django-powered page” text. When done, kill the sever.

Let’s install Celery:

Now we will integrate Celery into our Django project with the following steps:

Step 1:

Inside the myproject directory i.e beside your settings.py create a new file called celery.py and add the following code in that:

Let’s break down what happens in the first module, first we import absolute imports from the future, so that our celery.py module will not clash with the library:

Then we set the default DJANGO_SETTINGS_MODULE for the celery command-line program:

Specifying the settings here means the celery command line program will know where your Django project is. This statement must always appear before the app instance is created, which is what we do next:

This is your instance of the library, you can have many instances but there’s probably no reason for that when using Django.

We also add the Django settings module as a configuration source for Celery. This means that you don’t have to use multiple configuration files, and instead configure Celery directly from the Django settings.

You can pass the object directly here, but using a string is better since then the worker doesn’t have to serialize the object when using Windows or execv:

Next, a common practice for reusable apps is to define all tasks in a separate tasks.py module, and Celery does have a way to autodiscover these modules:

Step 2:

To ensure that the Celery app is loaded when Django starts, add the following code into the __init__.py file that sits next to your settings.py file:

Project layout should look like:

Step 3:

Celery uses “brokers” to pass messages between a Django Project and the Celery workers. In this article, we will use Redis as the message broker.

First, install Redis from the official download page and then turn to your terminal, in a new terminal window, fire up the server:

You can test that Redis is working properly by typing this into your terminal:

Redis should reply with PONG – try it!

Once Redis is up, add the following code to your settings.py file:

You also need to add Redis as a dependency in the Django Project:

Test that the Celery worker is ready to receive tasks:

Kill the process with CTRL-C. Now, test that the Celery task scheduler is ready for action:

That’s it! You can now use Celery with Django. For more information on setting up Celery with Django, please check out the official Celery documentation.

Sending emails asynchronously using Twisted – Part 2

In Part 1 of article, we saw how to send blocking emails using ‘smtplib’ module & non-blocking emails using Twisted framework. In this part, we will see how to send asynchronous emails to multiple recipients using Twisted

  • Sending multiple emails

    Refer following script.This script sends emails to given recipients asynchronously. Here we have used twisted.internet.defer.DeferredList API. This API is very useful in some scenarios. Suppose you have to finish multiple task asynchronously and then you have to finish one final task. For examples, your program is connected to 4 different clients & and before shutting it down, you have to make sure that all connections are closed properly. In such cases, DeferredList API is used. Create deferrands of each task & make their list. Pass this list to ‘DeferredList‘ API which will return you another deferrand. This final deferrand will be fired when all deferrands in list will be fired.


     
  • Sending multiple emails using coiterator

    Though above script runs fine, there is one problem. Here, recipients number is very small. But suppose you have to send emails to millions recipients then will this code work ?. Refer function ‘send_multiple_emails’.


    Here we have used ‘for’ loop which is blocking. So until this ‘for’ loop is iterated, program will not move to next line of code. For 3 recipients iteration will not take much time however for millions of recipients, it will not work.
    So lets modify our code to work like generators.

    Here, we have used twisted.internet.task.coiterate API. This API iterates over iterator by dividing reactor runtime between all iterators. Thus we can send millions of emails asynchronously.

Sending emails asynchronously using Twisted – Part 1

  • Using ‘smtplib‘ module

It is very easy to send emails using ‘smtplib‘ module of python. Check following recipe.

But ‘smtplib’ module sends emails synchronously. So code execution is blocked until email is sent. To overcome this, lets try to send email asynchornously.

  • Using Twisted

For this tutorial we are going to use Twisted framework. Twisted is event-driven networking engine. It uses reactor-pattern. Twisted uses deferred objects to address waiting IOs. Deferred is more like subset of promises. Check following recipe to send asynchronously MIME message using Twisted.

 

 

Building hello world using Python Tkinter

Continuing our series of desktop GUIs, in this post we are going to go over the topic of building a small hello world program using Python Tkinter.

What is Tkinter ?

Tkinter is python wrapper/binding to Tk library. Tk was developed as GUI library for Tcl language by John Ousterhout. For many other high level language authors coming to programming scene in 1990s this seemed like easy tool to capitalize on in bringing GUI library to their language. That’s why you can find many different language bindings to Tk. Tk brought easy GUI building to programming masses.

Often times many people write off Tk or Tkinter as ugly old GUI library. Their claims are not completely unfounded. But, Tk in recent times made strides to bring modern look by implementing Tile, themeing engine. Tile is also called as ttk. With ttk in place look and feel issues of Tk are addressed.

ttk xp blue theme demo
Tile xp blue theme demo

But, Tkinter still lags behind in terms available collection of default widgets compared to other libraries like PySide or wxPython . However, Tkinter is still a good candidate if you quickly want to dish out a GUI without having programming styles imposed on you. (You will see these style restrictions in our next posts about wxPython and PySide)

Hello world

Now, we will go over building a simple hello world program using Tkinter. In this program we will create a simple window with “Hello world” text in it. And a button that say “Kill Me”. Clicking that button will cause the python program to exit.

tkinter hello world

That import * line saves your some typing.

This line instantiates Tk and creates main window of the program. Next we create a Label (a widget which displays text ) and call it’s pack method. pack is one three available layout methods provided by Tkinter (rest of the two being grid and place). The widget will not show up until you call pack.  Next, we create a Button widget and pack it, similar to that of Label. The interesting point to note while creating button widget is, we provide command argument to it with a reference of sys.exit function. A newbie mistake that is often made is, passing function with () next to it. That will cause the target function to be invoked immediately. You are only supposed to pass the reference of function and Tkinter will call it when button is pressed. So, when this “Kill Me” button is pressed sys.exit will be called and program exits. One, last important line to notice is root.mainloop() . This line sets the event monitoring loop in motion and Tkinter take control of the program from here.

 

How to create ascii art using VIM plugin DrawIT

Installing VIM plugin DrawIT

Install the plugin if it is no available in your machine

This script/plugin is distributed as a vimball

This can be downloaded here

To download source go to /usr/local/src Create a directory VIM. go to the directory /usr/local/src/VIM (NOTE: it isn’t manadetory to keep source in this location, it’s upto you)

Dowload the source with following wget

wget http://www.vim.org/scripts/download_script.php?src_id=21108 -O DrawIt.vba.gz

To install follow the instructions

Open file vim DrawIT.vba.gz In command mode enter :so %

quit :q

Quick start with DrawIt

That’s all you have to do to install a plug in. Now to test, open a new file

and type the following in command mode

\di

If you see [DrawIt], the plugin is enabled an you can draw you art.

Here is how you will start and stop DrawIt

\di start DrawIt \ds stop DrawIt

User your arrow keys to draw whatever you want

To get help on drawit and to find handy key to draw, type the following in command mode in VIM :help drawit.