Friday, June 19, 2009

Auto disable touchpad while typing using syndaemon

Auto disable touchpad while typing using syndaemon

Often time, when working on laptop, my hands touch (tap) the touchpad while typing, which is really annoying, cause I end up writing in different location. Disabling the touchpad permanently is not a good idea, specially if you don't have a mouse with you. Fortunately, there is a very neat tool that monitors keyboard activity and has the ability to disable the touchpad during that event. The tool is syndaemon.

usage example:
$ syndaemon -d -i 1

That would execute syndaemon as daemon and set 1 second as the delay period.

To automatically run this daemon, for example in KDE desktop environment, I created a script which contains the code above, make it executable, and put it in ~/.kde/Autostart folder.

Also check out synclient, taken from the 'man page', synclient - commandline utility to query and modify Synaptics driver options.

Friday, June 12, 2009

Victor Wooten Introduction

Victor Wooten Introduction

My favorite jazz bassist. Enjoy!

Processing xml document in python using lxml

Processing xml document in python using lxml

In previous entry, I discussed about libxml2. Now, I am going to discuss about lxml, which in my opinion is awesome. I really appreciate the developer team that create lxml. It makes processing xml document easier.
The best way to get it by going to this link and choose the appropriate version. As soon as you installed lxml, try to use it. Here is a sample of how to use lxml.

from lxml import etree
import sys

def getChildInfo(child):
return "id: " + child.get("id") + ", data: " + child.text + "\n"

xmlString = "c1c2"
tree = etree.fromstring(xmlString)
children = tree.xpath("//child")
sys.stdout.write(str(len(children)) + "\n")
for el in [ getChildInfo(child) for child in children ]:
sys.stdout.write(el)

childList = tree.xpath("//child[@id='1']")
sys.stdout.write(getChildInfo(childList[0]))
child = tree.xpath("//child[@id='2']")
sys.stdout.write(getChildInfo(childList[0]))

For more examples and other information about lxml, check out this link.

Thursday, June 11, 2009

Processing xml document in python using libxml

Processing xml document in python using libxml

I had to spent sometimes to gather information about processing xml document in python. There are several different libraries to use, such as minidom, libxml, etree, etc. During my work, I had been working with xpath, so I need xpath support in the built-in library that I work with, so I chose libxml. It is difficult for me to gather information on how to use that API. There are minimum documentations, guides, examples, and tutorials out there.
After spending sometimes, I became familiar on using it. I am writing this, so other people could simply learn on how to use libxml in more realistic situation. Many examples I found in the internet is way too basic, and does not suit my need.


example 1:
--------------------------------------

import sys
import libxml2

def main():
doc = libxml2.parseFile("data.xml")
ctxt = doc.xpathNewContext()
# get student elements in xml doc
res = ctxt.xpathEval("//student")
# output all properties for "student" element using xpath
sys.stdout.write(res[0].get_properties().content + "\n")

for chld in res[0].children:
if chld.type == "element":
# output all information within the child element
sys.stdout.write(chld.content + "\n")

doc.freeDoc()
ctxt.xpathFreeContext()

if __name__ == '__main__':
main()


--------------------------------------


example 2:
--------------------------------------

import sys
import libxml2

def main():
doc = libxml2.parseFile("layout.xml")
ctxt = doc.xpathNewContext()
# get all "property" element that has attribute "Location",
# which parent is "object" element that has attribute "pickList*"
res = ctxt.xpathEval("//Object[starts-with(@name,'pickList')]/Property[@name='Location']")
for r in res:
sys.stdout.write(r.content + "\n")

sys.stdout.write(str(len(res)))

doc.freeDoc()
ctxt.xpathFreeContext()

if __name__ == '__main__':
main()


--------------------------------------


It is also useful to see the source code of libxml.py, just to see what's going on under the hood.

Not long after writing this post, I found even better xml API, which is lxml. I will make separate post about this wonderful xml API for python.

Monday, June 8, 2009

"service" like redhat

"service" like redhat

One of many things that I like in Redhat is "service" tool to start, stop registered service (daemons). I realised that in my current ubuntu 8.10, this tool is already installed by default, which is really good. I am pretty sure that it will still available in newer release.
What about in Debian? To have such tool, you need to install "sysvinit-utils" package.

# aptitude install sysvinit-utils


to use it, simply type like the following
# service  start
# service stop
# service restart
# service status


In ubuntu (also in debian, if I'm not mistaken), you can use "tab" command completion, when you are trying to enter the . Very neat!

Saturday, June 6, 2009

Trim Filename Extension

Trim Filename Extension

There is built-in functionality in Linux shell to trim extension of a file name.
${filename_env_var%.ext} --> note that dollar ($) sign in the beginning is NOT shell prompt.

Usage example:
$ filename=trace_20090606.txt
$ echo ${filename%.txt}
trace_20090606 --> this is the output of previous command

Broadcast Ping

Broadcast Ping

Perform the following commands:


$ ifconfig eth0 | grep -i bcast
inet addr:10.10.44.44 Bcast:10.10.255.255 Mask:255.255.0.0

$ ping -b 10.10.255.255


You should see several IP addresses respond to ping.

Problem with Java SocketChannel

Problem with Java SocketChannel

I created a simple java network application and wanted to put it as a service (windows and linux). To do that, I had to implement shutdown mechanism that releases all resources in proper manner for my java app. Obviously, I used Socket (and ServerSocket) class, which runs in separate thread, and used InputStream and OutputStream to read and write data.
So, the problem comes when I need to stop the thread that has Socket which block for incoming data (by invoking read method for example). Clearly, I need to be able to interrupt that blocking point and exit the thread gracefully.
After searching a bit in the internet, I found out about SocketChannel, which implements InterruptibleChannel. This is good, so I could simply send interrupt signal to the thread and the SocketChannel will also get it and quit from blocking state.

But there is a problem (perhaps bug) with the implementation of SocketChannel, that would prevent concurrent read and write to it. Please refer to sun java bug id 4774871 and 4509080.

The work around is like the following (taken from bug #4774871 details):

CUSTOMER WORKAROUND :
The above program works if you change the wrapChannel() method to wrap the SocketChannel to a dummy channel that only forwards the calls to it. It works because it is not an instance of SelectableChannel:


private static ByteChannel wrapChannel(final ByteChannel channel) {

return new ByteChannel() {
public int write(ByteBuffer src) throws IOException {
return channel.write(src);
}

public int read(ByteBuffer dst) throws IOException {
return channel.read(dst);
}

public boolean isOpen() {
return channel.isOpen();
}

public void close() throws IOException {
channel.close();
}
};
}

Friday, June 5, 2009

LiveCD PXE Network Boot

LiveCD Network Boot (pxe boot)

Server Side:
In case you are using VirtualBox, make sure you set the network to internal network.

On Server (Fedora 10):

turn off selinux
method 1: from GUI and reboot
method 2: $ sudo vi /etc/selinux/config
edit --> SELINUX=disabled
save and reboot

turn off firewall
method 1: from GUI (need to click apply button)
method 2: $ sudo iptables -F

configure the IP for NIC (assumption NIC to be configured is eth0):
method 1:use GUI and set the ip to 192.168.0.15 for eth0
method 2:
$ sudo ifconfig eth0 down
$ sudo ifconfig eth0 192.168.0.15 netmask 255.255.255.0 up


mount kubuntu to /mnt/kubuntu
method 1: mount kubuntu_desktop_9.04.iso to /mnt/kubuntu folder
$ sudo mount -o loop kubuntu_desktop_9.04.iso to /mnt/kubuntu
method 2: mount cdrom to /mnt/kubuntu
$ sudo mount /dev/sr0 /mnt/kubuntu

install syslinux:
method 1: from GUI install syslinux package
method 2: $ sudo yum install syslinux

install tftp server:
method 1: from GUI install tftp-server package
method 2: $ sudo yum install tftp-server

add necessary files for network boot in tftp share folder:
$ sudo mkdir -p /var/lib/tftpboot/livecd/pxelinux.cfg
$ sudoe cp -p /usr/lib/syslinux/pxelinux.0 /var/lib/tftpboot/livecd/
$ sudo cp -p /mnt/kubuntu/casper/vmlinuz /var/lib/tftpboot/livecd/
$ sudo cp -p /mnt/kubuntu/casper/initrd.gz /var/lib/tftpboot/livecd/
$ sudo vi /var/lib/tftpboot/livecd/pxelinux.cfg/default
add the following lines to the file:
############## start of pxe default ##############
default kubuntu
timeout 30

label kubuntu
kernel vmlinuz
append boot=casper netboot=nfs nfsroot=192.168.0.15:/mnt/kubuntu initrd=initrd.gz --
############## end of pxe default ##############

activate tftp server:
$ sudo chkconfig tftp on

restart xinetd:
method 1: from GUI
method 2: $ sudo service xinetd restart

install dhcp server:
method 1: from GUI install dhcp package
method 2: $ sudo yum install dhcp

configure dhcp server:
$ sudo vi /etc/dhcpd.conf

replace the content with:
############## start of dhcpd.conf ##############
allow booting;
allow bootp;
ddns-update-style interim;
ignore client-updates;

subnet 192.168.0.0 netmask 255.255.255.0 {
option subnet-mask 255.255.255.0;
option broadcast-address 192.168.0.255;
range dynamic-bootp 192.168.0.200 192.168.0.240;
next-server 192.168.0.15;
filename "livecd/pxelinux.0";
}
############## end of dhcpd.conf ##############

restart dhcp server:
method 1: from GUI
method 2: $ sudo service dhcpd restart
method 3: if you have more than one NICs, and you want to use dhcp only for eth1 perform the following,
$ sudo dhcpd -d eth1

install nfs server:
method 1: from GUI install nfs-utils package
method 2: $ sudo yum install nfs-utils

configure nfs server:
$ method vi /etc/exports
add the following line:
############## start of /etc/exports ##############
mnt/kubuntu 192.168.0.0/255.255.255.0(async,no_root_squash,no_subtree_check,ro)
############## end of /etc/exports ##############

restart nfs server:
method 1: from GUI
method 2: $ sudo service nfs restart

check nfs export:
$ sudo showmount -e 192.168.0.15


Client Side:
In case you are using VirtualBox, make sure you set the network to internal network.
Set BIOS boot option to PXE Network Boot
Boot the computer

You should be able to see the client PC getting DCHP and loading the LiveCD from network.