At A Glance Main Projects Tutorials Resume

Contact


Email: palen1c at gmail.com




Actionscript AS3 NTP Network Time Protocol Implementation for Adobe AIR

Sat, 12 Jan 2013 14:07:37 EST

A screen shot of the adobe AIR NTP sample application.
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.

DOWNLOAD THE STATIC AS3 NTP IMPLEMENTATION


OR

GET THE LATEST ON GITHUB

(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.

Charles Palen has been involved in the technology sector for several years. His formal education focused on Enterprise Database Administration. He currently works as the principal software architect and manager at Transcending Digital where he can be hired for your next contract project. Charles is a full stack developer who has been on the front lines of small business and enterprise for over 10 years. Charles current expertise covers the areas of .NET, Java, PHP, Node.js, Javascript, HTML, and CSS. Charles created Technogumbo in 2008 as a way to share lessons learned while making original products.

Comments

Charles
Charles
February 8, 2013 09:48 am

I'm really glad you were able to find a use for this Graham. It's good to know about the useWeakReference. Typically on all of my listeners in a big app I'll use that in order to help with memory management. I haven't run into the same thing yet, but it's good to be aware of.

Graham
Graham
February 7, 2013 10:37 pm

Works great, except I had to troubleshoot one thing. In all the addEventListener calls useWeakReference is set to true. When I ran the code the first time it wasn't receiving a response from the DNS. I think my runtime is garbage collecting the listeners prematurely. When i reset useWeakReference to its default value everything works perfect. I'm using flex 4.6.

Graham
Graham
February 7, 2013 10:00 pm

This is exactly what I was looking for. Thanks for sharing it. Flash will probably be around for a very long time, especially when people realize that HTML5 is not the miracle solution it has been hyped to be. Kind of like when everyone thought Obama was sure to loose reelection, then they saw the competition.

Comments are currently disabled.