Random Noise

September 9, 2007

Asynchronous Delegates in .NET 2.0

Filed under: .NET — Tags: , , , — Vivek Unune @ 2:30 am

One of the good features of .NET 2.0 is Asynchronous Delegates. Use these when you want to maintain the application responsive and reliable. Especially when the application wants to complete an IO call, database call, web service call or number crunching (CPU intensive) that can take up fairly long time. For example refreshing of the UI is handled by a single main thread. Which means all the events such as mouse click, mouse movement, keyboard events and even displaying text to the console are handled by this single thread. In this case we were told that we should use threads, i.e. spawn a worker thread and return quickly. But in .NET it is efficient to re-use threads, i.e. borrow them from .NET ThreadPool and not instantiate them. Jeffrey Richter goes on to describe in detail why so in this interview. To simply summarize Jeffrey I can say that spawning a new thread is a costly affair. One of the ways to re-use/borrow threads is to use Asynchronous Delegates. Asynchronous pattern basically involves fire and forget. Once the result is available .NET will call back a method that you must specify during the initial call.

The .NET runtime generates BeginInvoke and EndInvoke methods for each delegate defined. BeginInvoke is a non blocking method and returns right away.Lets take an example of a method that takes long time

public int MySlowTask(int someInput)
{
//simulate slow task
Thread.Sleep(20);
Console.WriteLine(“Processed input : {0}”,someInput);
return someInput+1;
}

In order to call a method asynchronously we must define a delegate with same method singnature as that method.

delegate int SlowTaskDelegate(int someInput);
SlowTaskDelegate slowTaskDelegate = MySlowTask;

Then we call the BeginInvoke method to call the long task asynchronously.

slowTaskDelegate.BeginInvoke(10, // Input parameter to MySlowTask()
EndInvokeMySlowTask, // Callback Method
slowTaskDelegate); // AsyncState

We must also write EndInvokeMySlowTask method (a callback method) that the runtime calls after completing the task.Its signature is fixed defined as
void MethodName(IAsyncResult result):

public void EndInvokeMySlowTask(IAsyncResult result)
{
SlowTaskDelegate myDelegate = result.AsyncState as SlowTaskDelegate;
// obtain the result
int resultVal = myDelegate.EndInvoke(result);
Console.WriteLine(“Returned output : {0}”,resultVal);
}

August 17, 2007

Creating a VMWare Image without VMWare Server

Filed under: Misc. — Tags: , , , , , — Vivek Unune @ 3:19 pm

Yesterday one of my friend wanted to know how to create a VMWare image. I thought it might be a good idea to write a post about this on my blog. Even though there are many blogs out there that have already explained how to make a VMWare image, it will be easy for me to just point my friends to this URL next time someone asks me about it.

The easiest way to create a vm image is to use easyvmx. Just start by creating a .vmx configuration file by simply filling out this online configuration form:

http://www.easyvmx.com/easyvmx.shtml.
Today we will create gentoo linux vm.

1. Baisc Info:
Remember that if you are going to use VMWare Player, you must specify
# of CPUs = 1, as VMWare Player does not support more than 1 CPU. The Guest OS can be a 64bit machine as well, but I haven’t tested it yet though.

2. Network configuration:
I have tried Intel Pro/1000 and vlance with NAT and Bridged network and they both seem to work fine.

3. Disk configuration: I generally use 10GB for my VM because nowadays 4.7GB seems little less for modern OSes. I have also read that making the disk SCSI type will improve your VMWare disk performance.

4. CD ROM: You will probably need two of them one for mounting/loading ISO images and another for loading physical CDs/DVDs. NOTE: When using VMWre player, you’ll need to change the ISO image in the configuration file manually to load an ISO image. For example you can set the ISO to install-x86-minimal-2007.0-r1.iso and boot the vm to install gentoo from this iso.The physical cdrom is used to load CDs from your existing cdrom/dvd drive.

You can leave rest of the options un-touched. and Click the “Create Virtual Machine” button. Download the configuration that we created here:

http://www.mediafire.com/?5f9d3711d6m (Gentoo_2007.zip, 77Kb).

Also, for people who don’t want to go through all this configuration and then installing the OS itself, there’s a completely configured Gentoo 2007.0 vmware image ready to use. You can download it from here:

http://thepiratebay.org/tor/3723438/Gentoo_2007.0_VMWare_Image(gentoo.zip, 1.95 GB torrent).

Once you download either of the above you must either get theVMWare Player (free version) or the VMWare Server or VMWare Workstation (retail versions can create, manage, compress vmware images and much more). For a n00b I would recommend to get the VMWare Player. Then open the .vmx file from the zip file above using VMWare Player (or VmWare Server).

August 14, 2007

Signing a JAR file with .spc and .pvk file

Filed under: Java — Tags: , , , — Vivek Unune @ 2:58 am

0. You must have a .spc(Certificate) file and a .pvk(Key) file

1. Download pvkimprt.exe from here.

And yes don’t forget to install the downloaded exe!!

2. To generate a .pfx file run:

pvkimprt -PFX myspcfile.spc mypvkfile.pvk

type in the password and give a pathname

3. Import this file from Firefox

4. Export the certificate from Firefox to a .p12 file

5. This file can be used with jarsigner

6. We also need to know the alias of the .p12 file, so copy the .p12 file to the Java bin directory and run:

keytool -list -storetype pkcs12 -keystore mycert-p12.p12

7. Enter the password

8. Then you will see output like this:

Keystore type: pkcs12

Keystore provider: SunJSSE

Your keystore contains 1 entry

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx, Aug 13, 2007, keyEntry,

Certificate fingerprint (MD5):

hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh:hh

9. The xxxx-xxx… number is the alias for the key

10. To sign a jar run:
jarsigner -storetype pkcs12 -keystore mycert-p12.p12 myjar.jar \
“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”

Update 18th April 2008:

You can also sign the jar using pkcs12 or .p12 certificate using this:

jarsigner -keystore mycert.p12 -storepass mystorepass -keypass mykeypass -storetype “pkcs12″ -signedjar myjar-signed.jar myjar.jar “xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx”

To verify your signed jar do:

jarsigner -verify -verbose -keystore mycert.p12 -storepass mystorepass -keypass mykeypass -storetype “pkcs12″ myjar-signed.jar

July 7, 2007

Better UI and fonts in linux – I

Filed under: gentoo, linux — Tags: , , , , , — Vivek Unune @ 1:01 am

So you want to have your linux GUI to look uber cool huh?

Remember last time when you installed linux on your home PC? If you are like me, probably you spent atleast a week before you feel comfortable with the settings and customizations. You make sure that all the required tools and applications installed. Like Firefox, OpenOffice, Mplayer and so on. But, no matter what you do linux GUI dosen’t look consistent! All the apps don’t use same fonts, font-sizes. The UI components look different for different apps etc. Probably you know the reason: All the applications in linux use different GUI toolkits. The two most commonly used toolkits are GTK and QT. So, what? Well they rely on different redering techniques unique to the toolkit. This gap between the two can be bridged by installing GTK-Qt Theme Engine.”The GTK-Qt Theme Engine is a plug-in for GTK that allows GTK applications to use Qt widget styles.”

Most of the Linux distros have GTK-Qt Theme Engine available as a package.
Install it :

Gentoo: emerge x11-themes/gtk-engines-qt
Ubuntu: apt-get install gtk-qt-engine
Fedora: yum install gtk-qt-engine

Once you done with that. Use it from KDE control center.
Find a screenshot from my Gentoo machine here.
Now it’s time to fix the fonts. The rendering of the fonts depends on following things:

  1. Screen Size (17″, 19″, 20″ etc.)
  2. Screen Resolution (1024×768, 1280×1024, 1680×1050 etc.)
  3. Dots per Inch or DPI in short (http://en.wikipedia.org/wiki/Dots_per_inch)
  4. Freetype’s Byte Code Interpreter switch
  5. Sub Pixel Rendering (http://en.wikipedia.org/wiki/Subpixel_rendering)
  6. Font Hinting

Dots per pixel:
“Dots per inch (DPI) is a measure of printing resolution, in particular the number of individual dots of ink a printer or toner can produce within a linear one-inch (2.54 cm) space.”. Modern monitors support DPI = 96×96 or sometimes more. Though 96×96 is enough to get good quality fonts.

The screen size, screen resolution and the DPI all are related by the following equation:
For monitor width in millimeters to achieve DPI of 96:

x = (25.4 x resolution width in pixels)/96

Same for monitor height in millimeters:

y = (25.4 x resolution height in pixels)/96

Do I need the above x and y values?
If you have NVidia graphics card, you don’t have to do the above calculation. You can tell the nvidia driver to calculate it for you.For this specify the following in /etc/X11/xorg.conf under device section:

Section “Device”

Driver “nvidia”

Option “UseEdidDpi” “FALSE” #
Option “DPI” “96 x 96″ # specify these two lines

End Section

If you have an ATI graphics card, the graphics drivers sucks. So we have to tell it what to use. For example you have monitor with native max resolution 1280 x 1024:

(25.4 x 1280)/96 = 338.6
(25.4 x 1024)/96 = 270.9

For this specify the following in /etc/X11/xorg.conf under monitor section:

Section “Monitor”

DisplaySize 338.6 270.9

End Section

After this restart your X. press Ctrl + Alt + F1, then if promted enter login info and enter following command:

/etc/init.d/xdm restart

In the next part, I’ll will continue with beautifying fonts in linux.

June 30, 2007

Installing ANT

Filed under: Java, Windows Tip — Tags: , , — Vivek Unune @ 2:41 pm
  1. Download the latest zip from http://ant.apache.org/bindownload.cgi.
  2. Extract it to a directory. For example C:\Users\Joe\apache-ant-1.7-bin.
  3. Edit your environment variables: On Windows XP: Right click on
    My Computer and select properties, then select Advance tab. On
    Vista: Right click on Computer and select properties => Advanced
    System Settings, then click on Environment Variables button.
  4. Add two system environment variables:
    JAVA_HOME = C:\Program Files\Java\jdk1.6.0_01
    ANT_HOME = C:\Users\Joe\apache-ant-1.7-bin
  5. Edit PATH envroment variable and add the following to the end:
    ;%JAVA_HOME%\bin;%ANT_HOME%\bin;

May 18, 2007

JNI debugging using Eclipse and Visual Studio 2005

Filed under: Java, VC++, linux — Tags: , , , , — Vivek Unune @ 11:58 pm

Recently I was involved in a project that involved java code accessing native c++ libraries. All went fine from creation of the JNI functions to its implementation. In one of the functions has java.nio.ByteBuffer as parameter. On the java side this buffer was initialized using allocateDirect. On the c++ side, the buffer content is altered and size gets reduced. The limit is changed using limit(int). I managed this all using cout/printfs/err to print the debug information.

I put up a screen cast (without voice) for anyone who needs to watch this method inaction. Download this video from here: http://www.hotlinkfiles.com/files/1266155_p61kq/jni-debug.divx

Short description of the same:

So, I really needed to look at the live data. Hence needed a way to debug JNI c++ code. So, as everyone else I turned to Google. Finally this article described the need for dual debuggers. I understood that we need to start the java application that uses jvm in debug mode (-Xdebug). We also need to make sure that Eclipse and Visual Studio co-operate. So we need the -Xrunjdwp jvm argument. This will specify the protocol and port address where Eclipse (remote java application) will listen to.

So, open the JNI C++ VS 2005 project and navigate to project->settings->debugging. in command specify the path to java.exe, in command arguments add this:

-Xmx400m -Xms400m -Xdebug -Xnoagent \
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 \
-Djava.library.path=”[path to debug folder containing JNI DLL]” \
-cp “.;[path to folder containing .class files for he java app]” \
[main java class]

And then set break points in the c++ code.

Then we need to create a remote java application configuration in Eclipse. Specify the port number (in this case 8000). Also in my case I had to add java source directory where Eclipse will search for the java code of the remote application. This is done from the source tab. Once done add break points where the java application loads the native library and where you call the native function.

Then start debugging the VS project (f5). The java application will be in suspended mode (because suspend=y argument) and wait for external debugger to attach. So, start the Eclipse’s remote java application configuration that we created in debug mode. And start debugging!

February 1, 2007

About this blog

Filed under: Misc. — Tags: — Vivek Unune @ 2:21 am

In this blog I’ll post intereting stuff that I come across as a Software Developer.
Cheers!

« Newer Posts

Blog at WordPress.com.