1*2be43276SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2be43276SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2be43276SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2be43276SAndrew Rist * distributed with this work for additional information 6*2be43276SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2be43276SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2be43276SAndrew Rist * "License"); you may not use this file except in compliance 9*2be43276SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*2be43276SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*2be43276SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2be43276SAndrew Rist * software distributed under the License is distributed on an 15*2be43276SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2be43276SAndrew Rist * KIND, either express or implied. See the License for the 17*2be43276SAndrew Rist * specific language governing permissions and limitations 18*2be43276SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*2be43276SAndrew Rist *************************************************************/ 21*2be43276SAndrew Rist 22*2be43276SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.lib.connections.socket; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper; 27cdf0e10cSrcweir import com.sun.star.connection.AlreadyAcceptingException; 28cdf0e10cSrcweir import com.sun.star.connection.ConnectionSetupException; 29cdf0e10cSrcweir import com.sun.star.connection.XAcceptor; 30cdf0e10cSrcweir import com.sun.star.connection.XConnection; 31cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 32cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 33cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 34cdf0e10cSrcweir import java.io.IOException; 35cdf0e10cSrcweir import java.net.InetAddress; 36cdf0e10cSrcweir import java.net.ServerSocket; 37cdf0e10cSrcweir import java.net.Socket; 38cdf0e10cSrcweir 39cdf0e10cSrcweir /** 40cdf0e10cSrcweir * A component that implements the <code>XAcceptor</code> interface. 41cdf0e10cSrcweir * 42cdf0e10cSrcweir * <p>The <code>socketAcceptor</code> is a specialized component that uses TCP 43cdf0e10cSrcweir * sockets for communication. The <code>socketAcceptor</code> is generally used 44cdf0e10cSrcweir * by the <code>com.sun.star.connection.Acceptor</code> service.</p> 45cdf0e10cSrcweir * 46cdf0e10cSrcweir * @see com.sun.star.connections.XAcceptor 47cdf0e10cSrcweir * @see com.sun.star.connections.XConnection 48cdf0e10cSrcweir * @see com.sun.star.connections.XConnector 49cdf0e10cSrcweir * @see com.sun.star.loader.JavaLoader 50cdf0e10cSrcweir * 51cdf0e10cSrcweir * @since UDK 1.0 52cdf0e10cSrcweir */ 53cdf0e10cSrcweir public final class socketAcceptor implements XAcceptor { 54cdf0e10cSrcweir /** 55cdf0e10cSrcweir * The name of the service. 56cdf0e10cSrcweir * 57cdf0e10cSrcweir * <p>The <code>JavaLoader</code> acceses this through reflection.</p> 58cdf0e10cSrcweir * 59cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 60cdf0e10cSrcweir */ 61cdf0e10cSrcweir public static final String __serviceName 62cdf0e10cSrcweir = "com.sun.star.connection.socketAcceptor"; 63cdf0e10cSrcweir 64cdf0e10cSrcweir /** 65cdf0e10cSrcweir * Returns a factory for creating the service. 66cdf0e10cSrcweir * 67cdf0e10cSrcweir * <p>This method is called by the <code>JavaLoader</code>.</p> 68cdf0e10cSrcweir * 69cdf0e10cSrcweir * @param implName the name of the implementation for which a service is 70cdf0e10cSrcweir * requested. 71cdf0e10cSrcweir * @param multiFactory the service manager to be used (if needed). 72cdf0e10cSrcweir * @param regKey the registry key. 73cdf0e10cSrcweir * @return an <code>XSingleServiceFactory</code> for creating the component. 74cdf0e10cSrcweir * 75cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 76cdf0e10cSrcweir */ __getServiceFactory( String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)77cdf0e10cSrcweir public static XSingleServiceFactory __getServiceFactory( 78cdf0e10cSrcweir String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey) 79cdf0e10cSrcweir { 80cdf0e10cSrcweir return implName.equals(socketAcceptor.class.getName()) 81cdf0e10cSrcweir ? FactoryHelper.getServiceFactory(socketAcceptor.class, 82cdf0e10cSrcweir __serviceName, multiFactory, 83cdf0e10cSrcweir regKey) 84cdf0e10cSrcweir : null; 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir /** 88cdf0e10cSrcweir * Accepts a connection request via the described socket. 89cdf0e10cSrcweir * 90cdf0e10cSrcweir * <p>This call blocks until a connection has been established.</p> 91cdf0e10cSrcweir * 92cdf0e10cSrcweir * <p>The connection description has the following format: 93cdf0e10cSrcweir * <code><var>type</var></code><!-- 94cdf0e10cSrcweir * -->*(<code><var>key</var>=<var>value</var></code>), 95cdf0e10cSrcweir * where <code><var>type</var></code> should be <code>socket</code> 96cdf0e10cSrcweir * (ignoring case). Supported keys (ignoring case) currently are 97cdf0e10cSrcweir * <dl> 98cdf0e10cSrcweir * <dt><code>host</code> 99cdf0e10cSrcweir * <dd>The name or address of the accepting interface (defaults to 100cdf0e10cSrcweir * <code>0</code>, meaning any interface). 101cdf0e10cSrcweir * <dt><code>port</code> 102cdf0e10cSrcweir * <dd>The TCP port number to accept on (defaults to <code>6001</code>). 103cdf0e10cSrcweir * <dt><code>backlog</code> 104cdf0e10cSrcweir * <dd>The maximum length of the acceptor's queue (defaults to 105cdf0e10cSrcweir * <code>50</code>). 106cdf0e10cSrcweir * <dt><code>tcpnodelay</code> 107cdf0e10cSrcweir * <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's 108cdf0e10cSrcweir * algorithm on the resulting connection. 109cdf0e10cSrcweir * </dl></p> 110cdf0e10cSrcweir * 111cdf0e10cSrcweir * @param connectionDescription the description of the connection. 112cdf0e10cSrcweir * @return an <code>XConnection</code> to the client. 113cdf0e10cSrcweir * 114cdf0e10cSrcweir * @see com.sun.star.connections.XConnection 115cdf0e10cSrcweir * @see com.sun.star.connections.XConnector 116cdf0e10cSrcweir */ accept(String connectionDescription)117cdf0e10cSrcweir public XConnection accept(String connectionDescription) throws 118cdf0e10cSrcweir AlreadyAcceptingException, ConnectionSetupException, 119cdf0e10cSrcweir com.sun.star.lang.IllegalArgumentException 120cdf0e10cSrcweir { 121cdf0e10cSrcweir ServerSocket serv; 122cdf0e10cSrcweir synchronized (this) { 123cdf0e10cSrcweir if (server == null) { 124cdf0e10cSrcweir ConnectionDescriptor desc 125cdf0e10cSrcweir = new ConnectionDescriptor(connectionDescription); 126cdf0e10cSrcweir String host = desc.getHost(); 127cdf0e10cSrcweir if (host.equals("0")) { 128cdf0e10cSrcweir host = null; 129cdf0e10cSrcweir } 130cdf0e10cSrcweir if (DEBUG) { 131cdf0e10cSrcweir System.err.println("##### " + getClass().getName() 132cdf0e10cSrcweir + ".accept: creating ServerSocket " 133cdf0e10cSrcweir + desc.getPort() + ", " 134cdf0e10cSrcweir + desc.getBacklog() + ", " + host); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir try { 137cdf0e10cSrcweir server = new ServerSocket(desc.getPort(), desc.getBacklog(), 138cdf0e10cSrcweir host == null ? null 139cdf0e10cSrcweir : InetAddress.getByName(host)); 140cdf0e10cSrcweir } catch (IOException e) { 141cdf0e10cSrcweir throw new ConnectionSetupException(e.toString()); 142cdf0e10cSrcweir } 143cdf0e10cSrcweir acceptingDescription = connectionDescription; 144cdf0e10cSrcweir tcpNoDelay = desc.getTcpNoDelay(); 145cdf0e10cSrcweir } else if (!connectionDescription.equals(acceptingDescription)) { 146cdf0e10cSrcweir throw new AlreadyAcceptingException(acceptingDescription 147cdf0e10cSrcweir + " vs. " 148cdf0e10cSrcweir + connectionDescription); 149cdf0e10cSrcweir } 150cdf0e10cSrcweir serv = server; 151cdf0e10cSrcweir } 152cdf0e10cSrcweir Socket socket; 153cdf0e10cSrcweir try { 154cdf0e10cSrcweir socket = serv.accept(); 155cdf0e10cSrcweir if (DEBUG) { 156cdf0e10cSrcweir System.err.println("##### " + getClass().getName() 157cdf0e10cSrcweir + ".accept: accepted " + socket); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir if (tcpNoDelay != null) { 160cdf0e10cSrcweir socket.setTcpNoDelay(tcpNoDelay.booleanValue()); 161cdf0e10cSrcweir } 162cdf0e10cSrcweir return new SocketConnection(acceptingDescription, socket); 163cdf0e10cSrcweir } 164cdf0e10cSrcweir catch(IOException e) { 165cdf0e10cSrcweir throw new ConnectionSetupException(e.toString()); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir } 168cdf0e10cSrcweir 169cdf0e10cSrcweir // see com.sun.star.connection.XAcceptor#stopAccepting stopAccepting()170cdf0e10cSrcweir public void stopAccepting() { 171cdf0e10cSrcweir ServerSocket serv; 172cdf0e10cSrcweir synchronized (this) { 173cdf0e10cSrcweir serv = server; 174cdf0e10cSrcweir } 175cdf0e10cSrcweir try { 176cdf0e10cSrcweir serv.close(); 177cdf0e10cSrcweir } 178cdf0e10cSrcweir catch (IOException e) { 179cdf0e10cSrcweir throw new com.sun.star.uno.RuntimeException(e.toString()); 180cdf0e10cSrcweir } 181cdf0e10cSrcweir } 182cdf0e10cSrcweir 183cdf0e10cSrcweir private static final boolean DEBUG = false; 184cdf0e10cSrcweir 185cdf0e10cSrcweir private ServerSocket server = null; 186cdf0e10cSrcweir private String acceptingDescription; 187cdf0e10cSrcweir private Boolean tcpNoDelay; 188cdf0e10cSrcweir } 189