Thursday, March 12, 2009

Swimming, one recovery strategy

Today, I made another 12 lap swimming at one shot and didn't get tired. Total of around 40 some laps. At this point, doing many laps without improving swim skill is meaningless. So, I thought what will be the first one to fix?
I made several laps just checking as many I can see myself, and found I made inconsistent recovery especially on my left arm. Sometimes I make a splash during recovery. So, let's fix this during March.

Recovery is a procedure of an arm going back to front for the next swing. Each arm takes turn to propel. The best strategy to recover is to relax or spend the least energy to its original position. Splashing obviously isn't good. Splash by itself isn't bad. Drag after splash is bad. So, I took this approach. During recovery, focus on the elbow. Forearm just hang below elbow. While driving the elbow above the water, stay 'stream-lined'. That is important not to waste energy. At the final stage of recovery, Imagine that a hand opens a hole at the surface of the water, and put the entire arm through the hole. While doing this, the next swing starts and the body rotates. Obviously, this is the best time to initiate the propel.

So, again, at the final step of recovery,
1) push the arm through the imaginary hole,
2) kick the other foot to initiate hip rotation,
3) start the other arm swing. ( Remember the best efficient path of arm )
4) stretch for the stream line.

Swim is so much fun!

Monday, March 9, 2009

Swimming is just fun.

From beginning of this year, I began to swim. The whole first month was just struggling in the water, but after two months later, I can do 8 laps (200m) without taking a break. For me, the process to improve my swim was like tuning a sloppy software. But, in the end, nothing is more important than the fact that "Swimming is just so much fun."

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.