Ever encountered an old computer that always has the wrong time? I can recall many an old 386, AMD K6, or Pentium that never had the right time.
As a young kid; I would relentlessly reset the time to an always abysmal result. Every time the computer turned off, the slate was wiped clean
and the clock was wrong again! Usually this has to do with the little coin cell battery on the computer motherboard running out of juice. The beard
wielding, ponytail donning, Hawaiian shirt wearing, computer scientists of the past created the network time protocol, NTP to help address this issue.
NTP is kind of like being able to pick up the phone and call someone who has access to an official atomic clock to make sure the time on your microwave,
oven, and nightstand are set correctly after a power failure. NTP is used in the background of almost all modern operating systems for automatically ensuring computer clocks stay synchronized.
When deploying software that runs on a remote client, there are situations that you run into that really need a good clock source. Cryptography is one example that can
require an accurate clock source for certain implementations. On the Flash platform, and in many other programming languages date and time are obtained directly from the
local computer clock. This is at odds with common security paradigms which follow a trust no one approach. So thinking like a hacker or someone trying to cheat at an
online game, I would alter the system clock to my benefit if I knew it were being used.
I encountered the need for a series of exhibits I am working on to have at a minimum accurate synchronization of the current day on their system clocks for a hashing operation
I am using for file uniqueness and retrieval. This implementation may be over-kill just to sync the day and the OS may handle it automatically; but when planning the solution
I was brought back to my experiences as a kid with always wrong computer clocks. I suddenly imagined myself getting support calls in ten years for a broken exhibit that for
whatever reason's clocks were wrong. That sealed the deal on this NTP implementation.
Searching around, there were no Actionscript 3 NTP implementations for the Flash Platform; so I made my own. The code is wrapped in a simple test application.
It was built using Flash Builder 4.6 or Flash CS4. You should be able to compile it with Flash Develop as well. You will need to use Adobe AIR, so this limits
deployment to desktop and mobile applications. This implementation is based heavily on Adam Buckley's implementation in Java.
OR
(Thanks to
Leo O'Donnell for requesting it)
Usage
ntpTime = new ntpTimeUtility();
ntpTime.addEventListener(ntpTimeUtility.NTP_TIME_RECIEVED, handleNTPTime, false, 0, true);
ntpTime.initiateUDPTimeRequest();
private function handleNTPTime(e:Event):void {
// The time returned from the time server which should be localized
// to the computers clock is in the ntpTIme.latestNTPTime variable
trace("main - ntp time utility return: " + ntpTime.latestNTPTime);
}
Watch out world; prepare for the resurgence of Flash as a viable web technology after this posting (get the sarcasm?)! In any case, I hope others out there can use this to implement the raw power of an atomic clock reference in their own applications.