001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/examples/org/apache/http/examples/nio/ElementalEchoServer.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: package org.apache.http.examples.nio;
032:
033: import java.io.IOException;
034: import java.io.InterruptedIOException;
035: import java.net.InetSocketAddress;
036: import java.nio.ByteBuffer;
037:
038: import org.apache.http.params.BasicHttpParams;
039: import org.apache.http.impl.nio.reactor.DefaultListeningIOReactor;
040: import org.apache.http.nio.reactor.EventMask;
041: import org.apache.http.nio.reactor.IOEventDispatch;
042: import org.apache.http.nio.reactor.IOSession;
043: import org.apache.http.nio.reactor.ListeningIOReactor;
044: import org.apache.http.params.HttpParams;
045:
046: public class ElementalEchoServer {
047:
048: public static void main(String[] args) throws Exception {
049: HttpParams params = new BasicHttpParams();
050: IOEventDispatch ioEventDispatch = new DefaultIoEventDispatch();
051: ListeningIOReactor ioReactor = new DefaultListeningIOReactor(2,
052: params);
053: ioReactor.listen(new InetSocketAddress(8080));
054: try {
055: ioReactor.execute(ioEventDispatch);
056: } catch (InterruptedIOException ex) {
057: System.err.println("Interrupted");
058: } catch (IOException e) {
059: System.err.println("I/O error: " + e.getMessage());
060: }
061: System.out.println("Shutdown");
062: }
063:
064: static class DefaultIoEventDispatch implements IOEventDispatch {
065:
066: private final ByteBuffer buffer = ByteBuffer.allocate(1024);
067:
068: public void connected(IOSession session) {
069: System.out.println("connected");
070: session.setEventMask(EventMask.READ);
071: session.setSocketTimeout(20000);
072: }
073:
074: public void inputReady(final IOSession session) {
075: System.out.println("readable");
076: try {
077: this .buffer.compact();
078: int bytesRead = session.channel().read(this .buffer);
079: if (this .buffer.position() > 0) {
080: session.setEventMask(EventMask.READ_WRITE);
081: }
082: System.out.println("Bytes read: " + bytesRead);
083: if (bytesRead == -1) {
084: session.close();
085: }
086: } catch (IOException ex) {
087: System.err.println("I/O error: " + ex.getMessage());
088: }
089: }
090:
091: public void outputReady(final IOSession session) {
092: System.out.println("writeable");
093: try {
094: this .buffer.flip();
095: int bytesWritten = session.channel().write(this .buffer);
096: if (!this .buffer.hasRemaining()) {
097: session.setEventMask(EventMask.READ);
098: }
099: System.out.println("Bytes written: " + bytesWritten);
100: } catch (IOException ex) {
101: System.err.println("I/O error: " + ex.getMessage());
102: }
103: }
104:
105: public void timeout(final IOSession session) {
106: System.out.println("timeout");
107: session.close();
108: }
109:
110: public void disconnected(final IOSession session) {
111: System.out.println("disconnected");
112: }
113: }
114:
115: }
|