Thursday, May 25, 2017

Raspberry Pi Settings


Hello guys, today, I am gonna share with you guys something different from programming, however, would be an interesting. Yes, Raspberry Pi is a computer, somebody would rather say "mini-computer". This is tiny but really powerful machine, on the other hand, consumes less power and resources. So, the best application of this device is to run 24/7 as embedded. I have looked around the online markets and the latest version is pi 3 (2015) costs around 38.00 €. This is powerful enough to carry out general tasks. It is suggested to buy memory card and housing also for better installment and other accessories based on requirements.

1) Operating System Installation

I have installed Raspbian Jessie Lite which is mini operating system and needs less space to install. I prefer mini version because we can later install whatever we want, rather than having unwanted application installed. The downloads are located here

https://www.raspberrypi.org/downloads/raspbian/

 The extraction of zip file gives an image which we have to write into our memory card. (Raspberry pi 3 needs micro SD card).

Raspberry Pi recommends Etcher tool to write images into the SD card, it is really cool tool for writing to SSD cards and it is available for Linux system also.

https://etcher.io/

After installation, we can run Etcher, then select image and drive and flash button. Just three step, and wait for a couple of minutes until flashing finishes,



2) Configuration

After image writing completes, we are ready to run Raspbian in our Raspberry Pi. Please note that the installed operating system has not GUI and we need to remotely access it and use secure connection tool (SSH) to connect it. To carry out this,we have two challenges, first, by default, the new operating systems have disabled SSH, second, for wifi connection, we need to set the wifi credentials also. So, lets begin:

a) Set WIFI

Via NIC card, the connection is automatic, we need to do nothing. The are nice tools (I used from http://angryip.org/) available and we can find out the IP address of the device. The default host-name is raspberry.

For WLAN, we have to provide WIFI information. We create a file wpa_supplicant.conf with the following information:


network={
    ssid="YOUR_SSID"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}

b) Enable SSH

There is a boot folder in the written image and we just create a file "ssh" (WITHOUT extension) in this folder, at the time of booting, it enables SSH so that use pi can access this with Secure Shell.

Default user name: pi
Default password: raspberry

For security reasons, it is advisable to change the password after you logged in.


Some Questions:

1) What is the power supply for Raspberry PI?
Raspberry Pi is really really cool, because it is suitable for everywhere regarding power supply. The smartphone charging cable completely fits. We can use our computer USB port or separate device(that comes with mobile charger) to provide power . That is enough!

2) Is there any chances of overheating CPU?
The short answer is no, because Raspberry PI manages itself to save from overheating and burning. It automatically detect the overheating and accordingly manages the frequency of CPU to reduce the temperature. So, basically saying, the temperature of RaPi never reach above 65 degree and you need not worry about temperate of Rapi which is above normal. The normal temperature is assumed to be 40-45 degrees. My RaPi always around 50 degree with normal operation and it reaches to 60 degree when I implement camera streaming application(motion). However, somebody prefers some cooling mechanism( head sink or fans). Heat Sink is okay, not recommended though, but use of fan makes simple Raspberry complicated, even a bit noisy.


Disclaimer: I have tested with Raspberry PI 3 version 1.3 (2015), may be the process is not exact for other Pi devices. 

Saturday, May 13, 2017

Unit Testing in Java

Why Testing? 

Testing is necessary to deliver a good quality product. Nobody is perfect and the development phase of a software is prone to error. So, if we find the software bugs at the beginning phase, we can reduce the maintenance cost. So, we need proper testing while we do programming and the developer can himself carry out some tests whether his function is working as desired. Basically, it is tedious to manually test all possibilities and the developer himself has tendency to test best case scenarios. But the output product is used by users who never know the details about it. So, a third party user has higher possibilities to discover bugs. But we do not get this chance until we deliver the product, and the customer also do not like buggy software, so the final delivered product should be Bug-Free.

JUnit Testing

In this article, I am sharing the steps on how we can use JUnit framework to test java classes.  To use JUnit testing, we need to include library file in class path. It can be downloaded from www.junit.org and include it as reference library.

How?

1) The unit testing files should be normally separated, you know why? These files are testing files only needed to developer, not the user. So, it is good practice not to include these test files in the production mode. Thus we create a separate folder to store test files.

2) Packaging makes it everything clear. So, we create the same package, and test classes with suffix Test.

Suppose, we create a class

package com.krishna.math;

public class MathFunction{
       private  int A;
       private int B;
       public MathFunction(int A, int B){
         this.A=A;
         this.B=B;
     }
    
public int getSum(){
     return this.A+this.B;
}

public int getDiff(){
    return this.A-this.B;
}  




Now, we create a test class. The above mentioned class MathFunction resides in src folder, while we create another folder test to store test files. Gradle and maven projects default structure is:

source: /src/main/java
test: /src/test/java

So, under test folder, we define a class to test the above-mentioned class functions:

package com.krishna.math;

import static org.junit.Assert.*;

import org.junit.Test;

public class MathFunctionTest{
     //Write test methods here
     @Test 
     public void testSum(){
       MathFunction mathFunction=new MathFunction(12,34);
       assertEquals(46, mathFunction.getSum());
     }

   @Test
   public void testDIff(){
    MathFunction mathFunction=new MathFunction(13,12);
    assertEquals(1, mathFunction.getDiff());
    }
}


So far so good, we can run it directly from eclipse, just right click and run as JUnit. That shows the test result. 

Running Multiple Tests (TestSuite Class )

The above mentioned method runs one test case (test class) at at time. But we are interested to test all test cases as a whole. So, the need of test suite class arises.

We define a test suite class as follows:

import org.junit.runner.RunWith
import org.junit.runners.Suite;

import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ LinkedListTest.class, MyStackTest.class })
public class AllTests {

}

After, we run it from eclipse IDE, it works like a charm. You see the result with proper coloring also, Eclipse helps you!

Test Automation 
So far, we have created test cases and test suite which is run by eclipse. What if you wanna run and get result and do some process. For that you need a test runner. It is an independent runner and you have control over the result of the test. You may create a separate class for test runner or just add it in TestSuite class. 


I create a separate class as follows:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner{
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(result.wasSuccessful());

}
}


The examples presented are just sample, the real test cases tend to have many methods. 





Wednesday, April 12, 2017

MySQL Server Installation

1. INTRODUCTION

Installation of mySQL Server in linux system is not that difficult. We just need to run a couple of commands and thats it!

2. INSTALLATION
 
Here are the required commands to install and prepare mysql server:

sudo apt-get update
sudo apt-get install mysql-server
sudo mysql_secure_installation 

Check the status of mysql server

sudo systemstl status mysql.service

This above command should show Active: active (running)  as output which means it is running.

The administration of mysql is carried out with mysqladmin command which can be run from terminal as follows:

mysqladmin -p -u root version

This will show the version information along with other information. 

3. LOCAL CONNECTION

We can use mysql to connect to the server as follows:

mysql -u root -p -h localhost

After connection, we need some basic commands as follows:

show databases;
use db_name;  
show tables; 
select * from [TABLE_NAME];
exit

We can see all the command by typing \h. 

 4. REMOTE CONNECTION

Local connection is not the problem we can directly use local server. But for security reasons, we can not connect if server is located remotely. We have to define some configuration settings to carry out remote connection. 

cd /etc/mysql/mysql.conf.d
sudo vi /mysql.conf.d

Check the section [mysqld] and modify the bind-address with the ip address of the server as follows:
bind-address =10.8.102.62

After restarting the server, we can now connect remotely.

Note: mysql uses port 3306, so this port should not be blocked from firewall. 

5. REMOTE USER CONFIGURATION 

Yes, the default root user is so configured that, only can connect locally. So, next task is to define this user so that it can connect remotely. That we do after carrying out local connection.

mysql -u root -p localhost
 
SELECT host FROM mysql.user WHERE User='root';

It shows localhost or 127.0.0.1 which means this user can not connect remotely. 

CREATE USER 'root'@'%' IDENTIFIED BY '[PASSORD]’;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

Reload permissions:

FLUSH PRIVIOLEGES;


Now, we can connect remotely with root user. % signifies the acceptance of connection from any external sources. 



Tuesday, February 28, 2017

Cassandra Walkthrough

INTRODUCTION

Today I am gonna write something about Cassandra database system.  Yes, first question comes in mind is why on earth do we need this database while already there are other more popular databases. Yes, that is true as far as our relational database has less amount of data. Since Company grows, so the database, and the conventional relational databases do not sufficiently fulfill our requirements to hold such a big data. We have to think about some alternative databases which overcomes this problem i.e. these no-SQL databases can store and access efficiently huge data.

WHY CASSANDRA?

Cassandra is no-SQL database which is used by large companies to store big data. The storage structure of cassandra is different from that of relational databases and provides faster data access with support of data replication in clusters. So, basically, cassandra can store huge data and faster access of data.

INSTALLATION

The installation of cassandra is very simple. We need to download cassandra from here

http://cassandra.apache.org/

in *.tar.gz format. We have to extract it somewhere (e.g. /opt/cassandra) and this is the root directory of cassandra.

So, to directly access cassandra related commands, we have to define /opt/cassandra/bin ad PATH directory. Just add this to /etc/environment file and restart the system.


STARTING THE SERVER

The cassandra/bin directory contains all the executables needed. To start server

cassandra -f (starts server in background, that makes it easier to close the server, just with CTRL+C)
cassandra This will start server in background. To close server, we have to kill the cassandra process id.

pgrep -f CassandraDaemon  (gets pid of cassandra)
kill pid or pkill -f CassandraDaemon

After server is started we can use the cassandra client to connect. Cassandra comes with a very handy client tool called cqlsh which connects to the cassandra server and we can execute queries to carry out database operations.

nodetool status
(Checks if cassandra is running )

Connection and running commands:
cqlsh localhost
cqlsh localhost -u cassandra -p cassandra 
describe keyspaces
use [KEYSPACE]
describe tables
exit  



NETWORK CONNECTION

Cassandra, by default, can be connected locally. That means, we have to do some changes in conf/cassandra.yaml file to carry out connection from network.
We have to not that, to carry out connection from network, the  port 9042 should not be blocked by firewall applications.

To carry out the changes, we open the file:

conf/cassandra.yaml

and fine the line listen_address line, and comment out, and define the IP address of the server.  e.g.

listen_address: 10.8.102.62

Again, we change the rpc_address also,

rpc_address: 0.0.0.0

and finally, we change the broadcast address 

broadcast_rpc_address: 10.8.102.255

If rpc_address is defined as a fixed address, then we can leave broadcast_rpc_address blank or commented. 

And, finally, we add seed provider also, here we have to add the ip-address as a seed provider. 

Just find the line which seed_provider and go to parameters, and there is already one seed defined, and you have to add the ip-address of the cassandra server. 

- seeds: "127. 0.0.1,10.8.102.62"

We save this configuration and restart server. Now, we can connect to the server from another computer in the network:


cqlsh 10.8.102.62


 

Tuesday, April 19, 2016

Deploying Django Project

Last couple of days, I am concentrating on python and giving some time in python related research. Initially, I found it really good for scripting, and later I noticed that it is really powerful language, can be used for desktop to web applications, from small testing applications to a big project.

Django is a framework developed in python and the web development using this framework makes the task simple, clear separation between view and controller. The view templates even can not insert custom codes, that is great, which separates coding from view(coding makes mess in html code)

So, creating a sample Django application and running through command is very easy which can be used for development purpose. And good thing is that, the changed code is reflected when browser is reloaded, that means we do not need to recompile the code. But what if I want to deploy to a dedicated server (I mean Apache2) ? It is not straight forward. Although there are many different possible configurations, I will focus on the basic configuration that make the website running.  Later we can extend it with more and more.


So, let us begin it.

1) Virtualenv

There might be other methods also, I will explain the methods which needs creation of virtualenv. So, first of all, we need python-pip which can be installed with

sudo apt-get install pip
sudo pip install virtualenv


I normally used Eclipse IDE and for a django project I have instally PyDev plugin which makes the task easier. With eclipse I create a Django project and after you have created it , you can directly test it(Thanks Eclipse, it creates everything for you).

Now we install virtualenv inside the directory so that we can deploy the whole directory to new server without worrying the server.

So, we run the following command from inside the project folder:

virtualenv ENV

Then,

activate it

source ENV/bin/activate


2) Install Django

We can now install django module using

pip install django

So, the basic project is ready to deploy.


3) Server Preparation

a) Module wsgi

My assumption is that we already installed apache2 . That is not enough, we need to install libapache2-mod-wsgi module also.

sudo apt-get install libapache2-mod-wsgi-py3
* py3 here means python 3
Now we provide the access definition for wsgi.py file in the project.

b) apache2.conf
We write the following line in /etc/apache2/apache2.conf

<Directory /Backup/workspace/java/misc/DjangoProject>
        <Files wsgi.py>
                Require all granted
        </Files>


c) Define site

Now, finally,  we define and enable site in /etc/apache2/sites-available. We create a file 0001-python.conf (for e.g.) with the following contents:

WSGIDaemonProcess sampleapp python-path=/Backup/workspace/java/misc/DjangoProject:/Backup/workspace/java/misc/DjangoProject/ENV/lib/python3.4/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias /sampleapp /Backup/workspace/java/misc/DjangoProject/DjangoProject/wsgi.py

And enable this site :

sudo a2ensite 0001-python.conf

sudo apt-get apache2 restart

Now, check your Django application running with the following URL:

http://localhost/sampleapp


Enjoy , you have successfully deploy your first Django application.


References:

https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/
https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps

Tuesday, April 12, 2016

Apache2 Server Subdomains

Apache2 Server configuration for multiple web applications is confusing for beginners. Since I have just started configuring apache server in my Ubuntu 14.04 system, I am going to share my experiences while installing and configuring Apache Server.

Basic Commands

Here are the basic commands need to learn:

1) Installation :
       sudo apt-get install apache2
2) Reload:
       sudo service apache2 reload
3) Restart:
       sudo service apache2 restart
4) Enable website:
       sudo a2ensite [WEBSITE_NAME]
5) Disable website:
       sudo a2dissite [WEBSITE_NAME]

Problem Scenario

So, the problem scenario is I have to install multiple web applications in the same host but with different URLs. And the web applications can be placed anywhere in the server. And I also discuss about defining sub-domains. 

For example, I have three web projects located in /home/frietec/www/redmine, /home/frietec/www/owncloud, /home/frietec/www/ldap and I want to access these projects with redmine.frietec.com, cloud.frietec.com, ldap.frietec.com.

Solution

The solution for this is not so difficult. We need to configure virtual host.  First of all, we have to define the virtual hosts in config file as below:




    DocumentRoot /home/kris/public_html
    ServerName myproject.localhost

    # Other directives here




    DocumentRoot /home/kris/public_html/myproject
    ServerName myotherproject.localhost
  
    # Other directives here

 
 
In this example, it is important that the defined sub-domains should point to the same local machine. So, we either place this information to a DNS server to publicly access. To test in local network, we modify the file /etc/hosts with the subdomains point to the same IP. This way removed the burgeon of remembering the ip-address of the system.

File: /etc/hosts

 127.0.0.1      localhost
 127.0.1.1      kpsharma
 192.168.1.82   myproject.localhost

 192.168.1.82   myotherproject.localhost 



Note: if we use IP address instead of domain-name, then it uses the topmost virtual host. In this case, the project "myproject.localhost" is called.

Friday, April 8, 2016

General Purpose IDEs

Update: Recently, I got to notice that Microsoft has recently released their own light weight IDE called Visual Studio Code(VSCode). After a couple of hours research and testing, I found this IDE really fast, light and with great intelliscent support. I was really impressed by Visual Studio IDE which is really good IDE when I was using Windows. The installer is less than 50 MB and can be installed in Linux/ Windows/ OSX. So, we should not use Windows or specific operating system to use it. Its really great feature. The code auto-complete is really really cool and great, which out-wins Atom, and we can install hundreds of extensions according to our requirements. I already tested python, html, javascript, css and VSCode works exceptionally. 

The only downside, not really downside, is the compilation and execution. Although we can define it in tasks, but somehow not-used-to for normal users. But if just look at the file tasks.json, you will understand everything and you can make your own compiler and runner, really great if you need your own compiler or executer or any custom task.



IDE stands for Integrated Development Environment which supports development of software application. IDE actually makes the task of software development faster and easier for a programmer. Thats why IDEs are so popular. There are several IDEs available, and depending upon our needs , we choose one which is better for specific requirement.


In this article, I present some overview on the currently popular IDEs and will provide my experience on there IDES. First I will talk about my requirement, and then discuss about my research work carried out to get the better one for my requirement. May be you guys also have the similar problem, then this article will help you to some extent.


First, lets talk about my background. I basically develop software using java programming language. Previously, I used Netbeans, but later, because of serveral plugins available in eclipse, I chose Eclipse as my primary IDE. However, both the IDEs are very very popular, and ranked on the top. Eclipse is, with no doubt, the best IDE we can get which is open source. I have heard of IntellisJ IDE for Java, I guess, the current Android IDE(i.e. Android Studio) is using this IDE. But I am not so interested on this, firstly because it is not free, secondly, I found it a bit odd for Eclipse users. Netbeans is good , but there are not a lot of plugins available. 


By the way, this article is not talking about Eclipse, this is very good IDE and very popular. No need to write more about this because everybody know about it. My concern is if I wanted to write short programs/scripts or small website, then opening eclipse(everybody know that eclipse is bloated IDE) which needs a lot of resources to load, make the development task slower.Creating a project, creating files, and many more eclipse generated files make it confusing and somehow complicated. For example, I want to write a python program, which is no more than 100 lines, or if I want to create a website with a couple of pages, then opening eclipse , installing plugins, installing java(Eclipse requires that), is a very lengthy process. For Python project, I installed a Python Eclipse plugin (PyDev) where I could devlep python projects, but I could not feel comfortable with it. I wanted better support (like auto code complete, documentation). Since eclipse makes confusing by creating many other supplementry files, which makes the deployment processes complicated. However, Eclipse is best IDE for Java and Java related frameworks. 

So, I started doing some research work regarding which IDE best meets my requirement: fast, easy, light IDE, which helps to create normal everyday task (like creating small scripts(PHP or Python) , creating a couple of web pages in html, Javascript, css or any other programming languages. I have installed several IDEs and tested if it fits for my requirements. 


My another requirement is support of Linux and Windows operating systems. I basically use bothe operating systems, and IDE should work both systems, you know what I mean, IDE should be platform independent. 

My third requirement is it should be opensource, you know why opensource, we do not need to pay any bucks for these IDEs. And we can have chance to support the product if we have interest, but that is not my concern.  Here I have listed a bunch of IDEs I have been familiar with and I will tell you why I did not choose that IDE. 


1) Notepad ++

It is very nice IDE, but not full support for specific programming language, For example, if you develope a python script, it would not support code auto-completion. But Notepad++ is a general purpose text editor, which has capability of sytay highlighting for several progrmaming languages. There are plugins alos, but I found it not so interesting. Another disadvantage is we can not directly install Notepad++ in linux systems.

2) Vim

Vim is also good, support syntax highlighing for several programing languages, but not enough to do some advanced tasks.

3) Blue Fish

This is good IDE, I basically did not like the menu structure. They are not properly managed. And code highlighting was not good(for me).

4) Bracket

This is very nice IDE, I have tested it and worked like a charm. Support almost everything, and simple concise IDE and everything properly positioned. We could see live pages so that we do not need to run same page again and again. This is a nice feature. Many programmers recommended this for website development, and it is really good.

5) Sublime Text

I have tested this also, but it has the same impression like Blue Fish, menus are not proper, or unwanted, or not normally needed(sometimes confusing also) . I can not tell more about this, because I have not fully tested it.


6) Atom

Finally, I have stucked with this IDE. This IDE is really nice where we can install packages with our requirements. For this IDE, everything is package, that is good for me, because I can removed unwanted packages to make the IDE faster. Some users say this IDE is slow, but I have not noticed it. Actually, it is not noticed for small applications which this article is talking about. Code completion is perfect, syntax highlighting super, and files management is so simple, we do not need any extra files, just a folder is enough. And menus are so proper and concise, I really liked this IDE. The best part is there are thousands of packages available which we can install for our specific requirements.



Here is my scenerio:

1) I want to develop a simple website: For this requirement, I need HTML, Javascript, CSS. And there should be some mechanism to view the output(better if it is live like Brackets). Yes it has everyting. To view live, we instll a package called browser-plus which previews a page, the page could be made live. And if we need, we can debug also. It has CSS and Javascript auto-completion and code highlighting.  So, for web pages, it is not the problem, everything is fine.

package: autoclose-html

2)  I want to write a script(Python) and run it: This is challenging, but it is not that difficult. We just need to install atom-runner which runs  the script.

Package for python: language-python

Python should be installed before you install the packages.

And there are several packages which you can search and install according to your requirement.

You can see how many times the packages are already downloaded which can give you a hint how popular (stable) is the package.


There are some interesting packages which help program development:

>> minimap
>> pigments
>> autocomplete-plus
>> autocomplete-xml
>> autocomplete-paths
>> terminal-plus
>> autosave
>> highlight-selected