Monday, November 28, 2011

Understanding C++ Traits

Suppose we have a function that does this
if class is foo, use a new implementation
else use an old implementation
There are many ways to achieve this. The naive way is as shown below.
if (instance.type() == "foo") {
    instance.new_impl();
} else {
    instance.old_impl();
}
Another way is to use a runtime polymorphism as shown below.
class foobar_base {
public:
    virtual ~foobar_base() {}
    virtual void impl() = 0;
}

class foo : public foobar_base {
public:
    virtual ~foo() {}
    void impl() { 
        cout << "New implementation" << endl;
    }
};

class bar : public foobar_base {
public:
    virtual ~bar() {}
    void impl() {
        cout << "Old implementation" << endl;
    }
}

void do_something(const foobar_base& fb) {
    fb.impl();
}
If we need to avoid any runtime type check and use a compile-time type check, we can achieve it by leveraging on the C++ templates and use C++ traits to store the type information as shown on the example below.
#include <iostream>
using namespace std;

template<typename T>
struct impl_traits {
    static const bool value = false;
};

template<typename T>
void do_something() {
    if (impl_traits<T>::value) {
        T::new_impl();
    } else {
        T::old_impl();
    }
};

class foo {
public:
    static void old_impl() {
        cout << "[foo] Old implementation" << endl;
    }

    static void new_impl() {
        cout << "[foo] New implementation" << endl;
    }
};

class bar {
public:
    static void old_impl() {
        cout << "[bar] Old implementation" << endl;
    }

    static void new_impl() {
        cout << "[bar] New implementation" << endl;
    }
};

template<>
struct impl_traits<foo> {
    static const bool value = true;
};


int main() {
    do_something<foo>();
    do_something<bar>();

    return 0;
}
The output is
[foo] New implementation
[bar] Old implementation

Thursday, November 17, 2011

Multithreading in wxPython

This innocent code below looks okay, but it has one major bug here. The bug can cause the application to crash. This is because we try to perform some GUI related stuff in another thread (MyThread). Many GUI toolkits are single-threaded. In other words, the main loop (the loop that starts the GUI application) should only be responsible for handling any GUI related stuff. Any other long running tasks can be done in another thread.
#!/usr/bin/env python

import wx, threading

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "MyApp")
        panel = wx.Panel(self)
        
        self.btn = wx.Button(panel, label="Click Me!")
        self.btn.Bind(wx.EVT_BUTTON, self._do_something)
        
    def _do_something(self, evt):
        MyThread(self).start()
        
class MyThread(threading.Thread):
    def __init__(self, frame):
        threading.Thread.__init__(self)
        self.frame = frame
        
    def run(self):
        wx.MessageDialog(self.frame, message="Test", caption="Test",
                         style=wx.ICON_ERROR | wx.CENTRE).ShowModal()
                             
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Centre()
    frame.Show(True)
    app.MainLoop()
To fix this bug, wxPython provides some thread-safe methods, such as wx.CallAfter, wx.CallLater, and wx.PostEvent. A combination of Publisher/Subscribe and wx.CallAfter can eliminate the bug as shown below.
#!/usr/bin/env python

import wx, threading
from wx.lib.pubsub import Publisher

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "MyApp")
        panel = wx.Panel(self)
        
        self.btn = wx.Button(panel, label="Click Me!")
        self.btn.Bind(wx.EVT_BUTTON, self._do_something)
        
        Publisher().subscribe(self._update, "update")
        
    def _do_something(self, evt):
        MyThread().start()
    
    def _update(self, msg):
        # msg.data is the data that was sent in the CallAfter
        wx.MessageDialog(self, message=msg.data, caption="Test",
                         style=wx.ICON_ERROR | wx.CENTRE).ShowModal()
        
class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        
    def run(self):
        msg = "Test Message"
        wx.CallAfter(Publisher().sendMessage, "update", msg)
                             
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Centre()
    frame.Show(True)
    app.MainLoop()

Tuesday, November 1, 2011

How to Create a JAR with Dependencies in Gradle

Creating a single executable JAR/fat JAR can be pretty useful. We can do something like this.
java -jar foobar.jar
In Maven, we need to write a pretty verbose XML to do that as shown in my previous blog. In Gradle, this task can be easily achieved in just few lines as shown in the example below.
apply plugin: 'java'

sourceCompatibility = 1.6
targetCompatibility = 1.6

defaultTasks = ['clean', 'jar']

dependencies {
   compile 'org.slf4j:slf4j-api:1.6.2'
   compile 'ch.qos.logback:logback-core:0.9.30'
   compile 'ch.qos.logback:logback-classic:0.9.30'
   compile 'javax.servlet:servlet-api:2.5'
   compile 'org.eclipse.jetty.aggregate:jetty-all:7.5.4.v20111024' 
   
   testCompile 'junit:junit:4.+'
   testCompile 'org.mockito:mockito-core:1.8.5'
}

jar {
   from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
   manifest { attributes 'Main-Class': 'test.Main' }
}

Friday, October 21, 2011

How to Set Name in the Threads Created by ExecutorService

If we create a java.util.concurrent.ExecutorService without any argument, it will use DefaultThreadFactory, which has a pre-defined thread name, e.g. pool-n-thread-. To be able to set a different name for threads created by the ExecutorService, we need to create our own ThreadFactory like the example below.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class MyThreadFactory implements ThreadFactory {

    private final String name;
    private final AtomicInteger integer = new AtomicInteger(1);
    
    public MyThreadFactory(String name) {
        this.name = name;
    }
    
    /** 
     * {@inheritDoc}
     */
    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r, name + integer.getAndIncrement());
    }
    
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(5, 
            new MyThreadFactory("Test"));
        try {
            for (int i = 1; i <= 5; i++) {
                es.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println(String.format("[%s] Sleeping for 3 secs",
                                Thread.currentThread().getName()));
                            TimeUnit.SECONDS.sleep(3);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
        finally {
            es.shutdown();
            try {
                es.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Tuesday, October 11, 2011

How to Schedule Recurring Tasks in Java

Prior to Java 5, to schedule recurring tasks, we need to use java.util.Timer and java.util.TimerTask.
import java.util.Timer;
import java.util.TimerTask;

public class WithTimerTask {
    
    public static void main(String[] args) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("Do something");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 0, 1000);
    }
}
From Java 5 onwards, we can easily replace java.util.Timer and java.util.TimerTask with java.util.concurrent.ScheduledExecutorService.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class WithScheduledExecutorService {

    public static void main(String[] args) {
        ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
        ses.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("Do something");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, 0, 1000, TimeUnit.MILLISECONDS);
    }
}
java.util.concurrent.ScheduledExecutorService has more advantages as compared to java.util.TimerTask, such as being able to use a thread pool and not being sensitive to system clock. So whenever possible, use java.util.ScheduledExecutorService as opposed to java.util.TimerTask.

Monday, October 3, 2011

How to Get a Return Value from a Thread in Java

If we need to get a return value from a thread in Java, prior to Java 5, we can do something like this.
public class ReturnValueWithThread {
    public static void main(String[] args) {
        MyThread t = new MyThread();
        t.start();

        // busy doing something.....

        while (!t.isDone()) {
            Thread.yield();
        }
        System.out.println(t.getMessage());
    }
    
    static class MyThread extends Thread {
        private boolean done;
        private String message;
        
        /** 
         * {@inheritDoc}
         */
        @Override
        public void run() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            done = true;
            message = "Hello World";
        }
        
        public boolean isDone() {
            return done;
        }
        
        public String getMessage() {
            return message;
        }
    }
}

In Java 5 onwards, this task can be easily achieved by using java.util.concurrent.ExecutorService and java.util.concurrent.Future.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ReturnValueWithExecutor {
    public static void main(String[] args) {
        ExecutorService e = Executors.newFixedThreadPool(1);
        Future<String> f = e.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "Hello World";
            }
        });

        // busy doing something...

        try {
            String message = f.get();
            System.out.println(message);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        } catch (ExecutionException e1) {
            e1.printStackTrace();
        }
    }
} 

How to Start Threads at the Same Time in Java

Suppose we need to start few threads in Java at the same time, the easy way to do it by using something like below.
public class WithForLoop {

    private static final int NUM_THREADS = 5;

    public static void main(String[] args) {
        for (int i = 0; i < NUM_THREADS; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(String.format(
                        "%s: Do something....",
                        Thread.currentThread().getName()));
                }
            }).start();
        }
    }
}
The problem with it is that we give a head-start for thread 1 to start first. For most cases, it may not matter, but if let's say we need to make sure all the threads get started at the same time, we can make use of java.util.concurrent.CountDownLatch.

import java.util.concurrent.CountDownLatch;

public class WithCountDownLatch {

    private static final int NUM_THREADS = 5;

    public static void main(String[] args) {
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(NUM_THREADS);
        for (int i = 0; i < NUM_THREADS; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        try {
                            startGate.await();
                            System.out.println(String.format(
                                 "%s: Do something....",
                                 Thread.currentThread().getName()));
                        } finally {
                            endGate.countDown();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
        
        // this will cause all the threads to get executed concurrently
        startGate.countDown();
        try {
            endGate.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Thursday, September 22, 2011

How to Implement a Shutdown Method in SocketServer.TCPServer for Python 2.5

Prior to Python 2.6, the SocketServer.TCPServer doesn't have a shutdown method. The SocketServer.TCPServer.serve_forever() also blocks. Here is a TCPServer implementation that works in Python 2.5, implements a shutdown method, and support non-blocking request. The implementation here is based on the TCPServer implementation in Python 2.6.
class Py25TCPServer(SocketServer.ThreadingTCPServer):
   def __init__(self, address_tuple, handler):
      SocketServer.ThreadingTCPServer.__init__(self, address_tuple, handler)
      self.__is_shut_down = threading.Event()
      self.__shutdown_request = False

   def serve_forever(self, poll_interval=0.5):
      """Handle one request at a time until shutdown.

         Polls for shutdown every poll_interval seconds. Ignores
         self.timeout. If you need to do periodic tasks, do them in
         another thread.
      """
      self.__is_shut_down.clear()
      try:
         while not self.__shutdown_request:
            r, w, e = select.select([self], [], [], poll_interval)
            if self in r:
               self._handle_request_noblock()
      finally:
         self.__shutdown_request = False
         self.__is_shut_down.set()

   def shutdown(self):
      """Stops the serve_forever loop.

         Blocks until the loop has finished. This must be called while
         serve_forever() is running in another thread, or it will
         deadlock.
      """
      self.__shutdown_request = True
      self.__is_shut_down.wait()

   def _handle_request_noblock(self):
      """Handle one request, without blocking.

         I assume that select.select has returned that the socket is
         readable before this function was called, so there should be
         no risk of blocking in get_request().
      """
      try:
         request, client_address = self.get_request()
      except socket.error:
         return
      if self.verify_request(request, client_address):
         try:
            self.process_request(request, client_address)
         except:
            self.handle_error(request, client_address)
            self.close_request(request)

To make our program work in both Python 2.5 and 2.6+, we can do something like this.
import sys

python_version = sys.version_info
server = None
if python_version[1] < 6:
    server = Py25TCPServer((self.host, self.port), MyHandler)
else:
    server = SocketServer.TCPServer((self.host, self.port), MyHandler)
server.server_forever()

Thursday, July 28, 2011

Gotcha in C++ Function Overloading

Suppose we have this code.
#include <iostream>
using namespace std;

class Base {
public:
    virtual void doSomething(double a) {
        cout << "This is double from Base: " << a << endl;
    }
    virtual void doSomething(int a) {
        cout << "This is int from Base: " << a << endl;
    }
    virtual ~Base() {}
};

class Derived : public Base {
public:
    virtual void doSomething(double a) {
        cout << "This is double from Derived: " << a << endl;
    }
    virtual ~Derived() {}
};

int main() {
    Derived d;

    int i = 2;
    double j = 0.1;
    d.doSomething(i);
    d.doSomething(j);

    return 0;
}
We would expect the output to be:
This is int from Base: 2
This is double from Derived: 0.1

Strangely, the output is:
This is double from Derived: 2
This is double from Derived: 0.1

The way overload resolution works in C++ is that the compiler will first search for function doSomething() inside the Derived class. If it finds it, it will stop there; otherwise it will look the one in the Base class for a matching function. To solve this problem, we need to add using Base::doSomething in order let it participate in the overload resolution.
#include <iostream>
using namespace std;

class Base {
public:
    virtual void doSomething(double a) {
        cout << "This is double from Base: " << a << endl;
    }
    virtual void doSomething(int a) {
        cout << "This is int from Base: " << a << endl;
    }
    virtual ~Base() {}
};

class Derived : public Base {
public:
    virtual void doSomething(double a) {
        cout << "This is double from Derived: " << a << endl;
    }
    using Base::doSomething; 
    virtual ~Derived() {}
};

int main() {
    Derived d;

    int i = 2;
    double j = 0.1;
    d.doSomething(i);
    d.doSomething(j);

    return 0;
}

The output is:
This is int from Base: 2
This is double from Derived: 0.1

Wednesday, July 27, 2011

Basic SVN Commands

Here I'm gonna show you some basic SVN commands with some examples.

Checking out
svn co http://[hostname]/trunk

Checking in
svn ci

Viewing SVN information
This will display information, such SVN URL, revision number, etc.
svn info

Viewing SVN status
This will display information, such as files modified, added, deleted, etc.
svn status

Updating the local SVN repository
svn up

Viewing the diff
svn diff

Viewing SVN logs
svn log

Adding a file into SVN
svn add new_file.txt

Moving/Renaming a file from one place to another place
svn move old_file.txt new_file.txt

Deleting a file from SVN
svn del junk_file.txt

Reverting local changes in a file
svn revert whatever_file.txt

Reverting local changes recursively
svn revert -R *

Creating a branch/tag
svn copy http://[hostname]/trunk http://[hostname]/branches/1.0
svn copy http://[hostname]/trunk http://[hostname]/tags/1.0

De-commiting changes in the trunk
This will first do a diff between HEAD and rev 123 in the trunk and then do a dry-run before performing an actual merge.
svn diff -r HEAD:1234 http:/[hostname]/trunk
svn merge -r --dry-run HEAD:1234 http:/[hostname]/trunk
svn merge -r HEAD:1234 http:/[hostname]/trunk
svn ci

Merging changes from the branch to the trunk
Assume that the current directory is where the trunk code was checked out.
svn diff -r 1234:HEAD http://[hostname]branches/1.0
svn merge -r 1234:HEAD --dry-run http://[hostname]/branches/1.0
svn merge -r 1234:HEAD http://[hostname]/branches/1.0
svn ci

Merging changes from the trunk to the branch
Assume that the current directory is where the branch code was checked out.
svn diff -r 1234:HEAD http://[hostname]/trunk
svn merge -r 1234:HEAD --dry-run http://[hostname]/trunk
svn merge -r 1234:HEAD http://[hostname]/trunk
svn ci

Viewing SVN externals
svn propset svn:externals .

Editing SVN externals
svn propedit svn:externals .

Viewing who modified the file
svn blame whosefault.txt

Wednesday, June 22, 2011

How to Convert C FILE* to C++ iostream

When we deal with a lot of C libraries in our C++ code, there may be cases where we need to convert the C FILE* into C++ iostream. Boost iostreams can help to deal with such problems easily. Here's an example how to do it.

#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>

void write() {
    FILE* fp = fopen("whatever.txt", "w");
    if (fp == NULL) {
        perror("fopen error");
    }
    int fd = fileno(fp);
    boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_sink> bis(fd);
    std::ostream os(&bis);
    os << "Hello World!" << std::endl;

    fclose(fp);
}

void read() {
    FILE* fp = fopen("whatever.txt", "r");
    if (fp == NULL) {
        perror("fopen error");
    }
    int fd = fileno(fp);
    boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> bis(fd);
    std::istream is(&bis);
    while (is) {
        std::string line;
        std::getline(is, line);
        std::cout << line << std::endl;
    }
    fclose(fp);
}

int main() {
    write();
    read();

    return 0;
}

Tuesday, June 14, 2011

Creating a Custom Builder in SCons

Suppose we need to create a custom builder in SCons. This custom builder will read a text file and append an author name at the end of the file.

import os

def my_builder(target, source, env):
    for i in range(0, len(source)):
       txt = open(source[i].abspath).read()
       txt = txt + os.linesep + "Author: " + env['AUTHOR']
       open(target[i].abspath, "wb").write(txt)

env = Environment()
env['BUILDERS']['MyBuilder'] = Builder(action = my_builder)

env.MyBuilder(['output1.txt', 'output2.txt'], ['input1.txt', 'input2.txt'], 
              AUTHOR = "Foo")

The custom builder allows an argument to be passed in into our custom builder. How we pass the argument is by using the Python's keyword argument, e.g. AUTHOR = "Foo". The custom builder function can then read this value from the env, e.g. env["AUTHOR"].

How to Flatten a List In Python

Here is a simple algorithm to flatten a list in Python. The algorithm also works with the list that have nested lists.

def flatten1(l, newlist):
    if isinstance(l, list):
        for i in l: flatten1(i, newlist)
    else: newlist.append(l)

def flatten2(l):
    if isinstance(l, list):
        newlist = []
        for i in l: newlist = newlist + flatten2(i)
        return newlist
    else: return [l]
    
def flatten3(l):
    if not l: return l
    if not isinstance(l, list): return [l]
    return flatten3(l[0]) + flatten3(l[1:])
    
if __name__ == '__main__':
    l = [1, [2, 3], 4, [[5]], [], [[]], [6, 7, [8]]]
    newlist = []
    flatten1(l, newlist)
    print newlist
    
    print flatten2(l)
    
    print flatten3(l)

The output:
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]

The first algorithm is a ugly since it requires a newlist parameter to be passed in. The second one is a bit better but it still requires a loop. The third one doesn't require any loop and is the shortest one.

Wednesday, June 1, 2011

Getting Started with SWIG

Creating JNI by hand can be pretty challenging especially if we intend to wrap a large C++ code base. In such scenario, SWIG can dramatically reduce the effort of creating the JNI wrapper. There is difference between using javah and swig tools. Swig is useful for creating JNI from C++ while javah is useful for creating JNI from Java.

Hello.h
#ifndef HELLO_H_
#define HELLO_H_

#include <string>
#include <stdint.h>

class Hello {
public:
    Hello();
    virtual ~Hello();
    void sayHello(const std::string& name) const;
    uint32_t getNumber() const;
    const char* getMessage() const;
};

#endif /* HELLO_H_ */

Hello.cpp
#include <iostream>
#include "Hello.h"
using namespace std;

Hello::Hello() {
}

Hello::~Hello() {
}

void Hello::sayHello(const std::string& name) const {
    cout << "Hello, " + name << endl;
}

uint32_t Hello::getNumber() const {
    return 10;
}

const char* Hello::getMessage() const {
    return "Hello";
}

SWIG requires an interface file.
Hello.i
%module JniHello
%{
#include "Hello.h"
typedef unsigned int uint32_t;
%}

%include "std_string.i"
%include "Hello.h"

typedef unsigned int uint32_t;
As we can see here, there is %module verbatim that indicates the module name. Everything in the %{..%} block will be copied to the resulting wrapper file created by SWIG. Here we use include std_string.i library, which will convert std::string to java.lang.String. Without including it, only char* will be converted to java.lang.String and std::string will be a pointer. We can also define a typedef to tell that uint32_t is basically an unsigned int. As a matter of fact, there is stdint.i library that can help to deal with stdint type mapping, but for this tutorial, let's keep it as it is.

Now let's compile the C++ programs and create shared libraries.
swig -c++ -java Hello.i

g++ -c -fpic Hello.cpp
g++ -shared Hello.o -o libhello.so

g++ -c -fpic Hello_wrap.cxx -I$JAVA_HOME/include -I$JAVA_HOME/include/linux
g++ -shared Hello_wrap.o -o libjnihello.so libhello.so

Test it out with a simple Java program. Make sure to add -Djava.library.path or set LD_LIBRARY_PATH to point to the directory where libhello.so and libjnihello.so are located.
Main.java
public class Main {
    static {
        try {
            System.loadLibrary("jnihello");
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Hello hello = new Hello();
        hello.sayHello("Foo");
        System.out.println(hello.getMessage());
        System.out.println(hello.getNumber());
    }
}

The output is:
Hello, Foo
Hello
10

Sunday, May 22, 2011

Creating Non-Blocking Client and Server with Java NIO

With Java NIO, creating non-blocking client and server is pretty straightforward. Below is a sample code how to do that.

HelloServer.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;

public class HelloServer {
    private static final String HELLO_REPLY = "Hello World!";
    
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.wrap(HELLO_REPLY.getBytes());
        ServerSocketChannel ssc = null;
        try {
            ssc = ServerSocketChannel.open();
            ssc.socket().bind(new InetSocketAddress(8888));
            ssc.configureBlocking(false);
            
            while (true) {
                SocketChannel sc = ssc.accept();
                // if sc == null, that means there is no connection yet
                // do something else
                if (sc == null) {
                    // pretend to do something useful here
                    System.out.println("Doing something useful....");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else { // received an incoming connection
                    System.out.println("Received an incoming connection from " +
                        sc.socket().getRemoteSocketAddress());
                    printRequest(sc);
                    buffer.rewind();
                    sc.write(buffer);
                    sc.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ssc != null) {
                try {
                    ssc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private static void printRequest(SocketChannel sc) throws IOException {
        ReadableByteChannel rbc = Channels.newChannel(
            sc.socket().getInputStream());
        WritableByteChannel wbc = Channels.newChannel(System.out);
        ByteBuffer b = ByteBuffer.allocate(8); // read 8 bytes 
        while (rbc.read(b) != -1) {
            b.flip();
            while (b.hasRemaining()) {
                wbc.write(b);
            }
            b.clear();
        }
    }
}

HelloClient.java
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class HelloClient {
    public static final String HELLO_REQUEST = "Hello!";
    public static void main(String[] args) {
        SocketChannel sc = null;
        try {
            sc = SocketChannel.open();
            sc.configureBlocking(false);
            // make sure to call sc.connect() or else 
            // calling sc.finishConnect() will throw 
            // java.nio.channels.NoConnectionPendingException
            sc.connect(new InetSocketAddress(8888));
            // if the socket has connected, sc.finishConnect() should 
            // return false
            while (!sc.finishConnect()) {
                // pretend to do something useful here
                System.out.println("Doing something useful...");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("Sending a request to HelloServer");
            ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());
            sc.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (sc != null) {
                try {
                    sc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

YouTube Downloader

I've created an open source GUI front-end of the famous youtube-dl script.

youtube-dl-gui

Wednesday, May 4, 2011

Getting Started with SCons to Build C/C++ Applications

SCons is a wonderful cross-platform build tool, mostly used for building C/C++ applications similar to make. It's based on Python and Python is an excellent programming language. Since Python is a fully-fledged programming language, there are many things that can be done easily in SCons as opposed to writing a shell script. In this blog, I'm going to show some basic stuff to get started with SCons.

SCons requires a file normally called SConstruct. The file structure in the below example looks like this.
scons-project
  |--- src/Hello.cpp
  |        Main.cpp
  |--- include/Hello.h
  |--- SConstruct

Hello.h
#ifndef HELLO_H_
#define HELLO_H_

class Hello {
public:
    Hello();
    virtual ~Hello();
    void sayHello() const;
};

#endif /* HELLO_H_ */

Hello.cpp
#include "Hello.h"
#include <iostream>
using namespace std;

Hello::Hello() {
}

Hello::~Hello() {
}

void Hello::sayHello() const {
    cout << "Hello!" << endl;
}

Main.cpp
#include "Hello.h"
#include <iostream>
using namespace std;

int main() {
    Hello hello;
    hello.sayHello();

#ifdef DEBUG
    cout << "Debugging..." << endl;
#endif
}

This is the simplest SConstruct file.
SConstruct
import os

env = Environment(CCFLAGS="-g -O0 -DDEBUG", CPPPATH="./include")

env.Program("bin/Main", ["src/Hello.cpp", "src/Main.cpp"])
This will produce an executable called Main. The CPPPATH here is to tell where the header files are located. And the CCFLAGS are the compiler flags used by the GCC compiler that tells the compiler to add the debug statement and turn off the optimization.

If we need to control how the object files are created, e.g. we want to put the object files in a directory called obj. We can use the Object node in our SConstruct. The SConstruct will look like this.
import os

env = Environment(CCFLAGS="-g -O0 -DDEBUG", CPPPATH="./include")

hello_obj = env.Object("obj/Hello", "src/Hello.cpp")
main_obj = env.Object("obj/Main", "src/Main.cpp")
env.Program("bin/Main", main_obj + hello_obj, LIBPATH="lib", LIBS="hello")

If we need to build a static library, i.e. libhello.a that can be linked against Main, we can modify the SConstruct to look this.
import os

env = Environment(CCFLAGS="-g -O0 -DDEBUG", CPPPATH="./include")

env.StaticLibrary("lib/hello", ["src/Hello.cpp"])
env.Program("bin/Main", ["src/Main.cpp"], LIBPATH="lib", LIBS="hello")

To build a shared library, i.e. libhello.so, we just need to change from StaticLibrary to SharedLibrary. The SConstruct will look like this.
import os

env = Environment(CCFLAGS="-g -O0 -DDEBUG", CPPPATH="./include")

env.SharedLibrary("lib/hello", ["src/Hello.cpp"])
env.Program("bin/Main", ["src/Main.cpp"], LIBPATH="lib", LIBS="hello")

Tuesday, April 12, 2011

How to Capture stdout into a Variable

To idea to capture stdout into a variable is relatively straightforward. What we need is basically to save the existing stdout into a temporary variable and then replace the existing stdout with another kind of stdout and then put the original stdout back. Below is how to achieve that in both Python and Java.

For Python:
def capture(func, *args, **kwargs):
    import sys, cStringIO
    
    tmpstdout = sys.stdout
    sys.stdout = cStringIO.StringIO()
    try:
        func(*args, **kwargs)
    finally:
        value = sys.stdout.getvalue()
        sys.stdout = tmpstdout
    return value

To use it:
def sayHello(): print "Hello World"

value = capture(sayHello)
print value.upper()

The output is:
HELLO WORLD

For Java:
package myproject;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public abstract class StdoutCaptor {
    
    public String capture() {
        PrintStream tmp = System.out;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        System.setOut(new PrintStream(baos));
        try {
            invoke();
        } finally {
            System.setOut(tmp);
        }
        return new String(baos.toByteArray());
    }
    
    public abstract void invoke();
}

To use it:
package myproject;

public class Main {
    
    public static void sayHello() {
        System.out.println("Hello World!");
    }
    
    public static void main(String[] args) {
        String value = new StdoutCaptor() {
            @Override
            public void invoke() {
                sayHello();
            }
        }.capture();
        System.out.println(value.toUpperCase());
    }
}

The output is:
HELLO WORLD

Unlike Python, Java doesn't allow a function/method to be passed around. Hence, the code is more verbose than Python.

Monday, April 4, 2011

Understanding C++ Functors

Functors or function objects are any objects that can be called with () operation. Creating functors is as easy as overriding the operator ().

HelloFunctor.h
#ifndef HELLOFUNCTOR_H_
#define HELLOFUNCTOR_H_

#include <string>
using namespace std;

class HelloFunctor {
public:
    HelloFunctor(string name);
    virtual ~HelloFunctor();
    void operator()(string message);
private:
    string name;
};

#endif /* HELLOFUNCTOR_H_ */

HelloFunctor.cpp
#include "HelloFunctor.h"
#include <iostream>
using namespace std;

HelloFunctor::HelloFunctor(string name) : name(name) {
}

HelloFunctor::~HelloFunctor() {
}

void HelloFunctor::operator ()(string message) {
    cout << name << ": " << message << endl;
}

Main.cpp
#include <algorithm>
#include <vector>
#include "HelloFunctor.h"
using namespace std;

int main() {
    HelloFunctor hf("Foo");
    hf("Hello World!");
    hf("Bye World!");

    vector<string> v;
    v.push_back("Hello World!");
    v.push_back("Bye World!");
    for_each(v.begin(), v.end(), hf);

    return 0;
}

The output is:
Foo: Hello World!
Foo: Bye World!
Foo: Hello World!
Foo: Bye World!

In this example, the for_each() function takes in a functor as opposed to a normal function. This makes functor a very powerful mechanism to make any class callable from within many of the STL functions.

Thursday, March 31, 2011

Implementing toString() in C++

In Java, every class inherits from java.lang.Object, which as toString() method. Hence, all classes can have the flexibility to override the toString() method. Unfortunately, for C++ things aren't so straightforward. Although we can easily override the bitwise left shift operator <<, we can do this in a more elegant manner such as below.
Printable.h
#ifndef PRINTABLE_H_
#define PRINTABLE_H_

#include <string>
#include <iostream>
using namespace std;

template <class T>
class Printable {
public:
    virtual string toString() = 0;
    friend ostream& operator<<(ostream& os, T t) {
        os << t.toString();
        os.flush();
        return os;
    }
};

#endif /* PRINTABLE_H_ */

Hello.h
#ifndef HELLO_H_
#define HELLO_H_

#include "Printable.h"

class Hello : public Printable<Hello> {
public:
    Hello();
    Hello(string message);
    virtual ~Hello();
    string toString();
private:
    string message;
};

#endif /* HELLO_H_ */

Hello.cpp
#include <string>
#include "Hello.h"
using namespace std;

Hello::Hello() : message("Hello World!") {
}

Hello::Hello(string message) : message(message) {
}

Hello::~Hello() {
}

string Hello::toString() {
    return "[Hello] message=" + this->message;
}

Main.cpp
#include <iostream>
#include "Hello.h"
using namespace std;

int main() {   
    Hello hello;
    cout << hello << endl;

    Hello anotherHello("Hi World!");
    cout << anotherHello << endl;

    return 0;
}

Wednesday, March 30, 2011

Understanding C++ Pimpl Idiom

In C++, whenever a header file changes, any files that include that header file will need to be recompiled. To avoid such an issue, we can use pimpl (pointer to implementation) idiom.

Hello.h
#ifndef HELLO_H_
#define HELLO_H_

#include <boost/shared_ptr.hpp>

class HelloImpl; // forward declare this

class Hello {
public:
    Hello();
    virtual ~Hello();
    void sayHello() const;
private:
    boost::shared_ptr<HelloImpl> impl;
};

#endif /* HELLO_H_ */

Hello.cpp
#include "Hello.h"
#include <iostream>
using namespace std;

class HelloImpl {
public:
    void sayHello() const {
        cout << "Hello World" << endl;
    }
};

Hello::Hello() : impl(new HelloImpl) {
}

Hello::~Hello() {
}

void Hello::sayHello() const {
    impl->sayHello();
}

In this example, the use of smart pointer, such as Boost shared_ptr is recommended to avoid the hassle of writing the copy constructor, assignment operator, and destructor. With this approach, whenever the implementation changes, the client is insulated from the changes.

Tuesday, March 8, 2011

Getting Started with Gradle

As a build tool, Gradle has a pretty steep learning curve. The official documentation is a good place to start.

build.gradle
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'

version = '1.0'

repositories {
    mavenCentral()   
    flatDir name: 'localRepository', dirs: 'lib'
}

dependencies {
    runtime group: 'log4j', name: 'log4j', version: '1.2.+'  
    compile ':junit:4.0'
}

task hello << {
    println System.properties['msg']
    ant.echo('Hello')
}

compileJava.dependsOn(hello)

gradle.properties
systemProp.http.proxyHost=myhost
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=userid
systemProp.http.proxyPassword=password
systemProp.msg

Project structure
gradle-project/src/main/java/MyJava.java 
               src/main/resources/
               src/main/groovy/MyGroovy.groovy
               src/test/java/MyJavaTest.java
               src/test/groovy/MyGroovy.grovy
               src/test/resources/
               build.gradle
               gradle.properties   
              /lib/junit-4.0.jar               

Here, we use Java, Groovy, and Eclipse plugins. With Java plugin, the conventional project structure is defined as below.
src/main/java
src/main/resources
src/test/java
src/test/resources

With Groovy plugin, the conventional project structure is defined as below.
src/main/groovy
src/main/resources
src/test/groovy
src/test/resources
As you can see here, it's very easy to mix between Java and Groovy projects. Gradle also supports another language such as Scala.

To generate Eclipse project:
gradle eclipse
To clean the generated Eclipse project
gradle cleanEclipse

To build the project:
gradle build
This will compile Java and Groovy files.

To clean the project:
gradle clean

As we can see here, it's pretty easy create a custom hello task and make it as a dependency of an existing compileJavaTask as shown in the example above. Because Gradle is based on Groovy, we can leverage on Groovy programming language and Groovy out of the box supports Ant.

To execute the individual task:
gradle hello

To view the list of available tasks:
gradle tasks

With Gradle, it's easy to mix dependency management between Maven and local repository as shown in the example above.

There are still so many things that Gradle can provide, but hopefully this simple tutorial is sufficient to get started with Gradle.

Tuesday, March 1, 2011

How to Remove Trailing Spaces in Python

Although most text editors provide this feature, just for fun, below is a Python script to do that :) This script preserves the newlines.

#!/usr/bin/env python

import sys, os

def del_trailing_spaces(path):
    if not os.path.exists(path): _error(path + "doesn't exist")
    f = open(path, "rb")
    lines = f.readlines()
    f.close()

    f = open(path, "wb")
    for line in lines:
        if line[len(line)-2] == '\r' and line[len(line)-1] == '\n':
            f.write(line[:len(line)-2].rstrip() + line[len(line)-2] +
                    line[len(line)-1]) 
        elif line[len(line)-1] == '\n' or line[len(line)-1] == '\r':
            f.write(line[:len(line)-1].rstrip() + line[len(line)-1])
        else:
            f.write(line.rstrip())
    f.close()                                    

def _error(msg):
    print "Error:", msg
    sys.exit(1)
    
if __name__ == "__main__":
    if len(sys.argv) != 2: _error("invalid argument")
    del_trailing_spaces(sys.argv[1])

Friday, February 25, 2011

Getting Started with GDB

GDB is a very powerful debugging tool for C/C++ and many other languages. Here I'm gonna show you how to get started with GDB.

testgdb.cpp
#include <iostream>
#include <string>
using namespace std;

string globalmsg = "Hello World";

void half(int& a) {
    a /= 2;
}

int whatever() {
    int i = 5;
    i *= 3;
    return i;
}

int main() {
    int a = 10;
    half(a);
    cout << a << endl;
    cout << whatever() << endl;
    return 0;
}

1. Compile the code
g++ -c -g -Wall -O0 testgdb.cpp
The -O0 flag is useful for debugging information because the compiler may do its own optimization that can eliminate some debugging information, such as -O2 flag for example.
2. Link it
g++ -o testgdb testgdb.o
3. Run it
gdb ./testgdb
After running this statement, there will be gdb prompt.
4. Set the breakpoint in line 18 and 19
break testgdb.cpp:18
break testgdb.cpp:19
5. See the list of breakpoints
info break
6. Remove the second breakpoint
delete 2
7. Run the program
run
8. See the current line
list
9. Step over to the next line
next
10. See the current line again
list
11. Step in to the half() function
step
12. Print the arguments
info args
13. Go to the next line
next
14. Print the value a now
print a
The new value should be 5.
15. Step out from half() function
finish
16. Print all the local variables
info locals
17. Set another breakpoint in line 13
break testgdb.cpp:13
18. Jump to that newly-set breakpoint
continue
19. Step over
next
20. Print the new value of i
print i
The value of i should be 15
21. Step out from the whatever() function
finish
22. Get the length of globalmessage string
print globalmessage.size()
23. Quit gdb
quit

I hope this brief introduction is useful. For more information, type
help <command>

Thursday, February 10, 2011

Getting Started with Distutils

Distutils is such a wonderful tool. It makes installation of Python modules, packages so easy. Here I'm gonna show you how to write a simple setup.py file, which is required by distutils.

setup.py
from distutils.core import setup
import os
import glob

pyfiles = glob.glob("*.py")
pyfiles = [pyfile[:-3] for pyfile in pyfiles]
setup(name='mymodule',
      version='1.0',
      py_modules=pyfiles,
      packages=['test.test1', 'abc.def'],
      package_dir={'abc.def':'mypackage/abc/def'})
Since setup.py is basically just a Python script, we can do pretty much anything with it. What this script is trying to do is it will copy all the python files in the root directory as well as the some python packages in the test.test1 and abc.def. The directory structure is like this:
root
 |--- test/__init__.py
 |--------/test1/__init__.py
 |--------------/mypackage1.py
 |--------------/mypackage2.py
 |--- mypackage/abc/__init__.py
 |-----------------/def/__init__.py
 |---------------------/mypackage3.py
 |--- mymodule1.py
 |--- mymodule2.py
 |--- mymodule3.py
As we can see mypackage is not part of package name (there isn't any __init__.py file there), thus we need package_dir={'abc.def':'mypackage/abc/def'} in the setup() function.

To build a source distribution:
python setup.py sdist
To build Linux RPM:
python setup.py bdist_prm
To build Windows MSI:
python setup.py bdist_msi
To build Windows installer:
python setup.py bdist_wininst

To install to the default Python installation:
python setup.py install
To install to a particular location:
python setup.py install --prefix=/home/whoever/wherever

Wednesday, February 9, 2011

Converting Tabs to Spaces

Mixing tabs and spaces in Python is a bad thing. In recent Python, Python will complain if there are such occurrences. Here I'm gonna show you a small tool to convert tabs to spaces.
import os, sys

def _error(msg):
    print "Error:", msg
    sys.exit(1)
    
def tab2space(path, numspace):
    """ Convert tabs to spaces """
    if not os.path.exists(path): _error(path + "does not exist")
    print "Converting tabs to spaces..."
    origfile = open(path, "r")
    origfilename = origfile.name
    tmpfile = open(path + ".tmp", "w")
    tmpfilename = tmpfile.name
    try:
        for l in origfile.readlines():
            newline = l.replace("\t", " " * numspace)
            tmpfile.write(newline)
    finally:
        # It is important to close the open files; otherwise the
        # remove statement below may not work, especially on Windows.
        tmpfile.close()
        origfile.close()
    os.remove(origfilename)
    os.rename(tmpfilename, origfilename)
    print "*** Done ***"

def _usage(progname):
    print "Usage:", progname, "<path> <number of spaces>"
    sys.exit(1)
    
if __name__ == "__main__":
    if len(sys.argv) != 3: _usage(sys.argv[0])
    tab2space(sys.argv[1], int(sys.argv[2]))

Enjoy!

Creating Custom Function Decorator in Python

Python provides many built-in function decorators, such as staticmethod, abstractmethod, etc. Creating our own function decorator is pretty easy. What we need to do is to overload the __call__ function. Here is how to do it.

class Log:
    def __init__(self, func):
        self.func = func
    def __call__(self, *args):
        print "Before", self.func.__name__ + "()"
        self.func(*args)
        print "After", self.func.__name__ + "()"

@Log
def hello():
    print "Hello"
    
if __name__ == "__main__":
    hello()

This is equivalent to:
class Log:
    def __init__(self, func):
        self.func = func
    def __call__(self, *args):
        print "Before", self.func.__name__ + "()"
        self.func(*args)
        print "After", self.func.__name__ + "()"

def hello():
    print "Hello"
hello = Log(hello)
    
if __name__ == "__main__":
    hello()

The output is
Before hello()
Hello
After hello()

Sunday, February 6, 2011

Creating Abstract Class in Python

Prior to Python 2.6, Python had no concept of abstract class. To enforce that a derived class implements some particular methods from the base class, we can do something like this.
class BaseClass:
    def do_something(self):
        assert False, "Not implemented"

class DerivedClass(BaseClass):
    def do_mystuff(self):
        print "Do my stuff!"
    def do_something(self):
        print "Do something!"
        
if __name__ == "__main__":
    c = DerivedClass()
    c.do_something()
    c.do_mystuff()
If we don't implement the do_something() method, an exception will be raised.

In Python 2.6 onwards, we can achieve the same functionality in a more elegant manner with the help from abc (Abstract Base Class) module.

from abc import ABCMeta, abstractmethod

class BaseClass:
    __metaclass__ = ABCMeta

    @abstractmethod
    def do_something(self): pass

class DerivedClass(BaseClass):
    def do_mystuff(self):
        print "Do my stuff!"
    def do_something(self):
        print "Do something!"
        
if __name__ == "__main__":
    c = DerivedClass()
    c.do_something()
    c.do_mystuff()

Understanding C++ Include Guard

In C++, include guard is often seen and it has a syntax like this
#ifndef MACRONAME_H
#define MACRONAME_H

....

#endif
The purpose of it is to disallow multiple redefinition. For example.
Person.h
//#ifndef PERSON_H_
//#define PERSON_H_

class Person
{
public:
    Person();
    virtual ~Person();
};

//#endif /* PERSON_H_ */

Person.cpp
#include "Person.h"

Person::Person()
{
}

Person::~Person()
{
}

Employee.h
//#ifndef EMPLOYEE_H_
//#define EMPLOYEE_H_

#include "Person.h"

class Employee : public Person
{
public:
    Employee();
    virtual ~Employee();
};

//#endif /* EMPLOYEE_H_ */

Employee.cpp
#include "Employee.h"

Employee::Employee()
{
}

Employee::~Employee()
{
}

Main.cpp
#include <iostream>
#include "Person.h"
#include "Employee.h"
using namespace std;

int main()
{
    Employee employee;
    return 0;
}

Here, we included the Person.h twice, one in the Employee.h and the other one is in the Main.cpp. This code won't compile because C++ has One Definition Rule. If we uncomment the include guard part in the code, the code will compile just fine.

Microsoft Visual C++ has #pragma once for such purpose, but it's not a standard, thus not portable.

Friday, January 21, 2011

Getting Java Class Version using Python

Getting Java class version is actually quite straightforward once we can understand the Java class format. For more information, see this link.

import struct, os, sys

java_versions = {'45.3' : '1.0',
                 '45.3' : '1.2',
                 '46.0' : '1.2',
                 '47.0' : '1.3',
                 '48.0' : '1.4',
                 '49.0' : '1.5',
                 '50.0' : '1.6'}

def __print_error(msg):
    print "Error:", msg
    sys.exit(1)
    
def get_java_version(class_file):
    if not os.path.exists(class_file):
        __print_error(class_file + " doesn't exist")
    
    f = open(class_file, 'rb')
    if (not struct.unpack('!i', f.read(4)) ==
        struct.unpack('!i', '\xca\xfe\xba\xbe')):
        __print_error("Invalid Java class file")
    minor = str(struct.unpack('!H', f.read(2))[0])
    major = str(struct.unpack('!H', f.read(2))[0])
    version = major + "." + minor      
    return java_versions[version]
                         
if __name__ == "__main__":
    if not len(sys.argv) == 2: __print_error("Invalid arguments")
    print get_java_version(sys.argv[1])

The most important thing to note here is that. The first 4 bytes (signed int) is the magic number, the next two bytes (unsigned short) is the minor version, and the next two bytes after that is the major version (unsigned short). This can also be easily coded in Java by using DataInputStream.

Thursday, January 20, 2011

Forward Declarations in C++

Using forward declarations in C++ can help to speed up the compilation time.

X.h
#ifndef X_H_
#define X_H_

class Y;

class X
{
public:
    X();
    void doYStuff(Y& y) const;
    void sayHello() const;
    virtual ~X();
};

#endif /* X_H_ */
Here we can use forward declare Y because we don't use any of the Y's members.

X.cpp
#include "X.h"
#include "Y.h"
#include <iostream>
using namespace std;

X::X()
{
}

X::~X()
{
}

void X::sayHello() const
{
    cout << "I'm X!" << endl;
}

void X::doYStuff(Y& y) const
{
    y.sayHello();
}
Here we need to include Y because we use the Y's member.

Y.h
#ifndef Y_H_
#define Y_H_

class X;

class Y
{
public:
    Y();
    void doXStuff(X& x) const;
    void sayHello() const;
    virtual ~Y();
};

#endif /* Y_H_ */
Here we can use forward declare X because we don't use any of the X's members.

Y.cpp
#include "Y.h"
#include "X.h"
#include <iostream>
using namespace std;

Y::Y()
{
}

Y::~Y()
{
}

void Y::sayHello() const
{
    cout << "I'm Y!" << endl;
}


void Y::doXStuff(X& x) const
{
    x.sayHello();
}
Here we need to include X because we use the X's member.

Main.cpp
#include <iostream>
#include "X.h"
#include "Y.h"
using namespace std;

int main()
{
    X x;
    Y y;

    x.doYStuff(y); // I'm Y!
    y.doXStuff(x); // I'm X!

    return 0;
}

Understanding C++ Type Casting

C++ has 4 different types of casting.
1. const_cast
This cast can used to cast away the the constant.
2. static_cast
This cast can be used to add constant, perform implicit conversion, and to convert from base-class to derived-class.
3. dynamic_cast
This cast can used to convert from derived-class to base-class.
4. reinterpret_cast
This cast can be used to perform conversion from one pointer to another unrelated pointer. This cast is normally used for low-level stuff and not portable.

Person.h
#ifndef PERSON_H_
#define PERSON_H_

#include <string>
using namespace std;

class Person
{
public:
    Person(string name);
    void sayHello() const;
    virtual ~Person();
protected:
    string name;
};

#endif /* PERSON_H_ */

Person.cpp
#include <iostream>
#include "Person.h"
using namespace std;

Person::Person(string name) : name(name)
{
    cout << "Person constructor is being called" << endl;
}

Person::~Person()
{
    cout << "Person destructor is being called" << endl;
}

void Person::sayHello() const
{
    cout << "Hello, " << this->name << endl;
}

Employee.h
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_

#include "Person.h"

class Employee : public Person
{
public:
    Employee(string name);
    void doWhatever() const;
    virtual ~Employee();
};

#endif /* EMPLOYEE_H_ */

Employee.cpp
#include <iostream>
#include "Employee.h"
using namespace std;

Employee::Employee(string name) : Person(name)
{
    cout << "Employee constructor is being called" << endl;
}

Employee::~Employee()
{
    cout << "Employee destructor is being called" << endl;
}

void Employee::doWhatever() const
{
    cout << "Whatever!" << endl;
}

Main.cpp
#include <iostream>
#include "Person.h"
#include "Employee.h"
using namespace std;

void testReinterpretCast()
{
    cout << "=== Testing reinterpret_cast ===" << endl;
    char* str = "Hello";
    int i = reinterpret_cast<int>(str);
    cout << i << endl; // will print garbage
    str = reinterpret_cast<char*>(i);
    cout << str << endl; // get the value back

    cout << endl;
}


void testDynamicCast()
{
    cout << "=== Testing dynamic_cast ===" << endl;

    // derived-class to base-class
    Employee* e = new Employee("Employee");
    Person* p = dynamic_cast<Person*>(e);
    p->sayHello();
    delete e;

    cout << endl;
}

void testStaticCast()
{
    cout << "=== Testing static_cast ===" << endl;

    // force implicit conversion
    double d = 10.20;
    int i = static_cast<int>(d);
    cout << i << endl;
    // add constant
    const int ci = static_cast<const int>(i);
    cout << ci << endl;

    Person* p = new Person("Person");
    p->sayHello();
    delete p;

    cout << endl;

    // base-class to derived-class
    Person* e = new Employee("Employee");
    e->sayHello();
    Employee* e1 = static_cast<Employee*>(e);
    e1->doWhatever();
    delete e;

    cout << endl;
}

void testConstCast()
{
    cout << "=== Testing const_cast ===" << endl;
    // remove the constant
    const char* cstr = "Hello";
    // char* str = cstr; // Won't compile!
    char* str = const_cast<char*>(cstr);
    cout << str << endl;

    cout << endl;
}

int main()
{
    testStaticCast();
    testConstCast();
    testDynamicCast();
    testReinterpretCast();

    return 0;
}

And this is the output.
=== Testing static_cast ===
10
10
Person constructor is being called
Hello, Person
Person destructor is being called

Person constructor is being called
Employee constructor is being called
Hello, Employee
Whatever!
Employee destructor is being called
Person destructor is being called

=== Testing const_cast ===
Hello

=== Testing dynamic_cast ===
Person constructor is being called
Employee constructor is being called
Hello, Employee
Employee destructor is being called
Person destructor is being called

=== Testing reinterpret_cast ===
4210901
Hello

Wednesday, January 12, 2011

SSH with Python

To use SSH with Python, we can use library like paramiko. That library depends on pycrypto, which is mostly a C library. Thus, we need to make sure that we have a C compiler. To install paramiko on Linux/Ubuntu, it's very straightforward.
sudo apt-get install python-paramiko
However, on Windows, we need a little bit of work. By default, distutils on Windows requires Visual Studio compiler.
1. Download Microsoft Visual Studio Express 2008.
2. Download Windows SDK and install the Windows Headers and Libraries.
pyconfig.h requires header files, such as basetsd.h that's available in Windows SDK.
3. Download pycryto library.
4. From the command line, go to pycrypto folder and type
python setup.py install
5. Download paramiko library.
6. From the command line, go to paramiko folder and type
python setup.py install

Below is the sample. The code is pretty self-explanotary.
import paramiko

def do_ssh(hostname, username, password):
    client = paramiko.SSHClient()
    try:
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(hostname, 22, username, password)
        stdin, stdout, stderr = client.exec_command('ls -l')
        print stdout.read()
    finally:
        client.close()

if __name__ == "__main__":
    do_ssh("hostname", "username", "password")

Wednesday, January 5, 2011

Dealing with Unsigned Types in Java

Suppose we need to create a binary file with structure like this
1 unsigned byte
1 unsigned short
1 unsigned int

In C, we can do it easily like below.
writec.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE* myfile;

    myfile = fopen("/tmp/writec.bin", "wb");
    if (!myfile)
    {
        printf("Unable to open the file!");
        return EXIT_FAILURE;
    }

    unsigned char x = 12;
    unsigned short y = 123;
    unsigned int z = 1234;
    fwrite(&x, sizeof(unsigned char), 1, myfile);
    fwrite(&y, sizeof(unsigned short), 1, myfile);
    fwrite(&z, sizeof(unsigned int), 1, myfile);

    fclose(myfile);

    return EXIT_SUCCESS;
}

To read such a file in Java can be quite tricky since the file has unsigned types. All data types in Java are signed, except for char. Here I'm gonna show you how to read such a file in Java.
ReadJava.java
package myproject;
 
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.nio.ByteOrder;
 
public class ReadJava {
 
    public static void main(String[] args) throws Exception {
        FileInputStream fis = null;
        ByteArrayOutputStream bos = null;
        try {
            fis = new FileInputStream("/tmp/writec.bin");
            bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[128];
            int bytesRead;
            while ((bytesRead = fis.read(bytes, 0, bytes.length)) != -1) {
                bos.write(bytes, 0, bytesRead);
            }
            byte[] rawBytes = bos.toByteArray();
           
            int index = -1;
            int firstByte = 0;
            int secondByte = 0;
            int thirdByte = 0;
            int fourthByte = 0;
            short unsignedByte = 0;
            int unsignedShort = 0;
            long unsignedInt = 0;
           
            firstByte = (0x000000FF & ((int) rawBytes[++index]));
            unsignedByte = (short) ((firstByte) & 0xFF);
            System.out.println("unsigned byte=" + unsignedByte);
           
            firstByte = (0x000000FF & ((int) rawBytes[++index]));
            secondByte = (0x000000FF & ((int) rawBytes[++index]));
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                unsignedShort = (int) ((firstByte | secondByte << 8) & 0xFFFF);
            } else {
                unsignedShort = (int) ((firstByte << 8 | secondByte) & 0xFFFF);
            }
            System.out.println("unsigned short=" + unsignedShort);
           
            firstByte = (0x000000FF & ((int) rawBytes[++index]));
            secondByte = (0x000000FF & ((int) rawBytes[++index]));
            thirdByte = (0x000000FF & ((int) rawBytes[++index]));
            fourthByte = (0x000000FF & ((int) rawBytes[++index]));
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                unsignedInt = (long) (((firstByte | secondByte << 8 |
                    thirdByte << 16 | fourthByte << 24)) & 0xFFFFFFFFL);
            } else {
                unsignedInt = (long) (((firstByte << 24 | secondByte << 16 |
                    thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL);
            }
            System.out.println("unsigned int=" + unsignedInt);
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
    }
}


Similarly, if we need to create such a file in Java, we can do the following.
WriteJava.java
package myproject;

import java.io.FileOutputStream;
import java.nio.ByteOrder;
 
public class WriteJava {
 
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("/tmp/writejava.bin");
       
        try {
            short unsignedByte = 0;
            int unsignedShort = 0;
            long unsignedInt = 0;
            int index = -1;
           
            byte[] rawBytes = new byte[7];
           
            unsignedByte = (short) (12 & 0XFF);
            rawBytes[++index] = (byte) ((unsignedByte & 0x00FF));
           
            unsignedShort = (int) (123 & 0XFFFF);
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                rawBytes[++index] = (byte) ((unsignedShort & 0x000000FF));
                rawBytes[++index] = (byte) ((unsignedShort & 0x0000FF00) >> 8);
            } else {
                rawBytes[++index] = (byte) ((unsignedShort & 0x0000FF00) >> 8);
                rawBytes[++index] = (byte) ((unsignedShort & 0x000000FF));
            }
           
            unsignedInt = (long) (1234 & 0xFFFFFFFFL);
            if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                rawBytes[++index] = (byte) ((unsignedInt & 0x000000FFL));
                rawBytes[++index] = (byte) ((unsignedInt & 0x0000FF00L) >> 8);
                rawBytes[++index] = (byte) ((unsignedInt & 0x00FF0000L) >> 16);
                rawBytes[++index] = (byte) ((unsignedInt & 0xFF000000L) >> 24);
            } else {
                rawBytes[++index] = (byte) ((unsignedInt & 0xFF000000L) >> 24);
                rawBytes[++index] = (byte) ((unsignedInt & 0x00FF0000L) >> 16);
                rawBytes[++index] = (byte) ((unsignedInt & 0x0000FF00L) >> 8);
                rawBytes[++index] = (byte) ((unsignedInt & 0x000000FFL));
            }
           
            fos.write(rawBytes);
        } finally {
            fos.close();
        }
    }
}

To read it in C, it's as easy as below.
readc.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE* myfile;

    myfile = fopen("/tmp/writejava.bin", "rb");
    if (!myfile)
    {
        printf("Unable to open the file!");
        return EXIT_FAILURE;
    }

    unsigned char x;
    unsigned short y;
    unsigned int z;
    fread(&x, sizeof(unsigned char), 1, myfile);
    fread(&y, sizeof(unsigned short), 1, myfile);
    fread(&z, sizeof(unsigned int), 1, myfile);

    printf("unsigned byte=%d\n", x);
    printf("unsigned short=%d\n", y);
    printf("unsigned int=%d\n", z);

    fclose(myfile);

    return EXIT_SUCCESS;
}