AsynchChannelToServerRequestChannel xref

View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Hiram Chirino
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   *  Unless required by applicable law or agreed to in writing, software
    12   *  distributed under the License is distributed on an "AS IS" BASIS,
    13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   *  See the License for the specific language governing permissions and
    15   *  limitations under the License.
    16   */
    17  package org.activeio.adapter;
    18
    19  import java.io.IOException;
    20
    21  import org.activeio.AsynchChannel;
    22  import org.activeio.AsynchChannelListener;
    23  import org.activeio.Packet;
    24  import org.activeio.RequestChannel;
    25  import org.activeio.RequestListener;
    26  import org.activeio.packet.EOSPacket;
    27
    28
    29  /***
    30   * Creates a {@see org.activeio.RequestChannel} out of a {@see org.activeio.AsynchChannel}.
    31   * Does not support sending requests.  It can only be used to handle requests.
    32   *
    33   * @version $Revision$
    34   */
    35  public class AsynchChannelToServerRequestChannel implements RequestChannel, AsynchChannelListener {
    36
    37      private final AsynchChannel next;
    38      private RequestListener requestListener;
    39
    40      public AsynchChannelToServerRequestChannel(AsynchChannel next) throws IOException {
    41          this.next = next;
    42          next.setAsynchChannelListener(this);
    43      }
    44
    45      public Packet request(Packet request, long timeout) throws IOException {
    46          throw new IOException("Operation not supported.");
    47      }
    48
    49      public void setRequestListener(RequestListener requestListener) throws IOException {
    50          this.requestListener = requestListener;
    51      }
    52
    53      public RequestListener getRequestListener() {
    54          return requestListener;
    55      }
    56
    57      public Object narrow(Class target) {
    58          return next.narrow(target);
    59      }
    60
    61      public void dispose() {
    62          next.dispose();
    63      }
    64
    65      public void start() throws IOException {
    66          next.start();
    67      }
    68
    69      public void stop(long timeout) throws IOException {
    70          next.stop(timeout);
    71      }
    72
    73      public void onPacket(Packet packet) {
    74          if( packet == EOSPacket.EOS_PACKET ) {
    75              return;
    76          }
    77          try {
    78              Packet response = requestListener.onRequest(packet);
    79              next.write(response);
    80              next.flush();
    81          } catch (IOException e) {
    82              requestListener.onRquestError(e);
    83          }
    84      }
    85
    86      public void onPacketError(IOException error) {
    87          requestListener.onRquestError(error);
    88      }
    89  }