Tuesday, February 14, 2012

Remove %M in vi

After I concat a file using a IO redirect (>>) in linux, it causes %M to appear in every lines in a file. I don't know if I did something wrong.
But, to remove %M from file in vi / vim use this command in vi

:s/[CTRL + V][CTRL + M]//g

Note that [CTRL +V] is to press CTRL + V on the keyboard as well as [CTRL + M]
That's the replace pattern (:s/[pattern]/[replace]/) in vim and /g is to replace all occurence in a file.

Saturday, August 27, 2011

Android Emulator and Eclipse

Last year, I bought Google Nexus One to use as my first smartphone. I chose Android over Iphone because it is easier for a casual developer like me to build an application with the current environment (windows and linux). Also, I would better stick with my comfortable programming language, Java.

After getting a phone, I've developed a simple application to help me generate my password for everything. Yes (Nerd Alert!!), I come up with my own algorithm to generate a password from a specific keyword. Therefore, I just need to choose an obvious and easy-to-remembered keyword. Moreover, I can use a different keyword for a different place/websites. For example, the word "Whitehouse" would generate a password "9jk7Gk62". The real algorithm is nothing much; it is built from a simple encryption algorithm, called "ceasar cipher", a bit operation, and a simple hash function.

Password Generator Application on Motorola Atrix 4g

After a long introduction, there is noting wrong with an application. The problem is when I got a new smartphone (Motorola Atrix 4g). The password generator application is working fine, but the screen size of the application is wrong because Atrix has a better resolution than Nexus One. I have to reinstall ADT Plugin for Eclispe and AndroidSDK. After open a project in Eclipse, I cannot start an emulator. The error in the console is

invalid command-line parameter: Files
Hint: use '@foo' to launch a virtual device named 'foo'.
please use -help for more information

After google to find the solution, from many sites[1,2,3], the problem is with R12 SDK. It doesn't allow space in SDK path. But, if you use AndroidSDK windows installer like me, normally default path is "c:\Programs file\android". First, I tried to change the path in the Eclipse configuration from "c:\Program files\android" to "c:\Progra~1\android". This doesn't work for me since Eclipse will tell me that the directory isn't existed. So, The solution for me is to reinstall an AndroidSDK to a location with out space such as "c:\android". This is definitely a bug!!!


[1] - http://www.android.net/forum/beginning-developers/62877-invalid-command-line-parameter-eclipse-android.html

[2] - http://www.wallpaperama.com/forums/_wlxpee.html
[3] - http://stackoverflow.com/questions/6638713/android-emulator-is-not-starting-showing-invalid-command-line-parameter

Thursday, June 23, 2011

Adding library in Ubuntu

Installing application in Ubuntu which doesn't come with .deb package is usually required more steps to make it work. When working with Teragrid, I need Globus Toolkit to connect to a cluster. After download and install required tools using

export GLOBUS_LOCATION=/path/to/install
make gsi-myproxy gsi-openssh

when I tried to run gsissh, I encounter " error while loading shared libraries: libglobus_gss_assist_gcc64dbg.so.0 ". The problem is gsissh doesn't link to the installed libraries. We can look up required libraries for application by using

ldd /path/to/application

The libglobus_gss_assist_gcc64dbg.so.0 is actually installed in the same location of Globus Toolkit, but Ubuntu doesn't automatically link it for us. So, we need to add libraries location to our system. Using command

sudo ldconfig /path/to/my/usr/local/lib/

[1] - http://ubuntuforums.org/showthread.php?t=496553

Wednesday, May 25, 2011

<code> and <pre> CSS Style

After long time without update, I just realize that Blogger pushed out a new template design. In my opinion, it looks better, so I've changed my template without hesitation. The problem is I used custom style for <pre> and <code> tag to represent a program code. When I've applied the template, I forgot to back up the old style. I had to spend some time find the correct style for it although, it is very easy CSS code. So, I will blog it as my backup.

pre {
  overflow: auto;
  border: solid 1px #9AACAE;
  font-family: courier,Georgia,Serif;
  color: $(body.text.color);
  background: #EFEFEF;
  margin: 5px;
  padding: 5px;

Saturday, August 14, 2010

Trivial but important - Java Random Number

Summer 2010 in Bloomington is nearly end (I hope it ends sooner since it's freaking hot here). I've worked as a researcher on TeraGrid project for entire summer. My main task is to create a test library (program) to use against GRAM5 gateway which will be deployed on TeraGrid (soon). It is coded in Java and used for both scalability and reliability test. The problem comes up when I try to submit multiple jobs to GRAM5 gateway from multiple machines. If I start submission at the same time for all machines, program will get a connection reset from a server. I think it is caused by the fact that network router or server has a mechanism to prevent flooding.

At first, I use a simple solution which is delaying a job submission randomly in each machine using java.util.Random class. It worked fine up to around 50 machines then it started to get connection reset again. The situation becomes worse when I use 100 nodes, 90% of jobs fail. Therefore, I google around knowing in my mind that there must be something wrong about Random class. Finally, I found this site[1]. The important properties of Random class are
  • Random number is generated in deterministic way based on a given number called seed
  • Default constructor will use System.currentTimeMillis()as seed.
So, if we give the same seed number to Random class, it will generate a same sequence of random number. In my case, it's likely that each machines create a Random object at the same time, so they get the same seed. The solution is simple, we need to give a different seed on different machines. I use java.util.UUID to generate a unique ID across multiple machines (the odd is very low for different JVM to generate the same UUID) and create a seed from it. Here is my final code

Random random = new Random(UUID.randomUUID().hashCode());
int sleepTime = random.nextInt(120)
Thread.sleep(sleepTime * 1000l);

[1] - http://java.about.com/od/javautil/a/randomnumbers.htm