Monday, March 30, 2015

Writing a DJango App - Part I


The example is to create a poll application. There are two parts of this application 

1. A public site that lets people view polls and vote in them 
2. An Admin site that lets admin to add polls , delete them 

To know whether Django is installed properly, below is the command that can be used 

python -c “import Django; print(django.get_version())”

If Django is installed, it will show the version number. For me, it shown as 1.9. 

Now to create a new Project, we need to run the command below 

django-admin.py startproject mysite

However, this was giving a lot of errors like below 

dministrators-MacBook-Pro-3:~ retheesh$ django-admin.py startproject mysite
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 2, in
    from django.core import management
  File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 68
    commands = {name: 'django.core' for name in find_commands(__path__[0])}
                                      ^
SyntaxError: invalid syntax

Investigating more on this, it looked like the Python version in which Django got installed is old version. 1.9 version of Django seems to require any python version greater than 3.x. Below few lines can identify in which python the django is installed. In my case, the python version that was installed from dmg file downloaded from python website was 3.4 

administrators-MacBook-Pro-3:~ retheesh$ python3.4
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named 'django'
>>> 

In the above, it says no module django found. At the same time, if run like below 

administrators-MacBook-Pro-3:~ retheesh$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> 

The above is the default system python which is in 2.7.6 and it successfully able to import django because django is installed in that python. 

Next series, need to work on installing the django on python 3.4 ! 

References: 

Sunday, March 29, 2015

IMS I-CSCF and S-CSCF

I-CSCF which is, Interrogating Call Session Control Function is one of the main elements in the IMS hardware architecture. I-CSCF is used to forward the SIP requests to the S-CSCF. There may be several S-CSCF in a network, I-CSCF helps to find out a right one. This acts as key element in Roaming methodology. 

The I-CSCF must find an S-CSCF in two instances

1. a P-CSCF forwards a SIP register request to the I-CSCF based on the home domain of the user. 
2. An S-CSCF forwards the SIP INVITE to the I-CSCF based on the domain of the called party. 

In both cases,  a CSCF node extracts the home domain of the originating or terminating endpoint and consults a DNS server to determine IP address of the I-CSCF 

Below is overall architecture of an IMS network. 




References:

Saturday, March 28, 2015

Android Launching System Apps

As part of my experiments, i had to launch two system apps Music Player and Map. Some notes from experience wrote down here. 

Below are few ways to launch native Android default applications from a Google play available application


Launching Music Player

 Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

This was working perfectly, however, in the code it was saying MediaStore.INTENT_ACTION_MUSIC_PLAYER is deprecated. 

The replacement for this was CATEGORY_APP_MUSIC 

As per the documentation, this Category should be used along with the ACTION_MAIN to launch music application. The activity should be able to play, browse, or manipulate music files stored on the device. 

This should not be used as a primary key for the intent since it will not result in the app launching with correct action and category. instead, we should use this with makeMainSelectorActivity(String, String) to generate a main intent with this category in the selector. 

However, below code was resulting in No Activity Found 

 Intent intent = new Intent();
                        intent.makeMainSelectorActivity(intent.ACTION_MAIN,
                                Intent.CATEGORY_APP_MUSIC);
                        startActivity(intent);

For time being decided to stay with the old approach itself. 

Launching google Map Application 

 String uri = String.format(Locale.ENGLISH, "http://maps.google.com/maps?daddr=%f,%f (%s)", 12.914081f, 77.609953f, "Cafe Coffee day");
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                        intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                        try
                        {
                            startActivity(intent);
                        }
                        catch(ActivityNotFoundException ex)
                        {
                            try
                            {
                                Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                                startActivity(unrestrictedIntent);
                            }
                            catch(ActivityNotFoundException innerEx)
                            {
                                //Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
                            }
                        }

The above code launches the map and shows the navigation view. 

Uri gmmIntentUri = Uri.parse("geo:?z=15");
                        Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                        mapIntent.setPackage("com.google.android.apps.maps");
                        startActivity(mapIntent);

The above code just launches the map view with zoom level as 15 and focuses the map with the current location of the user. 

References:

Thursday, March 26, 2015

iOS Silent Push notifications

This is a good feature especially as it helps apps to decide whether there is any thing valid in the message that is coming from the server. 

This needs both the client and server changes. the app specific changes are below 

1. Include the background run requirement as content update available. Below is the mode. “App downloads content in response to push notifications”

This is easy because we can just include this from Capabilities tab of the project. which will add this to the plist file. 

Change the code to include the new call back like below. 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
    NSLog(@"--- Application received remote notification ---");
    NSLog(@"-- User Info Received :%@",userInfo);
    
    UILocalNotification *notification = [[UILocalNotification alloc]init];
    notification.repeatInterval = NSDayCalendarUnit;
    [notification setAlertBody:@"Hello world"];
    [notification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    [notification setTimeZone:[NSTimeZone  defaultTimeZone]];
    [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
    
}

2. Modification to the server. 
On the server side, in general the “content-available:1” to be added in the payload. 

So the pay load could be something like this 

{
    "aps": {
        "content-available": "1",
        "sound": ""
    },
    "count": "1",
    "type": "1"
}

Below are few notes from the testings done

- If  app is running in background, when server sends a silent push, app receives it in background and app can do some task in background for a finite amount of time. And it can post a local notification or sit silent if no update to be given to the user 
- If the app was not running in the background or foreground, then OS invokes the app to give this notification payload and app can do brief operations.  
- IF the app was killed by user previously, the notification won’t be delivered to the app.  
- If the app was killed due to memory on device or device restarted for some reason etc, the app will be given the notificaton payload even if user don’t launch the app. 
- If the app was running in the foreground, then notification is directly delivered to the app. 


The page in the reference below, having good amount of nice content explaining the same thing. Nice pictures too on it !! 

References:

Wednesday, March 25, 2015

What is Django?


It is a high level Python Web Framework that encourages rapid development and clean pragmatic design. To install the framework, the reference link under reference section needs to be downloaded and installed. 

minimal Installation steps were given in the docs/install/install.text file. The steps to be followed in general are below 

1. Install python 
First of all, get the python framework from the link https://www.python.org/download/ 
The download gave a package 

And it gave the installation success like this below 

Apparently, there seems to be a Java Python implementation also, which is called Jython downloadable from http://www.jython.org/ 

To verify whether the python is installed, type “python” in terminal, it was giving something like below 

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Now, we need to import django, before installation, tried to run the django command it gave error like below 

>>> import django
Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named django
>>> 

Now for the installation, this text file was not giving any specific info, So after a search, it gave the below https://docs.djangoproject.com/en/1.7/topics/install/ 

and the installation is simple once after downloading the django package. enter the below command 

sudo python setup.py install 

after this, running the django command was giving like below. 

>>> import django
>>> print(django.get_version())
1.9
>>> 

References:


Monday, March 23, 2015

SIP Learning - Part IX - Registrar Flow diagram

Below is the overall logic at the Registrar end for processing a REGISTER request.



References:
https://www.ietf.org/rfc/rfc3261.txt Section 10.3 

Sunday, March 22, 2015

iOS checking WiFi Data usage

The data usage of a WiFi Device can be calculated like below. 

Below code snippet can help to get the data counters. 

BOOL   success;
    struct ifaddrs *addrs;
    const struct ifaddrs *cursor;
    const struct if_data *networkStatisc;
    
    int WiFiSent = 0;
    int WiFiReceived = 0;
    
    int WiFiPacketsSent = 0;
    int WiFiPacketsReceived = 0;
    
    int WWANSent = 0;
    int WWANReceived = 0;
    
    NSString *name=[[NSString alloc]init];
    
    success = getifaddrs(&addrs) == 0;
    if (success)
    {
        cursor = addrs;
        while (cursor != NULL)
        {
            name=[NSString stringWithFormat:@"%s",cursor->ifa_name];
            NSLog(@"ifa_name %s == %@\n", cursor->ifa_name,name);
            // names of interfaces: en0 is WiFi ,pdp_ip0 is WWAN
            
            if (cursor->ifa_addr->sa_family == AF_LINK)
            {
                if ([name hasPrefix:@"en"])
                {
                    networkStatisc = (const struct if_data *) cursor->ifa_data;
                    WiFiSent+=networkStatisc->ifi_obytes;
                    WiFiReceived+=networkStatisc->ifi_ibytes;
                    
                    WiFiPacketsSent+= networkStatisc->ifi_opackets;
                    WiFiPacketsReceived+= networkStatisc->ifi_ipackets;
                    



The complete code can be found here 

https://drive.google.com/file/d/0B0ZgwdHsnw1bTDhOVFNBekJIaDg/view?usp=sharing 

References: