Code::Blocks: Switching to a previously active tab

This was one of my qualms with Code::Blocks when I started using it. I’m used to working with VSCode and even with Visual Studio which have the feature that allows you to return to the previously active tab with Ctrl + Tab. While it might be a little annoying to get used to at first, it’s a really handy feature.

In Code::Blocks the same key binding does something else, it switches to the next tab in the chronological order that they are placed on the dashboard. For this reason, I began to search if there was any possibility of there being a way around this. What I found was surprisingly simple. The shortcut here is Alt + Arrow_Key. Might take a little getting used to, but I am glad that it exists!

Using doAsyncResult in Anko

I was stuck on this for a little while since it wasn’t updated in the documentation and also since I am still a newbie at Kotlin 😉

Some of the description has been already added in the code, as you can see. Let me explain the premise of this solution in any case. The problem arose because I had to perform a network task in the onCreate method and as you know we can’t normally perform a network task in the uiThread. Luckily, Anko provides an elegant solution to this without having the need to implement an AsyncTask interface. doAsync runs whatever task you give it but does not return anything. However, although doAsyncResult is similar to doAsync it can additionally return an object.

A Merry Christmas to you!

Switching over from http to https

Hip hip hurray! I recently made the switch using a popular and open source service known as Let’s Encrypt. Its service provides an SSL layer over the connection so that information sent to the server will not be understandable by the attacker even if they listen on the network. Well, to be honest I’ve been wanting to do this for a long time, but I did not know about this free service before! It works really well and a couple of tutorials helped me set up my Nginx WordPress installation for https.

So yeah, those lines above did it for me. But before you add them to your Nginx configuration file, follow the steps given on the Let’s Encrypt site. You need to start by installing certbot on your system, then create certificates and keys. Since these certificates expire after 90 days, you also need to renew them periodically. But rest assured, the setup was easier than I had expected, you should certainly give it a try. Not to mention that it’s free 🙂

Creating a Bootable USB

Recently, I had the need to create a bootable USB to install the new LTS version of Ubuntu on my laptop. I stumbled upon a tool called Rufus which did the trick very well.

But there were several problems along the way, most because there weren’t clear instructions on all the sites that I visited in order to learn how to create a bootable USB.

The first thing that you have to do is download the tool, whichever OS you are on. Run it, and browse for the disk image. Select the GPT partition style in the dropdown menu, and now the most important part:

Set Create bootable disk using to DD tools!

I was stuck here because I was using ISO image instead. When I tried booting my USB on startup I got a “Missing operating system” message. I got the same message even when I created the bootable USB using the tool provided by Ubuntu!

How the LinkedList class in Java works

I will explain how to perform operations like add, push, pop, poll, etc on LinkedList and what they mean. At first, it may seem a bit confusing, but I hope this article resolves a few conflicts.

A simple LinkedList of Strings declaration and definition looks like this:

There are several ways to add new elements to a LinkedList, but they mean and do different things. I will show you how each function works one by one.

add

One of the first methods you might encounter to add an element to a LinkedList is add. You can make use of add function in this manner:

Now let’s try to do the same with push!

push

We can conclude from this that add puts the element at the first position and then moves on to further positions. push on the other hand inserts elements quite like a stack. The element that is inserted first is the last when we traverse the linked list.

Let’s look at ways to fetch elements from the LinkedList. For this, we need to assume that the linked list already has some elements. Assume that for the subsequent examples, the LinkedList contains elements added using the add method like above.

pop

Just a reminder that elements are added in the same fashion as in add.

Since pop and poll are nearly the same (poll returns null when the list is empty whereas pop throws an exception) I will not discuss poll.

pollFirst

Just a reminder that elements are added in the same fashion as in add.

As can be seen, pollFirst and pop function produce similar output.

pollLast

Just a reminder that elements are added in the same fashion as in add.

And finally, pollLast!

The confusing yet awesome behaviour of the DatagramSocket class

One of the recent projects that I am doing involves using UDP datagrams for communication. The DatagramSocket class in Java enables me to use that feature. At first, I wasn’t aware of all the features of the class, but as I started coding and explored different methods of using that package, several more things became clear to me. Well, firstly the documentation and tutorials that I looked up to understand how to use the class gave me a very minimalistic approach to using it. So, let me clarify how this class manages the network and how you can use this.

Let’s start with the constructor. For a minimalist working server, you must provide a port (otherwise packets might not get delivered to you). Basically, the DatagramSocket class takes care of listening to a port and sending data from that same port (if needed). It does not automatically send ACKs when a packet arrives. It listens, halts the process till data arrives.

A client on the other hand does not require a port to be specified in the constructor for minimalist working. Why? Because we are assuming that the client is not listening for any data (for the time being). It only sends the provided data to a specific port specified by an InetAddress. Now there is an important thing to understand about all this. If a port is not specified by the client, it randomly chooses one. Not specifying a port does not mean that a port is not required by the client to send the data! When the DatagramPacket arrives at the server’s end, the server can find out from the packet several things like the InetAddress of the source and the port through which the packet was sent. This allows the server to send data to that source through it’s address and on the port that it’s using.

The conclusion that can be drawn from these facts is pretty cool. A thread that’s listening on a port can receive packets from multiple sources, and in the same way a thread that’s sending packets can send them to different hosts!

UPDATE:
The real important thing to remember is that you cannot use the same socket simultaneously for sending and listening. If a socket is listening on a certain port, you need to either make it stop listening and send your packet away or you need to open another socket to do so.

Of course, it is not possible to listen on multiple threads using just one socket. For this reason, one of a good architecture designing techniques for such a mechanism is as follows:

  • Listen on one socket
  • Send packets using another socket

Playing around with fx-991MS

I have a Math exam coming soon. Thought I should share a few tricks I know with the calculator most of use. First one you probably have encountered: solving a function (say f(x)) when the values of variables are given. I mostly used this while working on problems of Numerical Methods last sem. It is useful for monotonous calculations where you have one function that you don’t constantly want to re-type. Here’s how you go about doing it:

Do you see the A, B, C, D, E, F, X and Y in red? Those are your variables. Surely, these many will suffice in your equation. Assuming f(x, y) = x^2 + y^2
You press ALPHA + ‘)’ [or X] (or any other variable of your choice), then the ‘squaring’ button (x^2) and finally you do the same thing with Y. After that is done, your equation is ready! Press CALC. It prompts you for the values of X and Y respectively. Enter them and your calculator will have the answer for you.

The second trick is ‘Solving for X’. It is similar to the previous method except this is an equation not an expression. So, don’t miss out the ‘=’ in your equation (which can be inserted using ALPHA + CALC [or =]). Assume the equation to be 3X = 39
Type the equation now press SHIFT + CALC [SOLVE] twice. Yes, twice. If you press it once, it will prompt you for the value of X. When you press it twice it solves the equation. It may take time depending on the degree of the equation.

How to install flash player for Chromium

I am currently using Debian “Wheezy”, and I’ve been using it for a while now. Recently, I realized that I was browsing the web without Adobe Flash Player installed on my system. Although, I did a normal apt-get install flashplugin-nonfree things did not work. So I searched a little more and found Google’s custom flash player called Pepper Flash Player.

Installing PFP is a bit of a pickle unless you have already added backports to your repositories. To add the backports repositories, you need to add a line to sources.list file which is present in  /etc/apt/.

Add this line to sources.list,
deb http://ftp.debian.org/debian wheezy-backports main contrib non-free

Now update your package list,
apt-get update

Almost done, now install package pepperflashplugin-nonfree,
apt-get -t wheezy-backports install pepperflashplugin-nonfree

Done!

Scraping Animax India!

Since Animax India Community (AIC) is going to be taken down soon, I decided to make script to scrape what I wanted to keep. People might look at it as a failure, because it fails to deliver exactly what they want, but to be honest, it doesn’t matter.

The amount of knowledge I have gained in the past few days is not bad at all. I should take breaks more often, I am unable to think.

Let’s cut to the chase. I stopped developing only because I get a unicode error when I use the Beautiful Soup library and I think some files got corrupted.

/usr/lib/python2.7/dist-packages/bs4/dammit.py:231: UnicodeWarning: Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.
“Some characters could not be decoded, and were “

I think I know how this happened but it is probably because my laptop shuts down because of power cuts. My laptop doesn’t have a battery because I removed it. It’s dead that’s why. Well anyway, there have been quite a few power cuts here recently. So yeah, you can figure the rest. If you get any errors let me know.

That’s not all, the HTML file that I scrape is full of random meaningless characters. Makes me nauseous by just looking at it.

But here is what I have done. It should work on your computers (hopefully). You need to have the libraries: Beautiful Soup (python-bs4), Mechanize (python-mechanize). Of course, you need to have Python too! I am running it on 2.7.

Note for Windows users:
Download the libraries using this: https://pip.pypa.io/en/latest/ or https://pypi.python.org/pypi/setuptools

Finally, here it is.
https://mega.co.nz/#!epphnTSK!_0Jr8J1Fu4qaxUXZoa84StSflqRT21LYkEMLA3x5nCE

This is how you run it:
Go to the terminal or command prompt, whichever OS you use. Navigate to the location of the file. Execute the following:

python aic_scraper.py

(P.S: Don’t forget to set the path variable!)
Have fun!

Update:
Everything works fine now.

My favorite Chrome Extensions

Google Chrome is the most used web browser; more than 50% of internet users use Chrome (according to W3Schools). Apart from the amazing user experience it offers, Google has made sure that it can be modified as per the user’s wish. For that, Google has provided chrome users a webstore. Everybody has their own preferences and the wide variety of apps and extensions on the Chrome Webstore caters to exactly that.

Being a regular internet user for almost five years now, I have spent time choosing apps that would best suit me. Here is a list of the apps and extensions that I like.

1. Adblock

Adblock is by far my favorite app on any browser. It works perfectly, blocking ads as I go surfing the web which makes my browsing experience much more annoyance-free. If you haven’t tried it yet, I highly recommend this to you. It always shows up in reviews like ‘Top 10 Chrome Extensions’, so it has to be good. There is a good alternative to this called Adblock Plus, which may actually be more popular than this. I have tried that one too, but I was not sure how to compare the two, I mean they (just) block ads, what is to compare? Well anyway, kudos to a fantastic idea and implementation.

2. Facebook Unsee

A nice little extension that gives you anonymity on Facebook chat. It makes sure that the person or the group you are chatting with does not know whether you have read the messages sent to you. Basically, it blocks the response sent from your computer which tells the server that you have ‘Seen’ the messages sent to you. I had installed it a long time ago, and I don’t even know if the developer keeps it updated. But it works, so I won’t be switching over to any other extension any time soon.

3. Google Dictionary

A pretty handy app that is useful while browsing. Just double click on a word it to select it, and a tooltip will appear with a definition of the selected word. A nice feature that it has is that you can even look for the meaning of a word in the definition that is showed in the tooltip. If you want to look more definitions, just click on the extension’s icon on the top-right corner after you have selected a word. If even that doesn’t satisfy you, it links you to a  Google search of the word.

4. Photo Zoom for Facebook

This extension should win an award! It is greatly useful while on Facebook. What it does is, it enlarges any picture (it may be a profile picture too!) that your cursor hovers on, so you don’t have to click on a picture to look at its finer details. Simply amazing in terms of both the idea and implementation. This one extension can redefine your Facebooking experience.

5. Speed Dial 2

Nice interface, but its a little heavy. But now, since Chrome always runs in the background that isn’t an issue. It takes screenshots of the websites shown in the dial on its own. Another cool feature is that it can show your browsing history in the form of a pie chart which shows how many visits you have had to a certain website to sites that you normally visit. What disappointed me was the way the bookmark menu was shown. Same with the Chrome apps menu. They should have done something else, honestly. I haven’t really bothered searching for alternatives to a Speed Dial extension because I rarely use bookmarks and much less the apps.