AIOSynchChannelServer 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   **/
    18
    19  package org.activeio.net;
    20
    21  import java.io.IOException;
    22  import java.net.SocketException;
    23  import java.net.SocketTimeoutException;
    24  import java.net.URI;
    25
    26  import org.activeio.AsynchChannel;
    27  import org.activeio.Channel;
    28  import org.activeio.SynchChannelServer;
    29  import org.activeio.filter.WriteBufferedAsynchChannel;
    30  import org.activeio.packet.ByteBufferPacket;
    31
    32  import com.ibm.io.async.AsyncServerSocketChannel;
    33
    34  /***
    35   * @version $Revision$
    36   */
    37  public class AIOSynchChannelServer implements SynchChannelServer {
    38
    39      private final AsyncServerSocketChannel serverSocket;
    40      private final URI bindURI;
    41      private final URI connectURI;
    42      private int curentSoTimeout;
    43
    44      public AIOSynchChannelServer(AsyncServerSocketChannel serverSocket, URI bindURI, URI connectURI) throws IOException {
    45          this.serverSocket=serverSocket;
    46          this.bindURI=bindURI;
    47          this.connectURI=connectURI;
    48          this.curentSoTimeout = serverSocket.socket().getSoTimeout();
    49      }
    50
    51      public URI getBindURI() {
    52          return bindURI;
    53      }
    54
    55      public URI getConnectURI() {
    56          return this.connectURI;
    57      }
    58
    59      public void dispose() {
    60          try {
    61              serverSocket.close();
    62          } catch (IOException e) {
    63          }
    64      }
    65
    66      synchronized public void start() throws IOException {
    67      }
    68
    69      synchronized public void stop(long timeout) {
    70      }
    71
    72      public Channel accept(long timeout) throws IOException {
    73          try {
    74
    75              if (timeout == SynchChannelServer.WAIT_FOREVER_TIMEOUT)
    76  	            setSoTimeout(0);
    77  	        else if (timeout == SynchChannelServer.NO_WAIT_TIMEOUT)
    78  	            setSoTimeout(1);
    79  	        else
    80  	            setSoTimeout((int) timeout);
    81
    82              AsynchChannel channel = new AIOAsynchChannel(serverSocket.accept());
    83              channel = new WriteBufferedAsynchChannel(channel, ByteBufferPacket.createDefaultBuffer(true), false);
    84              return channel;
    85
    86          } catch (SocketTimeoutException ignore) {
    87          }
    88          return null;
    89      }
    90
    91      private void setSoTimeout(int i) throws SocketException {
    92          if (curentSoTimeout != i) {
    93              serverSocket.socket().setSoTimeout(i);
    94              curentSoTimeout = i;
    95          }
    96      }
    97
    98      public Object narrow(Class target) {
    99          if( target.isAssignableFrom(getClass()) ) {
    100             return this;
    101         }
    102         return null;
    103     }
    104
    105     public String toString() {
    106         return "AIO Server: "+getConnectURI();
    107     }
    108
    109 }