Thread: [Beepcore-java-commits] CVS: beepcore-java/src/org/beepcore/beep/transport/tcp TCPSessionCreator.jav
Status: Beta
Brought to you by:
huston
From: Huston F. <hu...@us...> - 2001-10-31 02:03:44
|
Update of /cvsroot/beepcore-java/beepcore-java/src/org/beepcore/beep/transport/tcp In directory usw-pr-cvs1:/tmp/cvs-serv8025/src/org/beepcore/beep/transport/tcp Modified Files: TCPSessionCreator.java Removed Files: AutomatedTCPSessionCreator.java Log Message: Removed AutomatedTCPSessionCreator Index: TCPSessionCreator.java =================================================================== RCS file: /cvsroot/beepcore-java/beepcore-java/src/org/beepcore/beep/transport/tcp/TCPSessionCreator.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -r1.2 -r1.3 *** TCPSessionCreator.java 2001/06/28 15:42:49 1.2 --- TCPSessionCreator.java 2001/10/31 02:03:41 1.3 *************** *** 18,22 **** --- 18,29 ---- + import java.io.IOException; + + import java.net.InetAddress; import java.net.Socket; + import java.net.ServerSocket; + import java.net.UnknownHostException; + + import java.util.Hashtable; import org.beepcore.beep.core.BEEPException; *************** *** 47,54 **** */ public class TCPSessionCreator { ! protected static final int CHANNEL_START_ODD = 1; ! protected static final int CHANNEL_START_EVEN = 2; /** * Method initiate --- 54,73 ---- */ public class TCPSessionCreator { + + // Constants + private static final int DEFAULT_TABLE_SIZE = 4; + private static final int DEFAULT_BACKLOG_SIZE = 100; + private static final String ERR_TCP_SOCKET_FAILURE = + "Unable to create a TCP socket"; + private static final String ERR_BIND_FAILURE = "Bind Failed"; + private static final String ERR_CONNECT_FAILURE = "Connect Failed"; + private static final String ERR_LISTEN_FAILURE = "Accept Failed"; ! // Data ! private static Hashtable listenerSockets = null; + private static final int CHANNEL_START_ODD = 1; + private static final int CHANNEL_START_EVEN = 2; + /** * Method initiate *************** *** 69,85 **** /** ! * Accessible only from TCPSession (kinda slick) */ ! static TCPSession initiate(Socket sock, ProfileRegistry registry, ! SessionCredential localCred, ! SessionCredential peerCred, ! SessionTuningProperties tuning) throws BEEPException { ! return new TCPSession(sock, (ProfileRegistry) registry.clone(), ! CHANNEL_START_ODD, localCred, peerCred, tuning); } /** * Method listen * --- 88,147 ---- /** ! * Method initiate ! * ! * ! * @param host ! * @param port ! * @param registry ! * ! * @throws BEEPException ! * */ ! public static TCPSession initiate(InetAddress host, int port, ! ProfileRegistry registry) throws BEEPException { ! ! // Connect and create TCPSession with the socket ! Socket socket; ! try { ! socket = new Socket(host, port); ! } catch (IOException x) { ! throw new BEEPException(x.getMessage()); ! } ! ! if (socket == null) { ! throw new BEEPException(ERR_TCP_SOCKET_FAILURE); ! } ! ! TCPSession t = TCPSessionCreator.initiate(socket, registry); ! ! return t; } /** + * Method initiate + * + * + * @param host + * @param port + * @param profiles + * @param ccls + * + * @throws BEEPException + * + */ + public static TCPSession initiate(String host, int port, + ProfileRegistry registry) + throws BEEPException + { + try { + return initiate(InetAddress.getByName(host), port, registry); + } catch (UnknownHostException x) { + throw new BEEPException("Unable to connect, unkown host"); + } + } + + /** * Method listen * *************** *** 96,99 **** --- 158,275 ---- return new TCPSession(sock, (ProfileRegistry) registry.clone(), CHANNEL_START_EVEN, null, null, null); + } + + /** + * Method listen + * + * + * @param port + * @param registry + * + * @throws BEEPException + * + */ + public static TCPSession listen(int port, ProfileRegistry registry) + throws BEEPException + { + InetAddress temp = null; + + return listen(temp, port, registry); + } + + /** + * Method listen + * + * + * @param localInterface + * @param port + * @param registry + * + * @throws BEEPException + * + */ + public static TCPSession listen(InetAddress localInterface, int port, + ProfileRegistry registry) + throws BEEPException + { + ServerSocket socket = null; + Socket peer = null; + + if (listenerSockets == null) { + listenerSockets = new Hashtable(DEFAULT_TABLE_SIZE); + } + + socket = (ServerSocket) listenerSockets.get(Integer.toString(port)); + + // Bind if we're not listening on this port + if (socket == null) { + + // Bind to interface/port pair + try { + if (localInterface == null) { + socket = new ServerSocket(port, DEFAULT_BACKLOG_SIZE); + } else { + socket = new ServerSocket(port, DEFAULT_BACKLOG_SIZE, + localInterface); + } + + listenerSockets.put(Integer.toString(port), socket); + } catch (Exception x) { + throw new BEEPException(x.getMessage()); + } + } + + // Listen + try { + peer = socket.accept(); + + return TCPSessionCreator.listen(peer, registry); + } catch (Exception e) { + throw new BEEPException(e.getMessage()); + } + } + + /** + * Method listen + * + * + * @param port + * @param registry + * @param localInterface + * + * @throws BEEPException + * + */ + public static TCPSession listen(String localInterface, int port, + ProfileRegistry registry) + throws BEEPException + { + try { + TCPSession temp = null; + InetAddress addr = null; + + if (localInterface != null) { + addr = InetAddress.getByName(localInterface); + } + + temp = listen(addr, port, registry); + + return temp; + } catch (UnknownHostException x) { + throw new BEEPException(x.getMessage()); + } + } + + /** + * Accessible only from TCPSession (kinda slick) + */ + static TCPSession initiate(Socket sock, ProfileRegistry registry, + SessionCredential localCred, + SessionCredential peerCred, + SessionTuningProperties tuning) + throws BEEPException + { + return new TCPSession(sock, (ProfileRegistry) registry.clone(), + CHANNEL_START_ODD, localCred, peerCred, tuning); } --- AutomatedTCPSessionCreator.java DELETED --- |