View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20  package org.apache.mina.transport.bio;
    21
    22  import java.io.IOException;
    23  import java.net.SocketAddress;
    24  import java.nio.ByteBuffer;
    25
    26  import org.apache.mina.api.IoFuture;
    27  import org.apache.mina.api.IoSession;
    28  import org.apache.mina.api.IoSessionConfig;
    29  import org.apache.mina.service.idlechecker.IdleChecker;
    30  import org.apache.mina.session.AbstractIoSession;
    31  import org.apache.mina.session.DefaultWriteRequest;
    32  import org.apache.mina.session.WriteRequest;
    33
    34  /**
    35   * A {@link IoSession} for {@link BioUdpServer}
    36   *
    37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    38   */
    39  public class BioUdpSession extends AbstractIoSession {
    40
    41      private SocketAddress remoteAddress;
    42
    43      public BioUdpSession(SocketAddress remoteAddress, BioUdpServer service, IdleChecker idleChecker) {
    44          super(service, idleChecker);
    45          this.remoteAddress = remoteAddress;
    46      }
    47
    48      /**
    49       * {@inheritDoc}
    50       */
    51      @Override
    52      public SocketAddress getRemoteAddress() {
    53          return remoteAddress;
    54      }
    55
    56      /**
    57       * {@inheritDoc}
    58       */
    59      @Override
    60      public SocketAddress getLocalAddress() {
    61          return ((BioUdpServer) getService()).getBoundAddress();
    62      }
    63
    64      /**
    65       * {@inheritDoc}
    66       */
    67      @Override
    68      public IoFuture<Void> close(boolean immediately) {
    69          processSessionClosed();
    70          return null;
    71      }
    72
    73      /**
    74       * {@inheritDoc}
    75       */
    76      @Override
    77      public void suspendRead() {
    78          throw new IllegalStateException("not supported");
    79      }
    80
    81      /**
    82       * {@inheritDoc}
    83       */
    84      @Override
    85      public void suspendWrite() {
    86          throw new IllegalStateException("not supported");
    87
    88      }
    89
    90      /**
    91       * {@inheritDoc}
    92       */
    93      @Override
    94      public void resumeRead() {
    95          throw new IllegalStateException("not supported");
    96      }
    97
    98      /**
    99       * {@inheritDoc}
    100      */
    101     @Override
    102     public void resumeWrite() {
    103         throw new IllegalStateException("not supported");
    104     }
    105
    106     /**
    107      * {@inheritDoc}
    108      */
    109     @Override
    110     public boolean isReadSuspended() {
    111         return false;
    112     }
    113
    114     /**
    115      * {@inheritDoc}
    116      */
    117     @Override
    118     public boolean isWriteSuspended() {
    119         return false;
    120     }
    121
    122     /**
    123      * {@inheritDoc}
    124      */
    125     @Override
    126     public IoSessionConfig getConfig() {
    127         return config;
    128     }
    129
    130     /**
    131      * {@inheritDoc}
    132      */
    133     @Override
    134     public WriteRequest enqueueWriteRequest(WriteRequest writeRequest) {
    135
    136         try {
    137             int written = ((BioUdpServer) getService()).channel.send((ByteBuffer) writeRequest.getMessage(),
    138                     remoteAddress);
    139             if (written > 0) {
    140                 incrementWrittenBytes(written);
    141
    142                 final Object highLevel = ((DefaultWriteRequest) writeRequest).getOriginalMessage();
    143
    144                 if (highLevel != null) {
    145                     processMessageSent(highLevel);
    146                 }
    147
    148             } else {
    149                 processException(new RuntimeException("unable to write"));
    150             }
    151         } catch (IOException e) {
    152             processException(e);
    153         }
    154
    155         return writeRequest;
    156
    157     }
    158 }