Time Server Example -- Error in documentation
Maverick.Crank.GRey
maverick.crank.grey at gmail.com
Wed Sep 7 09:59:16 EDT 2011
Hello Trustin,
In additional to the issue found by Tiberiu I would like to mentioned that
http://docs.jboss.org/netty/3.2/guide/html_single/index.html#d0e440 the
suggested implementation of http://tools.ietf.org/html/rfc868 TIME
protocol (RFC 868) is incorrect. According to RFC 868 "The Time service
sends back to the originating source the time in seconds since midnight on
*January first 1900*." and not "the difference, measured in milliseconds,
between the current time and midnight, *January 1, 1970 UTC*" returned by
http://download.oracle.com/javase/6/docs/api/java/lang/System.html#currentTimeMillis()
java.lang.System.currentTimeMillis() .
Here is a little bit corrected example of TimeServerHandler
package ru.spb.mcgrey.test.java.netty.server;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.*;
import java.util.Calendar;
public class TimeServerHandler extends SimpleChannelHandler {
@Override
public void channelConnected(ChannelHandlerContext ctx,
ChannelStateEvent e) {
Channel ch = e.getChannel();
ChannelBuffer time = ChannelBuffers.buffer(4);
Calendar rightNow = Calendar.getInstance();
rightNow.roll(Calendar.YEAR, 70); // to be comply with RFC 868
long timeMs = rightNow.getTimeInMillis();
long timeS = timeMs / 1000;
time.writeInt((int)timeS);
ChannelFuture f = ch.write(time);
f.addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
e.getCause().printStackTrace();
e.getChannel().close();
}
}
BTW, being comply with RFC 868 gives us the additional advantage. We can use
standard tools to test our time server instead of writing a Time Client. I
used http://linux.die.net/man/1/rdate rdate for my tests.
$ rdate -o 8037 -p localhost
Thu Sep 8 17:45:27 UTC 2011
I date to hope, my post can make Netty documentation a little bit correctly
=)
--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Time-Server-Example-Error-in-documentation-tp6710743p6767899.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list