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 23:58:04 UTC

svn commit: r125110 - in incubator/directory/dhcp/trunk: main/src/java/org/apache/dhcp protocol/src/java/org/apache/dhcp/protocol

Author: erodriguez
Date: Thu Jan 13 14:58:03 2005
New Revision: 125110

URL: http://svn.apache.org/viewcvs?view=rev&rev=125110
Log:
DHCP frontend implementation.
Added:
   incubator/directory/dhcp/trunk/main/src/java/org/apache/dhcp/Main.java
   incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpDecoder.java
   incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpEncoder.java
   incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolHandler.java
   incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolProvider.java

Added: incubator/directory/dhcp/trunk/main/src/java/org/apache/dhcp/Main.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/main/src/java/org/apache/dhcp/Main.java?view=auto&rev=125110
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/main/src/java/org/apache/dhcp/Main.java	Thu Jan 13 14:58:03 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.dhcp;
+
+import java.net.InetSocketAddress;
+
+import org.apache.dhcp.protocol.DhcpProtocolProvider;
+import org.apache.mina.io.datagram.DatagramAcceptor;
+import org.apache.mina.io.filter.IoThreadPoolFilter;
+import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
+import org.apache.mina.protocol.io.IoProtocolAcceptor;
+
+
+public class Main
+{
+    private static final int PORT = 67;
+
+    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 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 DhcpProtocolProvider() );
+
+        System.out.println( "Apache DHCP listening on port " + PORT );
+    }
+}
+

Added: incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpDecoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpDecoder.java?view=auto&rev=125110
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpDecoder.java	Thu Jan 13 14:58:03 2005
@@ -0,0 +1,45 @@
+/*
+ *   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.dhcp.protocol;
+
+import org.apache.dhcp.DhcpException;
+import org.apache.dhcp.io.DhcpMessageDecoder;
+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;
+
+
+public class DhcpDecoder implements ProtocolDecoder
+{
+    public void decode( ProtocolSession session, ByteBuffer in, ProtocolDecoderOutput out )
+            throws ProtocolViolationException
+    {
+    	 DhcpMessageDecoder decoder = new DhcpMessageDecoder();
+    	 try
+		 {
+    	 	out.write( decoder.decode( in.buf() ) );
+		 }
+    	 catch ( DhcpException de)
+		 {
+    	 	de.printStackTrace();
+		 }
+    }
+}
+

Added: incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpEncoder.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpEncoder.java?view=auto&rev=125110
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpEncoder.java	Thu Jan 13 14:58:03 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.dhcp.protocol;
+
+import org.apache.dhcp.io.DhcpMessageEncoder;
+import org.apache.dhcp.messages.DhcpMessage;
+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;
+
+
+public class DhcpEncoder implements ProtocolEncoder
+{
+    public void encode( ProtocolSession session, Object message, ProtocolEncoderOutput out )
+            throws ProtocolViolationException
+    {
+        DhcpMessageEncoder encoder = new DhcpMessageEncoder();
+        
+        ByteBuffer buf = ByteBuffer.allocate( 1024 );
+        encoder.encode( buf.buf(), (DhcpMessage)message );
+        
+        buf.flip();
+        
+        out.write( buf );
+    }
+}
+

Added: incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolHandler.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolHandler.java?view=auto&rev=125110
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolHandler.java	Thu Jan 13 14:58:03 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.dhcp.protocol;
+
+import org.apache.dhcp.DhcpService;
+import org.apache.dhcp.messages.DhcpMessage;
+import org.apache.dhcp.service.DhcpServiceImpl;
+import org.apache.mina.common.IdleStatus;
+import org.apache.mina.protocol.ProtocolHandler;
+import org.apache.mina.protocol.ProtocolSession;
+
+
+public class DhcpProtocolHandler 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 );
+        
+        DhcpService dhcpService = new DhcpServiceImpl();
+        DhcpMessage reply = dhcpService.getReplyFor( (DhcpMessage)message );
+        
+        session.write( reply );
+    }
+
+    public void messageSent( ProtocolSession session, Object message )
+    {
+        System.out.println( session.getRemoteAddress() + " SENT: " + message );
+    }
+}
+

Added: incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolProvider.java
Url: http://svn.apache.org/viewcvs/incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolProvider.java?view=auto&rev=125110
==============================================================================
--- (empty file)
+++ incubator/directory/dhcp/trunk/protocol/src/java/org/apache/dhcp/protocol/DhcpProtocolProvider.java	Thu Jan 13 14:58:03 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.dhcp.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 DhcpProtocolProvider implements ProtocolProvider
+{
+    // Protocol handler is usually a singleton.
+    private static ProtocolHandler HANDLER = new DhcpProtocolHandler();
+
+    // Codec factory is also usually a singleton.
+    private static ProtocolCodecFactory CODEC_FACTORY = new ProtocolCodecFactory()
+    {
+        public ProtocolEncoder newEncoder()
+        {
+            // Create a new encoder.
+            return new DhcpEncoder();
+        }
+
+        public ProtocolDecoder newDecoder()
+        {
+            // Create a new decoder.
+            return new DhcpDecoder();
+        }
+    };
+
+    public ProtocolCodecFactory getCodecFactory()
+    {
+        return CODEC_FACTORY;
+    }
+
+    public ProtocolHandler getHandler()
+    {
+        return HANDLER;
+    }
+}
+