Tuesday, February 24, 2009

Bash 4.0 released

I have not posted in Feb. But, I am not losing interest on my blog. Just my work was too busy and I was very tired at home.

Bash 4.0 is just released. Once again, I have to defer trying this out. I am still using fish. I like fish, but some features from bash are missed. In completion features, bash 4.0 didn't improve too much (as far as I read their change log.) So, I am not sure if I come back to bash.

Thursday, January 8, 2009

TCP segmentation example

Let's straight to the point.
Simply to make tcp segmented traffic, we need to turn off Nagle's algorithm. We can accomplish this with TCP_NODELAY option. I needed one for my test, but I couldn't find an example.
Since it is not that hard, I just wrote one. Don't expect too much in this example, however. I just needed this real quick, and wrote very sloppy. Here it comes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>


#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

#define MAXDATASIZE 8192
#define SERVER_ADDR "172.16.136.147"
#define SERVER_PORT 80
#define PAYLOAD "GET /index.html"
#define CHUNK 2 // chunk size. 2 byte segment for each.


int main()
{
    int sockfd, numbytes;
    char buf[MAXDATASIZE] = {0, };
    int optval;
    struct sockaddr_in srv;

    sockfd = socket(PF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
        handle_error("socket failed");

    optval = 1;
    if ( 0 > setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval))
        handle_error("sockopt, REUSEADDR");
    optval = 1;
    if ( 0 > setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &optval, sizeof optval))
        handle_error("sockopt, NO NAGLE");

    memset(&srv, '\0', sizeof srv);
    srv.sin_family = AF_INET;
    srv.sin_port = htons(SERVER_PORT);
    if ( 0 > inet_pton(AF_INET, SERVER_ADDR, &srv.sin_addr))
        handle_error("lookup fail");

    // CONNECT!
    if ( 0 > connect(sockfd, (struct sockaddr *)&srv, sizeof srv))
        handle_error("connect error");

    // Lazy: I'm not handling buffer overflow.
    strcpy(buf, PAYLOAD);
    
    int i;
    char sbuf[ CHUNK ];
    for (i=0; i<strlen(PAYLOAD); i += CHUNK) {
        strncpy(sbuf, buf+i, CHUNK);
        write(sockfd, sbuf, CHUNK);
        usleep(100000); // give time to clear the system call.
    }
    write(sockfd, "\n\n", 2);

    if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
        handle_error("recv");
    buf[numbytes] = '\0';

    printf("client: received '%s'\n",buf);
    close(sockfd);

    return 0;
}

Wednesday, December 24, 2008

Xterm with Truetype font?

I am not kidding. It has been a while, too. Just I knew that now. I simply googled and found this link.
This assumes xterm is compiled with xft library, which will be true for most of decent Linux distro like Fedora Core or Ubuntu. Especially, I am a fan of 'screen' utility, I don't need top menu, icons, tab names, etc. So!!! Try this:

$ xterm -fa 'Monospace-9'

Amazing.
Now, temptation for Enlightenment instead of KDE?

Thursday, December 18, 2008

new process state in linux kernel 2.6.25

I am obviously losing kernel tracking. I can't keep up linux kernel any more due to busy life. Today, while researching on my regular works, I ran into this article.

http://www.ibm.com/developerworks/linux/library/l-task-killable/index.html

In short, linux innovated new process state, called TASK_KILLABLE. Operating system is still evolving. Almost 50 years after its first version of Unix, still finding a way for innovation.

Wednesday, December 17, 2008

Adding unittest in python with TestSuite

Unittest is an important fundamental for solid software development. At the same time, maintaining proper unittests is also a burden. Managing good unittest is always a challenge.

One of the challenge in python comes when unittest.TestSuite is needed. Generally, each class will split out to each of unittest.TestCase class. But, to teach TestSuite what TestCase to load, we have to pass a list of TestCases, and writing a list manually like this isn't fun.

suite = unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(FirstTest),
unittest.TestLoader().loadTestsFromTestCase(SecondTest),
unittest.TestLoader().loadTestsFromTestCase(ThirdTest),
.....
])

It is error prone. More annoyingly, if I add a new test case, I have to modify suite, also. Using this driver will auto discover test classes in the current module.


moduleList = [ globals()[mod] for mod in globals().keys() if mod.endswith('Test') ]
suite = unittest.TestSuite([
unittest.TestLoader().loadTestsFromTestCase(i) for i in moduleList
])
unittest.TextTestRunner(verbosity=1).run(suite)


This uses naming convention of "SomethingTest" as a test case for class "Something".
Keeping this convention will be a keystroke save.

Friday, December 5, 2008

Tools for these days

I adapted two major changes in my tool set.

1. fish instead of zsh/bash
Most linux ships bash. I have used bash mostly. zsh was okay. But after I found fish, settled in fish. It is different but not difficult to migrate from bash. Nice color coding with good completion like zsh. Its script syntax resembles to tcl. I don't like tcl, but I could handle fish okay. Another unusual, but eventually more useful thing is the lack of history expansion. !! or !$ in bash does not exist in fish. Instead of them, fish simplifies all history recall with Up/Down and Alt-Up/Alt-Down. Action oriented :)
fish is very useful in many aspects. One useful thing is its history keeps not only history of the commands, but also keeps the timestamp when it ran.

2. screen instead of multiple xterm
Spreading xterms all around desktop looked sloppy. So, I tried to pack them into one screen session. At first, the environment was not familiar, but later I got used to it and navigated fine. So, mostly my dual screen is one emacs session plus one konsole with screen. These look more elegant than patching desktop with many xterms.

Tuesday, November 18, 2008

Best Python debugger

I don't use pdb often, but today, I needed one. I suspected that there should be a pdb module for emacs. Although GUD requires separate wrapper to PUD, there is a built-in module in Emacs. I just realized that GUD is a wrapper for another process instance. Not a big deal.
Create this file in somewhere PATHable and name it pdb.

#!/bin/sh
exec python2.6 /usr/lib/python2.6/pdb.py $1 $2 $3 $4 $5 $6 $7 $8 $9

And then, you are good to go. Someone mentioned that backtracing does not work across modules. I didn't test that because his/her post was dated long time ago with emacs 21.
With pdb, 'C-x +' (balance-windows) will be very useful.