Tuesday, November 18, 2008

RFC868 UDP Time Protocol Client

Here is an RFC 868 UDP Time Protocol Client implementation in less than 30 lines of python.
I know...just what you always wanted. It is a long story, but I needed a time protocol client (that would run on Windows) for testing a service we're developing at work. There are tons of implementations of the TCP version of the protocol, but I couldn't find a UDP implementation for Windows to save my life. Python to the rescue.
from socket import *
from struct import unpack
from time import ctime, sleep
from sys import argv

argv = argv[1:]
if len(argv) == 0:
argv = [ 'time-nw.nist.gov' ]

s = socket(AF_INET, SOCK_DGRAM)

for server in argv:
print server, ":",
s.sendto('', 0, (server, 37))
t = long(unpack('!L', s.recv(16)[:4])[0])
# Convert from 1900/01/01 epoch to 1970/01/01 epoch
t -= 2208988800
print ctime(t)
except timeout:
print "TIMEOUT"
print "ERROR"

In case you are curious, the sleep(2) at the end is there so our testers can simply click the icon on their desktop and have time to actually see the results before Windows closes the console window.

By default, it queries the time-nw.nist.gov server, but you can specify any number of servers to query on the command-line.

Sunday, November 16, 2008

A Calculus Lesson

I don't know what triggered it, but this morning I recalled a lesson I learned in high school Calculus class. You see, I was in one of those "advanced" AP Calculus classes, one that was supposed to prepare you for the Calculus AB AP Exam (i.e. the hard one). I don't know if that means anything more these days, but at the time, it was the class all the good students took.

But here is the rub: I was a terrible student. Seriously. This was senior year; while my classmates were keeping tabs on their class ranking to see who was in which position for the top 10, I just kind of floated along and daydreamed about computer programming. I think I had something of a C average in that class; I wasn't even in the top 25% of by graduating class, much less worried about being in the top 10.

Anyway, I never really bought the whole school competition thing. Don't get me wrong: I like learning stuff, but grades were always an afterthought.

The particular incident I remembered this morning, however, was a "challenge" assignment in my Advanced Calculus class. The teacher gave us an assortment of problems that he claimed he also used in his college classes. These problems were supposed to be difficult. We had a week or two to work on them, at the end of which we would turn them in and go over them. I don't recall whether they were to be graded or not, but I latched onto the challenge.

I think there were about 12 or so problems, a few of which weren't anything particularly challenging and everyone got those figured out pretty quick. A few more took a couple of days of trying to arrive at a solution. But the last few I recall being really hard. Each day, before class, my classmates would compare solutions to see if we all arrived at the same answers (we had no answer key).

To tell the truth, I rarely participated in these exchanges. The problem was that some students figured out they could be remoras to the ambitious students' sharks. While a few students compared their solutions and competed to be the first to solve each problem, others would take notes and write the solutions on their own papers.

Now, to me this seemed like cheating at the time. I thought the "smart kids" were getting taken advantage of. I suspect they knew others were copying their answers; maybe they liked the attention, I don't know. Nonetheless, I didn't want to be part of it. I tried solving each problem the best I could. If I was wrong, no big deal...I'd learn the proper solution when the challenge was over. The grade didn't matter, it was the challenge that was exciting. And I didn't want the accomplishment of solving a problem to be sullied by the doubt of having copied it from one of the good students.

Anyway, so things went for most of the assignment. It got down to just one problem that I didn't have a solution for. Again, I don't remember the details, but it had something to do with finding the area of a sphere inscribed in a cone inscribed in a cylinder with the dimensions of the cylinder given in some form. It turns out, that I was not the only one stuck on this problem. No one in my class had solved it.

Well I'll be darned if one morning the solution didn't come to me while I was in the shower. I think we only had a day or two left on the challenge assignment and I was thrilled that I might actually be able to solve all the problems. I think it was in Spanish class that I frantically jotted down my solution and solved to arrive at an answer. I used my intermediate results for the dimensions of the outer cylinder and the inscribed cone to calculate their respective volumes and confirmed that the volume I calculated for the inner sphere was less than the cone it was inscribed in and that the volume of the cone was less than the cylinder. In other words, if nothing else I knew my answer at least made sense.

So I kept my mouth shut in Calculus class that day. If anyone else had managed to solve it, they were apparently doing the same. I started getting excited at the thought that me, the lowest-ranked student in my class, may have actually done something no one there could do.

But the excitement didn't last. You see, the last day before the deadline I blabbed. No one had figured it out, the star students were getting frantic. And someone came over to my desk and asked me if I had figured it out. I hesitated for a second. With only one day left, the odds were I was going to be the only person to solve this particular problem.

And that is when it occurred to me: life isn't a contest. I could keep my solution to myself and be #1 for a day (which is saying something since I graduated 112th out of a class of 420 or so) or I could share the wealth so-to-speak. I didn't show them my answer, though. I spent the 10 minutes or so we had before class to explain how to solve the problem: what things you needed to calculate and how to obtain the parameters needed to calculate them. With that, I knew they could solve it and could still have a clear conscious knowing I didn't just give someone the answer.

Sure enough, by the end of class they had a solution and came back over to compare their answer with mine. Sure enough, they matched. They were happy to have it solved; I felt good having helped someone.

I won't say it wasn't a little disappointing to see them them immediately go back and share the answer with all of the other star students. I suspect almost everyone in the class ended up the answer for that very difficult problem. I'd be surprised if most of them were aware that I had solved it for them, but that was actually kind of fun in and of itself. More importantly, I knew at least one person solved the problem themselves, though, and I knew that they did it based on what I had told them. That felt really good.

Fast-forward 10 years and I found myself working at a Bay Area startup called Backplane with a bright guy named Matt Dillon. Not the actor, the software engineer. I remember a story that the CFO related to me about Matt's previous company, an Internet Service Provider named Best Internet. According to the story, Matt had found Best Internet with a few others and was largely responsible for the technical infrastructure. The servers and whatnot were all hosted in a data center and apparently Matt would, without hesitation, explain the details of how everything worked to any passerby who took an interest. This CFO, who was a childhood friend of Matt's, warned Matt that he shouldn't be giving away company secrets like that.

To which, Matt responded: if they know enough to understand what it is I'm telling them, then they already know enough to build it themselves anyway.

I think that is more-or-less the same thing I realized in my Calculus class so many years before, but he actually put it into words that have stuck with me ever since.

The insight, I think, is not that all information should be free, but rather that there are some truths that there is no benefit in hiding. While some processes, facts, or figures may hold special strategic value, an environment in which all information, no matter what its worth, is horded can be stifling to innovation.

In other words, you can assign a value to all information and a threshold on what you are comfortable sharing. The best engineers I have ever met tend to have pretty low information-sharing thresholds while management tends to err on the high side. The motivations are obvious. Matt, for example, is clearly an engineer while Backplane's CFO was coming from a management perspective.

What I discovered for myself, many moons ago in Calculus class, is that valuing information is a challenging thing. In my case, the information I possessed -- the solution to that problem -- was of questionable value to begin with (I didn't even know whether it was right) and the information would have zero value the following day. Furthermore, my threshold for sharing that information was unjustifiably high -- I didn't stand to gain or lose much if anything by sharing it. To make an unbiased assessment, the only obstacle I had to overcome was my own ego.

And that is the only thing I remember to this day about my high school Calculus class.