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  package org.activeio.net;
    19
    20  import java.io.IOException;
    21  import java.net.InetAddress;
    22  import java.net.ServerSocket;
    23  import java.net.Socket;
    24  import java.net.URI;
    25  import java.net.URISyntaxException;
    26
    27  import javax.net.ServerSocketFactory;
    28  import javax.net.SocketFactory;
    29
    30  import org.activeio.SynchChannel;
    31  import org.activeio.SynchChannelFactory;
    32  import org.activeio.SynchChannelServer;
    33
    34  /***
    35   * A TcpSynchChannelFactory creates {@see org.activeio.net.TcpSynchChannel}
    36   * and {@see org.activeio.net.TcpSynchChannelServer} objects.
    37   *
    38   * @version $Revision$
    39   */
    40  public class SocketSynchChannelFactory implements SynchChannelFactory {
    41
    42      protected static final int DEFAULT_BACKLOG = 500;
    43
    44      private final SocketFactory socketFactory;
    45      private final ServerSocketFactory serverSocketFactory;
    46      private int backlog = DEFAULT_BACKLOG;
    47
    48      public SocketSynchChannelFactory() {
    49          this(SocketFactory.getDefault(), ServerSocketFactory.getDefault());
    50      }
    51
    52      public SocketSynchChannelFactory(SocketFactory socketFactory, ServerSocketFactory serverSocketFactory) {
    53          this.socketFactory = socketFactory;
    54          this.serverSocketFactory = serverSocketFactory;
    55      }
    56
    57      /***
    58       * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
    59       *
    60       * @see org.activeio.SynchChannelFactory#openSynchChannel(java.net.URI)
    61       */
    62      public SynchChannel openSynchChannel(URI location) throws IOException {
    63          Socket socket=null;
    64          socket = socketFactory.createSocket(location.getHost(), location.getPort());
    65          return createSynchChannel(socket);
    66      }
    67
    68      /***
    69       * @param socket
    70       * @return
    71       * @throws IOException
    72       */
    73      protected SynchChannel createSynchChannel(Socket socket) throws IOException {
    74          return new SocketSynchChannel(socket);
    75      }
    76
    77      /***
    78       * Binds a server socket a the {@param location}'s port.
    79       *
    80       * @see org.activeio.SynchChannelFactory#bindSynchChannel(java.net.URI)
    81       */
    82      public SynchChannelServer bindSynchChannel(URI bindURI) throws IOException {
    83
    84          String host = bindURI.getHost();
    85          InetAddress addr;
    86          if( (host == null || host.length() == 0 || host.equals("localhost") ) ) {
    87              addr = InetAddress.getLocalHost();
    88          } else {
    89              addr = InetAddress.getByName(host);
    90          }
    91          ServerSocket serverSocket;
    92
    93          if (addr.equals(InetAddress.getLocalHost())) {
    94              serverSocket = serverSocketFactory.createServerSocket(bindURI.getPort(), backlog);
    95          } else {
    96              serverSocket = serverSocketFactory.createServerSocket(bindURI.getPort(), backlog, addr);
    97          }
    98
    99          URI connectURI=bindURI;
    100         try {
    101             connectURI = URISupport.changeHost(connectURI, addr.getHostName());
    102             connectURI = URISupport.changePort(connectURI, serverSocket.getLocalPort());
    103         } catch (URISyntaxException e) {
    104             throw (IOException)new IOException("Could build connect URI: "+e).initCause(e);
    105         }
    106
    107         return new SocketSynchChannelServer(serverSocket, bindURI, connectURI);
    108     }
    109
    110     /***
    111      * @return Returns the backlog.
    112      */
    113     public int getBacklog() {
    114         return backlog;
    115     }
    116
    117     /***
    118      * @param backlog
    119      *            The backlog to set.
    120      */
    121     public void setBacklog(int backlog) {
    122         this.backlog = backlog;
    123     }
    124
    125
    126 }