001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/main/java/org/apache/http/impl/nio/DefaultServerIOEventDispatch.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.nio;
033:
034: import org.apache.http.HttpRequestFactory;
035: import org.apache.http.impl.DefaultHttpRequestFactory;
036: import org.apache.http.nio.NHttpServerIOTarget;
037: import org.apache.http.nio.NHttpServiceHandler;
038: import org.apache.http.nio.reactor.IOEventDispatch;
039: import org.apache.http.nio.reactor.IOSession;
040: import org.apache.http.nio.util.ByteBufferAllocator;
041: import org.apache.http.nio.util.HeapByteBufferAllocator;
042: import org.apache.http.params.HttpParams;
043:
044: public class DefaultServerIOEventDispatch implements IOEventDispatch {
045:
046: private static final String NHTTP_CONN = "NHTTP_CONN";
047:
048: protected final ByteBufferAllocator allocator;
049: protected final NHttpServiceHandler handler;
050: protected final HttpParams params;
051:
052: public DefaultServerIOEventDispatch(
053: final NHttpServiceHandler handler, final HttpParams params) {
054: super ();
055: if (handler == null) {
056: throw new IllegalArgumentException(
057: "HTTP service handler may not be null");
058: }
059: if (params == null) {
060: throw new IllegalArgumentException(
061: "HTTP parameters may not be null");
062: }
063: this .allocator = createByteBufferAllocator();
064: this .handler = handler;
065: this .params = params;
066: }
067:
068: protected ByteBufferAllocator createByteBufferAllocator() {
069: return new HeapByteBufferAllocator();
070: }
071:
072: protected HttpRequestFactory createHttpRequestFactory() {
073: return new DefaultHttpRequestFactory();
074: }
075:
076: protected NHttpServerIOTarget createConnection(
077: final IOSession session) {
078: return new DefaultNHttpServerConnection(session,
079: createHttpRequestFactory(), this .allocator, this .params);
080: }
081:
082: public void connected(final IOSession session) {
083: NHttpServerIOTarget conn = createConnection(session);
084: session.setAttribute(NHTTP_CONN, conn);
085: this .handler.connected(conn);
086: }
087:
088: public void disconnected(final IOSession session) {
089: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
090: .getAttribute(NHTTP_CONN);
091: this .handler.closed(conn);
092: }
093:
094: public void inputReady(final IOSession session) {
095: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
096: .getAttribute(NHTTP_CONN);
097: conn.consumeInput(this .handler);
098: }
099:
100: public void outputReady(final IOSession session) {
101: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
102: .getAttribute(NHTTP_CONN);
103: conn.produceOutput(this .handler);
104: }
105:
106: public void timeout(final IOSession session) {
107: NHttpServerIOTarget conn = (NHttpServerIOTarget) session
108: .getAttribute(NHTTP_CONN);
109: this.handler.timeout(conn);
110: }
111:
112: }
|