mosh : The answer to ssh annoyances

I got to know about mosh recently from a tweet by gaveen. Mosh is a replacement to the good old ssh command. It solves some of ssh’s biggest annoyances.

mosh handles connections internally

When you’ve ssh’ed to a server and your internet suddenly drops, that ssh connection gets broken. With mosh, you wouldn’t have to worry about this because it will silently connect you back to the server once the internet is back and you won’t even know. So even if you sleep the computer, when you wake it up again the connection would be back on just as you left. When you open up your laptop in a meeting or a conference, that connection will still be there. I haven’t closed one mosh connection to the Best At LK server in more than a month!

mosh echoes keypresses instantly

Another headache with working on ssh connections is the lag while typing. If you type in something it will take some moments to reflect, which can be really annoying. Mosh’s work around for this is giving an instant response to typing. 90% of the time, what the server would do is just echo the key you typed. So why not do that without waiting for a response from the server? Mosh underlines those keystrokes till the response comes, so you would know. Rest of the time when the keypress does something else, mosh would adjust accordingly after the response comes.

Getting started

If you use a debian-based distro (like Crunchbang or Ubuntu), you can install mosh with apt-get.

sudo apt-get install mosh

For other distros and operating systems, the homepage has installation instructions.

If you’re feeling rather adventurous, or you’re the type that needs to stay in the edge, you can clone the mosh git repo and build it. The build instructions are in the home page as well.

Note that you have to install mosh in your server as well. Worry not, it doesn’t run as a daemon. It starts up only when you connect to the server.

Now ssh to the server using mosh, instead of ssh.

mosh user@111.222.333.444

Mosh respects ~/.ssh/config as well! So you can continue using your favorite aliases.

Gotchas

Mosh needs to open a UDP connection. In an ordinary vps this wouldn’t be a problem, but if it’s an important production server the server admin might not be willing to open up a UDP port. If you don’t have any contact with the admin, you might be out of luck.

In Amazon EC2 instances, the UDP ports are usually closed at the beginning. To open them up, add a ‘Custom UDP Rule’ in the Security Group settings in EC2 dashboard. In the rule, set the range of ports as 60000-61000.

Colombo Show HN Meetup

Update: Parinda has more to say about the meetup.

We had a small Colombo Show HN Meetup this weekend.

Show HN
The plan was to hold a Colombo Hacker News meetup. Then we thought it’s better to make it a Show HN meetup, where participants show some work they’re doing to the audience, explains the developer stories behind it and the crowd gets into a constructive discussion. A meetup of this nature is more engaging and actually worthwhile to everyone, unlike most of the meetups where two or three individuals do ‘talks’ and the crowd applauds in approval. More often than not, constructive criticism never takes place as the audience is rarely willing to comment or ask questions, at least here in Sri Lanka.

So a handful of us got together and organized a small, invite-only meetup this Saturday.

First to show his work was Chethiya. He demonstrated a browser extension which he calls the Web Task Switcher. He’s trying to solve the problem of tab overload in browsers. We open a new tab to search for one thing, then go on and on, clicking on interesting links we find, finally ending up with dozens of tabs and not really finding what we searched for either. Chethiya’s extension identifies this behavior, breaks down multiple searches into tasks and lets the user ‘switch’ between, save and close the tasks. At the moment, the project is available only for testers in the Chrome Web Store and will be released soon.

Next up was Thameera (which, funny enough, happens to be me). I demonstrated how grunt was used in several of my projects to automate the compilations, builds and testing. Half of the discussion was spent showing how cool zsh was though, which is much, much more important than automating your workflow IMNSHO.

Chanux walked us through configuring an nginx server using Lua, and using redis to store the redirections. This way the system admins no longer need to edit configuration files and restart nginx each time a new redirection is added or updated. A demonstration was done using a server placed in a Docker instance. Chanux is hoping to open-source the project, which he calls Rusty, some time in the future.

Then came Parinda explaining his newest frontend Javascript framework Sweet.js. Yes, there’s already another popular Sweet.js library out there and Parinda’s considering renaming it to a new name. (God bless him trying to find a name not used as a JS project). Sweet looks both like a cut down version and an improved version of Backbonejs. It’s lighter than Backbone as Sweet doesn’t implement sync or event handling. But it adds some useful functionality like multiple inheritance for models/views and an improved router, which supports HTML5 history states. It’s used in production at Forestpin and Nearby.lk and is freely available at Github with an MIT license. An annotated source that uses litcoffee can also be found there.

Chathuras then showed the Javascript charts plugin he’s creating. It uses RaphaelJS to render the charts as vectors, so this would work even in old browsers like IE 7. Since the computations involved can be frustratingly slow in older browsers, the pixel values for the graphs are calculated in the server and sent to the frontend library. We went on to talk about how worthwhile it is to focus on older browsers and choosing a single selling point that would make the library go viral.

We are hoping to have regular Show HN meetups, at least once a month. Most probably we’ll be increasing the number of invitees each time while making sure it doesn’t turn up to be a regular, one-person-talks-others-just-listen meetup. A meetup where you can show what you’ve made to like-minded developers, get their honest feedback, and learn from what others are doing.

Did Deep Blue win because of a software bug?

A few days back I watched the famous documentary ‘Game Over: Kasparov and the Machine‘. The IBM supercomputer Deep Blue defeated Kasparov at chess, a feat nobody thought was possible back then. Garry Kasparov was the ‘Classical’ World Chess Champion at the time (the FIDE champion was Anatoly Karpov).

Game Over: Kasparov and the Machine

The documentary is based on the epic 1997 rematch between Kasparov and Deep Blue. In fact, the first round of games happened in 1996 which ended with Kasparov winning 4-2. After much tweaks to the Deep Blue engine, IBM challenged for a rematch in 1997, which Kasparov accepted.

Deep Blue computer
Deep Blue

Kasparov won the first game after 45 moves. The turning point of the series was the second game. In the 37th move, Deep Blue played Be4!? which caught Kasparov off guard. After a few more moves Kasparov resigned and claimed that Be4 was not a move possibly considered as good by a machine and accused IBM of cheating. IBM fired back saying that Deep Blue wasn’t a regular chess program and if anyone thought it would play like a normal chess computer would do, that would be a big mistake.

Kasparov making a move against Deep Blue
Kasparov making a move against Deep Blue

Kasparov requested IBM to provide the game logs to check how Deep Blue concluded that Be4 was the best move, to which IBM did not comply. This would be fair during the series, as the logs would reveal the thinking pattern of Deep Blue which would lead to an unfair advantage to Kasparov. However, they could have at least released the logs later on. Several studies that followed concluded that a software bug helped Deep Blue win, i.e. it had chosen a random move in a seemingly dead-lock situation.

To make things worse, it was soon revealed that Kasparov could have drawn the game with a perpetual check. Critics claim that Kasparov had met with a mental breakdown after hearing this, which led to his defeat in the 6th game, after three more draws (3, 4, 5), thus making Deep Blue the winner of the series.

Kasparov resigning in Game 6
Kasparov resigning in Game 6

Deep Blue was retired soon after this match and no further development followed. Just like they retired sending people to the moon after the first successful attempt in 1969. The computers have become faster and more efficient by now and there exist dozens of popular chess engines like Deep Fritz, Rybka and Shredder. It’s not clear whether these actually exceed the power of Deep Blue.

‘Game Over: Kasparov and the Machine’ is an exciting documentary, featuring Kasparov himself and the actual scientists who built the Deep Blue. You can watch it in Youtube. Also, an annotated version of the second game can be found here.

Embarassing bugs in open source C/C++ projects

Viva64 develops PVS-Studio, which is a static code analyzer for C/C++/C++11. But their popularity has come from the fact that they run this software on quite a few major open source projects around the internet and post the results. They hit the mark of 1000 error samples yesterday.

Their bug database is a treasure trove. For one they expose bugs in open source projects that may not be easily seen by the naked eye. And it’s a great learning resource to see common pitfalls.

This bug from the Chromium project is obviously a hapless victim of copy-pasting:

bool AutoFillProfileHasName(const AutoFillProfile& profile) {
  return
   !profile.GetFieldText(AutofillType(NAME_FIRST)).empty() ||
   !profile.GetFieldText(AutofillType(NAME_MIDDLE)).empty() ||
   !profile.GetFieldText(AutofillType(NAME_MIDDLE)).empty();
}

Most probably the programmer intended to put NAME_LAST at the end.

Dereferencing a null pointer in Clang. Ouch.

void MatcherGen::EmitLeafMatchCode(const TreePatternNode *N) {
  ...
  if (DI == 0) {
    errs() << "Unknown leaf kind: " << *DI << "\n";
    abort();
  }
  ...
}

And in this snippet from WinMerge, it might not be clear what the programmer has intended at first.

void CDirView::GetItemFileNames(int sel,
  String& strLeft, String& strRight) const
{
  UINT_PTR diffpos = GetItemKey(sel);
  if (diffpos == (UINT_PTR)SPECIAL_ITEM_POS)
  {
    strLeft.empty();
    strRight.empty();
  }
  else
  {
     ...
  }
}

It turns out that the intention was to clear the string. Actually it should’ve been strLeft.clear(); and strRight.clear();. The static analyzer has caught the issue because the return value of empty() is required to be utilized, which is not done here.

Feeds in your inbox

Google Reader will be dead by the 1st of July. Everyone has moved or at least is considering moving to new feed aggregators. There is a heap of options out there (out of which I recommend that you try out Newsblur). But none of them has been able to boast of a good public API as our good old Google Reader did.

I used to have this IFTTT recipe that would send the articles of the most important feeds I follow to my inbox. Now that Reader is dying and no other options out there seem to offer IFTTT channels, the best solution seemed to be rss2email.

rss2email (how appropriately named!) is a tool used to send new feed items to your email inbox. It has been first written in Python by Aaron Swartz (who happened to commit suicide a few months back). The source can be found in github.

Prerequisites

It would be much convenient if you have your own server (or a shared host). Even a desktop that runs on Linux or Mac OS X would do, but make sure it stays powered on most of the time and has a constant internet connection. This is because rss2email should be invoked periodically to check feeds using a cron job. To send email, you need to have a mail server installed and configured. If you’re using a web host, this might have already been done for you. Also make sure you have Python installed.

Setting up rss2email

1. Download

Download rss2email with wget and extract.

wget http://www.allthingsrss.com/rss2email/rss2email-2.70.tar.gz
tar xf rss2email-2.70.tar.gz

2. Configure

This is the most important step. The newly extracted directory contains a file called config.py which contains all the necessary configs. Most are self-descriptive. Make sure you configure the mail server settings correct. Here’s my setup:

SMTP_SEND = 1
SMTP_SERVER = “mail.thameera.com:[port]
AUTHREQUIRED = 1
SMTP_USER = ‘[emailid]@thameera.com’
SMTP_PASS = ‘[password]

You can set the email address that would be in the ‘From‘ field of emails in the DEFAULT_FROM config.

3. Set your ‘To:’ address

See the r2e script in your extracted directory? That’s what you need to invoke for the tasks we do from now on. First, set the address of where the mail should be delivered to using the following command:

./r2e new

4. Add feeds

Time to add those feeds. You can add them one by one using ./r2e add. For example, to add the feed of this blog:

./r2e add http://blog.thameera.com/feed

Now if you run the following command it would grab all the feeds and send them to your email:

./r2e run

However, it would send all the items available in the feeds in the first run. This could even be hundreds. Since most of you wouldn’t want this to happen, make sure you run it with --no-send first.

./r2e run –no-send

The next time you run r2e, it would mail only the new feed items.

5. Set the cron job

You’re almost done. The last step would be to set up a cron job that would invoke r2e periodically. If you don’t know how to set up cron jobs, be a lamb and look for a tutorial online. The cron job should cd to the r2e directory and run the command ./r2e run. You can set the time interval as you wish. A small interval means you get the articles mailed quickly, but would cause more server load and cost bandwidth. I’ve set mine to 15 minutes.

That’s it. No more Google Reader. You can sit back and relax while rss2email makes sure you never miss an important feed item. If you’re feeling adventurous, you can also go ahead and edit the python scripts to change the email format and tweak to make it more awesome.

Check your movies

Ever started watching a movie only to realize after 30 minutes that you’ve actually watched that one before?

Django Unchained
Django Unchained Official Poster

There was this guy in our uni who kept a poster of each movie he watched, so that he wouldn’t watch the same movie again, ever. When you look at a movie’s poster you immediately get a flashback of what it was about, who the cast was as well. Added bonus.

But now we don’t have to go into such lengths to keep track of the movies. We have the great big movie database IMDb and dozens of other sites made right for the purpose. But then comes the problem, which one to choose?

IMDb logo

If you look around, there are many who use the former, the International Movie Database. You sign up for an account, then start rating the movies you’ve watched. 9/10 for Reservoir Dogs and 4/10 for What Happens in Vegas and so on. After that, there’s nothing much you can do. Also note that you can’t check in for a movie without actually giving it a rating. Heck, I watched Original Sin so many years back, all I remember of it is the Antonio Banderas and Angelina Jolie having sex. No way I could _rate_ that movie now.

WatchTh.is

I was using watchth.is since about 2010 until I got tired of it a few months ago. It pulls data from the IMDb’s database and lets you check and favorite movies and comment on them. Other than being able to add friends, that’s pretty much of what it can do. Unlike in IMDb, you don’t have to rate anything. Just check them and feel good about it.

goodfil.ms

Then there is goodfil.ms. It has a modern-looking UI and some cool features like movie performance graphs from various sources like iTunes and Netflix. What I hate about the site is that it asks you for two ratings, the “critical quality” and the “rewatchability”. It trashes my idea of simply checking in the movie. Too complex for a simple guy.

iCheckMovies

I finally settled down with iCheckMovies. The site is simple and lets you just check the movies without the burden of having to rate them. You can favorite or dislike them, keep a watchlist, and so on. There are movie lists based on various tastes and you are awarded with medals if you finish watching half, three quarters, etc of these official lists. That’s an extra element of fun. Movie data are pulled from IMDb and the respective IMDb pages are linked to. You can even import your existing IMDb ratings, something most other sites don’t offer. Besides from the free tier, there are Basic and Pro pricing plans: it’s a startup with a business model. If you’re not much of a movieholic the free plan is just enough.

There’s a slew of other similar sites that lets you achieve much the same, like Rotten Tomatoes, Flixter and SeenTh.at. Some of them offer you much more than just checking in movies. Don’t be overwhelmed.

Finally it all comes down to personal taste. If you’re looking for one, make sure you check out iCheckMovies. IMHO, it does a rather good job at what it’s supposed to do. You can find me there.

This blog just moved

This blog just moved from wordpress.com to its own host and domain. The new URL is http://blog.thameera.com. The new RSS feed URL is http://blog.thameera.com/feed.

But why?

I dunno. Having my own URL is nice, maybe. But mostly, I was getting tired of the unpredictable wordpress.com admin panel. It just won’t load and stuff won’t work, and things have gotten worse by the day.

Also, this means you get full control over everything. It’s possible to customize this inside out, which I haven’t done yet. The Jetpack plugin gives most of the niceties wordpress.com had, like site stats. So not losing much there.

This new theme is ugly

I know. Might mess with it soon and bring it into some level.

Why not Jekyll?

I considered Jekyll, but decided to stick with WordPress for the moment. You _can_ use Markdown in WordPress and compiling the whole thing after each and every update to the blog didn’t appeal. Also, have you noticed that 90% of the Jekyll blogs out there look pretty much the same?  I might still change my mind though.

Old links get broken?

Not really. The URLs of the old wordpress.com domain should redirect to the new site without a hitch. So all the link juice from Google will still come here. I’ve changed the permalink style by removing the date in the post URLs. This won’t affect incoming links as I’ve setup the Redirection plugin to deal with these. Not quite sure if email subscriptions get broken or not. Just resubscribe with the new box in the sidebar.

But still, you should subscribe to the new feed and ditch the old one. The redirections might not work indefinitely. (If you’re using an intelligent RSS reader like Newsblur, chances are it has done this already for you.)

 Why all these subheadings?

Heh. Just felt like. 🙂

Action.io, django and poya days

Last week I got an early access invite to the private beta of Action.io. Coincidentally, I wanted to learn some Django. Hence Next Poya When? was born and it only took a few hours to start coding from scratch to deploying successfully in the Google App Engine.

Action.io is a place to start your projects without worrying about all the mundane setup tasks. Once you sign up, you can create boxes. There are cardboard boxes, plastic boxes, and.., oh wait, wrong kind of boxes. The boxes in action.io are comparable to VPSs. You can choose from Ruby on Rails, Django, Node.js and Go boxes to start with. If you get a Django box it’s already pre-installed with python, django and all other utilities required for a typical Django project. All the boxes come with a Web IDE, Emacs and everyone’s favorite Vim as editors. There is PostgreSQL and MongoDB integration as well.

Action.io interface
Action.io interface (click to enlarge)

To set up, all I needed to do was copying my .bashrc and .vimrc files, install some Vim plugins with Pathogen and add the SSH key of my box to Github. All this takes less than 10 minutes. (But don’t tell that to your Project Manager, give him 2 hours effort and he wouldn’t know!)

Now create a project and start coding right away!

django-admin.py startproject poya

Once you build stuff to some extent and want to test how it’s doing, the site can be previewed easily. For example, start the server with

python manage.py runserver 0.0.0.0:3000

and click on Preview -> Port 3000. You can choose from a range of ports from 1024 to 9999.

Previewing the site
Previewing the site

Once done, deploying was a piece of cake. Action.io has Heroku and Google App Engine integration. Deploying the site in GAE took only a single command:

appcfg.py --oauth2 --noauth_local_webserver update nextpoyawhen

Then I pointed my domain to GAE and, voila, we have nextpoyawhen.com moving like jagger!

It’s really amazing how fast you can get everything done with action.io. If I did all this in my laptop it’d have taken ages. It goes without saying how mundane and time-consuming it is to install all libraries, dependencies and get the environment set up in your machine. More often than not, we get tired of the project even before we really start it. We spend 80% of time setting the things up, and much less time coding.

Also, since action.io lives in the web, it’s accessible from anywhere. If someone reports you a bug when you’re at office or travelling, you can just log into your box from the browser, do a hotfix and deploy within minutes. No effort spent for syncing files. You get your favorite environment setup anywhere in the world.

Action.io doesn’t work offline though, at least yet. It doesn’t require a fast connection, but if yours gets disconnected often, be wary. I hope they would implement the offline capability soon.

As for Django, I’m impressed by the framework. It’s quite easy to catch up with, and it’s Python. You can host Django sites in most web hosting providers (unlike node.js), including Google App Engine and Heroku both of which have free plans to serve small sites. I’m studying with The Django Book, recommended by Raditha. Of course, nextpoyawhen could’ve been done with some client-side javascript, but that’d have defeated the purpose. I wanted to do something with django and try out action.io, so it’s like catching two stones with one angry bird, or whatever that is. The source code is hosted in github.

So, the next time you are tired of working all week and want to know when the next Poya holiday is on, you know where to look. 🙂

Louie gives me hope

Louie is a good show. It’s the kind of show I’ve always wanted to watch. Not many shows interest me. TBBT was good for its humor, Two and a Half Men was equally good and Spartacus is good with all its blood. For the record, I’m not watching Spartacus for its sex. Of course you don’t believe.

louie

I wasn’t aware of Louie till @mdsoysa mentioned it on Twitter about a week back. Almost finished the first season and it’s hooked me up. For one, the episodes are just 20-25 minutes long, which is the right length for a TV episode. The black humor, when done right, is tempting, as in the case with Louie. But the best thing about Louie is, well, Louie. He’s the kind of washed up guy, single father, doesn’t really have much hope, but despite all of this, he’s cool and not complaining. He’s like myself, but done right. I ain’t no single father, but you get the idea. Even the opening credits scene cheers me up. And the music isn’t half bad.

I’ve decided to do stuff that cheers me up. Like watching Louie. And coding. Get immersed in a sea of Python or Javascript and watch your worries get washed away! I like my job; it may not be exactly what I want to be doing, but it’s hell of a lot better than what most people do for jobs out there. I don’t think Louie loves his job anyway. He’s a comedian. Comedians comede because they have to make a living. Not exactly the reason I code.

Being away from places like Facebook lessens your chances of depression. Even Twitter can be depressing at times. It used to be fun and lively some time back. Nothing is what they used to be anymore. I even registered at app.net looking for solace, but that ain’t the place.

Just sitting in your room with a lot of tic tac and a mug of Coke can cheer you up. Do you know what’s cool about tic tac and Coke? There’s this tiny little chance that you might die when you stuff yourself with a handful of tic tacs and a big gulp of Coke. That shit explodes when mixed right. Death by tic tac! Awesome!

Not sure if there’s anything that makes any sense in this post. Just ignore for good. It’s just that I like Louie coz it cheers me up.

Typing.lk beta released

The day before the christmas Thilanka said, ‘how about we build a Sinhala typing tutor?’. I said, ‘why not?’. We finally ended up building an English one. Its beta version is now live at http://typing.lk.

Typing.lk is basically a place to learn and practice touch typing. The site has three modes at the moment: Learn, Practice and Code. The first needs no explanation, it’s where you can learn to touch type. The Practice mode gives you stuff to practice typing with, like common English words, quotes from books, etc. The Code mode is for, well, coders. You can choose code snippets from you favorite programming language and practice typing them.

The three modes at typing.lk
The three modes at typing.lk

One thing we wanted to make sure was that the site should be hassle-free. You can start typing from the moment you visit typing.lk. No sign ups or any such crap. We _might_ bring an option to sign-up and keep track of your typing in the future, but that would be an _option_. And we tried to keep the design minimal.

And about the Code mode. Currently typing.lk has code from four major programming languages: C++, Java, PHP and Python. More languages will follow in the coming days. All the code snippets were extracted from popular repositories at github. Some snippets have been modified to suit the purpose and fit in the site.

Typing.lk is by no means perfect yet. The site may get effed up on some small screens. It’s recommended that you use Google Chrome. Works well in Firefox as well. Not on IE though. Who uses IE anyway?

It’s just a small start; we are hoping to keep adding cool features in the days to come. These include a better lesson structure, a more engaging experience, more languages for the Code mode and, of course, typing games. We might even implement a separate section to learn typing in Sinhala.

We’d love to hear your thoughts and suggestions of the app. Do mail us or send in a tweet. You can follow us on Twitter for updates and Like Typing.lk on Facebook. Happy typing! 🙂