hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r437358 - in /jakarta/httpcomponents/httpcore/trunk/module-nio/src: examples/ examples/org/ examples/org/apache/ examples/org/apache/http/ examples/org/apache/http/nio/ examples/org/apache/http/nio/examples/ main/java/org/apache/http/nio/ m...
Date Sun, 27 Aug 2006 10:29:10 GMT
Author: olegk
Date: Sun Aug 27 03:29:08 2006
New Revision: 437358

URL: http://svn.apache.org/viewvc?rev=437358&view=rev
Log:
HttpCore NIO: First take at the I/O multiplexing mechanism based on the I/O Reactor pattern

Added:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java
  (with props)
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java
  (with props)
Removed:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/NIOSocketHttpDataReceiver.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/NIOSocketHttpDataTransmitter.java

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,97 @@
+package org.apache.http.nio.examples;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+
+import org.apache.http.nio.EventMask;
+import org.apache.http.nio.IOEventDispatch;
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.IOReactor;
+import org.apache.http.nio.impl.DefaultIOReactor;
+
+public class ElementalEchoServer {
+
+    public static void main(String[] args) throws Exception {
+        if (args.length < 1) {
+            System.err.println("Please specify document root directory");
+            System.exit(1);
+        }
+        Thread t = new IOReactorThread(8080, new DefaultIoEventDispatch());
+        t.setDaemon(false);
+        t.start();
+    }
+    
+    static class IOReactorThread extends Thread {
+
+        private final IOReactor ioReactor;
+        private final IOEventDispatch ioEventDispatch;
+        
+        public IOReactorThread(int port, final IOEventDispatch ioEventDispatch) 
+                throws IOException {
+            this.ioReactor = new DefaultIOReactor(new InetSocketAddress(port));
+            this.ioEventDispatch = ioEventDispatch;
+        }
+        
+        public void run() {
+            try {
+                this.ioReactor.execute(this.ioEventDispatch);
+            } catch (InterruptedIOException ex) {
+            } catch (IOException e) {
+                System.err.println("I/O error: " + e.getMessage());
+            }
+        }
+        
+    }
+    
+    static class DefaultIoEventDispatch implements IOEventDispatch {
+
+        private final ByteBuffer buffer = ByteBuffer.allocate(1024);
+        
+        public void connected(IOSession session) {
+            System.out.println("connected");
+            session.setEventMask(EventMask.READ);
+            session.setSocketTimeout(20000);
+        }
+
+        public void inputReady(final IOSession session) {
+            System.out.println("readable");
+            try {
+                this.buffer.compact();
+                int bytesRead = session.channel().read(this.buffer);
+                if (this.buffer.position() > 0) {
+                    session.setEventMask(EventMask.READ_WRITE);
+                }
+                System.out.println("Bytes read: " + bytesRead);
+            } catch (IOException ex) {
+                System.out.println("I/O error: " + ex.getMessage());
+            }
+        }
+
+        public void outputReady(final IOSession session) {
+            System.out.println("writeable");
+            try {
+                this.buffer.flip();
+                int bytesWritten = session.channel().write(this.buffer);
+                if (!this.buffer.hasRemaining()) {
+                    session.setEventMask(EventMask.READ);
+                }
+                System.out.println("Bytes written: " + bytesWritten);
+            } catch (IOException ex) {
+                System.out.println("I/O error: " + ex.getMessage());
+            }
+        }
+
+        public void timeout(final IOSession session) {
+            System.out.println("timeout");
+            session.close();
+        }
+        
+        public void disconnected(final IOSession session) {
+            System.out.println("disconnected");
+            session.close();
+        }
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/examples/org/apache/http/nio/examples/ElementalEchoServer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,40 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio;
+
+import java.nio.channels.SelectionKey;
+
+public class EventMask {
+
+    public static final int READ = SelectionKey.OP_READ;
+    public static final int WRITE = SelectionKey.OP_WRITE;
+    public static final int READ_WRITE = READ | WRITE;
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/EventMask.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,44 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio;
+
+public interface IOEventDispatch {
+    
+    void connected(IOSession session);
+    
+    void inputReady(IOSession session);
+    
+    void outputReady(IOSession session);
+    
+    void timeout(IOSession session);
+    
+    void disconnected(IOSession session);
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOEventDispatch.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,42 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio;
+
+import java.io.IOException;
+
+public interface IOReactor {
+
+    void execute(IOEventDispatch eventDispatch) 
+        throws IOException;
+
+    void shutdown() 
+        throws IOException;
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOReactor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,56 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio;
+
+import java.nio.channels.ByteChannel;
+
+public interface IOSession {
+
+    ByteChannel channel();
+    
+    int getEventMask();
+    
+    void setEventMask(int ops);
+
+    void close();
+    
+    boolean isClosed();
+
+    int getSocketTimeout();
+    
+    void setSocketTimeout(int timeout);
+    
+    void setAttribute(String name, Object obj);
+    
+    Object getAttribute(String name);
+    
+    Object removeAttribute(String name);
+
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/IOSession.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,190 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.impl;
+
+import java.io.IOException;
+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.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.http.nio.IOSession;
+import org.apache.http.nio.IOEventDispatch;
+import org.apache.http.nio.IOReactor;
+
+public class DefaultIOReactor implements IOReactor {
+
+    public static int TIMEOUT_CHECK_INTERVAL = 1000;
+    
+    private volatile boolean closed = false;
+    private final ServerSocketChannel serverChannel;
+    private final Selector selector;
+    private final Set sessions;
+    private long lastTimeoutCheck;
+    
+    public DefaultIOReactor(final SocketAddress address) throws IOException {
+        super();
+        this.serverChannel = ServerSocketChannel.open();
+        this.serverChannel.configureBlocking(false);
+        this.serverChannel.socket().bind(address);
+        this.selector = Selector.open();
+        this.sessions = new HashSet();
+        this.lastTimeoutCheck = System.currentTimeMillis();
+    }
+    
+    public synchronized void execute(final IOEventDispatch eventDispatch) throws IOException
{
+        this.serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);
+        try {
+            for (;;) {
+                int readyCount = this.selector.select(TIMEOUT_CHECK_INTERVAL);
+                if (this.closed) {
+                    break;
+                }
+                if (readyCount > 0) {
+                    processEvents(this.selector.selectedKeys(), eventDispatch);
+                }
+                
+                long currentTime = System.currentTimeMillis();
+                if( (currentTime - this.lastTimeoutCheck) >= TIMEOUT_CHECK_INTERVAL) {
+                    this.lastTimeoutCheck = currentTime;
+                    Set keys = this.selector.keys();
+                    if (keys != null) {
+                        processSessionTimeouts(keys, eventDispatch);
+                    }
+                    processClosedSessions(eventDispatch);
+                }
+            }
+        } finally {
+            closeSessions(eventDispatch);
+        }
+    }
+    
+    private void processEvents(final Set selectedKeys, final IOEventDispatch eventDispatch)
+            throws IOException {
+        for (Iterator it = selectedKeys.iterator(); it.hasNext(); ) {
+            SelectionKey key = (SelectionKey) it.next();
+            it.remove();
+            if (key.isAcceptable()) {
+                
+                SocketChannel socketChannel = this.serverChannel.accept();
+                if (socketChannel != null) {
+                    
+                    // Register new channel with the selector
+                    socketChannel.configureBlocking(false);
+                    SelectionKey newkey = socketChannel.register(this.selector, 0);
+                    
+                    // Set up new session
+                    IOSession session = new DefaultIOSession(newkey);
+                    this.sessions.add(session);
+                    
+                    // Attach session handle to the selection key
+                    SessionHandle handle = new SessionHandle(session); 
+                    newkey.attach(handle);
+                    
+                    // Dispatch the event
+                    eventDispatch.connected(session);
+                }
+            }
+            if (key.isReadable()) {
+                SessionHandle handle = (SessionHandle) key.attachment();
+                IOSession session = handle.getSession();
+                handle.resetLastRead();
+
+                // Dispatch the event
+                eventDispatch.inputReady(session);
+            }
+            if (key.isWritable()) {
+                SessionHandle handle = (SessionHandle) key.attachment();
+                IOSession session = handle.getSession();
+                handle.resetLastWrite();
+                
+                // Dispatch the event
+                eventDispatch.outputReady(session);
+            }
+        }
+    }
+
+    private void processSessionTimeouts(final Set keys, final IOEventDispatch eventDispatch)
{
+        long now = System.currentTimeMillis();
+        for (Iterator it = keys.iterator(); it.hasNext();) {
+            SelectionKey key = (SelectionKey) it.next();
+            SessionHandle handle = (SessionHandle) key.attachment();
+            if (handle != null) {
+                IOSession session = handle.getSession();
+                int timeout = session.getSocketTimeout();
+                if (timeout > 0) {
+                    if (handle.getLastRead() + timeout < now) {
+                        // Dispatch the event
+                        eventDispatch.timeout(session);
+                    }
+                }
+            }
+        }
+    }
+
+    private void processClosedSessions(final IOEventDispatch eventDispatch) {
+        for (Iterator it = this.sessions.iterator(); it.hasNext(); ) {
+            IOSession session = (IOSession) it.next();
+            if (session.isClosed()) {
+                it.remove();
+                // Dispatch the event
+                eventDispatch.disconnected(session);
+            }
+        }
+    }
+    
+    private void closeSessions(final IOEventDispatch eventDispatch) {
+        for (Iterator it = this.sessions.iterator(); it.hasNext(); ) {
+            IOSession session = (IOSession) it.next();
+            if (!session.isClosed()) {
+                session.close();
+                // Dispatch the event
+                eventDispatch.disconnected(session);
+            }
+        }
+        this.sessions.clear();
+    }
+    
+    public void shutdown() throws IOException {
+        if (this.closed) {
+            return;
+        }
+        this.closed = true;
+        // Do not accept new connections
+        this.serverChannel.close();
+        // Stop dispatching I/O events
+        this.selector.close();
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOReactor.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,111 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.impl;
+
+import java.io.IOException;
+import java.nio.channels.ByteChannel;
+import java.nio.channels.SelectionKey;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.http.nio.IOSession;
+
+public class DefaultIOSession implements IOSession {
+    
+    private volatile boolean closed = false;
+    
+    private final SelectionKey key;
+    private final Map attributes;
+    
+    private int socketTimeout;
+    
+    public DefaultIOSession(final SelectionKey key) {
+        super();
+        if (key == null) {
+            throw new IllegalArgumentException("Selection key may not be null");
+        }
+        this.key = key;
+        this.attributes = Collections.synchronizedMap(new HashMap());
+        this.socketTimeout = 0;
+    }
+    
+    public ByteChannel channel() {
+        return (ByteChannel) this.key.channel();
+    }
+    
+    public int getEventMask() {
+        return this.key.interestOps();
+    }
+    
+    public void setEventMask(int ops) {
+        this.key.interestOps(ops);
+        this.key.selector().wakeup();
+    }
+    
+    public int getSocketTimeout() {
+        return this.socketTimeout;
+    }
+    
+    public void setSocketTimeout(int timeout) {
+        this.socketTimeout = timeout;
+    }
+    
+    public void close() {
+        if (this.closed) {
+            return;
+        }
+        this.closed = true;
+        this.key.cancel();
+        try {
+            this.key.channel().close();
+        } catch (IOException ex) {
+            // Munching exceptions is not nice
+            // but in this case it is justified
+        }
+    }
+    
+    public boolean isClosed() {
+        return this.closed || !this.key.isValid();
+    }
+    
+    public Object getAttribute(final String name) {
+        return this.attributes.get(name);
+    }
+
+    public Object removeAttribute(final String name) {
+        return this.attributes.remove(name);
+    }
+
+    public void setAttribute(final String name, final Object obj) {
+        this.attributes.put(name, obj);
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/DefaultIOSession.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java?rev=437358&view=auto
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java
(added)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java
Sun Aug 27 03:29:08 2006
@@ -0,0 +1,78 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.impl;
+
+import org.apache.http.nio.IOSession;
+
+public class SessionHandle {
+
+    private final IOSession session;
+    private final long started;
+
+    private long lastRead;
+    private long lastWrite;
+    
+    public SessionHandle(final IOSession session) {
+        super();
+        if (session == null) {
+            throw new IllegalArgumentException("Session may not be null");
+        }
+        this.session = session;
+        long now = System.currentTimeMillis();
+        this.started = now;
+        this.lastRead = now;
+        this.lastWrite = now;
+    }
+
+    public IOSession getSession() {
+        return this.session;
+    }
+
+    public long getStarted() {
+        return this.started;
+    }
+
+    public long getLastRead() {
+        return this.lastRead;
+    }
+
+    public long getLastWrite() {
+        return this.lastWrite;
+    }
+    
+    public void resetLastRead() {
+        this.lastRead = System.currentTimeMillis();
+    }
+    
+    public void resetLastWrite() {
+        this.lastWrite = System.currentTimeMillis();
+    }
+    
+}

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/impl/SessionHandle.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message