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.ServerSocket;
    23  import java.net.Socket;
    24  import java.net.SocketException;
    25  import java.net.SocketTimeoutException;
    26  import java.net.URI;
    27
    28  import org.activeio.Channel;
    29  import org.activeio.SynchChannelServer;
    30
    31  /***
    32   * A SynchChannelServer that creates
    33   * {@see org.activeio.net.TcpSynchChannel}objects from accepted
    34   * tcp socket connections.
    35   *
    36   * @version $Revision$
    37   */
    38  public class SocketSynchChannelServer implements SynchChannelServer {
    39
    40      private ServerSocket serverSocket;
    41      private int curentSoTimeout = 0;
    42      private final URI bindURI;
    43      private final URI connectURI;
    44
    45
    46      /***
    47       * @param serverSocket2
    48       * @param bindURI2
    49       * @param connectURI2
    50       */
    51      public SocketSynchChannelServer(ServerSocket serverSocket, URI bindURI, URI connectURI) {
    52          this.serverSocket=serverSocket;
    53          this.bindURI=bindURI;
    54          this.connectURI=connectURI;
    55      }
    56
    57      public Channel accept(long timeout) throws IOException {
    58          try {
    59              if (timeout == SynchChannelServer.WAIT_FOREVER_TIMEOUT)
    60                  setSoTimeout(0);
    61              else if (timeout == SynchChannelServer.NO_WAIT_TIMEOUT)
    62                  setSoTimeout(1);
    63              else
    64                  setSoTimeout((int) timeout);
    65
    66              Socket socket = serverSocket.accept();
    67              return createChannel(socket);
    68
    69          } catch (SocketTimeoutException ignore) {
    70          }
    71          return null;
    72      }
    73
    74      protected Channel createChannel(Socket socket) throws IOException {
    75          return new SocketSynchChannel(socket);
    76      }
    77
    78      private void setSoTimeout(int i) throws SocketException {
    79          if (curentSoTimeout != i) {
    80              serverSocket.setSoTimeout(i);
    81              curentSoTimeout = i;
    82          }
    83      }
    84
    85      /***
    86       * @see org.activeio.Disposable#dispose()
    87       */
    88      public void dispose() {
    89          if (serverSocket == null)
    90              return;
    91          try {
    92              serverSocket.close();
    93          } catch (IOException ignore) {
    94          }
    95          serverSocket = null;
    96      }
    97
    98      /***
    99       * @return Returns the bindURI.
    100      */
    101     public URI getBindURI() {
    102         return bindURI;
    103     }
    104
    105     /***
    106      * @return Returns the connectURI.
    107      */
    108     public URI getConnectURI() {
    109         return connectURI;
    110     }
    111
    112     public void start() throws IOException {
    113     }
    114
    115     public void stop(long timeout) throws IOException {
    116     }
    117
    118     public Object narrow(Class target) {
    119         if( target.isAssignableFrom(getClass()) ) {
    120             return this;
    121         }
    122         return null;
    123     }
    124
    125     public String toString() {
    126         return "Socket Server: "+getConnectURI();
    127     }
    128 }