Skip to content

Posts from the ‘Stuff I do’ Category

27
May

eTegrity gets awards

My senior design and research project, eTegrity, has received two awards at GWU this month!!
The Arnold P. Meltzer Award for Best Computer Science Senior Design Project in 2010
2nd Place for the Pelton Award for Outstanding Senior Project

This is a good time to thank everyone that has helped make this project possible, especially a huge thank you going out to my advisor Professor Poorvi Vora and my main collaborator Jan Rubio!

Time for celebration! And hopefully an open-source release soon…

18
Apr

MIA

I’m sorry for my recent lack of updates, but my senior design project is due just around the corner and a lot of work was put into that. As soon as things settle down, drafts will start getting published, I promise. Of course, I also will be giving more information on the project itself, as it is now approaching maturity.

4
Mar

A Java destructor that’s not really a destructor, but that actually works!.. sort of..

Issue:
Recently I ran into a rather weird problem. I needed certain operations to take place when my program finished running, irrelevant of the manner in which that happened (normal exit, error, crash etc.) Doing this in Java seemed to be a lot more complicated than what I was expecting.

finalize():
There is a method in java called finalize() which you might think does exactly this, but don’t let yourself be fooled. finalize() is called when the object implementing it gets collected by the Java garbage collector. This is not guaranteed to happen when all references to the object are removed (that is simply a condition for collecting, but it doesn’t imply that as soon as the last reference is removed, it will happen immediately or ever), nor is it guaranteed to happen at all before the program exits. In other words, you can’t use it for something you need done, because you have no way of knowing if it will ever get called. Actually, I find it very difficult to imagine why you would ever need to use finalize, but never mind that.

More specific issue:
So is there no way to make this happen? Well it really depends on what exactly you need done. If it is important for you to have code run when an object get destructed (or rather when you think it does), but you still have other code to run in your program following that operation, then I don’t have an answer for you, but probably in those situations there are numerous easy ways to work around the issue and you don’t really need to worry about something like this.

On the other hand, if you had my problem, you just need code to run before the program officially terminates. Enter…

Shutdown hooks
When your program exits, the JVM starts all threads registered as shutdown hooks at the same time and when they are all done, it wraps up and exits. So to do this, just initialize a new thread, put your code in it and register it as a shutdown hook.


class Test {
...
...
private Thread onShutdown=new Thread() {

public void run() {
System.out.println("My program just finished running!");
}
};
...
...
//anywhere in the code
Runtime.getRuntime().addShutdownHook(onShutdown);
....
}

Notes:
- when I say anywhere in the code I really mean that, but obviously, there will be some significance for you regarding where you want to put it and of course, if your code crashes before it reaches the part where you register the hook, the hook won’t run (d’oh!! why did I even say this? oh well)

- If you are working with Eclipse in Windows, you’ll notice that the above hooks don’t work when you press the terminate (red stop) button. That’s not your fault and not really Eclipse’s fault either. It seems to be related to the (bad?) way in which java.lang.Process.destroy() is implemented under Windows. Nonetheless, the hooks do work in Windows, just not when terminating with Eclipse.
More details about this issue.

15
Feb

Communicating over the USB cable

For the eTegrity project I’m working on for my senior design, I needed the smartphone and the computer to communicate over a USB connection. This turned out to be an unusually difficult thing to figure out, but really simple otherwise.

Now, an important thing to note here is that it does require software to be installed on the host computer, specifically the adb tool from the Android SDK and of course the driver (this depends on your OS, see details here)

That being said, all you need to do is to activate port forwarding using adb. What this does is it maps a port on your local computer to a port on the smartphone. For example:

adb forward tcp:1234 tcp:1234

If it returns no message, it executed correctly, otherwise it could be that the port you specified cannot be binded on one of the two machines or some other issue (it will tell you). The link above is great for figuring out how to use adb, so just check it out.

Then, you can go ahead and implement regular sockets on both machines. For some reason, at least in my case the server socket had to be on the smartphone, all attempts to have it be on the computer failed. So put the server socket on the phone, client socket(s) on the computer and you’re all set.

Make sure when you implement the socket on the phone that your application specifies the “INTERNET” permission in its manifest. I know, you’re only trying to do a local network connection, but you need that to be able to open sockets.

I’ll try to post some sample code to go together with this explanation soon.

Notes:
- I am using a Windows XP SP 3 box and an HTC Hero phone (running Android 1.5)
- I believe that in Linux there is a simpler way of doing this, but I haven’t tested at all
- You do have to run the adb port forwarding command again after reboot
- Since 2.0 they introduced Bluetooth so you can just do the socket thing over Bluetooth directly without the adb stuff. Then again, not many devices out there running 2.0 right now as far as I know.

15
Feb

Android category

I’ve just added a new category called Android. I’ll be posting here various things related to the Android architecture that I find interesting as I’m digging my way through it. And of course, my usual skeptical criticism.
Stay tuned!