Friday, February 25, 2011

RTP - Padding

It is possible that RTP packets contains padding bytes at the end of the payload.

If the padding field at the RTP header is set to 1, it means that padding existing in the packet. The last byte of the packet denote the number of padding bytes should be ignored.

The value include itself. So, if the last byte is a value of 9, it means that the last 9 bytes of the RTP packets are padding.

Some reasons to have padding are

1. The payload are some encrypted data with certain fixed block size
2. Carrying several RTP packets in a lower layer protocol data unit

Tuesday, February 22, 2011

Tomcat - JSP Precompilation

JSP is usually compiled during runtime by Java server. Some disadvantages are

1. If you JSP page is large, it will take time to compile at runtime. If that is the first hit, the user will have to wait before the page is served to the user. This is a performance bottlenect.

2. Although the current IDE, such as Eclipse, provide JSP syntax check, you may still run into the situation whereby runtime error occurs due to JSP runtime compilation issues.

To overcome these 2 issues, you can precompile your JSP page before putting them into the server. Tomcat come with JSP precompilation tools for your usage.

The following steps provide information on how to perform JSP precompilation

1. Make sure you have your Tomcat server installed. Or, you need the jar files from tomcat/bin and tomcat/lib, as well as tomcat/bin/catalina-tasks.xml. catalina-tasks.xml is a helper file for loading Catalina ant task for you, ie, jasper task.

2. Make sure you have Apache Ant installed

3. Add the following build script. This build script assume that your jsp source is at your web container.


4. run the script with ant -Dtomcat.home="your tomcat server install home" -Dwebapp.path="your jsp source path". As you can see, tomcat.home is used to locate catalina-tasks.xml and webapp.path is used to locate your libraries and jsp source code. By changing these variables accordingly, you can customize your build path.

5. By default, it will compile your jsp into class file and put them at your "webapp.path"/WEB_INF/classes

Now, how to use these JSP classes file. 2 ways

1. Locate "webapp.path"/WEB_INF/generated_web.xml. Copy these contents into your web.xml

2. Copy all files at "webapp.path"/WEB_INF/classes into your tomcat server work folder. The common path is "your_tomcat_home"/work/Catalina/localhost/_/

You may want to ask why are we not using Ant JSPC task. That task is deprecated due to known problem in Tomcat 1.5 and it won't be fix by Apache Ant as well.

To date, there are 2 known issues for JSP precompilation. below is the abstract from Tomcat

As described in bug 39089, a known JVM issue, bug 6294277, may cause a java.lang.InternalError: name is too long to represent exception when compiling very large JSPs. If this is observed then it may be worked around by using one of the following:

  • reduce the size of the JSP
  • disable SMAP generation and JSR-045 support by setting suppressSmap to true.

Sunday, February 20, 2011

Tomcat - IOException while loading persisted sessions

Tomcat save server state when shutdown. Thus, improper shutdown and save state may cause the following error when you start your tomcat server

Feb 21, 2011 6:01:18 PM org.apache.catalina.session.StandardManager doLoad
SEVERE: IOException while loading persisted sessions: java.io.EOFException
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)

To solve this issue, all you need is to delete the content in the your_tomcat/work directory and restart your server.

Basically, tomcat is trying to look for and read in SESSION.ser. In general, the work directory is working directory for tomcat which reside the JSP and their resulting compiled class files.

Friday, February 18, 2011

MS Virtual PC - Recover Missing Console

For some reason, if you MS Virtual PC console is missing after you double click the icon, and, all you can see is the tray icon for Virtual PC, this entry may help you.

It is highly possible that you have previously used a dual monitor and you have drag your Virtual PC console to the external monitor. Whenever Virtual PC shuts down, it will save its last state, inclusive the X,Y coordinate of the console. The following will fix it

For XP, goes to C:\Documents and Settings\\Application Data\Microsoft\Virtual PC

For Vista or Win7, goes to C:\User\\appdata\roaming\Microsoft\Virtual PC

Look for a Options.xml. In the xml, look for the following



As you can see, left_position and top_position is at a ridiculous coordinates. Reset these 2 values to 0. Also, set visible to true. These setting will make your Virtual PC console reappears at the top left corner of your primary screen

Ant - Creating SVN Ant Task

You can perform SVN checkout/update via Ant task. Follow these steps

1. Go to http://subclipse.tigris.org/svnant.html and download the latest svnant-x.x.x.zip

2. Extract svnant.jar, svnClientAdaptor.jar, svnjavahl.jar and svnkit.jar from the zip file and put them into your classpath

3. Create a ant properties file that contains your svn classpath setting and put it at the same level as your build.xml. Below is an example where svnant jar files are located at lib folder.


4. Put the following at your build.xml


FeedBurner - How to setup Blogspot feed to Twitter via FeedBurner

Thanks to my friend, HJ. I am able to dump TwitterFeed and use FeedBurner.

TwitterFeed has been failing for me and it only works manually. So, I decided to try on FeedBurner. Below are the critical steps to set up FeedBurner to redirect your post to twitter

1. Go to http://feedburner.google.com and enter your blog RSS feed in the text field. Then, just follow the wizard


2. Click on Publicize tab after the wizard setup. Create a twitter account linkage via Add a Twitter account. Click save at the bottom


3. Click on Edit Feed Detail link, look at the feed address and copy it down


4. Go to your Blogspot setting, click on Site Feed tab. Enter your FeedBurner's feed URL at Post Feed Redirect URL. Click save.


Now, you can try posting a blog entry at your blogspot site. It should be reflected in your twitter.


Ant - If and Unless

Apache Ant support condition for target tag and they are if and unless


Above show an example of unless and if usage on target task. You may ask, when to use it? You can phase it as follow with the above example

if -> the update target will ONLY be performed if should.update variable is true

That is, if should.update is defined by either as property tag or as argument input
-Dshould.update="true", the update target will be called

unless -> the update target will ALWAYS be performed unless shouldnot.update is true

That is, if shouldnot.update is defined by either as property tag or as argument input
-Dshouldnot.update="true", the update target will be called

if and unless are extremely useful to control when to perform the target task.

Sunday, February 13, 2011

SVN Commands

SVN is a version tools for collaboration development. For Window, you can use TortoiseSVN with GUI interface. For Linux, it is more common to use commands. Below are some commonly used commands. The bold header are the actual command.

svn --help

Provide help access to all commands. For example, svn --help co will give you help on check out command

svn add file.txt or svn add dir

Add a file or the whole directory to the repository. Note that, adding is not equal to check in.

svn co URL folder

Checkout the file from repository from the given URL. To checkout, I recommend the following steps

1. Create a folder, ie, myFolder
2. Navigate to the folder
3 Type svn co http://your_svn_url .

This will help you check out the latest revision of your repository files into your current folder

If you want to checkout a certain revision of the file, use

svn co -r revisionNum http://your_svn_url

svn cleanup

It will help to cleanup your repository and release any holding lock

svn ci filename or svn ci dir

Commit the file into the repository. For example,

svn ci your_file
svn ci your_dir

If your server require username and password for checking in, you can use the following with a specific check in message

svn ci --username yourname --password yourpwd -m "your check in message" your_file_name

svn diff filename

It will show the different between your work copy against your repository copy

svn update

Update to the latest version in the repository. If filename or directory name given, it will only update the given filename or directory

svn delete filename or svn delete dir

Delete the give filename or directory in the repository

svn log filename or svn log dir

Show the log messages for the given file or directory input by the previous check in

svn info filename

Show the repository information about the file

svn status

Show the status of file changed in the current directory and sub-directory. It is useful to see what files are just added, changed or conflict.

svn switch

There are 2 usage of switch command

1. svn switch http://your_svn_url/newbranch
This will switch your current working copy to a new branch.

2. svn switch --relocate http://your_old_svn_url/trunk http://your_new_svn_url/trunk

This will relocate your current checkout url to the new url. Extremely useful if you are working in both extranet and intranet environment when your IP/domain name changes frequently

svn cp source destination

It is useful to create tag/branch in your repository. For example

svn cp http://your_svn_url/trunk http://your_svn_url/tags/your_tags_version

svn merge source destination

It is useful to merge your branch to trunk. Merging is the most headache of svn as it may create a lot of conflict. You can use the following to test merging

svn merge --dry-run http://your_svn_rul/branch http://your_svn_url/trunk

This will not perform merge, instead, it will tell you the svn feedback from svn such as file added, deleted or conflict

To perform real merge

svn merge http://your_svn_rul/branch http://your_svn_url/trunk

svn resolved filename

Tell svn that the given filename is resolved from conflict. Remember to commit the file after that.





Friday, February 11, 2011

Interprocess Communication

Interprocess communication (IPC) often occurs between processes and threads. Since multiple processes and threads are accessing shared data and communication with one another, it is important to have a synchronized way to perform such action. This entry is to provide some information about IPC.

Race Condition

Most of the time, IPC requires access to shared data. Race condition occurs when 2 or more processes/threads try to read or write onto the shared data and the outcome of the shared data is depends on who runs precisely when. The outcome is usually indeterministic. For example, 2 processes trying to write to a shared variable. Process A update the X=5, and before Process A managed to use the variable for further calculation, it is being context switched. Now, Process B is allow to update X=6 and carry on with its calculation. When Process A allows to continue, it will see X=6 and the calculation for Process A is wrong.

Critical Section

Race condition bring in the idea of critical section. Critical section is a mutually exclusive section whereby more than 1 process reading or writing the shared data is prohibited. So to avoid Race Condition, the following 4 conditions are needed
  1. No 2 processes may be simultaneously inside their critical section
  2. No assumptions may be made about speeds or the number of CPU
  3. No process running outside its critical region may block other processes
  4. No process should have to wait forever to enter its critical region

The TSL Instruction

TSL means Test and Set Lock. It is a hardware level instruction that guarantee no other processor can access the memory word until the instruction finished. The instruction is atomic. Atomic means that TSL cannot be interrupted when it is being performed. So, to create a critical section with TSL, an example can be as follows:

enter_region:
TSL REGISTER LOCK -> Set lock to 1 using TSL and put in register
CMP REGISTER #0 -> Was lock 0
JNE enter_region -> if it is not 0, jump to enter_region
RET -> return if 0

leave_region
MOVE LOCK #0 -> store 0 to LOCK
RET -> return

enter_region helps to flag that you are going into critical section and no other processes are able to use that section. However, the downside of this enter_region is if someone is inside the critical section, enter_region will does busy waiting until someone release the LOCK. In general, busy waiting is bad as it wastes CPU cycles.

Semaphores

Semaphores was invented by E. W. Dijkstra. He proposed 2 operations, up and down. It requires a variable initiated with 0. The up operation increment the value of this variable and the down operation decrement the value of this variable. It is important the the up and down operation is atomic. Furthermore, during the down operation, if the value reaches 0, the process will goes into sleep state and wait for the value to increase. When some process perform up operation, it will increase the value. At the same time, it will wake up any processes that are asleep due to their down operation reaches 0.

Normally, semaphores can be implement with short disable of interrupt. Disabling interrupt is generally bad and may cause hardware malfunction if the process forget to re-enable the interrupt. Moreover, disable interrupt will not work for multi-processor. Thus, it is more common to implement semaphores with TSL (similar to the example above).

Since semaphores have a value to be initialized, and if it is initialized to 1, it is a binary semaphores which only allow 1 processes to enter the critical section at any 1 time.

Mutex

Mutex is a simplified version of semaphores. It does not have the ability of counting. It is good when you want to only manage mutual exclusive access to resource. Mutex have 2 states, lock and unlock. Below is an example

mutex_lock:
TSL REGISTER MUTEX -> copy mutex to register and set it to 1
CMP REGISTER #0 -> check if previously 0
JZE ok -> get lock
CALL thread_yield -> give up thread CPU time
JMP mutex_lock -> try acquire mutex lock again
ok: RET -> return

mutex_unlock
MOVE MUTEX #0 -> store 0 to mutex
RET -> return

Comparing to enter_region, instead of busy wait, it calls thread_yield when it fails to acquire the lock. This has the benefit of saving CPU cycles. Also, thread_yield calls thread scheduler in user space. Thus, it does not require kernel call and can be easily implemented at user level.

Note that Mutex is actually binary semaphores. The main different is that Semaphores allow access to the shared resource up to the maximum N initialized while Mutex only allow 1 at any time.

Monitor

It is a high level synchronization primitive. It allows user to provide mutually exclusive access to procedures, variables and data structure. However, monitor is a programming language construct. That is, it is compiler dependent and it is up to the compiler how to interpret monitor entry. Commonly, monitor is similar to mutex and binary semaphore.

When a process enter a critical section and realized that it cannot continue, it will goes into wait state and wait for critical section to be free. When some procedure finish using the critical section, it will wake up sleeping processes by a signal. If you are familiarized with Java, the synchronized keyword is actually a monitor. By now, you may also feel that wake and signal is similar to Java wait and notify. However, wait and notify are subjected to race condition if they are not used within synchronized keyword.

Barrier

Barrier are designed for synchronizing a group of processes. Since different processes may complete their task at different rate, barrier provide a synchronization mechanism for waiting all processes to complete their task before going into next phase.

Imagine that you have a big mathematical problem that have 2 computation phase and want to perform parallel computation with multiprocessors. You can break the first phase of computation into parts and calculated by each processors. However, all these minor part have to be completed before second phase of calculation. This is when barrier is useful.

Reference: Modern Operating System by Andrew S. Tanenbaum

Wednesday, February 9, 2011

Jython - Simple Setup and Example

Jython (http://www.jython.org/) is a Python for Java project. It is useful for people who want to run Python code in JVM.

Here is to teach you some simple set up.

1. Make sure you download latest Jython at http://www.jython.org/downloads.html. When I post this, the latest version is 2.5.3.RC3

2. The 2 most important file in the download is jython.jar and the Lib folder. jython.jar only provide a small implementation of Python functions, seehttp://www.jython.org/javadoc/index.html. Thus, you have to include the provided Lib folder. As long as the Lib folder exist along jython.jar, you can import the provided Python libraries. For example, "from re import *" to import Python regular expression library.

3. Write your own Python code with .py extension

4. Then, you can run this Python code with Java by calling

java -cp "jython.jar" org.python.util.jython your_python_code.py

Monday, February 7, 2011

Ant - OS specific Copy

Sometime, when you work with Apache Ant, you may want to do OS specific copy. For example, only copy .dll in Windows OS and .so in Linux OS.

You can do the following

Facebook - Control privacy setting of liked page

By default, Facebook displayed your liked page to public. It is the user responsibility to adjust the privacy setting. And in my opinion, F...