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.ConnectionSetupException; 28cdf0e10cSrcweir import com.sun.star.connection.NoConnectException; 29cdf0e10cSrcweir import com.sun.star.connection.XConnection; 30cdf0e10cSrcweir import com.sun.star.connection.XConnector; 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.Socket; 37cdf0e10cSrcweir import java.net.UnknownHostException; 38cdf0e10cSrcweir 39cdf0e10cSrcweir /** 40cdf0e10cSrcweir * A component that implements the <code>XConnector</code> interface. 41cdf0e10cSrcweir * 42cdf0e10cSrcweir * <p>The <code>socketConnector</code> is a specialized component that uses TCP 43cdf0e10cSrcweir * sockets for communication. The <code>socketConnector</code> is generally 44cdf0e10cSrcweir * used by the <code>com.sun.star.connection.Connector</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 socketConnector implements XConnector { 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.socketConnector"; 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(socketConnector.class.getName()) 81cdf0e10cSrcweir ? FactoryHelper.getServiceFactory(socketConnector.class, 82cdf0e10cSrcweir __serviceName, multiFactory, 83cdf0e10cSrcweir regKey) 84cdf0e10cSrcweir : null; 85cdf0e10cSrcweir } 86cdf0e10cSrcweir 87cdf0e10cSrcweir /** 88cdf0e10cSrcweir * Connects via the described socket to a waiting server. 89cdf0e10cSrcweir * 90cdf0e10cSrcweir * <p>The connection description has the following format: 91cdf0e10cSrcweir * <code><var>type</var></code><!-- 92cdf0e10cSrcweir * -->*(<code><var>key</var>=<var>value</var></code>), 93cdf0e10cSrcweir * where <code><var>type</var></code> should be <code>socket</code> 94cdf0e10cSrcweir * (ignoring case). Supported keys (ignoring case) currently are 95cdf0e10cSrcweir * <dl> 96cdf0e10cSrcweir * <dt><code>host</code> 97cdf0e10cSrcweir * <dd>The name or address of the server. Must be present. 98cdf0e10cSrcweir * <dt><code>port</code> 99cdf0e10cSrcweir * <dd>The TCP port number of the server (defaults to <code>6001</code>). 100cdf0e10cSrcweir * <dt><code>tcpnodelay</code> 101cdf0e10cSrcweir * <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's 102cdf0e10cSrcweir * algorithm on the resulting connection. 103cdf0e10cSrcweir * </dl></p> 104cdf0e10cSrcweir * 105cdf0e10cSrcweir * @param connectionDescription the description of the connection. 106cdf0e10cSrcweir * @return an <code>XConnection</code> to the server. 107cdf0e10cSrcweir * 108cdf0e10cSrcweir * @see com.sun.star.connections.XAcceptor 109cdf0e10cSrcweir * @see com.sun.star.connections.XConnection 110cdf0e10cSrcweir */ connect(String connectionDescription)111cdf0e10cSrcweir public synchronized XConnection connect(String connectionDescription) 112cdf0e10cSrcweir throws NoConnectException, ConnectionSetupException 113cdf0e10cSrcweir { 114cdf0e10cSrcweir if (connected) { 115cdf0e10cSrcweir throw new ConnectionSetupException("alread connected"); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir ConnectionDescriptor desc; 118cdf0e10cSrcweir try { 119cdf0e10cSrcweir desc = new ConnectionDescriptor(connectionDescription); 120cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 121cdf0e10cSrcweir throw new ConnectionSetupException(e.toString()); 122cdf0e10cSrcweir } 123cdf0e10cSrcweir if (desc.getHost() == null) { 124cdf0e10cSrcweir throw new ConnectionSetupException("host parameter missing"); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir // Try all (IPv4 and IPv6) addresses, in case this client is on a 127cdf0e10cSrcweir // dual-stack host and the server process is an IPv4-only process, also 128cdf0e10cSrcweir // on a dual-stack host (see Stevens, Fenner, Rudoff: "Unix Network 129cdf0e10cSrcweir // Programming, Volume 1: The Sockets Networking API, 3rd Edition", 130cdf0e10cSrcweir // p. 359): 131cdf0e10cSrcweir InetAddress[] adr; 132cdf0e10cSrcweir try { 133cdf0e10cSrcweir adr = InetAddress.getAllByName(desc.getHost()); 134cdf0e10cSrcweir } catch (UnknownHostException e) { 135cdf0e10cSrcweir throw new ConnectionSetupException(e.toString()); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir Socket socket = null; 138cdf0e10cSrcweir for (int i = 0; i < adr.length; ++i) { 139cdf0e10cSrcweir try { 140cdf0e10cSrcweir socket = new Socket(adr[i], desc.getPort()); 141cdf0e10cSrcweir break; 142cdf0e10cSrcweir } catch (IOException e) { 143cdf0e10cSrcweir if (i == adr.length - 1) { 144cdf0e10cSrcweir throw new NoConnectException(e.toString()); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir } 147cdf0e10cSrcweir } 148cdf0e10cSrcweir XConnection con; 149cdf0e10cSrcweir try { 150cdf0e10cSrcweir if (desc.getTcpNoDelay() != null) { 151cdf0e10cSrcweir socket.setTcpNoDelay(desc.getTcpNoDelay().booleanValue()); 152cdf0e10cSrcweir } 153cdf0e10cSrcweir con = new SocketConnection(connectionDescription, socket); 154cdf0e10cSrcweir } catch (IOException e) { 155cdf0e10cSrcweir throw new NoConnectException(e.toString()); 156cdf0e10cSrcweir } 157cdf0e10cSrcweir connected = true; 158cdf0e10cSrcweir return con; 159cdf0e10cSrcweir } 160cdf0e10cSrcweir 161cdf0e10cSrcweir private boolean connected = false; 162cdf0e10cSrcweir } 163