Author: trustin Date: Thu Dec 16 22:38:15 2004 New Revision: 122618 URL: http://svn.apache.org/viewcvs?view=rev&rev=122618 Log: * Reformatted code * Updated license header Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java
Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/IdleStatus.java Thu Dec 16 22:38:15 2004 @@ -1,45 +1,44 @@ /* - * Copyright 2004 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. - * + * Copyright 2004 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. */ /* * @(#) $Id$ */ package org.apache.mina.core; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class IdleStatus { - public static final IdleStatus READER_IDLE = new IdleStatus("reader idle"); - public static final IdleStatus WRITER_IDLE = new IdleStatus("writer idle"); - public static final IdleStatus BOTH_IDLE = new IdleStatus("both idle"); +public class IdleStatus +{ + public static final IdleStatus READER_IDLE = new IdleStatus( "reader idle" ); + + public static final IdleStatus WRITER_IDLE = new IdleStatus( "writer idle" ); + + public static final IdleStatus BOTH_IDLE = new IdleStatus( "both idle" ); + private final String strValue; /** * Creates a new instance. */ - private IdleStatus(String strValue) { + private IdleStatus( String strValue ) + { this.strValue = strValue; } - public String toString() { + public String toString() + { return strValue; } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/core/SessionConfig.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,22 +16,19 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.core; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface SessionConfig { - int getIdleTime(IdleStatus status); +public interface SessionConfig +{ + int getIdleTime( IdleStatus status ); - long getIdleTimeInMillis(IdleStatus status); + long getIdleTimeInMillis( IdleStatus status ); - void setIdleTime(IdleStatus status, int idleTime); -} + void setIdleTime( IdleStatus status, int idleTime ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Acceptor.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,25 +16,22 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io; import java.io.IOException; import java.net.SocketAddress; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface Acceptor { - void bind(SocketAddress address, IoSessionHandler defaultHandler) - throws IOException; +public interface Acceptor +{ + void bind( SocketAddress address, IoSessionHandler defaultHandler ) + throws IOException; - void unbind(SocketAddress address); -} + void unbind( SocketAddress address ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/Connector.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,26 +16,23 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io; import java.io.IOException; import java.net.SocketAddress; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface Connector { - void connect(SocketAddress address, IoSessionHandler defaultHandler) - throws IOException; +public interface Connector +{ + void connect( SocketAddress address, IoSessionHandler defaultHandler ) + throws IOException; - void connect(SocketAddress address, int timeout, - IoSessionHandler defaultHandler) throws IOException; -} + void connect( SocketAddress address, int timeout, + IoSessionHandler defaultHandler ) throws IOException; +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSession.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io; import java.net.SocketAddress; @@ -24,20 +23,19 @@ import org.apache.mina.core.IdleStatus; import org.apache.mina.core.SessionConfig; - - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface IoSession { +public interface IoSession +{ void close(); Object getAttachment(); - void setAttachment(Object attachment); + void setAttachment( Object attachment ); ReadBuffer getReadBuffer(); @@ -63,5 +61,5 @@ long getLastWriteTime(); - boolean isIdle(IdleStatus status); -} + boolean isIdle( IdleStatus status ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/IoSessionHandler.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,33 +16,29 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io; import org.apache.mina.core.IdleStatus; - - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface IoSessionHandler { - void sessionOpened(IoSession IoSession); +public interface IoSessionHandler +{ + void sessionOpened( IoSession IoSession ); + + void sessionClosed( IoSession IoSession ); - void sessionClosed(IoSession IoSession); + void sessionIdle( IoSession IoSession, IdleStatus status ); - void sessionIdle(IoSession IoSession, IdleStatus status); + void exceptionCaught( IoSession IoSession, Throwable cause ); - void exceptionCaught(IoSession IoSession, Throwable cause); + void dataRead( IoSession IoSession, int readBytes ); - void dataRead(IoSession IoSession, int readBytes); + void dataWritten( IoSession IoSession, int writtenBytes ); - void dataWritten(IoSession IoSession, int writtenBytes); - - void markerReleased(IoSession IoSession, Object marker); -} + void markerReleased( IoSession IoSession, Object marker ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/ReadBuffer.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,25 +16,22 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io; import java.nio.ByteBuffer; import java.nio.ByteOrder; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -public interface ReadBuffer { +public interface ReadBuffer +{ boolean hasRemaining(); - ReadBuffer skip(int length); + ReadBuffer skip( int length ); ReadBuffer skipAll(); @@ -48,9 +47,9 @@ byte get(); - ReadBuffer get(byte[] dst); + ReadBuffer get( byte[] dst ); - ReadBuffer get(byte[] dst, int offset, int length); + ReadBuffer get( byte[] dst, int offset, int length ); char getChar(); @@ -66,7 +65,7 @@ ByteOrder order(); - ReadBuffer order(ByteOrder order); + ReadBuffer order( ByteOrder order ); ByteBuffer asByteBuffer(); -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/WriteBuffer.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,22 +16,19 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io; import java.nio.ByteBuffer; import java.nio.ByteOrder; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -public interface WriteBuffer { +public interface WriteBuffer +{ boolean hasRemaining(); WriteBuffer clear(); @@ -42,35 +41,35 @@ WriteBuffer reset(); - WriteBuffer put(byte b); + WriteBuffer put( byte b ); - WriteBuffer put(byte[] src); + WriteBuffer put( byte[] src ); - WriteBuffer put(byte[] src, int offset, int length); + WriteBuffer put( byte[] src, int offset, int length ); - WriteBuffer put(ByteBuffer buf); + WriteBuffer put( ByteBuffer buf ); - WriteBuffer put(ReadBuffer buf); + WriteBuffer put( ReadBuffer buf ); - WriteBuffer putChar(char c); + WriteBuffer putChar( char c ); - WriteBuffer putDouble(double d); + WriteBuffer putDouble( double d ); - WriteBuffer putFloat(float f); + WriteBuffer putFloat( float f ); - WriteBuffer putInt(int i); + WriteBuffer putInt( int i ); - WriteBuffer putLong(long l); + WriteBuffer putLong( long l ); - WriteBuffer putShort(short s); + WriteBuffer putShort( short s ); ByteOrder order(); - WriteBuffer order(ByteOrder order); + WriteBuffer order( ByteOrder order ); ByteBuffer asByteBuffer(); WriteBuffer flush(); - - WriteBuffer putMarker(Object marker); -} + + WriteBuffer putMarker( Object marker ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpAcceptor.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.io.IOException; @@ -36,114 +35,138 @@ import org.apache.mina.io.Acceptor; import org.apache.mina.io.IoSessionHandler; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class TcpAcceptor implements Acceptor { +public class TcpAcceptor implements Acceptor +{ private static volatile int nextId = 0; - private static final Log log = LogFactory.getLog(TcpAcceptor.class); + + private static final Log log = LogFactory.getLog( TcpAcceptor.class ); + private final int id = nextId++; + private final Selector selector; + private final Map channels = new HashMap(); + private Worker worker; /** * Creates a new instance. + * * @throws IOException */ - public TcpAcceptor() throws IOException { + public TcpAcceptor() throws IOException + { selector = Selector.open(); } - public void bind(SocketAddress address, IoSessionHandler defaultHandler) - throws IOException { - this.bind(address, 50, defaultHandler); + public void bind( SocketAddress address, IoSessionHandler defaultHandler ) + throws IOException + { + this.bind( address, 50, defaultHandler ); } - public synchronized void bind(SocketAddress address, int backlog, - IoSessionHandler defaultHandler) - throws IOException { - Validate.notNull(address); - Validate.notNull(defaultHandler); - - if (!(address instanceof InetSocketAddress)) - throw new IllegalArgumentException("Unexpected address type: " + - address.getClass()); + public synchronized void bind( SocketAddress address, int backlog, + IoSessionHandler defaultHandler ) + throws IOException + { + Validate.notNull( address ); + Validate.notNull( defaultHandler ); + + if( ! ( address instanceof InetSocketAddress ) ) + throw new IllegalArgumentException( "Unexpected address type: " + + address.getClass() ); ServerSocketChannel ssc = ServerSocketChannel.open(); - ssc.configureBlocking(false); - ssc.socket().bind(address, backlog); - ssc.register(selector, SelectionKey.OP_ACCEPT, defaultHandler); + ssc.configureBlocking( false ); + ssc.socket().bind( address, backlog ); + ssc.register( selector, SelectionKey.OP_ACCEPT, defaultHandler ); - channels.put(address, ssc); + channels.put( address, ssc ); - if (worker == null) { + if( worker == null ) + { worker = new Worker(); worker.start(); } } - public synchronized void unbind(SocketAddress address) { - Validate.notNull(address); + public synchronized void unbind( SocketAddress address ) + { + Validate.notNull( address ); - ServerSocketChannel ssc = (ServerSocketChannel) channels.get(address); + ServerSocketChannel ssc = ( ServerSocketChannel ) channels + .get( address ); - if (ssc == null) - throw new IllegalArgumentException("Unknown address: " + address); + if( ssc == null ) + throw new IllegalArgumentException( "Unknown address: " + address ); - SelectionKey key = ssc.keyFor(selector); + SelectionKey key = ssc.keyFor( selector ); key.cancel(); - channels.remove(address); + channels.remove( address ); - try { + try + { ssc.close(); - } catch (IOException e) { - log.error("Unexpected exception", e); + } + catch( IOException e ) + { + log.error( "Unexpected exception", e ); } } - private class Worker extends Thread { - public Worker() { - super("TcpAcceptor-" + id); + private class Worker extends Thread + { + public Worker() + { + super( "TcpAcceptor-" + id ); } - public void run() { - for (;;) { - try { + public void run() + { + for( ;; ) + { + try + { int nKeys = selector.select(); - if (nKeys == 0) + if( nKeys == 0 ) continue; Iterator it = selector.selectedKeys().iterator(); - while (it.hasNext()) { - SelectionKey key = (SelectionKey) it.next(); + while( it.hasNext() ) + { + SelectionKey key = ( SelectionKey ) it.next(); it.remove(); - if (!key.isAcceptable()) + if( !key.isAcceptable() ) continue; - ServerSocketChannel ssc = - (ServerSocketChannel) key.channel(); + ServerSocketChannel ssc = ( ServerSocketChannel ) key + .channel(); SocketChannel ch = ssc.accept(); - if (ch == null) + if( ch == null ) continue; - TcpSession session = - new TcpSession(ch, - (IoSessionHandler) key.attachment()); - TcpIoProcessor.getInstance().addSession(session); + TcpSession session = new TcpSession( + ch, + ( IoSessionHandler ) key + .attachment() ); + TcpIoProcessor.getInstance().addSession( session ); } - } catch (IOException e) { - log.error("Unexpected exception.", e); + } + catch( IOException e ) + { + log.error( "Unexpected exception.", e ); } } } } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpConnector.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.io.IOException; @@ -35,108 +34,134 @@ import org.apache.mina.io.Connector; import org.apache.mina.io.IoSessionHandler; - /** - * TODO Insert type comment. - * TODO Stop worker thread when not used. - * + * TODO Insert type comment. TODO Stop worker thread when not used. + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class TcpConnector implements Connector { +public class TcpConnector implements Connector +{ private static volatile int nextId = 0; - private static final Log log = LogFactory.getLog(TcpConnector.class); + + private static final Log log = LogFactory.getLog( TcpConnector.class ); + private final int id = nextId++; + private final Selector selector; + private Worker worker; /** * Creates a new instance. + * * @throws IOException */ - public TcpConnector() throws IOException { + public TcpConnector() throws IOException + { selector = Selector.open(); } - public void connect(SocketAddress address, IoSessionHandler defaultHandler) - throws IOException { - connect(address, Integer.MAX_VALUE, defaultHandler); - } - - public void connect(SocketAddress address, int timeout, - IoSessionHandler defaultHandler) - throws IOException { - Validate.notNull(address); - Validate.notNull(defaultHandler); - - if (timeout <= 0) - throw new IllegalArgumentException("Illegal timeout: " + timeout); - - if (!(address instanceof InetSocketAddress)) - throw new IllegalArgumentException("Unexpected address type: " + - address.getClass()); + public void connect( SocketAddress address, IoSessionHandler defaultHandler ) + throws IOException + { + connect( address, Integer.MAX_VALUE, defaultHandler ); + } + + public void connect( SocketAddress address, int timeout, + IoSessionHandler defaultHandler ) throws IOException + { + Validate.notNull( address ); + Validate.notNull( defaultHandler ); + + if( timeout <= 0 ) + throw new IllegalArgumentException( "Illegal timeout: " + timeout ); + + if( ! ( address instanceof InetSocketAddress ) ) + throw new IllegalArgumentException( "Unexpected address type: " + + address.getClass() ); SocketChannel ch = SocketChannel.open(); - ch.configureBlocking(false); + ch.configureBlocking( false ); - if (ch.connect(address)) { - newSession(ch, defaultHandler); - } else { - ConnectEntry entry = new ConnectEntry(timeout, defaultHandler); - ch.register(selector, SelectionKey.OP_CONNECT, entry); - - if (worker == null) { - synchronized (this) { - if (worker == null) { + if( ch.connect( address ) ) + { + newSession( ch, defaultHandler ); + } + else + { + ConnectEntry entry = new ConnectEntry( timeout, defaultHandler ); + ch.register( selector, SelectionKey.OP_CONNECT, entry ); + + if( worker == null ) + { + synchronized( this ) + { + if( worker == null ) + { worker = new Worker(); worker.start(); } } } - synchronized (entry) { - while (!entry.done) { - try { + synchronized( entry ) + { + while( !entry.done ) + { + try + { entry.wait(); - } catch (InterruptedException e) { + } + catch( InterruptedException e ) + { } } } - if (entry.exception != null) + if( entry.exception != null ) throw entry.exception; } } - private void processSessions(Set keys) { + private void processSessions( Set keys ) + { Iterator it = keys.iterator(); - while (it.hasNext()) { - SelectionKey key = (SelectionKey) it.next(); + while( it.hasNext() ) + { + SelectionKey key = ( SelectionKey ) it.next(); - if (!key.isConnectable()) + if( !key.isConnectable() ) continue; - SocketChannel ch = (SocketChannel) key.channel(); - ConnectEntry entry = (ConnectEntry) key.attachment(); + SocketChannel ch = ( SocketChannel ) key.channel(); + ConnectEntry entry = ( ConnectEntry ) key.attachment(); - try { + try + { ch.finishConnect(); entry.done = true; - synchronized (entry) { + synchronized( entry ) + { entry.notify(); } - newSession(ch, entry.handler); - } catch (IOException e) { + newSession( ch, entry.handler ); + } + catch( IOException e ) + { entry.exception = e; entry.done = true; - synchronized (entry) { + synchronized( entry ) + { entry.notify(); } - } finally { + } + finally + { key.cancel(); } } @@ -144,23 +169,27 @@ keys.clear(); } - private void processTimedOutSessions(Set keys) { + private void processTimedOutSessions( Set keys ) + { long currentTime = System.currentTimeMillis(); Iterator it = keys.iterator(); - while (it.hasNext()) { - SelectionKey key = (SelectionKey) it.next(); + while( it.hasNext() ) + { + SelectionKey key = ( SelectionKey ) it.next(); - if (!key.isValid()) + if( !key.isValid() ) continue; - ConnectEntry entry = (ConnectEntry) key.attachment(); + ConnectEntry entry = ( ConnectEntry ) key.attachment(); - if (currentTime >= entry.deadline) { + if( currentTime >= entry.deadline ) + { entry.exception = new ConnectException(); entry.done = true; - synchronized (entry) { + synchronized( entry ) + { entry.notify(); } @@ -169,41 +198,54 @@ } } - private void newSession(SocketChannel ch, IoSessionHandler handler) { - TcpSession session = new TcpSession(ch, handler); - TcpIoProcessor.getInstance().addSession(session); - } - - private class Worker extends Thread { - public Worker() { - super("TcpConnector-" + id); - } - - public void run() { - for (;;) { - try { - int nKeys = selector.select(1000); - - if (nKeys > 0) - processSessions(selector.selectedKeys()); - - processTimedOutSessions(selector.keys()); - } catch (IOException e) { - log.error("Unexpected exception.", e); + private void newSession( SocketChannel ch, IoSessionHandler handler ) + { + TcpSession session = new TcpSession( ch, handler ); + TcpIoProcessor.getInstance().addSession( session ); + } + + private class Worker extends Thread + { + public Worker() + { + super( "TcpConnector-" + id ); + } + + public void run() + { + for( ;; ) + { + try + { + int nKeys = selector.select( 1000 ); + + if( nKeys > 0 ) + processSessions( selector.selectedKeys() ); + + processTimedOutSessions( selector.keys() ); + } + catch( IOException e ) + { + log.error( "Unexpected exception.", e ); } } } } - private static class ConnectEntry { + private static class ConnectEntry + { private final long deadline; + private final IoSessionHandler handler; + private boolean done; + private IOException exception; - private ConnectEntry(int timeout, IoSessionHandler handler) { + private ConnectEntry( int timeout, IoSessionHandler handler ) + { this.deadline = System.currentTimeMillis() + timeout * 1000L; this.handler = handler; } } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpIoProcessor.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.io.IOException; @@ -34,25 +33,29 @@ import org.apache.mina.util.ByteBufferPool; import org.apache.mina.util.Queue; - /** - * TODO Document me. - * TODO Stop worker thread if there is no session to process - * + * TODO Document me. TODO Stop worker thread if there is no session to process + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -class TcpIoProcessor { - private static final Log log = LogFactory.getLog(TcpIoProcessor.class); +class TcpIoProcessor +{ + private static final Log log = LogFactory.getLog( TcpIoProcessor.class ); + private static final TcpIoProcessor instance; - static { + static + { TcpIoProcessor tmp; - try { + try + { tmp = new TcpIoProcessor(); - } catch (IOException e) { - log.fatal("Failed to open selector.", e); + } + catch( IOException e ) + { + log.fatal( "Failed to open selector.", e ); tmp = null; } @@ -60,190 +63,244 @@ } private final Selector selector; - private final Queue newSessions = new Queue(16); - private final Queue removingSessions = new Queue(16); - private final Queue flushingSessions = new Queue(16); + + private final Queue newSessions = new Queue( 16 ); + + private final Queue removingSessions = new Queue( 16 ); + + private final Queue flushingSessions = new Queue( 16 ); + private Worker worker; + private long lastIdleCheckTime = System.currentTimeMillis(); - private TcpIoProcessor() throws IOException { + private TcpIoProcessor() throws IOException + { selector = Selector.open(); } - public static TcpIoProcessor getInstance() { + public static TcpIoProcessor getInstance() + { return instance; } - public void addSession(TcpSession session) { - if (worker == null) { - synchronized (this) { - if (worker == null) { + public void addSession( TcpSession session ) + { + if( worker == null ) + { + synchronized( this ) + { + if( worker == null ) + { worker = new Worker(); worker.start(); } } } - synchronized (newSessions) { - newSessions.push(session); + synchronized( newSessions ) + { + newSessions.push( session ); } selector.wakeup(); } - public void removeSession(TcpSession session) { - scheduleRemove(session); + public void removeSession( TcpSession session ) + { + scheduleRemove( session ); selector.wakeup(); } - public void flushSession(TcpSession session) { - scheduleFlush(session); + public void flushSession( TcpSession session ) + { + scheduleFlush( session ); selector.wakeup(); } - public void addReadableSession(TcpSession session) { + public void addReadableSession( TcpSession session ) + { SelectionKey key = session.getSelectionKey(); - if ((key.interestOps() & SelectionKey.OP_READ) == 0) - key.interestOps(key.interestOps() | SelectionKey.OP_READ); + if( ( key.interestOps() & SelectionKey.OP_READ ) == 0 ) + key.interestOps( key.interestOps() | SelectionKey.OP_READ ); } - private void addSessions() { - if (newSessions.size() == 0) + private void addSessions() + { + if( newSessions.size() == 0 ) return; TcpSession session; - for (;;) { - synchronized (newSessions) { - session = (TcpSession) newSessions.pop(); + for( ;; ) + { + synchronized( newSessions ) + { + session = ( TcpSession ) newSessions.pop(); } - if (session == null) + if( session == null ) break; SocketChannel ch = session.getChannel(); boolean registered; - try { - ch.configureBlocking(false); - ch.socket().setSendBufferSize(ByteBufferPool.CAPACITY); - ch.socket().setReceiveBufferSize(ByteBufferPool.CAPACITY); - session.setSelectionKey(ch.register(selector, - SelectionKey.OP_READ, - session)); + try + { + ch.configureBlocking( false ); + ch.socket().setSendBufferSize( ByteBufferPool.CAPACITY ); + ch.socket().setReceiveBufferSize( ByteBufferPool.CAPACITY ); + session.setSelectionKey( ch.register( selector, + SelectionKey.OP_READ, + session ) ); registered = true; - } catch (IOException e) { + } + catch( IOException e ) + { registered = false; - fireExceptionCaught(session, e); + fireExceptionCaught( session, e ); } - if (registered) { - fireSessionOpened(session); + if( registered ) + { + fireSessionOpened( session ); } } } - private void removeSessions() { - if (removingSessions.size() == 0) + private void removeSessions() + { + if( removingSessions.size() == 0 ) return; - for (;;) { + for( ;; ) + { TcpSession session; - synchronized (removingSessions) { - session = (TcpSession) removingSessions.pop(); + synchronized( removingSessions ) + { + session = ( TcpSession ) removingSessions.pop(); } - if (session == null) + if( session == null ) break; SocketChannel ch = session.getChannel(); session.getSelectionKey().cancel(); session.dispose(); - try { + try + { ch.close(); - } catch (IOException e) { - fireExceptionCaught(session, e); - } finally { - fireSessionClosed(session); + } + catch( IOException e ) + { + fireExceptionCaught( session, e ); + } + finally + { + fireSessionClosed( session ); } } } - private void processSessions(Set selectedKeys) { + private void processSessions( Set selectedKeys ) + { Iterator it = selectedKeys.iterator(); - while (it.hasNext()) { - SelectionKey key = (SelectionKey) it.next(); - TcpSession session = (TcpSession) key.attachment(); - - if (key.isReadable()) { - read(session); + while( it.hasNext() ) + { + SelectionKey key = ( SelectionKey ) it.next(); + TcpSession session = ( TcpSession ) key.attachment(); + + if( key.isReadable() ) + { + read( session ); } - if (key.isWritable()) { - scheduleFlush(session); + if( key.isWritable() ) + { + scheduleFlush( session ); } } selectedKeys.clear(); } - private void read(TcpSession session) { - TcpReadBuffer lock = (TcpReadBuffer) session.getReadBuffer(); + private void read( TcpSession session ) + { + TcpReadBuffer lock = ( TcpReadBuffer ) session.getReadBuffer(); ByteBuffer readBuf = lock.buf(); SocketChannel ch = session.getChannel(); - try { + try + { int readBytes = 0; int ret; - synchronized (lock) { + synchronized( lock ) + { readBuf.compact(); - try { - while ((ret = ch.read(readBuf)) > 0) { + try + { + while( ( ret = ch.read( readBuf ) ) > 0 ) + { readBytes += ret; } - } finally { + } + finally + { readBuf.flip(); readBuf.mark(); } - session.increaseReadBytes(readBytes); + session.increaseReadBytes( readBytes ); - if (ret >= 0) { - if (readBytes > 0) { - fireDataRead(session, readBytes); - } else { + if( ret >= 0 ) + { + if( readBytes > 0 ) + { + fireDataRead( session, readBytes ); + } + else + { SelectionKey key = session.getSelectionKey(); - key.interestOps(key.interestOps() & - (~SelectionKey.OP_READ)); + key.interestOps( key.interestOps() + & ( ~SelectionKey.OP_READ ) ); } - } else { - scheduleRemove(session); + } + else + { + scheduleRemove( session ); } } - } catch (Throwable e) { - fireExceptionCaught(session, e); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void scheduleRemove(TcpSession session) { - synchronized (removingSessions) { - removingSessions.push(session); + private void scheduleRemove( TcpSession session ) + { + synchronized( removingSessions ) + { + removingSessions.push( session ); } } - private void scheduleFlush(TcpSession session) { - synchronized (flushingSessions) { - flushingSessions.push(session); + private void scheduleFlush( TcpSession session ) + { + synchronized( flushingSessions ) + { + flushingSessions.push( session ); } } - private void notifyIdleSessions() { + private void notifyIdleSessions() + { Set keys = selector.keys(); Iterator it; TcpSession session; @@ -251,210 +308,285 @@ // process idle sessions long currentTime = System.currentTimeMillis(); - if ((keys != null) && ((currentTime - lastIdleCheckTime) >= 1000)) { + if( ( keys != null ) && ( ( currentTime - lastIdleCheckTime ) >= 1000 ) ) + { lastIdleCheckTime = currentTime; it = keys.iterator(); - while (it.hasNext()) { - SelectionKey key = (SelectionKey) it.next(); - session = (TcpSession) key.attachment(); + while( it.hasNext() ) + { + SelectionKey key = ( SelectionKey ) it.next(); + session = ( TcpSession ) key.attachment(); - notifyIdleSession(session, currentTime); + notifyIdleSession( session, currentTime ); } } } - private void notifyIdleSession(TcpSession session, long currentTime) { + private void notifyIdleSession( TcpSession session, long currentTime ) + { SessionConfig config = session.getConfig(); - notifyIdleSession0(session, currentTime, - config.getIdleTimeInMillis(IdleStatus.BOTH_IDLE), - IdleStatus.BOTH_IDLE, session.getLastIoTime()); - notifyIdleSession0(session, currentTime, - config.getIdleTimeInMillis(IdleStatus.READER_IDLE), - IdleStatus.READER_IDLE, session.getLastReadTime()); - notifyIdleSession0(session, currentTime, - config.getIdleTimeInMillis(IdleStatus.WRITER_IDLE), - IdleStatus.WRITER_IDLE, session.getLastWriteTime()); + notifyIdleSession0( + session, + currentTime, + config.getIdleTimeInMillis( IdleStatus.BOTH_IDLE ), + IdleStatus.BOTH_IDLE, session.getLastIoTime() ); + notifyIdleSession0( + session, + currentTime, + config + .getIdleTimeInMillis( IdleStatus.READER_IDLE ), + IdleStatus.READER_IDLE, session.getLastReadTime() ); + notifyIdleSession0( + session, + currentTime, + config + .getIdleTimeInMillis( IdleStatus.WRITER_IDLE ), + IdleStatus.WRITER_IDLE, session.getLastWriteTime() ); } - private void notifyIdleSession0(TcpSession session, long currentTime, + private void notifyIdleSession0( TcpSession session, long currentTime, long idleTime, IdleStatus status, - long lastIoTime) { - if (idleTime > 0 && !session.isIdle(status) && - (currentTime - lastIoTime) >= idleTime) { - session.setIdle(status); - fireSessionIdle(session, status); + long lastIoTime ) + { + if( idleTime > 0 && !session.isIdle( status ) + && ( currentTime - lastIoTime ) >= idleTime ) + { + session.setIdle( status ); + fireSessionIdle( session, status ); } } - private void flushSessions() { - if (flushingSessions.size() == 0) + private void flushSessions() + { + if( flushingSessions.size() == 0 ) return; - for (;;) { + for( ;; ) + { TcpSession session; - synchronized (flushingSessions) { - session = (TcpSession) flushingSessions.pop(); + synchronized( flushingSessions ) + { + session = ( TcpSession ) flushingSessions.pop(); } - if (session == null) + if( session == null ) break; - if (session.isClosed()) + if( session.isClosed() ) continue; - flush(session); + flush( session ); } } - private void flush(TcpSession session) { - TcpWriteBuffer lock = (TcpWriteBuffer) session.getWriteBuffer(); + private void flush( TcpSession session ) + { + TcpWriteBuffer lock = ( TcpWriteBuffer ) session.getWriteBuffer(); ByteBuffer writeBuf = lock.buf(); SocketChannel ch = session.getChannel(); - try { - synchronized (lock) { + try + { + synchronized( lock ) + { writeBuf.flip(); int writtenBytes; - try { - writtenBytes = ch.write(writeBuf); - } finally { + try + { + writtenBytes = ch.write( writeBuf ); + } + finally + { SelectionKey key = session.getSelectionKey(); - if (writeBuf.hasRemaining()) { + if( writeBuf.hasRemaining() ) + { // Kernel buffer is full - key.interestOps(key.interestOps() | - SelectionKey.OP_WRITE); - } else { - key.interestOps(key.interestOps() & - (~SelectionKey.OP_WRITE)); + key.interestOps( key.interestOps() + | SelectionKey.OP_WRITE ); + } + else + { + key.interestOps( key.interestOps() + & ( ~SelectionKey.OP_WRITE ) ); } writeBuf.compact(); writeBuf.mark(); } - if (writtenBytes > 0) { - session.increaseWrittenBytes(writtenBytes); - fireDataWritten(session, writtenBytes); + if( writtenBytes > 0 ) + { + session.increaseWrittenBytes( writtenBytes ); + fireDataWritten( session, writtenBytes ); Queue markers = lock.getMarkers(); - for (;;) { - TcpWriteBuffer.Marker marker = (TcpWriteBuffer.Marker) markers.first(); - if (marker == null) - break; - - int bytesLeft = marker.getBytesLeft(); - if (bytesLeft > writtenBytes) { - marker.setBytesLeft(bytesLeft - writtenBytes); - break; - } else if (bytesLeft == writtenBytes) { - markers.pop(); - fireMarkerRemoved(session, marker.getValue()); - break; - } else { - markers.pop(); - fireMarkerRemoved(session, marker.getValue()); - writtenBytes -= bytesLeft; - } + for( ;; ) + { + TcpWriteBuffer.Marker marker = ( TcpWriteBuffer.Marker ) markers + .first(); + if( marker == null ) + break; + + int bytesLeft = marker.getBytesLeft(); + if( bytesLeft > writtenBytes ) + { + marker.setBytesLeft( bytesLeft - writtenBytes ); + break; + } + else if( bytesLeft == writtenBytes ) + { + markers.pop(); + fireMarkerRemoved( session, marker.getValue() ); + break; + } + else + { + markers.pop(); + fireMarkerRemoved( session, marker.getValue() ); + writtenBytes -= bytesLeft; + } } } } - } catch (IOException e) { - fireExceptionCaught(session, e); + } + catch( IOException e ) + { + fireExceptionCaught( session, e ); } } - private void fireSessionOpened(TcpSession session) { - try { - session.getHandler().sessionOpened(session); - } catch (Throwable e) { - fireExceptionCaught(session, e); + private void fireSessionOpened( TcpSession session ) + { + try + { + session.getHandler().sessionOpened( session ); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void fireSessionClosed(TcpSession session) { - try { - session.getHandler().sessionClosed(session); - } catch (Throwable e) { - fireExceptionCaught(session, e); + private void fireSessionClosed( TcpSession session ) + { + try + { + session.getHandler().sessionClosed( session ); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void fireSessionIdle(TcpSession session, IdleStatus status) { - try { - session.getHandler().sessionIdle(session, status); - } catch (Throwable e) { - fireExceptionCaught(session, e); + private void fireSessionIdle( TcpSession session, IdleStatus status ) + { + try + { + session.getHandler().sessionIdle( session, status ); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void fireDataRead(TcpSession session, int readBytes) { - try { - session.getHandler().dataRead(session, readBytes); - } catch (Throwable e) { - fireExceptionCaught(session, e); + private void fireDataRead( TcpSession session, int readBytes ) + { + try + { + session.getHandler().dataRead( session, readBytes ); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void fireDataWritten(TcpSession session, int writtenBytes) { - try { - session.getHandler().dataWritten(session, writtenBytes); - } catch (Throwable e) { - fireExceptionCaught(session, e); + private void fireDataWritten( TcpSession session, int writtenBytes ) + { + try + { + session.getHandler().dataWritten( session, writtenBytes ); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void fireMarkerRemoved(TcpSession session, Object marker) { - try { - session.getHandler().markerReleased(session, marker); - } catch (Throwable e) { - fireExceptionCaught(session, e); + private void fireMarkerRemoved( TcpSession session, Object marker ) + { + try + { + session.getHandler().markerReleased( session, marker ); + } + catch( Throwable e ) + { + fireExceptionCaught( session, e ); } } - private void fireExceptionCaught(TcpSession session, Throwable cause) { - try { - session.getHandler().exceptionCaught(session, cause); + private void fireExceptionCaught( TcpSession session, Throwable cause ) + { + try + { + session.getHandler().exceptionCaught( session, cause ); - if (cause instanceof IOException) { - scheduleRemove(session); + if( cause instanceof IOException ) + { + scheduleRemove( session ); } - } catch (Throwable t) { - log.error("Exception from excaptionCaught.", t); + } + catch( Throwable t ) + { + log.error( "Exception from excaptionCaught.", t ); } } - private class Worker extends Thread { - public Worker() { - super("TcpIoProcessor"); - setDaemon(true); + private class Worker extends Thread + { + public Worker() + { + super( "TcpIoProcessor" ); + setDaemon( true ); } - public void run() { - for (;;) { - try { - int nKeys = selector.select(1000); + public void run() + { + for( ;; ) + { + try + { + int nKeys = selector.select( 1000 ); addSessions(); - if (nKeys > 0) { - processSessions(selector.selectedKeys()); + if( nKeys > 0 ) + { + processSessions( selector.selectedKeys() ); } flushSessions(); removeSessions(); notifyIdleSessions(); - } catch (IOException e) { - log.error("Unexpected exception.", e); - - try { - Thread.sleep(1000); - } catch (InterruptedException e1) { + } + catch( IOException e ) + { + log.error( "Unexpected exception.", e ); + + try + { + Thread.sleep( 1000 ); + } + catch( InterruptedException e1 ) + { } } } } } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpReadBuffer.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.nio.ByteBuffer; @@ -24,111 +23,134 @@ import org.apache.mina.io.ReadBuffer; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -class TcpReadBuffer implements ReadBuffer { +class TcpReadBuffer implements ReadBuffer +{ private final TcpSession parent; + private final ByteBuffer buf; - TcpReadBuffer(TcpSession parent, ByteBuffer buf) { + TcpReadBuffer( TcpSession parent, ByteBuffer buf ) + { this.parent = parent; this.buf = buf; } - ByteBuffer buf() { + ByteBuffer buf() + { return buf; } - public byte get() { + public byte get() + { return buf.get(); } - public ReadBuffer get(byte[] dst) { - buf.get(dst); + public ReadBuffer get( byte[] dst ) + { + buf.get( dst ); return this; } - public ReadBuffer get(byte[] dst, int offset, int length) { - buf.get(dst, offset, length); + public ReadBuffer get( byte[] dst, int offset, int length ) + { + buf.get( dst, offset, length ); return this; } - public char getChar() { + public char getChar() + { char ret = buf.getChar(); return ret; } - public double getDouble() { + public double getDouble() + { return buf.getDouble(); } - public float getFloat() { + public float getFloat() + { return buf.getFloat(); } - public int getInt() { + public int getInt() + { return buf.getInt(); } - public long getLong() { + public long getLong() + { return buf.getLong(); } - public short getShort() { + public short getShort() + { return buf.getShort(); } - public ByteOrder order() { + public ByteOrder order() + { return buf.order(); } - public ReadBuffer order(ByteOrder order) { - buf.order(order); + public ReadBuffer order( ByteOrder order ) + { + buf.order( order ); return this; } - public ByteBuffer asByteBuffer() { + public ByteBuffer asByteBuffer() + { return buf.duplicate().asReadOnlyBuffer(); } - public boolean hasRemaining() { + public boolean hasRemaining() + { return buf.hasRemaining(); } - public ReadBuffer skip(int length) { - buf.position(buf.position() + length); + public ReadBuffer skip( int length ) + { + buf.position( buf.position() + length ); return this; } - public ReadBuffer skipAll() { - return skip(remaining()); + public ReadBuffer skipAll() + { + return skip( remaining() ); } - public int capacity() { + public int capacity() + { return buf.capacity(); } - public int remaining() { + public int remaining() + { return buf.remaining(); } - public ReadBuffer mark() { + public ReadBuffer mark() + { buf.mark(); return this; } - public ReadBuffer reset() { + public ReadBuffer reset() + { buf.reset(); return this; } - public ReadBuffer signal() { - TcpIoProcessor.getInstance().addReadableSession(parent); + public ReadBuffer signal() + { + TcpIoProcessor.getInstance().addReadableSession( parent ); return this; } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSession.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.net.SocketAddress; @@ -32,158 +31,201 @@ import org.apache.mina.io.WriteBuffer; import org.apache.mina.util.ByteBufferPool; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -class TcpSession implements IoSession { +class TcpSession implements IoSession +{ private final SocketChannel ch; + private final TcpSessionConfig config; + private final TcpReadBuffer readBuf; + private final TcpWriteBuffer writeBuf; + private final IoSessionHandler handler; + private SelectionKey key; + private Object attachment; + private long readBytes; + private long writtenBytes; + private long lastReadTime; + private long lastWriteTime; + private boolean idleForBoth; + private boolean idleForRead; + private boolean idleForWrite; /** * Creates a new instance. */ - TcpSession(SocketChannel ch, IoSessionHandler defaultHandler) { + TcpSession( SocketChannel ch, IoSessionHandler defaultHandler ) + { this.ch = ch; - this.config = new TcpSessionConfig(ch); - this.readBuf = - new TcpReadBuffer(this, (ByteBuffer) ByteBufferPool.open().limit(0)); - this.writeBuf = new TcpWriteBuffer(this, ByteBufferPool.open()); + this.config = new TcpSessionConfig( ch ); + this.readBuf = new TcpReadBuffer( + this, + ( ByteBuffer ) ByteBufferPool + .open() + .limit( + 0 ) ); + this.writeBuf = new TcpWriteBuffer( this, ByteBufferPool.open() ); this.handler = defaultHandler; } - SocketChannel getChannel() { + SocketChannel getChannel() + { return ch; } - - IoSessionHandler getHandler() { - return handler; + + IoSessionHandler getHandler() + { + return handler; } - SelectionKey getSelectionKey() { + SelectionKey getSelectionKey() + { return key; } - void setSelectionKey(SelectionKey key) { + void setSelectionKey( SelectionKey key ) + { this.key = key; } - void dispose() { - ByteBufferPool.close(readBuf.buf()); - ByteBufferPool.close(writeBuf.buf()); + void dispose() + { + ByteBufferPool.close( readBuf.buf() ); + ByteBufferPool.close( writeBuf.buf() ); } - public void close() { - TcpIoProcessor.getInstance().removeSession(this); + public void close() + { + TcpIoProcessor.getInstance().removeSession( this ); } - public Object getAttachment() { + public Object getAttachment() + { return attachment; } - public void setAttachment(Object attachment) { + public void setAttachment( Object attachment ) + { this.attachment = attachment; } - public ReadBuffer getReadBuffer() { + public ReadBuffer getReadBuffer() + { return readBuf; } - public WriteBuffer getWriteBuffer() { + public WriteBuffer getWriteBuffer() + { return writeBuf; } - void flush() { - TcpIoProcessor.getInstance().flushSession(this); + void flush() + { + TcpIoProcessor.getInstance().flushSession( this ); } - - public boolean isConnected() { + + public boolean isConnected() + { return ch.isConnected(); } - public boolean isClosed() { + public boolean isClosed() + { return !isConnected(); } - public SessionConfig getConfig() { + public SessionConfig getConfig() + { return config; } - public SocketAddress getRemoteAddress() { + public SocketAddress getRemoteAddress() + { return ch.socket().getRemoteSocketAddress(); } - public SocketAddress getLocalAddress() { + public SocketAddress getLocalAddress() + { return ch.socket().getLocalSocketAddress(); } - public long getReadBytes() { + public long getReadBytes() + { return readBytes; } - public long getWrittenBytes() { + public long getWrittenBytes() + { return writtenBytes; } - void increaseReadBytes(int increment) { + void increaseReadBytes( int increment ) + { readBytes += increment; lastReadTime = System.currentTimeMillis(); } - void increaseWrittenBytes(int increment) { + void increaseWrittenBytes( int increment ) + { writtenBytes += increment; lastWriteTime = System.currentTimeMillis(); } - public long getLastIoTime() { - return Math.max(lastReadTime, lastWriteTime); + public long getLastIoTime() + { + return Math.max( lastReadTime, lastWriteTime ); } - public long getLastReadTime() { + public long getLastReadTime() + { return lastReadTime; } - public long getLastWriteTime() { + public long getLastWriteTime() + { return lastWriteTime; } - public boolean isIdle(IdleStatus status) { - if (status == IdleStatus.BOTH_IDLE) + public boolean isIdle( IdleStatus status ) + { + if( status == IdleStatus.BOTH_IDLE ) return idleForBoth; - if (status == IdleStatus.READER_IDLE) + if( status == IdleStatus.READER_IDLE ) return idleForRead; - if (status == IdleStatus.WRITER_IDLE) + if( status == IdleStatus.WRITER_IDLE ) return idleForWrite; - throw new IllegalArgumentException("Unknown idle status: " + status); + throw new IllegalArgumentException( "Unknown idle status: " + status ); } - void setIdle(IdleStatus status) { - if (status == IdleStatus.BOTH_IDLE) + void setIdle( IdleStatus status ) + { + if( status == IdleStatus.BOTH_IDLE ) idleForBoth = true; - else if (status == IdleStatus.READER_IDLE) + else if( status == IdleStatus.READER_IDLE ) idleForRead = true; - else if (status == IdleStatus.WRITER_IDLE) + else if( status == IdleStatus.WRITER_IDLE ) idleForWrite = true; else - throw new IllegalArgumentException("Unknown idle status: " + - status); + throw new IllegalArgumentException( "Unknown idle status: " + + status ); } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpSessionConfig.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.net.SocketException; @@ -25,66 +24,78 @@ import org.apache.mina.util.BasicSessionConfig; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -public class TcpSessionConfig extends BasicSessionConfig { +public class TcpSessionConfig extends BasicSessionConfig +{ private final SocketChannel ch; - TcpSessionConfig(SocketChannel ch) { + TcpSessionConfig( SocketChannel ch ) + { this.ch = ch; } - public boolean getKeepAlive() throws SocketException { + public boolean getKeepAlive() throws SocketException + { return ch.socket().getKeepAlive(); } - public void setKeepAlive(boolean on) throws SocketException { - ch.socket().setKeepAlive(on); + public void setKeepAlive( boolean on ) throws SocketException + { + ch.socket().setKeepAlive( on ); } - public boolean getOOBInline() throws SocketException { + public boolean getOOBInline() throws SocketException + { return ch.socket().getOOBInline(); } - public void setOOBInline(boolean on) throws SocketException { - ch.socket().setOOBInline(on); + public void setOOBInline( boolean on ) throws SocketException + { + ch.socket().setOOBInline( on ); } - public boolean getReuseAddress() throws SocketException { + public boolean getReuseAddress() throws SocketException + { return ch.socket().getReuseAddress(); } - public void setReuseAddress(boolean on) throws SocketException { - ch.socket().setReuseAddress(on); + public void setReuseAddress( boolean on ) throws SocketException + { + ch.socket().setReuseAddress( on ); } - public int getSoLinger() throws SocketException { + public int getSoLinger() throws SocketException + { return ch.socket().getSoLinger(); } - public void setSoLinger(boolean on, int linger) - throws SocketException { - ch.socket().setSoLinger(on, linger); + public void setSoLinger( boolean on, int linger ) throws SocketException + { + ch.socket().setSoLinger( on, linger ); } - public boolean getTcpNoDelay() throws SocketException { + public boolean getTcpNoDelay() throws SocketException + { return ch.socket().getTcpNoDelay(); } - public void setTcpNoDelay(boolean on) throws SocketException { - ch.socket().setTcpNoDelay(on); + public void setTcpNoDelay( boolean on ) throws SocketException + { + ch.socket().setTcpNoDelay( on ); } - public int getTrafficClass() throws SocketException { + public int getTrafficClass() throws SocketException + { return ch.socket().getTrafficClass(); } - public void setTrafficClass(int tc) throws SocketException { - ch.socket().setTrafficClass(tc); + public void setTrafficClass( int tc ) throws SocketException + { + ch.socket().setTrafficClass( tc ); } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/io/socket/TcpWriteBuffer.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.io.socket; import java.nio.ByteBuffer; @@ -26,167 +25,205 @@ import org.apache.mina.io.WriteBuffer; import org.apache.mina.util.Queue; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -class TcpWriteBuffer implements WriteBuffer { +class TcpWriteBuffer implements WriteBuffer +{ private final TcpSession session; + private final ByteBuffer buf; - private final Queue markers = new Queue(16); - TcpWriteBuffer(TcpSession session, ByteBuffer buf) { + private final Queue markers = new Queue( 16 ); + + TcpWriteBuffer( TcpSession session, ByteBuffer buf ) + { this.session = session; this.buf = buf; } - ByteBuffer buf() { + ByteBuffer buf() + { return buf; } - public WriteBuffer put(byte b) { - buf.put(b); + public WriteBuffer put( byte b ) + { + buf.put( b ); return this; } - public WriteBuffer put(byte[] src) { - buf.put(src); + public WriteBuffer put( byte[] src ) + { + buf.put( src ); return this; } - public WriteBuffer put(byte[] src, int offset, int length) { - buf.put(src, offset, length); + public WriteBuffer put( byte[] src, int offset, int length ) + { + buf.put( src, offset, length ); return this; } - public WriteBuffer put(ReadBuffer buf) { - if (!(buf instanceof TcpReadBuffer)) { - throw new IllegalArgumentException("Incompatible buffer type: " + - buf.getClass()); + public WriteBuffer put( ReadBuffer buf ) + { + if( ! ( buf instanceof TcpReadBuffer ) ) + { + throw new IllegalArgumentException( "Incompatible buffer type: " + + buf.getClass() ); } - this.buf.put(((TcpReadBuffer) buf).buf()); + this.buf.put( ( ( TcpReadBuffer ) buf ).buf() ); return this; } - public WriteBuffer put(ByteBuffer buf) { - this.buf.put(buf); + public WriteBuffer put( ByteBuffer buf ) + { + this.buf.put( buf ); return this; } - public WriteBuffer putChar(char c) { - buf.putChar(c); + public WriteBuffer putChar( char c ) + { + buf.putChar( c ); return this; } - public WriteBuffer putDouble(double d) { - buf.putDouble(d); + public WriteBuffer putDouble( double d ) + { + buf.putDouble( d ); return this; } - public WriteBuffer putFloat(float f) { - buf.putFloat(f); + public WriteBuffer putFloat( float f ) + { + buf.putFloat( f ); return this; } - public WriteBuffer putInt(int i) { - buf.putInt(i); + public WriteBuffer putInt( int i ) + { + buf.putInt( i ); return this; } - public WriteBuffer putLong(long l) { - buf.putLong(l); + public WriteBuffer putLong( long l ) + { + buf.putLong( l ); return this; } - public WriteBuffer putShort(short s) { - buf.putShort(s); + public WriteBuffer putShort( short s ) + { + buf.putShort( s ); return this; } - public ByteOrder order() { + public ByteOrder order() + { return buf.order(); } - public WriteBuffer order(ByteOrder order) { - buf.order(order); + public WriteBuffer order( ByteOrder order ) + { + buf.order( order ); return this; } - public ByteBuffer asByteBuffer() { + public ByteBuffer asByteBuffer() + { return buf.duplicate(); } - public WriteBuffer flush() { + public WriteBuffer flush() + { session.flush(); return this; } - - public boolean hasRemaining() { + + public boolean hasRemaining() + { return buf.hasRemaining(); } - public WriteBuffer clear() { + public WriteBuffer clear() + { buf.clear(); return this; } - public int capacity() { + public int capacity() + { return buf.capacity(); } - public int remaining() { + public int remaining() + { return buf.remaining(); } - public WriteBuffer mark() { + public WriteBuffer mark() + { buf.mark(); return this; } - public WriteBuffer reset() { + public WriteBuffer reset() + { buf.reset(); return this; } - - Queue getMarkers() { - return markers; - } - - public WriteBuffer putMarker(Object marker) { - int bytesLeft; - if (markers.isEmpty()) { - bytesLeft = buf.position(); - } else { - bytesLeft = buf.position() - ((Marker) markers.last()).getBytesLeft(); - } - - markers.push(new Marker(marker, bytesLeft)); - return this; - } - - static class Marker { - private final Object value; - private int bytesLeft; - - private Marker(Object value, int bytesLeft) { - this.value = value; - this.bytesLeft = bytesLeft; - } - - public Object getValue() { - return value; - } - - public int getBytesLeft() { - return bytesLeft; - } - - public void setBytesLeft(int bytesLeft) { - this.bytesLeft = bytesLeft; - } + + Queue getMarkers() + { + return markers; + } + + public WriteBuffer putMarker( Object marker ) + { + int bytesLeft; + if( markers.isEmpty() ) + { + bytesLeft = buf.position(); + } + else + { + bytesLeft = buf.position() + - ( ( Marker ) markers.last() ).getBytesLeft(); + } + + markers.push( new Marker( marker, bytesLeft ) ); + return this; + } + + static class Marker + { + private final Object value; + + private int bytesLeft; + + private Marker( Object value, int bytesLeft ) + { + this.value = value; + this.bytesLeft = bytesLeft; + } + + public Object getValue() + { + return value; + } + + public int getBytesLeft() + { + return bytesLeft; + } + + public void setBytesLeft( int bytesLeft ) + { + this.bytesLeft = bytesLeft; + } } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/IoAdapter.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,12 +16,8 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.protocol; - import java.net.SocketAddress; import org.apache.commons.logging.Log; @@ -31,30 +29,26 @@ import org.apache.mina.io.WriteBuffer; import org.apache.mina.util.Queue; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date: 2004-12-08 10:40:31 -0500 (Wed, 08 Dec 2004) - * $, + * @version $Rev$, $Date$, */ public class IoAdapter { private static final Log log = LogFactory.getLog( IoAdapter.class ); - public static IoSessionHandler adapt( ProtocolProvider protocolProvider ) { return new SessionHandlerAdapter( protocolProvider ); } - private static class SessionHandlerAdapter implements IoSessionHandler { private final ProtocolCodec codec; - private final ProtocolSessionHandler handler; + private final ProtocolSessionHandler handler; public SessionHandlerAdapter( ProtocolProvider protocolProvider ) { @@ -62,7 +56,6 @@ this.handler = protocolProvider.getHandler(); } - public void sessionOpened( IoSession session ) { ProtocolSession psession = new ProtocolSessionImpl( session, this ); @@ -70,30 +63,27 @@ fireSessionOpened( psession ); } - public void sessionClosed( IoSession session ) { fireSessionClosed( ( ProtocolSession ) session.getAttachment() ); } - public void sessionIdle( IoSession session, IdleStatus status ) { - fireSessionIdle( ( ProtocolSession ) session.getAttachment(), status ); + fireSessionIdle( ( ProtocolSession ) session.getAttachment(), + status ); } - public void exceptionCaught( IoSession session, Throwable cause ) { fireExceptionCaught( ( ProtocolSession ) session.getAttachment(), - cause ); + cause ); } - public void dataRead( IoSession session, int readBytes ) { - ProtocolSession psession = - ( ProtocolSession ) session.getAttachment(); + ProtocolSession psession = ( ProtocolSession ) session + .getAttachment(); ReadBuffer in = session.getReadBuffer(); int sizeBefore; int sizeAfter; @@ -105,19 +95,19 @@ { result = null; - synchronized ( in ) + synchronized( in ) { sizeBefore = in.remaining(); result = codec.decode( psession, in ); sizeAfter = in.remaining(); } - if ( sizeBefore != sizeAfter ) + if( sizeBefore != sizeAfter ) { in.signal(); } - if ( result != null ) + if( result != null ) { fireMessageReceived( psession, result ); } @@ -126,34 +116,32 @@ break; } } - while ( sizeAfter > 0 ); + while( sizeAfter > 0 ); } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( psession, t ); } } - public void dataWritten( IoSession session, int writtenBytes ) { write( session ); } - public void markerReleased( IoSession session, Object marker ) { - fireMessageSent( ( ProtocolSession ) session.getAttachment(), marker ); + fireMessageSent( ( ProtocolSession ) session.getAttachment(), + marker ); } - private void write( IoSession session ) { - ProtocolSessionImpl psession = - ( ProtocolSessionImpl ) session.getAttachment(); + ProtocolSessionImpl psession = ( ProtocolSessionImpl ) session + .getAttachment(); Queue writeQueue = psession.writeQueue; - if ( writeQueue.isEmpty() ) + if( writeQueue.isEmpty() ) { return; } @@ -162,11 +150,11 @@ try { - while ( !writeQueue.isEmpty() ) + while( !writeQueue.isEmpty() ) { - synchronized ( out ) + synchronized( out ) { - if ( codec.encode( psession, writeQueue.first(), out ) ) + if( codec.encode( psession, writeQueue.first(), out ) ) { out.putMarker( writeQueue.pop() ); out.flush(); @@ -179,88 +167,82 @@ } } } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( psession, t ); } } - private void fireSessionOpened( ProtocolSession session ) { try { handler.sessionOpened( session ); } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( session, t ); } } - private void fireSessionClosed( ProtocolSession session ) { try { handler.sessionClosed( session ); } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( session, t ); } } - private void fireSessionIdle( ProtocolSession session, - IdleStatus idleStatus ) + IdleStatus idleStatus ) { try { handler.sessionIdle( session, idleStatus ); } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( session, t ); } } - private void fireMessageReceived( ProtocolSession session, - Object message ) + Object message ) { try { handler.messageReceived( session, message ); } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( session, t ); } } - private void fireMessageSent( ProtocolSession session, Object message ) { try { handler.messageSent( session, message ); } - catch ( Throwable t ) + catch( Throwable t ) { fireExceptionCaught( session, t ); } } - private void fireExceptionCaught( ProtocolSession session, - Throwable cause ) + Throwable cause ) { try { handler.exceptionCaught( session, cause ); } - catch ( Throwable t ) + catch( Throwable t ) { log.error( "Exception from excaptionCaught.", t ); } @@ -270,40 +252,38 @@ private static class ProtocolSessionImpl implements ProtocolSession { private final IoSession session; + private final SessionHandlerAdapter adapter; + private final Queue writeQueue = new Queue( 16 ); - private Object attachment; + private Object attachment; private ProtocolSessionImpl( IoSession session, - SessionHandlerAdapter adapter ) + SessionHandlerAdapter adapter ) { this.session = session; this.adapter = adapter; } - public void close() { session.close(); } - public Object getAttachment() { return attachment; } - public void setAttachment( Object attachment ) { this.attachment = attachment; } - public boolean write( Object message ) { - synchronized ( session.getWriteBuffer() ) + synchronized( session.getWriteBuffer() ) { writeQueue.push( message ); } @@ -312,58 +292,49 @@ return true; } - public boolean isConnected() { return session.isConnected(); } - public boolean isClosed() { return session.isClosed(); } - public SessionConfig getConfig() { return session.getConfig(); } - public SocketAddress getRemoteAddress() { return session.getRemoteAddress(); } - public SocketAddress getLocalAddress() { return session.getLocalAddress(); } - public long getLastIoTime() { return session.getLastIoTime(); } - public long getLastReadTime() { return session.getLastReadTime(); } - public long getLastWriteTime() { return session.getLastWriteTime(); } - public boolean isIdle( IdleStatus status ) { return session.isIdle( status ); } } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolCodec.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,25 +16,22 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.protocol; import org.apache.mina.io.ReadBuffer; import org.apache.mina.io.WriteBuffer; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface ProtocolCodec { - boolean encode(ProtocolSession session, Object message, WriteBuffer out) - throws ProtocolViolationException; +public interface ProtocolCodec +{ + boolean encode( ProtocolSession session, Object message, WriteBuffer out ) + throws ProtocolViolationException; - Object decode(ProtocolSession session, ReadBuffer in) - throws ProtocolViolationException; -} + Object decode( ProtocolSession session, ReadBuffer in ) + throws ProtocolViolationException; +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolProvider.java Thu Dec 16 22:38:15 2004 @@ -1,5 +1,20 @@ /* - * @(#) $Id$ + * @(#) $Id$ + * + * Copyright 2004 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.mina.protocol; @@ -7,9 +22,11 @@ * TODO Document me. * * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$, + * @version $Rev$, $Date$, */ -public interface ProtocolProvider { - ProtocolCodec newCodec(); - ProtocolSessionHandler getHandler(); -} +public interface ProtocolProvider +{ + ProtocolCodec newCodec(); + + ProtocolSessionHandler getHandler(); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSession.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.protocol; import java.net.SocketAddress; @@ -24,22 +23,22 @@ import org.apache.mina.core.IdleStatus; import org.apache.mina.core.SessionConfig; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface ProtocolSession { +public interface ProtocolSession +{ void close(); Object getAttachment(); - void setAttachment(Object attachment); + void setAttachment( Object attachment ); + + boolean write( Object message ); - boolean write(Object message); - boolean isConnected(); boolean isClosed(); @@ -56,5 +55,5 @@ long getLastWriteTime(); - boolean isIdle(IdleStatus status); -} + boolean isIdle( IdleStatus status ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolSessionHandler.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,30 +16,27 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.protocol; import org.apache.mina.core.IdleStatus; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface ProtocolSessionHandler { - void sessionOpened(ProtocolSession session); +public interface ProtocolSessionHandler +{ + void sessionOpened( ProtocolSession session ); - void sessionClosed(ProtocolSession session); + void sessionClosed( ProtocolSession session ); - void sessionIdle(ProtocolSession session, IdleStatus status); + void sessionIdle( ProtocolSession session, IdleStatus status ); - void exceptionCaught(ProtocolSession session, Throwable cause); + void exceptionCaught( ProtocolSession session, Throwable cause ); - void messageReceived(ProtocolSession session, Object message); + void messageReceived( ProtocolSession session, Object message ); - void messageSent(ProtocolSession session, Object message); -} + void messageSent( ProtocolSession session, Object message ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/protocol/ProtocolViolationException.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,60 +25,69 @@ import org.apache.commons.lang.Validate; import org.apache.mina.util.ByteBuffers; - /** - * An exception that is thrown when [EMAIL PROTECTED] ProtocolCodec} cannot understand or + * An exception that is thrown when [EMAIL PROTECTED] ProtocolCodec}cannot understand or * validate incoming data. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class ProtocolViolationException extends IOException { +public class ProtocolViolationException extends IOException +{ private ByteBuffer buffer; /** * Constructs a new instance. */ - public ProtocolViolationException() { + public ProtocolViolationException() + { } /** * Constructs a new instance with the specified message. */ - public ProtocolViolationException(String message) { - super(message); + public ProtocolViolationException( String message ) + { + super( message ); } /** * Constructs a new instance with the specified cause. */ - public ProtocolViolationException(Throwable cause) { - initCause(cause); + public ProtocolViolationException( Throwable cause ) + { + initCause( cause ); } /** * Constructs a new instance with the specified message and the specified * cause. */ - public ProtocolViolationException(String message, Throwable cause) { - super(message); - initCause(cause); + public ProtocolViolationException( String message, Throwable cause ) + { + super( message ); + initCause( cause ); } /** * Returns the message and the hexdump of the unknown part. */ - public String getMessage() { + public String getMessage() + { String message = super.getMessage(); - if (message == null) { + if( message == null ) + { message = ""; } - if (buffer != null) { - return message + ((message.length() > 0) ? " " : "") + - "(Hexdump: " + ByteBuffers.getHexdump(buffer) + ')'; - } else { + if( buffer != null ) + { + return message + ( ( message.length() > 0 ) ? " " : "" ) + + "(Hexdump: " + ByteBuffers.getHexdump( buffer ) + ')'; + } + else + { return message; } } @@ -84,15 +95,17 @@ /** * Returns unknown message part. */ - public ByteBuffer getBuffer() { + public ByteBuffer getBuffer() + { return buffer; } /** * Sets unknown message part. */ - public void setBuffer(ByteBuffer buffer) { - Validate.notNull(buffer); + public void setBuffer( ByteBuffer buffer ) + { + Validate.notNull( buffer ); this.buffer = buffer; } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/Service.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,22 +16,19 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.registry; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public interface Service { +public interface Service +{ String getName(); TransportType getTransportType(); int getPort(); -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/ServiceRegistry.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,9 +16,6 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.registry; import java.io.IOException; @@ -24,46 +23,47 @@ import org.apache.mina.io.IoSessionHandler; - /** - * Interface for the internet service registry. The registry is used by - * Netty to associate services with ports and transport protocols. - * + * Interface for the internet service registry. The registry is used by Netty + * to associate services with ports and transport protocols. + * * @author [EMAIL PROTECTED] * @author [EMAIL PROTECTED] * @version $Rev$, $Date$ */ -public interface ServiceRegistry { - void bind(Service service, - IoSessionHandler sessionHandler) - throws IOException; +public interface ServiceRegistry +{ + void bind( Service service, IoSessionHandler sessionHandler ) + throws IOException; - void unbind(Service service); + void unbind( Service service ); - Service getByName(String name, TransportType transportType); + Service getByName( String name, TransportType transportType ); - Service getByPort(int port, TransportType transportType); + Service getByPort( int port, TransportType transportType ); Iterator getAll(); - Iterator getByTransportType(TransportType transportType); + Iterator getByTransportType( TransportType transportType ); /** * Gets an iteration over all the entries for a service by the name of the * service. - * - * @param name the authoritative name of the service + * + * @param name + * the authoritative name of the service * @return an Iterator over InetServiceEntry objects */ - Iterator getByName(String name); + Iterator getByName( String name ); /** * Gets an iteration over all the entries for a service by port number. * This method returns an Iterator over the set of InetServiceEntry objects * since more than one transport protocol can be used on the same port. - * - * @param port the port one which the service resides + * + * @param port + * the port one which the service resides * @return an Iterator over InetServiceEntry objects */ - Iterator getByPort(int port); -} + Iterator getByPort( int port ); +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/SimpleService.java Thu Dec 16 22:38:15 2004 @@ -1,5 +1,20 @@ /* - * @(#) $Id$ + * @(#) $Id$ + * + * Copyright 2004 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.mina.registry; @@ -9,34 +24,41 @@ * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class SimpleService implements Service { - +public class SimpleService implements Service +{ + private final String name; + private final TransportType transportType; + private final int port; - public SimpleService(String name, TransportType transportType, int port) { - if (name == null) - throw new NullPointerException("name"); - if (transportType == null) - throw new NullPointerException("transportType"); - if (port < 0 || port > 65535) - throw new IllegalArgumentException("port: " + port); + public SimpleService( String name, TransportType transportType, int port ) + { + if( name == null ) + throw new NullPointerException( "name" ); + if( transportType == null ) + throw new NullPointerException( "transportType" ); + if( port < 0 || port > 65535 ) + throw new IllegalArgumentException( "port: " + port ); this.name = name; this.transportType = transportType; this.port = port; } - - public String getName() { + + public String getName() + { return name; } - public TransportType getTransportType() { + public TransportType getTransportType() + { return transportType; } - public int getPort() { + public int getPort() + { return port; } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/registry/TransportType.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,33 +16,37 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.registry; - /** * TODO Insert type comment. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class TransportType { - public static final TransportType SOCKET = new TransportType("SOCKET"); - public static final TransportType DATAGRAM = new TransportType("DATAGRAM"); - public static final TransportType MULTICAST = new TransportType("MULTICAST"); - public static final TransportType VM = new TransportType("VM"); +public class TransportType +{ + public static final TransportType SOCKET = new TransportType( "SOCKET" ); + + public static final TransportType DATAGRAM = new TransportType( "DATAGRAM" ); + + public static final TransportType MULTICAST = new TransportType( + "MULTICAST" ); + + public static final TransportType VM = new TransportType( "VM" ); + private final String strVal; /** * Creates a new instance. */ - private TransportType(String strVal) { + private TransportType( String strVal ) + { this.strVal = strVal; } - public String toString() { + public String toString() + { return strVal; } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/BasicSessionConfig.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,59 +16,62 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.util; import org.apache.mina.core.IdleStatus; import org.apache.mina.core.SessionConfig; - /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ -public abstract class BasicSessionConfig implements SessionConfig { +public abstract class BasicSessionConfig implements SessionConfig +{ private int idleTimeForRead; + private int idleTimeForWrite; + private int idleTimeForBoth; - protected BasicSessionConfig() { + protected BasicSessionConfig() + { } - public int getIdleTime(IdleStatus status) { - if (status == IdleStatus.BOTH_IDLE) + public int getIdleTime( IdleStatus status ) + { + if( status == IdleStatus.BOTH_IDLE ) return idleTimeForBoth; - if (status == IdleStatus.READER_IDLE) + if( status == IdleStatus.READER_IDLE ) return idleTimeForRead; - if (status == IdleStatus.WRITER_IDLE) + if( status == IdleStatus.WRITER_IDLE ) return idleTimeForWrite; - throw new IllegalArgumentException("Unknown idle status: " + status); + throw new IllegalArgumentException( "Unknown idle status: " + status ); } - public long getIdleTimeInMillis(IdleStatus status) { - return getIdleTime(status) * 1000L; + public long getIdleTimeInMillis( IdleStatus status ) + { + return getIdleTime( status ) * 1000L; } - public void setIdleTime(IdleStatus status, int idleTime) { - if (idleTime < 0) - throw new IllegalArgumentException("Illegal idle time: " + - idleTime); + public void setIdleTime( IdleStatus status, int idleTime ) + { + if( idleTime < 0 ) + throw new IllegalArgumentException( "Illegal idle time: " + + idleTime ); - if (status == IdleStatus.BOTH_IDLE) + if( status == IdleStatus.BOTH_IDLE ) idleTimeForBoth = idleTime; - else if (status == IdleStatus.READER_IDLE) + else if( status == IdleStatus.READER_IDLE ) idleTimeForRead = idleTime; - else if (status == IdleStatus.WRITER_IDLE) + else if( status == IdleStatus.WRITER_IDLE ) idleTimeForWrite = idleTime; else - throw new IllegalArgumentException("Unknown idle status: " + - status); + throw new IllegalArgumentException( "Unknown idle status: " + + status ); } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBufferPool.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,30 +20,36 @@ import java.nio.ByteBuffer; - /** * TODO Insert type comment. - * + * * @version $Rev$, $Date$ * @author Trustin Lee (http://gleamynode.net/dev/) */ -public class ByteBufferPool { +public class ByteBufferPool +{ public static final int CAPACITY = 8192; - private static Queue buffers = new Queue(16); - public static synchronized ByteBuffer open() { - ByteBuffer buf = (ByteBuffer) buffers.pop(); + private static Queue buffers = new Queue( 16 ); - if (buf == null) { - buf = ByteBuffer.allocateDirect(CAPACITY); - } else { + public static synchronized ByteBuffer open() + { + ByteBuffer buf = ( ByteBuffer ) buffers.pop(); + + if( buf == null ) + { + buf = ByteBuffer.allocateDirect( CAPACITY ); + } + else + { buf.clear(); } return buf; } - public static synchronized void close(ByteBuffer buf) { - buffers.push(buf); + public static synchronized void close( ByteBuffer buf ) + { + buffers.push( buf ); } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/ByteBuffers.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,67 +16,70 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.util; import java.nio.ByteBuffer; - /** * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$ */ -public class ByteBuffers { +public class ByteBuffers +{ private static final byte[] highDigits; + private static final byte[] lowDigits; // initialize lookup tables - static { - final byte[] digits = - { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static + { + final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', + '9', 'A', 'B', 'C', 'D', 'E', 'F' }; int i; - byte[] high = new byte[256]; - byte[] low = new byte[256]; + byte[] high = new byte[ 256 ]; + byte[] low = new byte[ 256 ]; - for (i = 0; i < 256; i++) { - high[i] = digits[i >>> 4]; - low[i] = digits[i & 0x0F]; + for( i = 0; i < 256; i++ ) + { + high[ i ] = digits[ i >>> 4 ]; + low[ i ] = digits[ i & 0x0F ]; } highDigits = high; lowDigits = low; } - public static String getHexdump(ByteBuffer in) { + public static String getHexdump( ByteBuffer in ) + { int size = in.remaining(); - if (size == 0) { + if( size == 0 ) + { return "empty"; } - StringBuffer out = new StringBuffer((in.remaining() * 3) - 1); + StringBuffer out = new StringBuffer( ( in.remaining() * 3 ) - 1 ); in.mark(); // fill the first int byteValue = in.get() & 0xFF; - out.append((char) highDigits[byteValue]); - out.append((char) lowDigits[byteValue]); + out.append( ( char ) highDigits[ byteValue ] ); + out.append( ( char ) lowDigits[ byteValue ] ); size--; // and the others, too - for (; size > 0; size--) { - out.append(' '); + for( ; size > 0; size-- ) + { + out.append( ' ' ); byteValue = in.get() & 0xFF; - out.append((char) highDigits[byteValue]); - out.append((char) lowDigits[byteValue]); + out.append( ( char ) highDigits[ byteValue ] ); + out.append( ( char ) lowDigits[ byteValue ] ); } in.reset(); return out.toString(); } -} +} \ No newline at end of file Modified: incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java Url: http://svn.apache.org/viewcvs/incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java?view=diff&rev=122618&p1=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java&r1=122617&p2=incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java&r2=122618 ============================================================================== --- incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java (original) +++ incubator/directory/network/trunk/mina/src/java/org/apache/mina/util/Queue.java Thu Dec 16 22:38:15 2004 @@ -1,4 +1,6 @@ /* + * @(#) $Id$ + * * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,46 +16,47 @@ * limitations under the License. * */ -/* - * @(#) $Id$ - */ package org.apache.mina.util; import java.io.Serializable; import java.util.Arrays; - /** * <p> * A simple queue class. This class is <b>NOT </b> thread-safe. * </p> - * + * * @author Trustin Lee (http://gleamynode.net/dev/) * href="http://projects.gleamynode.net/">http://projects.gleamynode.net/ * </a>) - * * @version $Rev$, $Date$ */ -public class Queue implements Serializable { +public class Queue implements Serializable +{ private Object[] items; + private int first = 0; + private int last = 0; + private int size = 0; /** * Construct a new, empty <code>Queue</code> with the specified initial * capacity. */ - public Queue(int initialCapacity) { - items = new Object[initialCapacity]; + public Queue( int initialCapacity ) + { + items = new Object[ initialCapacity ]; } /** * Clears this queue. */ - public void clear() { - Arrays.fill(items, null); + public void clear() + { + Arrays.fill( items, null ); first = 0; last = 0; size = 0; @@ -61,18 +64,20 @@ /** * Dequeues from this queue. - * + * * @return <code>null</code>, if this queue is empty or the element is * really <code>null</code>. */ - public Object pop() { - if (size == 0) { + public Object pop() + { + if( size == 0 ) + { return null; } - Object ret = items[first]; - items[first] = null; - first = (first + 1) % items.length; + Object ret = items[ first ]; + items[ first ] = null; + first = ( first + 1 ) % items.length; size--; @@ -82,17 +87,22 @@ /** * Enqueue into this queue. */ - public void push(Object obj) { - if (size == items.length) { + public void push( Object obj ) + { + if( size == items.length ) + { // expand queue final int oldLen = items.length; - Object[] tmp = new Object[oldLen * 2]; + Object[] tmp = new Object[ oldLen * 2 ]; - if (first < last) { - System.arraycopy(items, first, tmp, 0, last - first); - } else { - System.arraycopy(items, first, tmp, 0, oldLen - first); - System.arraycopy(items, 0, tmp, oldLen - first, last); + if( first < last ) + { + System.arraycopy( items, first, tmp, 0, last - first ); + } + else + { + System.arraycopy( items, first, tmp, 0, oldLen - first ); + System.arraycopy( items, 0, tmp, oldLen - first, last ); } first = 0; @@ -100,44 +110,50 @@ items = tmp; } - items[last] = obj; - last = (last + 1) % items.length; + items[ last ] = obj; + last = ( last + 1 ) % items.length; size++; } /** * Returns the first element of the queue. - * + * * @return <code>null</code>, if the queue is empty, or the element is * really <code>null</code>. */ - public Object first() { - if (size == 0) { + public Object first() + { + if( size == 0 ) + { return null; } - return items[first]; + return items[ first ]; } - - public Object last() { - if (size == 0) { - return null; - } - - return items[(last + items.length - 1) % items.length]; + + public Object last() + { + if( size == 0 ) + { + return null; + } + + return items[ ( last + items.length - 1 ) % items.length ]; } /** * Returns <code>true</code> if the queue is empty. */ - public boolean isEmpty() { - return (size == 0); + public boolean isEmpty() + { + return ( size == 0 ); } /** * Returns the number of elements in the queue. */ - public int size() { + public int size() + { return size; } -} +} \ No newline at end of file
