Month: July 2015

Releasing NetworkEvents 2.0.0

2015-07-31 Android, Open source No comments

I’ve recently released NetworkEvents library v. 2.0.0.
It’s an Android library listening network connection state and change of the Wifi signal strength.
It has a few significant changes in the API and new features, which people were requesting on GitHub.
Moreover, codebase was slightly refactored and updated.

I’m going to keep major.minor.patch convention AKA semantic versioning now:

  • major – new feature or update – backward incompatible
  • minor – new feature – backward compatible
  • patch – bug-fix – backward compatible

A few changes are backward incompatible, so I’ve increased major number.

Below, you can find list of changes. As you can see, there is a lot of stuff. That’s why I’ve made the most important changes bold.

  • removed withPingUrl(url) method
  • removed withPingTimeout() method
  • removed withoutPing() method
  • removed withoutWifiAccessPointsScan() method
  • removed Otto dependency (now, it’s available only for unit tests)
  • removed example-disabling-ping-and-wifi-scan app sample
  • removed example-ping-customization app sample
  • removed NetworkHelper class and moved its method to specific classes with changed scope
  • moved permissions to Manifest of library
  • disabled WiFi scan by default
  • disabled Internet connection check by default
  • added BusWrapper, which is abstraction for Event Bus required by NetworkEvents object
  • added example-greenrobot-bus app sample
  • added enableWifiScan() method
  • added enableInternetCheck() method
  • added getWifiScanResults() method in WifiSignalStrengthChanged event
  • added getMobileNetworkType() method in ConnectivityChanged event
  • added JavaDoc at: http://pwittchen.github.io/NetworkEvents/
  • updated existing sample applications
  • updated documentation in README.md and library code

Feel free to download, use or fork this library!

Routing internet via SSH tunnel on Raspberry Pi

2015-07-25 Linux, Network, Raspberry Pi, Ubuntu No comments

Problem overview

Recently I had the following problem. I wanted to connect to my Raspberry Pi from my laptop. For some reason WiFi dongle on the Raspberry Pi was not working properly. Moreover, I had only one slot available for the LAN connection via cable to the router, so I could connect only one device this way. I wanted to have reliable and fast internet connection on my laptop, but via WiFi it’s quite slow.

To sum the things up two goals were clear:

  1. to have fast and reliable internet connection on the laptop
  2. to be able to connect to Raspberry Pi via SSH in order to control it from the laptop

Enabling SSH server on Raspberry Pi

Before we proceed to the next steps, we need to enable SSH server on Raspberry Pi if we haven’t done it in the past.
In order to do that, we need to go through the following steps:

  1. login with the following information when prompted:
    username: pi
    password: raspberry
  2. type the following command in the terminal: sudo raspi-config
  3. then navigate to ssh
  4. hit Enter
  5. select enable ssh server
  6. reboot device
  7. we are done!

Sshuttle to the rescue

I tried a few different approaches to solve described problem, but up to now the only one solution satisfies me well.
I’ve found discussion starting with the question How do I route my internet through a SSH tunnel? In this discussion, I’ve read about great tool called sshuttle, which works as a poor man’s VPN.

sshuttle is a transparent proxy server that forwards over a SSH connection and sets up a proxy by running Python scripts on the remote server. sshuttle can be run under the following conditions:

  • client machine or router is Linux-based, FreeBSD or Mac OS
  • administrative privileges on client
  • access to remote network via SSH
  • no administrator privileges on remote network
  • availability of Python on remote server

sshutle can be installed with the following command:

sudo apt-get install sshuttle

The basic command for running sshuttle with routing all traffic is:

sshuttle -r username@sshserver:port 0/0

Upon the execution of the command, a sudo password prompt will appear and subsequently the password to SSH account. No other details will appear except for a short message and return to shell upon failure. For more status messages, run sshuttle in verbose mode with the -v flag.

In this example all internet traffic except DNS is routed through the VPN. -r flag denotes the remote hostname and optional username and port that follows in the above example. 0/0 is short for 0.0.0.0/0 that represents the subnets to route over the VPN. The usage of 0/0 routes all the traffic except DNS requests to the remote server. DNS tunelling is possible with the usage of -H flag.

Please read the man page (man sshuttle) for the details of options and modes under which sshuttle can run. For information about the concept and more examples, refer to the project page.

– André Paramés on askubuntu.com

Connecting to Raspberry Pi and tunneling internet connection

Luckily, all requirements were satisfied by Raspberry Pi and I could use it with sshuttle, so I’ve installed this software on my laptop.
I’ve connected to my local network through WiFi. Then, I’ve scanned network with nmap to find IP of Raspberry Pi.

nmap -sP 192.168.1.0/24

My local network consists of only few devices and each of them has a description, so I could easily identify IP of the Raspberry Pi.
Output looked like this:

Nmap scan report for livebox.home (192.168.1.1)
Host is up (0.0070s latency).
Nmap scan report for laptop-piotra-*samsung-ultrabook*.home (192.168.1.14)
Host is up (0.00011s latency).
Nmap scan report for pc36.home (192.168.1.17)
Host is up (0.018s latency).
Nmap done: 256 IP addresses (3 hosts up) scanned in 2.64 seconds

Right now, active devices in my network are: router, my laptop and Raspberry Pi, with IP: 192.168.1.17.
After that, I connected to Raspberry Pi with the following command:

sshuttle -r pi@192.168.1.17 0/0

It asked me about the local password and then about the password of remote host, which is raspberry by default and I saw the following message:

Connected.

Everything was fine. I was connected to Raspberry Pi and I was able to access internet through it.
Next, I opened another terminal window and tried to connect to Raspberry Pi via SSH.
Please note, that you have to enable remote access via SSH on Raspberry Pi before you try to establish connection.

When SSH is enabled, you can type the following command:

ssh pi@192.168.1.17

Then I typed correct password and I was connected!

Linux raspberrypi 3.18.7+ #755 PREEMPT Thu Feb 12 17:14:31 GMT 2015 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Jul 25 01:49:00 2015 from pc36.home

Short summary

What is surprising tunneled internet connection was fast and stable when I was accessing it through Raspberry Pi.
When I was accessing internet directly through WiFi on the router it was really slow for some unknown reason.
Luckily, I’ve achieved my goals: I’ve fast internet connection on the laptop and I coul establish SSH connection with Raspberry Pi over local network.

Searching word in a string with KMP algorithm

2015-07-22 Algorithms, Java No comments

Sometimes it’s good to revise some algorithms and try to implement them in order to get to know, how the built-in functions of high level programming languages actually works. One of the popular problems is string searching. We have many approaches to solve this problem. For example:

  • Naive string search algorithm
  • Rabin-Karp string search algorithm
  • Finite-state automaton based search
  • Knuth-Morris-Pratt algorithm
  • Boyer-Moore string search algorithm
  • Bitap algorithm

This time, I’ve decided to focus on Knuth-Morris-Pratt (KMP) algorithm. It’s quite easy to implement, when you understand it. In addition, its time complexity is quite good and can be defined as O(n).

In this algorithm, we simply go through all letters in a given string and compare them with searched string. When position of searched string reaches length of searched string, we can assume, that our string was found. If two compared letters are different, we set position of searched string to zero and start new search from the next position after which we started searching process before. It’s quite good described on Wikipedia with some pseudo-code. You can also take a look at my code in Java available below, which I’ve written just for practice.

Result of the execution of this program should be as follows:

String found at 15 position.

Please note, that we count position of the letter from zero like in the most cases in Computer Science.

Releasing Weather Icon View v. 1.0.0 for Android

2015-07-21 Android, Open source No comments

Today I released Weather Icon View library v. 1.0.0
It’s very simple and easy to use Android library, which allows you to use Weather Icons created by Erik Flowers in your mobile applications. This library was already available on GitHub as I wrote in one of my previous posts, but today it was released to Maven Central Repository and you can use it as Maven or Gradle dependency. I was encouraged to make this project more available by the open-source community and people who find it useful as I could read in the issue reported on the repository website. Thanks for that.

You can find more details, usage examples and sample app in the repository of the project at: https://github.com/pwittchen/WeatherIconView.

By the way, this is article no. 100 on this blog. Thanks for your visits. 🙂

Glances – light, but powerful system monitoring

2015-07-11 Devops, Linux, Python No comments

Recently, during reading information in The Art of Command Line repository, I’ve found Glances, which is a cross-platform curses-based system monitoring tool written in Python. It’s light, but very powerful tool, which allows to monitor your system. Moreover, it’s open-source.

glances

Before you install Glances, you need Python, PyPI and Psutil. If you want to run Glaces as a web server, you also need to install Bottle.
To monitor temperature of your CPU, install Pysensors.

You can install the most important packages with the following command:

$ sudo pip install psutil bottle pysensors

and then install Glances

$ sudo pip install glances

After that, just run glances

$ glances

If you want to run Glances as a web server, type:

$ glances -w

open the following address in your favorite web browser: http://<your_ip>:61208/ and replace <your_ip> with appropriate value.
If you just want to check it locally, you can use this address: http://127.0.0.1:61208/.

Get past the syntax, the real scare is in the semantics

2015-07-06 Java, Software Development No comments

Dr. Venkat Subramaniam presented an interesting point of view during his talk at Devoxx conference. Programmers, who start learning a new language, often complain about syntax. They focus on using available keywords and constructions instead of trying to understand their meaning and purpose. Programmers also have their own habits. That’s why Java has similar syntax to C and C++. Creators of Java designed new language with syntax, which was familiar to them, with significant improvements. Perception of the world is based on experience and things we get used to. The same rule applies to programming languages. Most software developers are familiar with imperative programming paradigm. They often focus on describing computation in terms of statements and changing program state. This approach leads us to necessity of creating a lot of temporary variables and boilerplate code. Let’s have a look at the following code snippet:

This code prints sum of the square roots of the first ten even numbers starting from zero. It’s not so complicated task, but there is a lot of code, temporary variables, two loops, we have to be careful with operators to avoid ArrayIndexOutOfBoundsException and so on. Moreover, code is quite hard to analyze and we can make a mistake in many places. Let’s see what happens when we use functional programming approach, Java 8 with stream API and lambdas. We can do the same task in the following way:

As we can see, the code is simpler, cleaner and easier to analyze. The only difference is the fact that we changed approach from imperative to the functional one and applied different semantics. In this case, instead of focusing on how to do the task, we focused on the result we want to obtain. Instead of learning only syntax, we should spend more time on learning semantics to understand its purpose. This will allow us to create better and robust solutions in less time.

This article was also published as a part of summary of Devoxx 2015 Conference in Kraków, Poland on technical blog of Future Processing:
http://www.future-processing.pl/blog/devoxx-conference-summary/

Oh My Zsh!

2015-07-01 Linux No comments

If you are Unix user and don’t use Oh My Zsh, you should definitely start. Oh My Zsh is a framework for Zsh, the Z shell.
It has many plugins and great community. Actually, it’s enhanced and more intelligent terminal. I find it very useful while working with Git. It’s much more comfortable than via default Ubuntu shell. In addition, you can customize its look & feel. My favorite theme is minimal. It’s just a piece of functionality of this tool, you can discover much more by yourself.

daveverwer

You can install Zsh in the following way:

$ sudo apt-get install zsh

To set it as your default shell type:

chsh -s /usr/bin/zsh

Now, you can install Oh My Zsh via the following command:

$ curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh

Project is open-source. You can find it on GitHub: https://github.com/robbyrussell/oh-my-zsh.
Visit official website at: http://ohmyz.sh/.