Saturday, April 18, 2009

Java HashMap vs. C# Hashtable

One of the things I have found has been more frustrating while picking up C# .NET for the World Wind project is that some of the simple basic programming constructs that are automatic for me in Java don't seem to be directly parallel in C#. On top of that the online documentation at the MSDN site and other places is either not intuitive or I am looking in the wrong areas.

One of the basic data structures I depend on in Java are Maps. One of the simple classes that implement the Map interface in Java is java.util.HashMap.

Like other data structure objects in Java, there is no special syntax to interact with this object, it has methods like other objects where you pass in arguments and get return values.

The most basic usage of the HashMap generally goes like this:

//construct the new map
HashMap javaMap = new HashMap();
//enter a new key value pair
javaMap.put("key","value");
//to get the all values out of the map
Set keys = javaMap.keySet();
Iterator keyIter = keys.iterator();
while(keyIter.hasNext()){
String key = (String) keyIter.next();
String value = (String) javaMap.get(key);
System.out.println(key+" = "+value);
}






So it is pretty straightforward usage (in my mind), the put method "puts" data into the Map and the "get" method retrieves data from the Map given a key. Great

C# on the other hand uses a more elaborate syntax and combination of properties, keywords and methods. Not that this is more difficult but it took some time for me to figure out for example, that there was no equivalent to the "get" method in Hashtable, that you needed to use the "[]" syntax as if this were like an array

For the equivalent C# as the above, you can use the System.Collections.Hashtable class which implements the IDictionary interface:

//construct the Hashtable
Hashtable cSharpTable = new Hashtable();
//enter new key value pairs
cSharpTable.Add("key", "value");
cSharpTable.Add("hello", "world");
//for all Keys, a public property on Hashtable class, print out the key and value
foreach (String key in cSharpTable.Keys)
{
String value = (String) cSharpTable[key];
System.Console.WriteLine(key + " = " + value);
}


So in some ways the C# code is definitely more concise. But for a Java programmer it feels a bit strange, instead of 3 methods in Java(all with clear Javadoc descriptions of use, IDE auto-complete and tool tips, same syntax as all other method calls in Java etc..) for put(),get(),keySet(), C# requires you to use three different language features, a method, Add(), a property on the object Keys (why not just use a method?), and a [] operator. Granted in Visual Studio 2005 typing "cSharpTable." and you would see a list of the public methods and properties including the Add() and Keys, but you would only see the auto-assist tool-tip if you knew to type "cSharpTable[" and then it would give you the basic description of the syntax "object Hashtable[object key]".

I guess it is just something I'll have to learn.

I basically learned Java on the fly on my first job. I learned it fairly easily because once you got it that everything is just a method its pretty easy to find and locate what you need, plus the clear and standardized format of Javadoc based sites makes learning new classes or finding methods fairly simple. Try comparing the ease of use of Sun's Java API site with Microsoft's MSDN .NET Framework Reference(see link to HashMap and Hashtable classes above). Is it really an apples to oranges comparison? Or is the MSDN site just not very readable (for that one class there is no list of methods, just pages of examples in multiple languages.

References:
Holzner, Steven Microsoft Visual C# .NET 2003 Kick Start. Indianapolis: Sams Publishing, 2004. pp243-245

Saturday, April 11, 2009

Contribution Update

I went back and did some further rework for the changes to the "Download code" I submitted to the forum earlier. I posted these on the forum as well (still do not have commit access to SVN). There were no clear directions so I took some suggestions from forum users and combined that with already existing functionality (that was in the code but not used due to a bug I think I fixed).

I spent this day making these updates. Basically there were 3 files updated.

Changes include:
  • Now individual tiles will be put in a HashTable if they get 3 404 errors. They will be prevented from being readded to the download queue. Pressing 'F5' will clear this HashTable as will restarting the program.
  • After 5 failed tiles download attempts (tiles reaching the status above) the current anti-hammer logic kicks in to prevent further requests to that server for that layer. The default timeout period is 2 minutes before it will try to download for this layer again. Pressing 'Pressing F5' will clear this counter, and will stop the timeout and restart the download queue for this layer.
  • When the above condition is reached it will display a message in the top right saying "Problem connecting to server... Trying again in 2 minutes" when that layer is rendered after a timeout is reached. This stays visible for the first 20 seconds of the timeout then will be fade out...this logic was actually in there currently, but I was able to confirm that you can see it now (plus I extended the time it is visible from 15 to 20 seconds).

Tuesday, April 7, 2009

World Wind 1.4.1 Issues

I reported two more issues based on my work over the weekend. I did not get a whole lot of responses yet.

Issue 1:
I notice that when you uncheck "Starfield" in the Layer Manager, it doesn't actually remove the stars that are in the background.

Response: confirmed issue

Issue 2:
I have noticed this on my new development environment which is Vista (64-bit).

Sometimes, but not every time (can't seem to pinpoint an obvious cause or pattern), after I have run World Wind from VCE2005 and exit it from the File menu, then try to Run it in Debug again it will not start up. There is no Worldwind.exe process running and the problem remains if I just restart Visual Studio.


This was a juicy one, I had seen this before but thought it was some change to the code I had made caused this problem.

Thankfully I found that in WorldWind.cs it is actually checking for a Window handle named 'NASA World Wind', finding one and then returning after sending arguments for it.

I have had to comment out this return. Otherwise I need to log off of Windows and log back on to fix the problem.

The bug is that probably the Window is not being shut down completely.

// If World Wind is already running, pass any commandline
// arguments from this instance, and quit.
IntPtr handle = GetWWHandle();
if (!System.IntPtr.Zero.Equals(handle))
{


if(args.Length>0)
NativeMethods.SendArgs( handle, string.Join("\n",args) );

//[Ammianus] by commenting out this return I can get it to start every time.
//return;
}


Both bugs are being discussed in this stickied thread on the World Wind forums.

First contribution?

Technically I did not check-in any changes to SVN, but I did suggest a fix for World Wind on the forums.

I have been spending a good while looking into the "download code" issue. I have taken a step back and this past weekend I dove into the code using my new development machine.

By tweaking a couple of lines of code I managed to unlock behavior already built in to the download classes that would prevent multiple failed downloads from repeating for a particular layer (after 5 failures, stop downloads for a timeout period of 2 minutes). This was slightly different from the expressed requirement to flag specific tiles for not being found and not retrying those.

So on April 5, 2009, I went back to the World Wind forums and posted my update including the changes to the two class files to make the existing functionality work correctly. I even got some replies by a few active members that they would try it out. I asked for clarification as well if I should go back and make the anti-hammer logic work for tiles specifically rather than whole layers which is what I initially wanted to do.

It was such a successful feeling on Sunday to get this working the way I wanted. My wife and I spent all day working on our various projects, me on World Wind, her on her doctoral dissertation. It took about 5-6 hours of steady work, debugging and reading log files to really wrap my head around how these classes were truly interacting. Making a simple change (which I understood) and having it work was very rewarding, it gives me confidence to continue with some more changes.

Things to check:

* One recommendation was to display a message in the UI that the connection to the server is on hold for the 2 minute timeout period. I did in fact see something like that at one point in my testing but it quickly went away, may have been unrelated to my code changes
* Pressing 'F5' will clear the cache for a specific area and force a retry of the download of the visible tiles. Suggestion was that this should reset any timeout or counters of failures if the user doesn't want to wait (I suppose would be the case if they scrolled back over some location that they were more confident had tiles to download from the server)
* Need validation on whether current anti-hammer logic is ok, or we need per-tile anti-hammer logic

New development machine

Not wanting to abuse my work laptop too much, I decided that for my own personal development I needed a new machine, laptops are relatively cheap these days.

I decided on getting a lower-end machine (which means less on the CPU and Graphics card side, but leaving ample memory and hard disk space). After browsing around different reviews and shopping a bit, I decided to settle on a Toshiba Satellite M305-S4907

Basic specs:
* 2GHz Intel® Pentium® Processor T3400
* 4GB PC6400 DDR2 800MHz SDRAM
* 320GB HDD (5400rpm)
* Windows Vista® Home Premium (SP1, 64-bit)
* 14.1" widescreen display
* 5.2 lbs

It took me a few weekends to get used to how use Windows Vista, but I am really enjoying this pc now. I recommend it. Graphics scored somewhat low on the Windows Vista benchmark, but other than World Wind, I don't really use any graphics intensive applications. Unfortunately, World Wind is fairly graphics intensive and that was one of the primary reasons I bought this laptop, to do open source development when not at home.