6.02.2008

Bittorrent Client: Part II: The Setup: Version Control

Alright... we now have some code written for this stupid thing.

We can now read BEncoded data from the torrent file (torrent info hash, files, piece size, etc), talk to the tracker, and store the peer list / other important information in the class.

We would be farther along due to all of the time we wasted away yesterday dealing with version control software.

Why is it so difficult to get Subversion or CVS up and running and usable? After finding a developer site that kept track of some free SVN servers, we picked one that was labeled as the best free service for open source code. This is the latest status message on their page, in big scary red text:

Due to errors in our backup scripts, we have gone two weeks without backups of the repositories. Unfortunately, we made an error when cleaning our filesystems. As a result, some repositories have been deleted. Some of them have been recovered from our last backup, which we regret is two weeks old. If you can't access your repository, mostly likely it is lost. We sincerely apologize for any loss of data and inconvenience.

Needless to say, we're keeping our own backups.

But that was half (or one third) the battle. We now needed client SVN software to access the server. I spent about two hours on this, which was two hours more than I wanted to spend... the command line interface was clumsy and poorly documented, or else I'm just bad at man pages and the internet. I tried a couple Mac OS X apps, either they didn't work with Leopard or required you to use the command line interface as well. Finally, we found "subclipse", an Eclipse plugin that puts all of the SVN commands innocently into the eclipse GUI. This lets us commit, update changes, and handle conflicts with a reasonably shallow learning curve. But when you make software where four Caltech undergrad CS majors can't figure out how to handle conflicts for a good 20 minutes, that either says something about your interface or this institution. Anyways, aside from the times where subclipse completely stops working when you click "commit file", you lose your changes, and then you have to recheck out the entire source package, it's great and it lets the four of us all edit the same files with some degree of safety. But it's basically like playing Russian roulette with your last few minutes of work sometimes.

So, I commit my changes compulsively and often.

While I'm bitching (all I seem to do is bitch), why doesn't Java have a function to take a byte and return the two hex characters that byte corresponds to? After at least 15 minutes of searching for strings like "java byte to hex class" and "url encoding binary data hex byte" and finding java forums on sun's site that varied from absolutely useless to of marginally useful, I gave up. Does this class exist? There is a method that takes a byte and creates a signed integer, but what use is that here?

Again, am I just bad at the internet and documentation? Anyways, my dear developer blog reader, if you need to use binary data in a URL(like sending the info hash in a torrent tracker request), here is a function that takes a byte[] and creates a URL safe string. Hopefully somehow who doesn't care how this works and just needs the right format will find it will be useful.


/**
* Java function that takes a byte[] of binary data and returns a URL safe string of hex chars
*/
private String urlEncodeBinaryData(byte[] o){
String[] hexDigits = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
// stuff has to look like %20%a4%20%20
String x = new String();
for (int i=0; i < o.length; i++){
// for each byte, strip off the top 4 bits, then the bottom 4 bits.
// convert each to an integer, and index into the array.
int firstIndex = (int)((o[i] & 0xF0) // grab the higher bits of the byte
>>> 4); // shift them so they're the lower bits
int secondIndex = (int)((o[i] & 0x0F)); // grab the lower bits of the byte
x = x + "%" + hexDigits[firstIndex] + hexDigits[secondIndex];
}
return x;
}

TODO: talk to peers, and start downloading pieces. Away to wrestle with java.nio.channels, threads, and file I/O!

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home