Author: trustin Date: Sun Dec 5 00:20:39 2004 New Revision: 109876 URL: http://svn.apache.org/viewcvs?view=rev&rev=109876 Log: * Fixed: 100% CPU consumption when read buffer is full and user don't clear it. * Added: ReadBuffer and WriteBuffer in downstream
TODO: * Implement TcpConnector Added: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/ReadBuffer.java (contents, props changed) incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/WriteBuffer.java (contents, props changed) incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpReadBuffer.java (contents, props changed) incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpWriteBuffer.java (contents, props changed) Modified: incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java Modified: incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java (original) +++ incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/EchoServerSessionHandler.java Sun Dec 5 00:20:39 2004 @@ -1,54 +1,80 @@ /* + * 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.netty.examples.echo.server; -import java.nio.ByteBuffer; - import org.apache.netty.common.IdleStatus; +import org.apache.netty.downstream.ReadBuffer; import org.apache.netty.downstream.Session; import org.apache.netty.downstream.SessionHandler; +import org.apache.netty.downstream.WriteBuffer; + /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$, + * @version $Rev$, $Date$, */ public class EchoServerSessionHandler implements SessionHandler { - - public void sessionOpened(Session session) { - System.out.println(session.getRemoteAddress() + ": OPEN"); - } - - public void sessionClosed(Session session) { - System.out.println(session.getRemoteAddress() + ": CLOSED"); - } - - public void sessionIdle(Session session, IdleStatus status) { - System.out.println(session.getRemoteAddress() + ": IDLE"); - } - - public void exceptionCaught(Session session, Throwable cause) { - System.out.println(session.getRemoteAddress() + ": EXCEPTION"); - cause.printStackTrace(System.out); - } - - public void dataRead(Session session, ByteBuffer buf, int readBytes) { - System.out.println(session.getRemoteAddress() + ": READ (" + readBytes + " B)"); - if (buf.remaining() <= session.getWriteBuffer().remaining()) { - session.getWriteBuffer().put(buf); - session.flush(); - } - } - - public void dataWritten(Session session, ByteBuffer buf, int writtenBytes) { - System.out.println(session.getRemoteAddress() + ": WRITTEN (" + writtenBytes + "B)"); - ByteBuffer readBuf = session.getReadBuffer(); - System.out.println(readBuf.remaining() + " " + buf.remaining()); - if (readBuf.remaining() <= buf.remaining()) { - buf.put(readBuf); - session.flush(); - } - } + public void sessionOpened(Session session) { + System.out.println(session.getRemoteAddress() + ": OPEN"); + } + + public void sessionClosed(Session session) { + System.out.println(session.getRemoteAddress() + ": CLOSED"); + } + + public void sessionIdle(Session session, IdleStatus status) { + System.out.println(session.getRemoteAddress() + ": IDLE"); + } + + public void exceptionCaught(Session session, Throwable cause) { + System.out.println(session.getRemoteAddress() + ": EXCEPTION"); + cause.printStackTrace(System.out); + } + + public void dataRead(Session session, int readBytes) { + System.out.println(session.getRemoteAddress() + ": READ (" + + readBytes + "B)"); + + ReadBuffer rb = session.getReadBuffer(); + WriteBuffer wb = session.getWriteBuffer(); + + if (rb.remaining() <= wb.remaining()) { + wb.put(rb); + wb.flush(); + rb.signal(); + } + } + + public void dataWritten(Session session, int writtenBytes) { + System.out.println(session.getRemoteAddress() + ": WRITTEN (" + + writtenBytes + "B)"); + + ReadBuffer rb = session.getReadBuffer(); + WriteBuffer wb = session.getWriteBuffer(); + + if (rb.hasRemaining() && rb.remaining() <= wb.remaining()) { + wb.put(rb); + wb.flush(); + rb.signal(); + } + } } Modified: incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java (original) +++ incubator/directory/seda/branches/trustin/src/examples/org/apache/netty/examples/echo/server/Main.java Sun Dec 5 00:20:39 2004 @@ -1,4 +1,20 @@ /* + * 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.netty.examples.echo.server; @@ -8,18 +24,20 @@ import org.apache.netty.downstream.Acceptor; import org.apache.netty.downstream.impl.tcp.TcpAcceptor; + /** * TODO Document me. - * + * * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$, + * @version $Rev$, $Date$, */ public class Main { - private static final int PORT = 8080; + private static final int PORT = 8080; - public static void main(String[] args) throws Exception { - Acceptor acceptor = new TcpAcceptor(); - acceptor.bind(new InetSocketAddress(PORT), new EchoServerSessionHandler()); - System.out.println("Listening on port " + PORT); - } + public static void main(String[] args) throws Exception { + Acceptor acceptor = new TcpAcceptor(); + acceptor.bind(new InetSocketAddress(PORT), + new EchoServerSessionHandler()); + System.out.println("Listening on port " + PORT); + } } Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IdleStatus.java Sun Dec 5 00:20:39 2004 @@ -1,45 +1,45 @@ -/* - * 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.netty.common; - - -/** - * 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"); - private final String strValue; - - /** - * Creates a new instance. - */ - private IdleStatus(String strValue) { - this.strValue = strValue; - } - - public String toString() { - return strValue; - } -} +/* + * 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.netty.common; + + +/** + * 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"); + private final String strValue; + + /** + * Creates a new instance. + */ + private IdleStatus(String strValue) { + this.strValue = strValue; + } + + public String toString() { + return strValue; + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/IntraVmAddress.java Sun Dec 5 00:20:39 2004 @@ -1,72 +1,72 @@ -/* - * 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.netty.common; - -import java.net.SocketAddress; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public class IntraVmAddress extends SocketAddress { - private final int port; - - /** - * Creates a new instance. - */ - public IntraVmAddress(int port) { - if ((port < 0) || (port > 65535)) { - throw new IllegalArgumentException(); - } - - this.port = port; - } - - public int getPort() { - return port; - } - - public int hashCode() { - return port; - } - - public boolean equals(Object o) { - if (o == null) { - return false; - } - - if (this == o) { - return true; - } - - if (o instanceof IntraVmAddress) { - return port == ((IntraVmAddress) o).port; - } - - return false; - } - - public String toString() { - return "vm://localhost:" + port; - } -} +/* + * 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.netty.common; + +import java.net.SocketAddress; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public class IntraVmAddress extends SocketAddress { + private final int port; + + /** + * Creates a new instance. + */ + public IntraVmAddress(int port) { + if ((port < 0) || (port > 65535)) { + throw new IllegalArgumentException(); + } + + this.port = port; + } + + public int getPort() { + return port; + } + + public int hashCode() { + return port; + } + + public boolean equals(Object o) { + if (o == null) { + return false; + } + + if (this == o) { + return true; + } + + if (o instanceof IntraVmAddress) { + return port == ((IntraVmAddress) o).port; + } + + return false; + } + + public String toString() { + return "vm://localhost:" + port; + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/SessionConfig.java Sun Dec 5 00:20:39 2004 @@ -1,35 +1,35 @@ -/* - * 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.netty.common; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface SessionConfig { - int getIdleTime(IdleStatus status); - - long getIdleTimeInMillis(IdleStatus status); - - void setIdleTime(IdleStatus status, int idleTime); -} +/* + * 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.netty.common; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface SessionConfig { + int getIdleTime(IdleStatus status); + + long getIdleTimeInMillis(IdleStatus status); + + void setIdleTime(IdleStatus status, int idleTime); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/ByteBufferPool.java Sun Dec 5 00:20:39 2004 @@ -1,47 +1,47 @@ -/* - * 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.netty.common.util; - -import java.nio.ByteBuffer; - - -/** - * TODO Insert type comment. - * - * @version $Rev$, $Date$ - * @author Trustin Lee (http://gleamynode.net/dev/) - */ -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(); - - if (buf == null) { - buf = ByteBuffer.allocateDirect(CAPACITY); - } else { - buf.clear(); - } - - return buf; - } - - public static synchronized void close(ByteBuffer buf) { - buffers.push(buf); - } -} +/* + * 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.netty.common.util; + +import java.nio.ByteBuffer; + + +/** + * TODO Insert type comment. + * + * @version $Rev$, $Date$ + * @author Trustin Lee (http://gleamynode.net/dev/) + */ +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(); + + if (buf == null) { + buf = ByteBuffer.allocateDirect(CAPACITY); + } else { + buf.clear(); + } + + return buf; + } + + public static synchronized void close(ByteBuffer buf) { + buffers.push(buf); + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/common/util/Queue.java Sun Dec 5 00:20:39 2004 @@ -1,135 +1,135 @@ -/* - * 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.netty.common.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 { - 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]; - } - - /** - * Clears this queue. - */ - public void clear() { - Arrays.fill(items, null); - first = 0; - last = 0; - size = 0; - } - - /** - * 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) { - return null; - } - - Object ret = items[first]; - items[first] = null; - first = (first + 1) % items.length; - - size--; - - return ret; - } - - /** - * Enqueue into this queue. - */ - public void push(Object obj) { - if (size == items.length) { - // expand queue - final int oldLen = items.length; - 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); - } - - first = 0; - last = oldLen; - items = tmp; - } - - 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) { - return null; - } - - return items[first]; - } - - /** - * Returns <code>true</code> if the queue is empty. - */ - public boolean isEmpty() { - return (size == 0); - } - - /** - * Returns the number of elements in the queue. - */ - public int size() { - return size; - } -} +/* + * 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.netty.common.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 { + 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]; + } + + /** + * Clears this queue. + */ + public void clear() { + Arrays.fill(items, null); + first = 0; + last = 0; + size = 0; + } + + /** + * 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) { + return null; + } + + Object ret = items[first]; + items[first] = null; + first = (first + 1) % items.length; + + size--; + + return ret; + } + + /** + * Enqueue into this queue. + */ + public void push(Object obj) { + if (size == items.length) { + // expand queue + final int oldLen = items.length; + 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); + } + + first = 0; + last = oldLen; + items = tmp; + } + + 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) { + return null; + } + + return items[first]; + } + + /** + * Returns <code>true</code> if the queue is empty. + */ + public boolean isEmpty() { + return (size == 0); + } + + /** + * Returns the number of elements in the queue. + */ + public int size() { + return size; + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Acceptor.java Sun Dec 5 00:20:39 2004 @@ -1,38 +1,38 @@ -/* - * 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.netty.downstream; - -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, SessionHandler defaultHandler) - throws IOException; - - void unbind(SocketAddress address); -} +/* + * 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.netty.downstream; + +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, SessionHandler defaultHandler) + throws IOException; + + void unbind(SocketAddress address); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Connector.java Sun Dec 5 00:20:39 2004 @@ -1,31 +1,31 @@ -/* - * 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.netty.downstream; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface Connector { - void connect(Session session, SessionHandler defaultHandler); -} +/* + * 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.netty.downstream; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface Connector { + void connect(Session session, SessionHandler defaultHandler); +} Added: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/ReadBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/ReadBuffer.java?view=auto&rev=109876 ============================================================================== --- (empty file) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/ReadBuffer.java Sun Dec 5 00:20:39 2004 @@ -0,0 +1,72 @@ +/* + * 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.netty.downstream; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + + +/** + * TODO Document me. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$, + */ +public interface ReadBuffer { + boolean hasRemaining(); + + ReadBuffer skip(int length); + + ReadBuffer skipAll(); + + int capacity(); + + int remaining(); + + ReadBuffer mark(); + + ReadBuffer reset(); + + ReadBuffer signal(); + + byte get(); + + ReadBuffer get(byte[] dst); + + ReadBuffer get(byte[] dst, int offset, int length); + + char getChar(); + + double getDouble(); + + float getFloat(); + + int getInt(); + + long getLong(); + + short getShort(); + + ByteOrder order(); + + ReadBuffer order(ByteOrder order); + + ByteBuffer asByteBuffer(); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/Session.java Sun Dec 5 00:20:39 2004 @@ -1,69 +1,66 @@ -/* - * 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.netty.downstream; - -import java.net.SocketAddress; -import java.nio.ByteBuffer; - -import org.apache.netty.common.IdleStatus; -import org.apache.netty.common.SessionConfig; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface Session { - SessionHandler getHandler(); - - void setHandler(SessionHandler handler); - - void close(); - - ByteBuffer getReadBuffer(); - - ByteBuffer getWriteBuffer(); - - void flush(); - - boolean isConnected(); - - boolean isClosed(); - - SessionConfig getConfig(); - - SocketAddress getRemoteAddress(); - - SocketAddress getLocalAddress(); - - long getReadBytes(); - - long getWrittenBytes(); - - long getLastIoTime(); - - long getLastReadTime(); - - long getLastWriteTime(); - - boolean isIdle(IdleStatus status); -} +/* + * 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.netty.downstream; + +import java.net.SocketAddress; + +import org.apache.netty.common.IdleStatus; +import org.apache.netty.common.SessionConfig; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface Session { + SessionHandler getHandler(); + + void setHandler(SessionHandler handler); + + void close(); + + ReadBuffer getReadBuffer(); + + WriteBuffer getWriteBuffer(); + + boolean isConnected(); + + boolean isClosed(); + + SessionConfig getConfig(); + + SocketAddress getRemoteAddress(); + + SocketAddress getLocalAddress(); + + long getReadBytes(); + + long getWrittenBytes(); + + long getLastIoTime(); + + long getLastReadTime(); + + long getLastWriteTime(); + + boolean isIdle(IdleStatus status); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/SessionHandler.java Sun Dec 5 00:20:39 2004 @@ -1,45 +1,43 @@ -/* - * 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.netty.downstream; - -import java.nio.ByteBuffer; - -import org.apache.netty.common.IdleStatus; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface SessionHandler { - void sessionOpened(Session session); - - void sessionClosed(Session session); - - void sessionIdle(Session session, IdleStatus status); - - void exceptionCaught(Session session, Throwable cause); - - void dataRead(Session session, ByteBuffer readBuf, int readBytes); - - void dataWritten(Session session, ByteBuffer writeBuf, int writtenBytes); -} +/* + * 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.netty.downstream; + +import org.apache.netty.common.IdleStatus; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface SessionHandler { + void sessionOpened(Session session); + + void sessionClosed(Session session); + + void sessionIdle(Session session, IdleStatus status); + + void exceptionCaught(Session session, Throwable cause); + + void dataRead(Session session, int readBytes); + + void dataWritten(Session session, int writtenBytes); +} Added: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/WriteBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/WriteBuffer.java?view=auto&rev=109876 ============================================================================== --- (empty file) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/WriteBuffer.java Sun Dec 5 00:20:39 2004 @@ -0,0 +1,74 @@ +/* + * 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.netty.downstream; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + + +/** + * TODO Document me. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$, + */ +public interface WriteBuffer { + boolean hasRemaining(); + + WriteBuffer clear(); + + int capacity(); + + int remaining(); + + WriteBuffer mark(); + + WriteBuffer reset(); + + WriteBuffer put(byte b); + + WriteBuffer put(byte[] src); + + WriteBuffer put(byte[] src, int offset, int length); + + WriteBuffer put(ByteBuffer buf); + + WriteBuffer put(ReadBuffer buf); + + WriteBuffer putChar(char c); + + WriteBuffer putDouble(double d); + + WriteBuffer putFloat(float f); + + WriteBuffer putInt(int i); + + WriteBuffer putLong(long l); + + WriteBuffer putShort(short s); + + ByteOrder order(); + + WriteBuffer order(ByteOrder order); + + ByteBuffer asByteBuffer(); + + WriteBuffer flush(); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpAcceptor.java Sun Dec 5 00:20:39 2004 @@ -1,143 +1,152 @@ -/* - * 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.netty.downstream.impl.tcp; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -import org.apache.commons.lang.Validate; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.netty.downstream.Acceptor; -import org.apache.netty.downstream.SessionHandler; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public class TcpAcceptor implements Acceptor { - private static volatile int nextId = 0; - 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 { - selector = Selector.open(); - } - - public void bind(SocketAddress address, SessionHandler defaultHandler) - throws IOException { - this.bind(address, 50, defaultHandler); - } - - public synchronized void bind(SocketAddress address, int backlog, - SessionHandler 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); - - channels.put(address, ssc); - - if (worker == null) { - worker = new Worker(); - worker.start(); - } - } - - public synchronized void unbind(SocketAddress address) { - Validate.notNull(address); - - ServerSocketChannel ssc = (ServerSocketChannel) channels.get(address); - - if (ssc == null) - throw new IllegalArgumentException("Unknown address: " + address); - - SelectionKey key = ssc.keyFor(selector); - key.cancel(); - channels.remove(address); - - try { - ssc.close(); - } catch (IOException e) { - log.error("Unexpected exception", e); - } - } - - private class Worker extends Thread { - public Worker() { - super("TcpAcceptor-" + id); - } - - public void run() { - for (;;) { - try { - int nKeys = selector.select(); - - if (nKeys == 0) - continue; - - Iterator it = selector.selectedKeys().iterator(); - while (it.hasNext()) { - SelectionKey key = (SelectionKey) it.next(); - it.remove(); - - if (!key.isAcceptable()) - continue; - - ServerSocketChannel ssc = (ServerSocketChannel) key.channel(); - SocketChannel ch = ssc.accept(); - if (ch == null) - continue; - - TcpSession session = new TcpSession(ch, (SessionHandler) key.attachment()); - TcpIoProcessor.getInstance().addSession(session); - } - } catch (IOException e) { - log.error("Unexpected exception.", e); - } - } - } - } -} +/* + * 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.netty.downstream.impl.tcp; + +import java.io.IOException; + +import java.net.InetSocketAddress; +import java.net.SocketAddress; + +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +import org.apache.commons.lang.Validate; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.netty.downstream.Acceptor; +import org.apache.netty.downstream.SessionHandler; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public class TcpAcceptor implements Acceptor { + private static volatile int nextId = 0; + 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 { + selector = Selector.open(); + } + + public void bind(SocketAddress address, SessionHandler defaultHandler) + throws IOException { + this.bind(address, 50, defaultHandler); + } + + public synchronized void bind(SocketAddress address, int backlog, + SessionHandler 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); + + channels.put(address, ssc); + + if (worker == null) { + worker = new Worker(); + worker.start(); + } + } + + public synchronized void unbind(SocketAddress address) { + Validate.notNull(address); + + ServerSocketChannel ssc = (ServerSocketChannel) channels.get(address); + + if (ssc == null) + throw new IllegalArgumentException("Unknown address: " + address); + + SelectionKey key = ssc.keyFor(selector); + key.cancel(); + channels.remove(address); + + try { + ssc.close(); + } catch (IOException e) { + log.error("Unexpected exception", e); + } + } + + private class Worker extends Thread { + public Worker() { + super("TcpAcceptor-" + id); + } + + public void run() { + for (;;) { + try { + int nKeys = selector.select(); + + if (nKeys == 0) + continue; + + Iterator it = selector.selectedKeys().iterator(); + + while (it.hasNext()) { + SelectionKey key = (SelectionKey) it.next(); + it.remove(); + + if (!key.isAcceptable()) + continue; + + ServerSocketChannel ssc = + (ServerSocketChannel) key.channel(); + SocketChannel ch = ssc.accept(); + + if (ch == null) + continue; + + TcpSession session = + new TcpSession(ch, + (SessionHandler) key.attachment()); + TcpIoProcessor.getInstance().addSession(session); + } + } catch (IOException e) { + log.error("Unexpected exception.", e); + } + } + } + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpIoProcessor.java Sun Dec 5 00:20:39 2004 @@ -20,10 +20,12 @@ package org.apache.netty.downstream.impl.tcp; import java.io.IOException; + import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; + import java.util.Iterator; import java.util.Set; @@ -38,7 +40,7 @@ /** * TODO Document me. * TODO Implement markRemoved - * + * * @author Trustin Lee ([EMAIL PROTECTED]) * @version $Rev$, $Date$, */ @@ -92,15 +94,21 @@ } public void removeSession(TcpSession session) { - scheduleRemove(session); + scheduleRemove(session); selector.wakeup(); } public void flushSession(TcpSession session) { - scheduleFlush(session); + scheduleFlush(session); selector.wakeup(); } + public void addReadableSession(TcpSession session) { + SelectionKey key = session.getSelectionKey(); + if ((key.interestOps() & SelectionKey.OP_READ) == 0) + key.interestOps(key.interestOps() | SelectionKey.OP_READ); + } + private void addSessions() { if (newSessions.size() == 0) return; @@ -175,7 +183,7 @@ if (key.isReadable()) { read(session); } - + if (key.isWritable()) { scheduleFlush(session); } @@ -185,28 +193,39 @@ } private void read(TcpSession session) { - ByteBuffer readBuf = session.getReadBuffer(); + TcpReadBuffer lock = (TcpReadBuffer) session.getReadBuffer(); + ByteBuffer readBuf = lock.buf(); SocketChannel ch = session.getChannel(); try { int readBytes = 0; int ret; - synchronized (readBuf) { + synchronized (lock) { readBuf.compact(); + try { - while ((ret = ch.read(readBuf)) > 0) { - readBytes += ret; - } + while ((ret = ch.read(readBuf)) > 0) { + readBytes += ret; + } } finally { - readBuf.flip(); + readBuf.flip(); + readBuf.mark(); } - + session.increaseReadBytes(readBytes); - if (ret >= 0) - fireDataRead(session, readBuf, readBytes); - else + + if (ret >= 0) { + if (readBytes > 0) { + fireDataRead(session, readBytes); + } else { + SelectionKey key = session.getSelectionKey(); + key.interestOps(key.interestOps() & + (~SelectionKey.OP_READ)); + } + } else { scheduleRemove(session); + } } } catch (Throwable e) { fireExceptionCaught(session, e); @@ -291,30 +310,37 @@ } private void flush(TcpSession session) { - ByteBuffer writeBuf = session.getWriteBuffer(); + TcpWriteBuffer lock = (TcpWriteBuffer) session.getWriteBuffer(); + ByteBuffer writeBuf = lock.buf(); SocketChannel ch = session.getChannel(); try { - synchronized (writeBuf) { + synchronized (lock) { writeBuf.flip(); + int writtenBytes; + try { - writtenBytes = ch.write(writeBuf); + writtenBytes = ch.write(writeBuf); } finally { - if (writeBuf.hasRemaining()) { - // Kernel buffer is full - session.getSelectionKey().interestOps(SelectionKey.OP_READ | - SelectionKey.OP_WRITE); - } else { - session.getSelectionKey().interestOps(SelectionKey.OP_READ); - } + SelectionKey key = session.getSelectionKey(); + + if (writeBuf.hasRemaining()) { + // Kernel buffer is full + key.interestOps(key.interestOps() | + SelectionKey.OP_WRITE); + } else { + key.interestOps(key.interestOps() & + (~SelectionKey.OP_WRITE)); + } - writeBuf.compact(); + writeBuf.compact(); + writeBuf.mark(); } - + if (writtenBytes > 0) { - session.increaseWrittenBytes(writtenBytes); - fireDataWritten(session, writeBuf, writtenBytes); + session.increaseWrittenBytes(writtenBytes); + fireDataWritten(session, writtenBytes); } } } catch (IOException e) { @@ -346,17 +372,17 @@ } } - private void fireDataRead(TcpSession session, ByteBuffer readBuf, int readBytes) { + private void fireDataRead(TcpSession session, int readBytes) { try { - session.getHandler().dataRead(session, readBuf, readBytes); + session.getHandler().dataRead(session, readBytes); } catch (Throwable e) { fireExceptionCaught(session, e); } } - private void fireDataWritten(TcpSession session, ByteBuffer writeBuf, int writtenBytes) { + private void fireDataWritten(TcpSession session, int writtenBytes) { try { - session.getHandler().dataWritten(session, writeBuf, writtenBytes); + session.getHandler().dataWritten(session, writtenBytes); } catch (Throwable e) { fireExceptionCaught(session, e); } Added: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpReadBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpReadBuffer.java?view=auto&rev=109876 ============================================================================== --- (empty file) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpReadBuffer.java Sun Dec 5 00:20:39 2004 @@ -0,0 +1,134 @@ +/* + * 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.netty.downstream.impl.tcp; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import org.apache.netty.downstream.ReadBuffer; + + +/** + * TODO Document me. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$, + */ +class TcpReadBuffer implements ReadBuffer { + private final TcpSession parent; + private final ByteBuffer buf; + + TcpReadBuffer(TcpSession parent, ByteBuffer buf) { + this.parent = parent; + this.buf = buf; + } + + ByteBuffer buf() { + return buf; + } + + public byte get() { + return buf.get(); + } + + public ReadBuffer get(byte[] dst) { + buf.get(dst); + return this; + } + + public ReadBuffer get(byte[] dst, int offset, int length) { + buf.get(dst, offset, length); + return this; + } + + public char getChar() { + char ret = buf.getChar(); + return ret; + } + + public double getDouble() { + return buf.getDouble(); + } + + public float getFloat() { + return buf.getFloat(); + } + + public int getInt() { + return buf.getInt(); + } + + public long getLong() { + return buf.getLong(); + } + + public short getShort() { + return buf.getShort(); + } + + public ByteOrder order() { + return buf.order(); + } + + public ReadBuffer order(ByteOrder order) { + buf.order(order); + return this; + } + + public ByteBuffer asByteBuffer() { + return buf.duplicate().asReadOnlyBuffer(); + } + + public boolean hasRemaining() { + return buf.hasRemaining(); + } + + public ReadBuffer skip(int length) { + buf.position(buf.position() + length); + return this; + } + + public ReadBuffer skipAll() { + return skip(remaining()); + } + + public int capacity() { + return buf.capacity(); + } + + public int remaining() { + return buf.remaining(); + } + + public ReadBuffer mark() { + buf.mark(); + return this; + } + + public ReadBuffer reset() { + buf.reset(); + return this; + } + + public ReadBuffer signal() { + TcpIoProcessor.getInstance().addReadableSession(parent); + return this; + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpSession.java Sun Dec 5 00:20:39 2004 @@ -1,167 +1,187 @@ -/* - * @(#) $Id$ - */ -package org.apache.netty.downstream.impl.tcp; - -import java.net.SocketAddress; -import java.nio.ByteBuffer; -import java.nio.channels.SelectionKey; -import java.nio.channels.SocketChannel; - -import org.apache.commons.lang.Validate; -import org.apache.netty.common.IdleStatus; -import org.apache.netty.common.SessionConfig; -import org.apache.netty.common.util.ByteBufferPool; -import org.apache.netty.downstream.Session; -import org.apache.netty.downstream.SessionHandler; - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -class TcpSession implements Session { - - private final SocketChannel ch; - private final TcpSessionConfig config; - private final ByteBuffer readBuf; - private final ByteBuffer writeBuf; - - private SelectionKey key; - private SessionHandler handler; - 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, SessionHandler defaultHandler) { - this.ch = ch; - this.config = new TcpSessionConfig(ch); - this.readBuf = (ByteBuffer) ByteBufferPool.open().limit(0); - this.writeBuf = ByteBufferPool.open(); - this.handler = defaultHandler; - } - - SocketChannel getChannel() { - return ch; - } - - SelectionKey getSelectionKey() { - return key; - } - - void setSelectionKey(SelectionKey key) { - this.key = key; - } - - void dispose() { - ByteBufferPool.close(readBuf); - ByteBufferPool.close(writeBuf); - } - - public SessionHandler getHandler() { - return handler; - } - - public void setHandler(SessionHandler handler) { - Validate.notNull(handler); - this.handler = handler; - } - - public void close() { - TcpIoProcessor.getInstance().removeSession(this); - } - - public ByteBuffer getReadBuffer() { - return readBuf; - } - - public ByteBuffer getWriteBuffer() { - return writeBuf; - } - - public void flush() { - TcpIoProcessor.getInstance().flushSession(this); - } - - public boolean isConnected() { - return ch.isConnected(); - } - - public boolean isClosed() { - return !isConnected(); - } - - public SessionConfig getConfig() { - return config; - } - - public SocketAddress getRemoteAddress() { - return ch.socket().getRemoteSocketAddress(); - } - - public SocketAddress getLocalAddress() { - return ch.socket().getLocalSocketAddress(); - } - - public long getReadBytes() { - return readBytes; - } - - public long getWrittenBytes() { - return writtenBytes; - } - - void increaseReadBytes(int increment) { - readBytes += increment; - lastReadTime = System.currentTimeMillis(); - } - - void increaseWrittenBytes(int increment) { - writtenBytes += increment; - lastWriteTime = System.currentTimeMillis(); - } - - public long getLastIoTime() { - return Math.max(lastReadTime, lastWriteTime); - } - - public long getLastReadTime() { - return lastReadTime; - } - - public long getLastWriteTime() { - return lastWriteTime; - } - - public boolean isIdle(IdleStatus status) { - if (status == IdleStatus.BOTH_IDLE) - return idleForBoth; - - if (status == IdleStatus.READER_IDLE) - return idleForRead; - - if (status == IdleStatus.WRITER_IDLE) - return idleForWrite; - - throw new IllegalArgumentException("Unknown idle status: " + status); - } - - void setIdle(IdleStatus status) { - if (status == IdleStatus.BOTH_IDLE) - idleForBoth = true; - else if (status == IdleStatus.READER_IDLE) - idleForRead = true; - else if (status == IdleStatus.WRITER_IDLE) - idleForWrite = true; - else - throw new IllegalArgumentException("Unknown idle status: " + status); - } -} +/* + * 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.netty.downstream.impl.tcp; + +import java.net.SocketAddress; + +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.SocketChannel; + +import org.apache.commons.lang.Validate; +import org.apache.netty.common.IdleStatus; +import org.apache.netty.common.SessionConfig; +import org.apache.netty.common.util.ByteBufferPool; +import org.apache.netty.downstream.ReadBuffer; +import org.apache.netty.downstream.Session; +import org.apache.netty.downstream.SessionHandler; +import org.apache.netty.downstream.WriteBuffer; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +class TcpSession implements Session { + private final SocketChannel ch; + private final TcpSessionConfig config; + private final TcpReadBuffer readBuf; + private final TcpWriteBuffer writeBuf; + private SelectionKey key; + private SessionHandler handler; + 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, SessionHandler 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.handler = defaultHandler; + } + + SocketChannel getChannel() { + return ch; + } + + SelectionKey getSelectionKey() { + return key; + } + + void setSelectionKey(SelectionKey key) { + this.key = key; + } + + void dispose() { + ByteBufferPool.close(readBuf.buf()); + ByteBufferPool.close(writeBuf.buf()); + } + + public SessionHandler getHandler() { + return handler; + } + + public void setHandler(SessionHandler handler) { + Validate.notNull(handler); + this.handler = handler; + } + + public void close() { + TcpIoProcessor.getInstance().removeSession(this); + } + + public ReadBuffer getReadBuffer() { + return readBuf; + } + + public WriteBuffer getWriteBuffer() { + return writeBuf; + } + + void flush() { + TcpIoProcessor.getInstance().flushSession(this); + } + + public boolean isConnected() { + return ch.isConnected(); + } + + public boolean isClosed() { + return !isConnected(); + } + + public SessionConfig getConfig() { + return config; + } + + public SocketAddress getRemoteAddress() { + return ch.socket().getRemoteSocketAddress(); + } + + public SocketAddress getLocalAddress() { + return ch.socket().getLocalSocketAddress(); + } + + public long getReadBytes() { + return readBytes; + } + + public long getWrittenBytes() { + return writtenBytes; + } + + void increaseReadBytes(int increment) { + readBytes += increment; + lastReadTime = System.currentTimeMillis(); + } + + void increaseWrittenBytes(int increment) { + writtenBytes += increment; + lastWriteTime = System.currentTimeMillis(); + } + + public long getLastIoTime() { + return Math.max(lastReadTime, lastWriteTime); + } + + public long getLastReadTime() { + return lastReadTime; + } + + public long getLastWriteTime() { + return lastWriteTime; + } + + public boolean isIdle(IdleStatus status) { + if (status == IdleStatus.BOTH_IDLE) + return idleForBoth; + + if (status == IdleStatus.READER_IDLE) + return idleForRead; + + if (status == IdleStatus.WRITER_IDLE) + return idleForWrite; + + throw new IllegalArgumentException("Unknown idle status: " + status); + } + + void setIdle(IdleStatus status) { + if (status == IdleStatus.BOTH_IDLE) + idleForBoth = true; + else if (status == IdleStatus.READER_IDLE) + idleForRead = true; + else if (status == IdleStatus.WRITER_IDLE) + idleForWrite = true; + else + throw new IllegalArgumentException("Unknown idle status: " + + status); + } +} Added: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpWriteBuffer.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpWriteBuffer.java?view=auto&rev=109876 ============================================================================== --- (empty file) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/downstream/impl/tcp/TcpWriteBuffer.java Sun Dec 5 00:20:39 2004 @@ -0,0 +1,152 @@ +/* + * 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.netty.downstream.impl.tcp; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import org.apache.netty.downstream.ReadBuffer; +import org.apache.netty.downstream.WriteBuffer; + + +/** + * TODO Document me. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$, + */ +class TcpWriteBuffer implements WriteBuffer { + private final TcpSession session; + private final ByteBuffer buf; + + TcpWriteBuffer(TcpSession session, ByteBuffer buf) { + this.session = session; + this.buf = buf; + } + + ByteBuffer buf() { + return buf; + } + + public WriteBuffer put(byte b) { + buf.put(b); + return this; + } + + public WriteBuffer put(byte[] src) { + buf.put(src); + return this; + } + + 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()); + } + + this.buf.put(((TcpReadBuffer) buf).buf()); + return this; + } + + public WriteBuffer put(ByteBuffer buf) { + this.buf.put(buf); + return this; + } + + public WriteBuffer putChar(char c) { + buf.putChar(c); + return this; + } + + public WriteBuffer putDouble(double d) { + buf.putDouble(d); + return this; + } + + public WriteBuffer putFloat(float f) { + buf.putFloat(f); + return this; + } + + public WriteBuffer putInt(int i) { + buf.putInt(i); + return this; + } + + public WriteBuffer putLong(long l) { + buf.putLong(l); + return this; + } + + public WriteBuffer putShort(short s) { + buf.putShort(s); + return this; + } + + public ByteOrder order() { + return buf.order(); + } + + public WriteBuffer order(ByteOrder order) { + buf.order(order); + return this; + } + + public ByteBuffer asByteBuffer() { + return buf.duplicate(); + } + + public WriteBuffer flush() { + session.flush(); + return this; + } + + public boolean hasRemaining() { + return buf.hasRemaining(); + } + + public WriteBuffer clear() { + buf.clear(); + return this; + } + + public int capacity() { + return buf.capacity(); + } + + public int remaining() { + return buf.remaining(); + } + + public WriteBuffer mark() { + buf.mark(); + return this; + } + + public WriteBuffer reset() { + buf.reset(); + return this; + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/Service.java Sun Dec 5 00:20:39 2004 @@ -1,37 +1,37 @@ -/* - * 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.netty.registry; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface Service { - String getName(); - - TransportType getTransportType(); - - int getPort(); - - Object getSessionHandler(); -} +/* + * 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.netty.registry; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface Service { + String getName(); + + TransportType getTransportType(); + + int getPort(); + + Object getSessionHandler(); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/ServiceRegistry.java Sun Dec 5 00:20:39 2004 @@ -1,72 +1,72 @@ -/* - * 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.netty.registry; - -import java.io.IOException; - -import java.util.Iterator; - - -/** - * 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, - org.apache.netty.downstream.SessionHandler sessionHandler) - throws IOException; - - void bind(Service service, - org.apache.netty.upstream.SessionHandler sessionHandler) - throws IOException; - - void unbind(Service service); - - Service getByName(String name, TransportType transportType); - - Service getByPort(int port, TransportType transportType); - - Iterator getAll(); - - 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 - * @return an Iterator over InetServiceEntry objects - */ - 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 - * @return an Iterator over InetServiceEntry objects - */ - Iterator getByPort(int port); -} +/* + * 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.netty.registry; + +import java.io.IOException; + +import java.util.Iterator; + + +/** + * 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, + org.apache.netty.downstream.SessionHandler sessionHandler) + throws IOException; + + void bind(Service service, + org.apache.netty.upstream.SessionHandler sessionHandler) + throws IOException; + + void unbind(Service service); + + Service getByName(String name, TransportType transportType); + + Service getByPort(int port, TransportType transportType); + + Iterator getAll(); + + 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 + * @return an Iterator over InetServiceEntry objects + */ + 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 + * @return an Iterator over InetServiceEntry objects + */ + Iterator getByPort(int port); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/registry/TransportType.java Sun Dec 5 00:20:39 2004 @@ -1,45 +1,45 @@ -/* - * 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.netty.registry; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public class TransportType { - public static final TransportType TCP = new TransportType("TCP"); - public static final TransportType UDP = new TransportType("UDP"); - public static final TransportType VM = new TransportType("VM"); - private final String strVal; - - /** - * Creates a new instance. - */ - private TransportType(String strVal) { - this.strVal = strVal; - } - - public String toString() { - return strVal; - } -} +/* + * 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.netty.registry; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public class TransportType { + public static final TransportType TCP = new TransportType("TCP"); + public static final TransportType UDP = new TransportType("UDP"); + public static final TransportType VM = new TransportType("VM"); + private final String strVal; + + /** + * Creates a new instance. + */ + private TransportType(String strVal) { + this.strVal = strVal; + } + + public String toString() { + return strVal; + } +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Acceptor.java Sun Dec 5 00:20:39 2004 @@ -1,38 +1,38 @@ -/* - * 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.netty.upstream; - -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, SessionHandler defaultHandler) - throws IOException; - - void unbind(SocketAddress address); -} +/* + * 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.netty.upstream; + +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, SessionHandler defaultHandler) + throws IOException; + + void unbind(SocketAddress address); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Codec.java Sun Dec 5 00:20:39 2004 @@ -1,35 +1,35 @@ -/* - * 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.netty.upstream; - -import java.nio.ByteBuffer; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface Codec { - boolean encode(Session session, Object message, ByteBuffer out); - - Object decode(Session session, ByteBuffer in); -} +/* + * 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.netty.upstream; + +import java.nio.ByteBuffer; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface Codec { + boolean encode(Session session, Object message, ByteBuffer out); + + Object decode(Session session, ByteBuffer in); +} Modified: incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java Url: http://svn.apache.org/viewcvs/incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java?view=diff&rev=109876&p1=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java&r1=109875&p2=incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java&r2=109876 ============================================================================== --- incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java (original) +++ incubator/directory/seda/branches/trustin/src/java/org/apache/netty/upstream/Connector.java Sun Dec 5 00:20:39 2004 @@ -1,33 +1,33 @@ -/* - * 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.netty.upstream; - -import java.net.SocketAddress; - - -/** - * TODO Insert type comment. - * - * @author Trustin Lee ([EMAIL PROTECTED]) - * @version $Rev$, $Date$ - */ -public interface Connector { - void connect(SocketAddress address, SessionHandler defaultHandler); -} +/* + * 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.netty.upstream; + +import java.net.SocketAddress; + + +/** + * TODO Insert type comment. + * + * @author Trustin Lee ([EMAIL PROTECTED]) + * @version $Rev$, $Date$ + */ +public interface Connector { + void connect(SocketAddress address, SessionHandler defaultHandler); +}
