bash directory bookmarks with autocompletion

March 3, 2012

Just stick this in your ~/.bashrc

declare -A bookmarks
bookmarks["myproject"]="/home/user/projects/folder/myproject"
bookmarks["tmp"]="/tmp"
keys=${!bookmarks[@]}
function bm {
if [ -z $1 ];then return; fi
local directory=${bookmarks[$1]}
if [ -n "$directory" ];then
cd $directory
else
echo "Directory does not exist"
fi
}
complete -W '$keys' bm

VNC in your browser through WebSockets, handled by Gevent

November 12, 2011

Reading on websockets the other day I ended in this article about websockets and vnc and it draw my attention immediately over the showmethedesktop project.

Now, given my interest on async servers and my recent interest on their implementation on python along with the rustiness of my perl I decided to try a reimplementation on async python as a way to introduce me to the gevent framework.

First impressions

  • It is actually feasible to draw a whole screen using just Javascript through canvas and (put/get)ImageData
  • Some browsers are remarkable faster than others doing this, chrome 15 has a 800×600 draw time of 0-1 msec while Firefox 7 has about 5-13 msec draw time
  • It is perfectly usable unless you want to play videos or something like that

Project

Available in github, comments and patches are welcome! still in development though.

How did I do it

Very quickly actually, thanks to python and gevent, and specially thanks to other projects I took and adapted code from, such as showmethedesktop, python-vnc-viewer and gevent-websocket.

  • Most of the html+javascript is just copied from showmethedesktop with some adaptations of my own
  • I forked gevent-websocket on bitbucket and implemented the new hybi WS protocol on it, it is actually the only thing that current firefox and chrome speak so there was no option there, I also introduced some other small changes
  • I copied all the rfb(Remote Frame Buffer, VNC protocol) implementation from python-vnc-viewer and then removed the twisted dependency and adapted everything to work on gevent.
  • Finally thanks to gevent I created the “main” of my app, really just a gevent app

Issues

Gevent and greenlet have some kind of incompatibility on python 2.7, requirements.txt had to include this version http://bitbucket.org/snaury/greenlet/get/tip.zip#egg=greenlet

Howto

git clone git://github.com/arkaitzj/wsvnc.git &&
cd wsvnc &&
virtualenv --no-site-packages sandbox &&
sandbox/bin/pip install -r requirements.txt &&
sandbox/bin/python wsvnc.py



C++ tuples, a quick overview

November 20, 2009

Ok, c++ tuples, here is quick and dirty:

// g++ 4.1
#include <string>
#include <iostream>
#include <tr1/tuple>
using std::tr1::tuple;
using std::tr1::tie;
using std::tr1::make_tuple;

tuple<std::string,int> parse_url(const std::string& url){
    return make_tuple("127.0.0.1",9000); //Cheat for parsing
}
int main(){
    std::string host;
    int port;
    tie(host,port) = parse_url("127.0.0.1:9000");
    std::cout << "Host: "<<host<< ":"<<port<<"\n";
}

At the first look, returning several values from a function/method in C++ may look awkward and contrary to the C++ way, being more usual in languages like python or perl.
However the fact is that template metaprogramming gives us the ability to do such good tricks and renders the rest for us in compile-time. This tuple templates are part of the TR1 and thats why we need to include them from <tr1/tuple> and use them from std::tr1 namespace, but they’ll be part of the next standard c++0x.

The logic behind the metaprogramming is simple, by the time you compile a tuple the compiler knows perfectly the size of the new type because you supply the template parameters.

tie() for example is a template that gets the given parameters and put references to them in an lvalue obj that can be assigned to, so its a painless way of getting results from a tuple_returning method.
make_tuple() goes the same way but creates a new tuple with copies of the provided parameters, so it is a quick way of creating tuples of any size, its like the current make_pair() but for tuples.


Widen wordpress.com Contempt theme

November 8, 2009

Just buy the “edit css” improvement and add the next lines to your css to get a wider theme, you’ll lose the shadows on the sides though.

#pagebar { width:955px; }
#page { width:955px; }
#content { width:700px; }
#sidebar { margin:-60px 0 0 750px; }
#headerimg {
background-image:url('http://s2.wordpress.com/wp-content/themes/pub/contempt/images/blue_flower/head.jpg?m=1341019597g');
background-repeat:repeat-x;
}
#footer {width:955px; }

EDIT: Thanks to Ivan Zahariev for the tip on keeping the side shadows


Static locals and threadsafety in g++

November 7, 2009

Often when looking in the internet for implementations of some patterns in C++ we come across the well-known Meyers singleton, which is basically the next code:

class Singleton{   
    Singleton(){
        // Thread-unsafe code
    }
    static Singleton* getInstance(){
        static Singleton instance;
        return &instance;
    }
};

This essentially reserves static memory for the size of the instance and executes the class constructor during the first call to getInstance(), this of course occurs only if we are dealing with a non-POD object.

The problem here is that we might run in problems if we get inside getInstance with 2 threads at the same time for the first time. Obviously the constructor is not thread-safe per se.

However, GCCs g++ gives us a help here and protects every local static variable construction with thread-safe guards, __cxa_guard_acquire and __cxa_guard_release do the job here protecting the constructor. Note that this only happens with local statics as global static construction occurs before any function of the translation unit gets executed.

Differences are obvious if we take a look at the generated code, PODs are just data so no construction is needed, non-PODs are properly protected

POD type, function body highlighted:

class pod_class {    public: 
        static pod_class* instance(){ 
            static pod_class once; 
            return &once;
        }};
int main(){ return (int)pod_class::instance; }
080483fd <_ZN9pod_class8instanceEv>:
 80483fd:       55                push   %ebp
 80483fe:       89 e5             mov    %esp,%ebp
 8048400:       b8 f8 95 04 08    mov    $0x80495f8,%eax
 8048405:       5d                pop    %ebp
 8048406:       c3                ret          

Non-POD type, class constructor and function body higlighted:
Read the rest of this entry »


C++0x cool features: Lambda function

September 18, 2009

Have you heard that a new c++ draft is coming soon? It’s called c++0x for the moment and includes some new cool features.
A good description of the new features.

Basics

I’ve been recently playing around with lambda function support and looks pretty stable in the gcc compiler. I haven’t tried but I read that VC2010 has lambda support as well.

It gives support for in-place anonymous function definitions with context access, which basically helps us in a lot of situations, for example, if a given function, say wait_5minutes_call(callback_function), requires a callback we could easily do:

wait_5minutes_call( [ ](){ std::cout << "Callback called\n";} );

And thats all!! cool isn’t it?
A more complete example:

#include <iostream>
#include <functional>
#include <algorithm>
void print_function(int size, std::function funct){
    std::cout << funct(size) << std::endl;
}
int main(int argc, char* argv[])
{
    auto myfnc = [](int size){return size*size;};
    print_function(10,myfnc);
    return 0;
}

The syntax is:
Read the rest of this entry »


Follow

Get every new post delivered to your Inbox.