2016-05-05 10:04:17 +02:00
|
|
|
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-21 11:53:00 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2000-09-18 14:29:57 +00:00
|
|
|
|
|
|
|
package com.sun.star.lib.connections.socket;
|
|
|
|
|
|
|
|
import com.sun.star.comp.loader.FactoryHelper;
|
2002-10-07 12:17:19 +00:00
|
|
|
import com.sun.star.connection.ConnectionSetupException;
|
|
|
|
import com.sun.star.connection.NoConnectException;
|
2000-09-18 14:29:57 +00:00
|
|
|
import com.sun.star.connection.XConnection;
|
|
|
|
import com.sun.star.connection.XConnector;
|
|
|
|
import com.sun.star.lang.XMultiServiceFactory;
|
|
|
|
import com.sun.star.lang.XSingleServiceFactory;
|
|
|
|
import com.sun.star.registry.XRegistryKey;
|
2014-08-15 16:17:25 +02:00
|
|
|
|
2002-10-07 12:17:19 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2000-09-18 14:29:57 +00:00
|
|
|
|
|
|
|
/**
|
2002-10-07 12:17:19 +00:00
|
|
|
* A component that implements the <code>XConnector</code> interface.
|
|
|
|
*
|
|
|
|
* <p>The <code>socketConnector</code> is a specialized component that uses TCP
|
|
|
|
* sockets for communication. The <code>socketConnector</code> is generally
|
|
|
|
* used by the <code>com.sun.star.connection.Connector</code> service.</p>
|
|
|
|
*
|
2012-06-28 11:47:16 +02:00
|
|
|
* @see com.sun.star.connection.XAcceptor
|
|
|
|
* @see com.sun.star.connection.XConnection
|
|
|
|
* @see com.sun.star.connection.XConnector
|
2012-08-20 12:54:24 +01:00
|
|
|
* @see com.sun.star.comp.loader.JavaLoader
|
2002-10-07 12:17:19 +00:00
|
|
|
*
|
|
|
|
* @since UDK 1.0
|
2000-09-18 14:29:57 +00:00
|
|
|
*/
|
2002-10-07 12:17:19 +00:00
|
|
|
public final class socketConnector implements XConnector {
|
2000-09-18 14:29:57 +00:00
|
|
|
/**
|
2002-10-07 12:17:19 +00:00
|
|
|
* The name of the service.
|
|
|
|
*
|
2015-02-21 13:53:17 +01:00
|
|
|
* <p>The <code>JavaLoader</code> accesses this through reflection.</p>
|
2002-10-07 12:17:19 +00:00
|
|
|
*
|
|
|
|
* @see com.sun.star.comp.loader.JavaLoader
|
2000-09-18 14:29:57 +00:00
|
|
|
*/
|
2002-10-07 12:17:19 +00:00
|
|
|
public static final String __serviceName
|
|
|
|
= "com.sun.star.connection.socketConnector";
|
2000-09-18 14:29:57 +00:00
|
|
|
|
|
|
|
/**
|
2002-10-07 12:17:19 +00:00
|
|
|
* Returns a factory for creating the service.
|
|
|
|
*
|
|
|
|
* <p>This method is called by the <code>JavaLoader</code>.</p>
|
|
|
|
*
|
|
|
|
* @param implName the name of the implementation for which a service is
|
|
|
|
* requested.
|
|
|
|
* @param multiFactory the service manager to be used (if needed).
|
|
|
|
* @param regKey the registry key.
|
|
|
|
* @return an <code>XSingleServiceFactory</code> for creating the component.
|
|
|
|
*
|
|
|
|
* @see com.sun.star.comp.loader.JavaLoader
|
2000-09-18 14:29:57 +00:00
|
|
|
*/
|
2002-10-07 12:17:19 +00:00
|
|
|
public static XSingleServiceFactory __getServiceFactory(
|
|
|
|
String implName, XMultiServiceFactory multiFactory, XRegistryKey regKey)
|
2000-09-18 14:29:57 +00:00
|
|
|
{
|
2002-10-07 12:17:19 +00:00
|
|
|
return implName.equals(socketConnector.class.getName())
|
|
|
|
? FactoryHelper.getServiceFactory(socketConnector.class,
|
|
|
|
__serviceName, multiFactory,
|
|
|
|
regKey)
|
|
|
|
: null;
|
2000-09-18 14:29:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2002-10-07 12:17:19 +00:00
|
|
|
* Connects via the described socket to a waiting server.
|
|
|
|
*
|
|
|
|
* <p>The connection description has the following format:
|
|
|
|
* <code><var>type</var></code><!--
|
|
|
|
* -->*(<code><var>key</var>=<var>value</var></code>),
|
|
|
|
* where <code><var>type</var></code> should be <code>socket</code>
|
2014-08-29 14:00:45 +02:00
|
|
|
* (ignoring case). Supported keys (ignoring case) currently are</p>
|
2000-09-18 14:29:57 +00:00
|
|
|
* <dl>
|
2002-10-07 12:17:19 +00:00
|
|
|
* <dt><code>host</code>
|
|
|
|
* <dd>The name or address of the server. Must be present.
|
|
|
|
* <dt><code>port</code>
|
|
|
|
* <dd>The TCP port number of the server (defaults to <code>6001</code>).
|
|
|
|
* <dt><code>tcpnodelay</code>
|
|
|
|
* <dd>A flag (<code>0</code>/<code>1</code>) enabling or disabling Nagle's
|
|
|
|
* algorithm on the resulting connection.
|
2014-08-29 14:00:45 +02:00
|
|
|
* </dl>
|
2002-10-07 12:17:19 +00:00
|
|
|
*
|
|
|
|
* @param connectionDescription the description of the connection.
|
|
|
|
* @return an <code>XConnection</code> to the server.
|
|
|
|
*
|
2012-06-28 11:47:16 +02:00
|
|
|
* @see com.sun.star.connection.XAcceptor
|
|
|
|
* @see com.sun.star.connection.XConnection
|
2000-09-18 14:29:57 +00:00
|
|
|
*/
|
2002-10-07 12:17:19 +00:00
|
|
|
public synchronized XConnection connect(String connectionDescription)
|
|
|
|
throws NoConnectException, ConnectionSetupException
|
2000-09-18 14:29:57 +00:00
|
|
|
{
|
2014-07-23 18:51:20 +02:00
|
|
|
if (connected)
|
2002-10-07 12:17:19 +00:00
|
|
|
throw new ConnectionSetupException("alread connected");
|
2014-07-23 18:51:20 +02:00
|
|
|
|
2002-10-07 12:17:19 +00:00
|
|
|
ConnectionDescriptor desc;
|
|
|
|
try {
|
|
|
|
desc = new ConnectionDescriptor(connectionDescription);
|
|
|
|
} catch (com.sun.star.lang.IllegalArgumentException e) {
|
2014-08-15 16:17:25 +02:00
|
|
|
throw new ConnectionSetupException(e);
|
2000-09-18 14:29:57 +00:00
|
|
|
}
|
2014-07-23 18:51:20 +02:00
|
|
|
|
|
|
|
if (desc.getHost() == null)
|
2002-10-07 12:17:19 +00:00
|
|
|
throw new ConnectionSetupException("host parameter missing");
|
2004-08-20 08:21:35 +00:00
|
|
|
// Try all (IPv4 and IPv6) addresses, in case this client is on a
|
|
|
|
// dual-stack host and the server process is an IPv4-only process, also
|
|
|
|
// on a dual-stack host (see Stevens, Fenner, Rudoff: "Unix Network
|
|
|
|
// Programming, Volume 1: The Sockets Networking API, 3rd Edition",
|
|
|
|
// p. 359):
|
|
|
|
InetAddress[] adr;
|
2000-09-18 14:29:57 +00:00
|
|
|
try {
|
2004-08-20 08:21:35 +00:00
|
|
|
adr = InetAddress.getAllByName(desc.getHost());
|
2002-10-07 12:17:19 +00:00
|
|
|
} catch (UnknownHostException e) {
|
2014-08-15 16:17:25 +02:00
|
|
|
throw new ConnectionSetupException(e);
|
2000-09-18 14:29:57 +00:00
|
|
|
}
|
2004-08-20 08:21:35 +00:00
|
|
|
Socket socket = null;
|
2015-01-11 13:54:04 +02:00
|
|
|
boolean isLoopbackAddress = false;
|
2004-08-20 08:21:35 +00:00
|
|
|
for (int i = 0; i < adr.length; ++i) {
|
|
|
|
try {
|
2015-01-11 13:54:04 +02:00
|
|
|
isLoopbackAddress = adr[i].isLoopbackAddress();
|
2004-08-20 08:21:35 +00:00
|
|
|
socket = new Socket(adr[i], desc.getPort());
|
|
|
|
break;
|
|
|
|
} catch (IOException e) {
|
2014-07-23 18:51:20 +02:00
|
|
|
if (i == adr.length - 1)
|
2014-08-15 16:17:25 +02:00
|
|
|
throw new NoConnectException(e);
|
2004-08-20 08:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-07 12:17:19 +00:00
|
|
|
XConnection con;
|
|
|
|
try {
|
2015-01-11 13:54:04 +02:00
|
|
|
// we enable tcpNoDelay for loopback connections because
|
|
|
|
// it can make a significant speed difference on linux boxes.
|
2015-07-30 15:38:41 +02:00
|
|
|
if (desc.getTcpNoDelay() != null)
|
2002-10-07 12:17:19 +00:00
|
|
|
socket.setTcpNoDelay(desc.getTcpNoDelay().booleanValue());
|
2015-07-30 15:38:41 +02:00
|
|
|
else if (isLoopbackAddress)
|
|
|
|
socket.setTcpNoDelay(true);
|
2014-07-23 18:51:20 +02:00
|
|
|
|
2002-10-07 12:17:19 +00:00
|
|
|
con = new SocketConnection(connectionDescription, socket);
|
2004-08-20 08:21:35 +00:00
|
|
|
} catch (IOException e) {
|
2016-08-19 11:26:58 +01:00
|
|
|
if (socket != null) {
|
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
} catch(IOException ioException) {
|
|
|
|
}
|
2016-08-19 11:22:27 +01:00
|
|
|
}
|
2014-08-15 16:17:25 +02:00
|
|
|
throw new NoConnectException(e);
|
2000-09-18 14:29:57 +00:00
|
|
|
}
|
2002-10-07 12:17:19 +00:00
|
|
|
connected = true;
|
|
|
|
return con;
|
2000-09-18 14:29:57 +00:00
|
|
|
}
|
|
|
|
|
2002-10-07 12:17:19 +00:00
|
|
|
private boolean connected = false;
|
|
|
|
}
|
2016-05-05 10:04:17 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|