You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2005/01/13 06:19:16 UTC
svn commit: r125039 - in incubator/directory/ntp/trunk: main/src/java/org/apache/ntp protocol/src/java/org/apache/ntp/protocol
Author: erodriguez
Date: Wed Jan 12 21:19:15 2005
New Revision: 125039
URL: http://svn.apache.org/viewcvs?view=rev&rev=125039
Log:
NTP front-end. Tested on FC2 with ntpdate.
Added:
incubator/directory/ntp/trunk/main/src/java/org/apache/ntp/Main.java
incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpDecoder.java
incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpEncoder.java
incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolHandler.java
incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolProvider.java
Added: incubator/directory/ntp/trunk/main/src/java/org/apache/ntp/Main.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/main/src/java/org/apache/ntp/Main.java?view=auto&rev=125039
==============================================================================
--- (empty file)
+++ incubator/directory/ntp/trunk/main/src/java/org/apache/ntp/Main.java Wed Jan 12 21:19:15 2005
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.ntp;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.io.datagram.DatagramAcceptor;
+import org.apache.mina.io.filter.IoThreadPoolFilter;
+import org.apache.mina.io.socket.SocketAcceptor;
+import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
+import org.apache.mina.protocol.io.IoProtocolAcceptor;
+import org.apache.ntp.protocol.NtpProtocolProvider;
+
+
+public class Main
+{
+ private static final int PORT = 123;
+
+ public static void main( String[] args ) throws Exception
+ {
+ // Create I/O and Protocol thread pool filter.
+ // I/O thread pool performs encoding and decoding of messages.
+ // Protocol thread pool performs actual protocol flow.
+ IoThreadPoolFilter ioThreadPoolFilter = new IoThreadPoolFilter();
+ ProtocolThreadPoolFilter protocolThreadPoolFilter = new ProtocolThreadPoolFilter();
+
+ // and start both.
+ ioThreadPoolFilter.start();
+ protocolThreadPoolFilter.start();
+
+ // Create a TCP/IP acceptor.
+ IoProtocolAcceptor acceptor = new IoProtocolAcceptor( new SocketAcceptor() );
+
+ // Add both thread pool filters.
+ acceptor.getIoAcceptor().addFilter( Integer.MAX_VALUE, ioThreadPoolFilter );
+ acceptor.addFilter( Integer.MAX_VALUE, protocolThreadPoolFilter );
+
+ // Bind
+ acceptor.bind( new InetSocketAddress( PORT ), new NtpProtocolProvider() );
+
+ // Create a UDP/IP acceptor
+ IoProtocolAcceptor datagramAcceptor = new IoProtocolAcceptor( new DatagramAcceptor() );
+
+ // Add both thread pool filters.
+ datagramAcceptor.getIoAcceptor().addFilter( Integer.MAX_VALUE, ioThreadPoolFilter );
+ datagramAcceptor.addFilter( Integer.MAX_VALUE, protocolThreadPoolFilter );
+
+ // Bind
+ datagramAcceptor.bind( new InetSocketAddress( PORT ), new NtpProtocolProvider() );
+
+ System.out.println( "Apache NTP listening on port " + PORT );
+ }
+}
+
Added: incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpDecoder.java?view=auto&rev=125039
==============================================================================
--- (empty file)
+++ incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpDecoder.java Wed Jan 12 21:19:15 2005
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.ntp.protocol;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolDecoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+import org.apache.ntp.io.NtpMessageDecoder;
+
+
+public class NtpDecoder implements ProtocolDecoder
+{
+ public void decode( ProtocolSession session, ByteBuffer in, ProtocolDecoderOutput out )
+ throws ProtocolViolationException
+ {
+ NtpMessageDecoder decoder = new NtpMessageDecoder();
+ out.write( decoder.decode( in.buf() ) );
+ }
+}
+
Added: incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpEncoder.java?view=auto&rev=125039
==============================================================================
--- (empty file)
+++ incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpEncoder.java Wed Jan 12 21:19:15 2005
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.ntp.protocol;
+
+import org.apache.mina.common.ByteBuffer;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolEncoderOutput;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.mina.protocol.ProtocolViolationException;
+import org.apache.ntp.io.NtpMessageEncoder;
+import org.apache.ntp.message.NtpMessage;
+
+
+public class NtpEncoder implements ProtocolEncoder
+{
+ public void encode( ProtocolSession session, Object message, ProtocolEncoderOutput out )
+ throws ProtocolViolationException
+ {
+ NtpMessageEncoder encoder = new NtpMessageEncoder();
+
+ ByteBuffer buf = ByteBuffer.allocate( 1024 );
+ encoder.encode( buf.buf(), (NtpMessage)message );
+
+ buf.flip();
+
+ out.write( buf );
+ }
+}
+
Added: incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolHandler.java?view=auto&rev=125039
==============================================================================
--- (empty file)
+++ incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolHandler.java Wed Jan 12 21:19:15 2005
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.ntp.protocol;
+
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolSession;
+import org.apache.ntp.NtpService;
+import org.apache.ntp.message.NtpMessage;
+import org.apache.ntp.service.NtpServiceImpl;
+
+
+public class NtpProtocolHandler implements ProtocolHandler
+{
+
+ public void sessionOpened( ProtocolSession session )
+ {
+ System.out.println( session.getRemoteAddress() + " OPENED" );
+ }
+
+ public void sessionClosed( ProtocolSession session )
+ {
+ System.out.println( session.getRemoteAddress() + " CLOSED" );
+ }
+
+ public void sessionIdle( ProtocolSession session, IdleStatus status )
+ {
+ System.out.println( session.getRemoteAddress() + " IDLE(" + status + ")" );
+ }
+
+ public void exceptionCaught( ProtocolSession session, Throwable cause )
+ {
+ System.out.println( session.getRemoteAddress() + " EXCEPTION" );
+ cause.printStackTrace( System.out );
+
+ session.close();
+ }
+
+ public void messageReceived( ProtocolSession session, Object message )
+ {
+ System.out.println( session.getRemoteAddress() + " RCVD: " + message );
+
+ NtpService ntpService = new NtpServiceImpl();
+ NtpMessage reply = ntpService.getReplyFor( (NtpMessage)message );
+
+ session.write( reply );
+ }
+
+ public void messageSent( ProtocolSession session, Object message )
+ {
+ System.out.println( session.getRemoteAddress() + " SENT: " + message );
+ }
+}
+
Added: incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolProvider.java?view=auto&rev=125039
==============================================================================
--- (empty file)
+++ incubator/directory/ntp/trunk/protocol/src/java/org/apache/ntp/protocol/NtpProtocolProvider.java Wed Jan 12 21:19:15 2005
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.ntp.protocol;
+
+import org.apache.mina.protocol.ProtocolCodecFactory;
+import org.apache.mina.protocol.ProtocolDecoder;
+import org.apache.mina.protocol.ProtocolEncoder;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolProvider;
+
+
+public class NtpProtocolProvider implements ProtocolProvider
+{
+ // Protocol handler is usually a singleton.
+ private static ProtocolHandler HANDLER = new NtpProtocolHandler();
+
+ // Codec factory is also usually a singleton.
+ private static ProtocolCodecFactory CODEC_FACTORY = new ProtocolCodecFactory()
+ {
+ public ProtocolEncoder newEncoder()
+ {
+ // Create a new encoder.
+ return new NtpEncoder();
+ }
+
+ public ProtocolDecoder newDecoder()
+ {
+ // Create a new decoder.
+ return new NtpDecoder();
+ }
+ };
+
+ public ProtocolCodecFactory getCodecFactory()
+ {
+ return CODEC_FACTORY;
+ }
+
+ public ProtocolHandler getHandler()
+ {
+ return HANDLER;
+ }
+}
+