2009-11-28

SuperTunnel

Due to some problems I encountered while using GNU HttpTunnel (namely that HttpTunnel would get packets in the wrong order when I was trying to forward ports over SSH over HttpTunnel and cause SSH to disconnect with a failed MAC error), I decided to write an alternative. My dad's work firewalls all outbound traffic besides HTTP and HTTPS, which greatly inconvienences me when I go to work with him as I can't upload new versions of Evaluation Portal or JZBot to my web server.

Anyway, the alternative is called SuperTunnel. There are three main differences between SuperTunnel and HttpTunnel:
  • HttpTunnel has a tendency to get packets out of order when processing high-volume traffic. This caused SSH to repeatedly crash when I would use it to forward ports over HttpTunnel. SuperTunnel does not do this; I've verified that it works when both downloading and uploading data as fast as SuperTunnel will send and receive it.
  • HttpTunnel only allows one socket connection to be active at a time. SuperTunnel allows up to 200 concurrent connections, and in the future that limit will be specified by a configuration file.
  • HttpTunnel uses requests that are long-lived in the sense that a single request will be opened and data streamed down through it as data is received by the server. Since this causes problems with buffering proxies, SuperTunnel instead follows an approach where a single connection will only be used once to receive data. Once even a single byte is received, the connection ends and a new one is made. SuperTunnel waits one second between receive connections so that an excessive number of connections aren't made with each one holding only a couple of bytes.
Instructions are present at http://supertunnel.googlecode.com on how to download SuperTunnel. Currently, you'll have to check out the Subversion repository SuperTunnel is stored in. I plan on making a direct download available soon. You'll also need your own server to run SuperTunnel on.

2009-11-07

Wave Othello Gadget

A few days ago, I spent a day and wrote a Google Wave Othello gadget. The gadget's URL is http://trivergia.com/wavyothello/gadget/jw.othello.client.OthelloGadget.gadget.xml. Add it to a wave, play a game with a friend, and add a comment to this post and let me know how it went or if there are any new features you'd like.

2009-10-17

Single-file IRC server

Those of you that know what IRC is are probably already thinking I'm off my rocker, given the title of this post. And I probably am. I decided to try my hand at writing an IRC server in a single Java file. I've got it to the point that people can connect to it, join channels, set the channel topic, send messages (and receive them) at channels, send private messages, and change nicks, so I figured I'd blog about it and include a link to it if anyone wants to use it. It's not intended to become a production IRC server; I'm mostly writing it so that I can test out an IRC bot that I'm writing when I don't have an internet connection.

Anyway, here's the source code for the server. Download it, compile it with "javac Connection.java", and then run it with "java Connection <hostname>", replacing <hostname> with your computer's hostname. If you're not sure what your computer's hostname is, just use localhost as the hostname. Then, open your favorite IRC client, connect to "127.0.0.1:6667", and join any channel you want. You can switch nicks, set the channel's topic (even though you're not a channel op), and other users can connect, join channels, and chat with you. Sending messages directly between users also works. Modes do not, however (channels always act as if they are mode +nt, no matter what you set their mode to be, but anyone can change the topic even though the channels have +t present). I'm hoping to add more functionality to this server soon.Link

Java Proxy instance throws a NullPointerException

This is something I just found out, and I figured I'd blog about it for those that might come across this in the future.

I'm writing some code in a project (JZBot specifically, http://jzbot.googlecode.com) that uses java.lang.reflect.Proxy. At one point, I was calling a method on a proxy instance, and it was throwing a NullPointerException. The weird thing about this was that the NullPointerException was being thrown from within the proxy instance itself, not from the InvocationHandler that it is supposed to call. Other methods on the proxy worked, indicating that there wasn't some error with the invocation handler somehow being set to null.

Eventually, I found the problem: I had declared the method on the interface that the proxy implemented to return a boolean, but the invocation handler was returning null. The auto-unboxing that was being done by the proxy instance internally was messing things up. I changed the method's return type to void (which is what it should have been), and everything worked.

This is yet another example of where to watch out for autoboxing and unboxing. Admittedly, it would be more helpful if the NPE had a message stating something like, for example, "null can't be unboxed to a boolean", which would most likely have saved me quite a bit of time. Oh well.

2009-09-10

Airplane problem simulator

I just got back from a party where a friend of mine told me the classic "Airplane seating problem", and the resulting answer. The answer was somewhat hard for me to believe, so, since I had brought my laptop, I pulled it out and spent 10 minutes writing this program to simulate the airplane problem. It turns out that the answer he had told me is correct. I'll be darned.

Anyway, here's the program, written in Java. Paste it into a file called PlaneSim.java, run "javac PlaneSim.java" and then "java PlaneSim" to run it. The javadoc at the beginning of the program explains what the airplane problem is, in case you haven't heard of it before.

import java.util.Random;

/**
* There's a common puzzle that goes something like this: There's a plane that
* has 100 seats in it. Each person has a ticket that assigns them to a
* particular seat. However, the first person doesn't follow his ticket, instead
* choosing a random seat to sit in. Each successive person tries to sit in the
* seat listed on their ticket. If that seat is already taken, they choose a
* random vacant seat to sit in. This goes on until all 100 passengers have been
* seated. What is the chance that the last person will be in his correct seat?
* The puzzle's correct answer is 0.5 (IE he's in his correct seat about half of
* the time on average). This program simulates the airplane problem several
* thousand times to verify this answer, and it does, indeed, verify that 0.5 is
* the correct answer.
*
* @author Alexander Boyd
*
*/
public class PlaneSim
{
private static int seatcount;

/**
* @param args
*/
public static void main(String[] args)
{
seatcount = 100;
// Run 100,000 simulations
int simcount = 100000;
int correctTimes = 0;
for (int i = 0; i < simcount; i++)
{
correctTimes += ((sim() == (seatcount - 1)) ? 1 : 0);
}
System.out.println("Simulation count: " + simcount);
System.out.println("Last person was in correct seat " + correctTimes
+ " times");
}

public static int sim()
{
int[] seats = new int[seatcount];
// Change all seats to be empty
for (int i = 0; i < seats.length; i++)
{
seats[i] = -1;
}
// Run the simulation
for (int i = 0; i < seatcount; i++)
{
if (i == 0)
{
// First person, so random seat
seats[random()] = 0;
}
else
{
// Put this person in their seat if it's empty
if (seats[i] == -1)
{
seats[i] = i;
}
// If it's not empty, randomly place this person until we get an
// empty seat to put them in.
else
{
int randomIndex = random();
while (seats[randomIndex] != -1)
randomIndex = random();
seats[randomIndex] = i;
}
}
}
// Simulation is done. Now see who's in the last seat.
return seats[seatcount - 1];
}

private static Random random = new Random();

public static int random()
{
return random.nextInt(seatcount);
}
}

2009-09-04

Natural bee repellant

This is a new discovery made by my mom that I figured might be helpful to other people. In our yard, we grow spearmint. I don't remember exactly why she tried using it against bees, but placing a cutting of spearmint on our picnic table has proved to be a very effective bee repellant. Like I mean one day we were getting swarmed with meatbees while eating outside, so we put a cutting of spearmint on the table, and we didn't see a single bee after that. So if you want to get rid of bees, plant spearmint and use cuttings of it whenever you eat outdoors.

And if you're going to plant some spearmint, I should probably tell you that it's extremely agressive in terms of spreading by root. So plant it in a pot for best results.

2009-08-07

A test blog post

I'm posting this with a GNOME applet to see if it works. So everyone can pretty much ignore this post.

UPDATE: it appears to work, except that it doesn't place the title correctly.

2009-07-07

Chinese Firebox

This one is my dad's brainchild. He came up with the design for it a couple of years ago, and we've built one at my grandma's every 4th/24th that we're there since. Here's a video:


Cool, huh. The whole thing is made with the contents of 24 crackle bombs (the little green round things about the size of a smoke bomb). It takes its name from the fact that the actual firework, once built, looks like a miniature take-out box from a Chinese restaurant. I'll post instructions on how to make one, along with some pictures of the actual firework, sometime in the future.