Package com.barchart.udt.net

Source Code of com.barchart.udt.net.NetServerSocketUDT

/**
* Copyright (C) 2009-2012 Barchart, Inc. <http://www.barchart.com/>
*
* All rights reserved. Licensed under the OSI BSD License.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package com.barchart.udt.net;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.nio.channels.ServerSocketChannel;

import com.barchart.udt.ExceptionUDT;
import com.barchart.udt.SocketUDT;
import com.barchart.udt.TypeUDT;

public class NetServerSocketUDT extends ServerSocket implements IceServerSocket {

  protected final SocketUDT serverSocketUDT;

  public NetServerSocketUDT() throws IOException {
    this.serverSocketUDT = new SocketUDT(TypeUDT.STREAM);
    this.serverSocketUDT.configureBlocking(true);
  }

  // exception thanks to JDK designers
  /** NOTE: you just carefully choose TypeUDT */
  public NetServerSocketUDT(final SocketUDT socketUDT) throws IOException {
    this.serverSocketUDT = socketUDT;
  }

  //

  @Override
  public Socket accept() throws IOException {
    final SocketUDT connector = serverSocketUDT.accept();
    return new NetSocketUDT(connector);
  }

  @Override
  public void bind(SocketAddress endpoint) throws IOException {
    final int backlog = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE;
    bind(endpoint, backlog);
  }

  // NOTE: bind() means listen() for UDT server socket
  /*
   * The listen method lets a UDT socket enter listening state. The socket
   * must call bind before a listen call.
   */
  @Override
  public void bind(SocketAddress bindpoint, int backlog) throws IOException {
    if (bindpoint == null) {
      bindpoint = new InetSocketAddress(0);
    }
    if (backlog <= 0) {
      backlog = SocketUDT.DEFAULT_ACCEPT_QUEUE_SIZE;
    }
    serverSocketUDT.bind((InetSocketAddress) bindpoint);
    serverSocketUDT.listen(backlog);
  }

  @Override
  public void close() throws IOException {
    serverSocketUDT.close();
  }

  @Override
  public ServerSocketChannel getChannel() {
    throw new RuntimeException("feature not available");
  }

  @Override
  public InetAddress getInetAddress() {
    return serverSocketUDT.getLocalInetAddress();
  }

  @Override
  public int getLocalPort() {
    return serverSocketUDT.getLocalInetPort();
  }

  @Override
  public SocketAddress getLocalSocketAddress() {
    try {
      return serverSocketUDT.getLocalSocketAddress();
    } catch (ExceptionUDT e) {
      return null;
    }
  }

  @Override
  public int getReceiveBufferSize() throws SocketException {
    return serverSocketUDT.getReceiveBufferSize();
  }

  @Override
  public boolean getReuseAddress() throws SocketException {
    return serverSocketUDT.getReuseAddress();
  }

  @Override
  public int getSoTimeout() throws IOException {
    return serverSocketUDT.getSoTimeout();
  }

  @Override
  public boolean isBound() {
    return serverSocketUDT.isBound();
  }

  @Override
  public boolean isClosed() {
    return serverSocketUDT.isClosed();
  }

  @Override
  public void setPerformancePreferences(int connectionTime, int latency,
      int bandwidth) {
    throw new RuntimeException("feature not available");
  }

  // NOTE: set both send and receive, since they are inherited on accept()
  @Override
  public void setReceiveBufferSize(int size) throws SocketException {
    serverSocketUDT.setReceiveBufferSize(size);
    serverSocketUDT.setSendBufferSize(size);
  }

  @Override
  public void setReuseAddress(boolean on) throws SocketException {
    serverSocketUDT.setReuseAddress(on);
  }

  @Override
  public void setSoTimeout(int timeout) throws SocketException {
    serverSocketUDT.setSoTimeout(timeout);
  }

}
TOP

Related Classes of com.barchart.udt.net.NetServerSocketUDT

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.