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.DatagramSocket;
    22  import java.net.InetAddress;
    23  import java.net.URI;
    24
    25  import org.activeio.SynchChannel;
    26  import org.activeio.SynchChannelFactory;
    27  import org.activeio.SynchChannelServer;
    28
    29  /***
    30   * A TcpSynchChannelFactory creates {@see org.activeio.net.TcpSynchChannel}
    31   * and {@see org.activeio.net.TcpSynchChannelServer} objects.
    32   *
    33   * @version $Revision$
    34   */
    35  public class DatagramSocketSynchChannelFactory implements SynchChannelFactory {
    36
    37      /***
    38       * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
    39       *
    40       * @see org.activeio.SynchChannelFactory#openSynchChannel(java.net.URI)
    41       */
    42      public SynchChannel openSynchChannel(URI location) throws IOException {
    43          DatagramSocket socket=null;
    44          socket = new DatagramSocket();
    45          if( location != null ) {
    46              InetAddress address = InetAddress.getByName(location.getHost());
    47              socket.connect(address, location.getPort());
    48          }
    49          return createSynchChannel(socket);
    50      }
    51
    52      /***
    53       * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
    54       *
    55       */
    56      public SynchChannel openSynchChannel(URI location, URI localLocation) throws IOException {
    57          DatagramSocket socket=null;
    58          InetAddress address = InetAddress.getByName(localLocation.getHost());
    59          socket = new DatagramSocket(localLocation.getPort(), address);
    60
    61          if( location != null ) {
    62              address = InetAddress.getByName(location.getHost());
    63              socket.connect(address, location.getPort());
    64          }
    65          return createSynchChannel(socket);
    66      }
    67
    68      /***
    69       * @param socket
    70       * @return
    71       * @throws IOException
    72       */
    73      protected SynchChannel createSynchChannel(DatagramSocket socket) throws IOException {
    74          return new DatagramSocketSynchChannel(socket);
    75      }
    76
    77      /***
    78       * @throws IOException allways thrown.
    79       * @see org.activeio.SynchChannelFactory#bindSynchChannel(java.net.URI)
    80       */
    81      public SynchChannelServer bindSynchChannel(URI location) throws IOException {
    82          throw new IOException("A SynchChannelServer is not available for this channel.");
    83      }
    84
    85  }