1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package com.sun.star.comp.connections; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir import com.sun.star.connection.XConnection; 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 36*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory; 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey; 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir /** 41*cdf0e10cSrcweir * The PipedConnection is a component that implements the 42*cdf0e10cSrcweir * <code>XConnection</code> Interface. 43*cdf0e10cSrcweir * It is useful for <code>Thread</code> communication 44*cdf0e10cSrcweir * in one Process. 45*cdf0e10cSrcweir * <p> 46*cdf0e10cSrcweir * @version $Revision: 1.3 $ $ $Date: 2008-04-11 11:09:30 $ 47*cdf0e10cSrcweir * @author Kay Ramme 48*cdf0e10cSrcweir * @see com.sun.star.connections.XConnection 49*cdf0e10cSrcweir * @see com.sun.star.loader.JavaLoader 50*cdf0e10cSrcweir * @since UDK1.0 51*cdf0e10cSrcweir */ 52*cdf0e10cSrcweir public class PipedConnection implements XConnection { 53*cdf0e10cSrcweir /** 54*cdf0e10cSrcweir * When set to true, enables various debugging output. 55*cdf0e10cSrcweir */ 56*cdf0e10cSrcweir public static final boolean DEBUG = false; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir /** 59*cdf0e10cSrcweir * The name of the service, the <code>JavaLoader</code> acceses this through reflection. 60*cdf0e10cSrcweir */ 61*cdf0e10cSrcweir static private final String __serviceName = "com.sun.star.connection.PipedConnection"; 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir /** 64*cdf0e10cSrcweir * Gives a factory for creating the service. 65*cdf0e10cSrcweir * This method is called by the <code>JavaLoader</code> 66*cdf0e10cSrcweir * <p> 67*cdf0e10cSrcweir * @return returns a <code>XSingleServiceFactory</code> for creating the component 68*cdf0e10cSrcweir * @param implName the name of the implementation for which a service is desired 69*cdf0e10cSrcweir * @param multiFactory the service manager to be uses if needed 70*cdf0e10cSrcweir * @param regKey the registryKey 71*cdf0e10cSrcweir * @see com.sun.star.comp.loader.JavaLoader 72*cdf0e10cSrcweir */ 73*cdf0e10cSrcweir public static XSingleServiceFactory __getServiceFactory(String implName, 74*cdf0e10cSrcweir XMultiServiceFactory multiFactory, 75*cdf0e10cSrcweir XRegistryKey regKey) 76*cdf0e10cSrcweir { 77*cdf0e10cSrcweir XSingleServiceFactory xSingleServiceFactory = null; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir if (implName.equals(PipedConnection.class.getName()) ) 80*cdf0e10cSrcweir xSingleServiceFactory = FactoryHelper.getServiceFactory(PipedConnection.class, 81*cdf0e10cSrcweir __serviceName, 82*cdf0e10cSrcweir multiFactory, 83*cdf0e10cSrcweir regKey); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir return xSingleServiceFactory; 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /** 89*cdf0e10cSrcweir * The amount of time in milliseconds, to wait to 90*cdf0e10cSrcweir * see check the buffers. 91*cdf0e10cSrcweir */ 92*cdf0e10cSrcweir protected static final int __waitTime = 10000; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir protected byte _buffer[] = new byte[4096]; 95*cdf0e10cSrcweir protected int _in, 96*cdf0e10cSrcweir _out; 97*cdf0e10cSrcweir protected boolean _closed; 98*cdf0e10cSrcweir protected PipedConnection _otherSide; 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir /** 101*cdf0e10cSrcweir * Constructs a new <code>PipedConnection</code>, sees if there 102*cdf0e10cSrcweir * is an other side, which it should be connected to. 103*cdf0e10cSrcweir * <p> 104*cdf0e10cSrcweir * @param args Another side could be in index 0. 105*cdf0e10cSrcweir */ 106*cdf0e10cSrcweir public PipedConnection(Object args[]) throws com.sun.star.uno.RuntimeException { 107*cdf0e10cSrcweir if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated"); 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir _otherSide = (args.length == 1) ? (PipedConnection)args[0] : null; 110*cdf0e10cSrcweir if(_otherSide != null) { 111*cdf0e10cSrcweir if(_otherSide == this) 112*cdf0e10cSrcweir throw new RuntimeException("can not connect to myself"); 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir _otherSide._otherSide = this; 115*cdf0e10cSrcweir } 116*cdf0e10cSrcweir } 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir /** 119*cdf0e10cSrcweir * This is a private method, used to cummunicate 120*cdf0e10cSrcweir * internal in the pipe. 121*cdf0e10cSrcweir */ 122*cdf0e10cSrcweir private synchronized void receive(byte aData[]) throws com.sun.star.io.IOException { 123*cdf0e10cSrcweir int bytesWritten = 0; 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir if(DEBUG) System.err.println("##### PipedConnection.receive - bytes:" + aData.length + " at:" + _out); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir while(bytesWritten < aData.length) { 128*cdf0e10cSrcweir // wait until it is not full anymore 129*cdf0e10cSrcweir while(_out == (_in - 1) || (_in == 0 && _out == _buffer.length - 1)) { 130*cdf0e10cSrcweir try { 131*cdf0e10cSrcweir notify(); // the buffer is full, signal it 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir wait(__waitTime); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir catch(InterruptedException interruptedException) { 136*cdf0e10cSrcweir throw new com.sun.star.io.IOException(interruptedException.toString()); 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir if(_closed) throw new com.sun.star.io.IOException("connection has been closed"); 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir int bytes ; 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir if(_out < _in) { 145*cdf0e10cSrcweir bytes = Math.min(aData.length - bytesWritten, _in - _out - 1); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir System.arraycopy(aData, bytesWritten, _buffer, _out, bytes); 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir else { 150*cdf0e10cSrcweir if(_in > 0){ 151*cdf0e10cSrcweir bytes = Math.min(aData.length - bytesWritten, _buffer.length - _out); 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir else { 154*cdf0e10cSrcweir bytes = Math.min(aData.length - bytesWritten, _buffer.length - _out - 1); 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir System.arraycopy(aData, bytesWritten, _buffer, _out, bytes); 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir bytesWritten += bytes; 161*cdf0e10cSrcweir _out += bytes; 162*cdf0e10cSrcweir if(_out >= _buffer.length) 163*cdf0e10cSrcweir _out = 0; 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir /** 168*cdf0e10cSrcweir * Read the required number of bytes. 169*cdf0e10cSrcweir * <p> 170*cdf0e10cSrcweir * @return the number of bytes read 171*cdf0e10cSrcweir * @param aReadBytes the outparameter, where the bytes have to be placed 172*cdf0e10cSrcweir * @param nBytesToRead the number of bytes to read 173*cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#read 174*cdf0e10cSrcweir */ 175*cdf0e10cSrcweir public synchronized int read(/*OUT*/byte[][] aReadBytes, int nBytesToRead) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 176*cdf0e10cSrcweir aReadBytes[0] = new byte[nBytesToRead]; 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir if(DEBUG) System.err.println("##### PipedConnection.read - bytes:" + nBytesToRead + " at:" + _in); 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir // loop while not all bytes read or when closed but there is still data 181*cdf0e10cSrcweir while(nBytesToRead > 0 && (_in != _out || !_closed)) { 182*cdf0e10cSrcweir while(_in == _out && !_closed) { 183*cdf0e10cSrcweir try { 184*cdf0e10cSrcweir notify(); // the buffer is empty, signal it 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir wait(__waitTime); // we wait for data or for the pipe to be closed 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir catch(InterruptedException interruptedException) { 189*cdf0e10cSrcweir throw new com.sun.star.io.IOException(interruptedException.toString()); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir if(_in < _out) { 194*cdf0e10cSrcweir int bytes = Math.min(nBytesToRead, _out - _in); 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir System.arraycopy(_buffer, _in, aReadBytes[0], aReadBytes[0].length - nBytesToRead, bytes); 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir nBytesToRead -= bytes; 199*cdf0e10cSrcweir _in += bytes; 200*cdf0e10cSrcweir } 201*cdf0e10cSrcweir else if(_in > _out) { 202*cdf0e10cSrcweir int bytes = Math.min(nBytesToRead, _buffer.length - _in); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir System.arraycopy(_buffer, _in, aReadBytes[0], aReadBytes[0].length - nBytesToRead, bytes); 205*cdf0e10cSrcweir 206*cdf0e10cSrcweir nBytesToRead -= bytes; 207*cdf0e10cSrcweir _in += bytes; 208*cdf0e10cSrcweir if(_in >= _buffer.length) 209*cdf0e10cSrcweir _in = 0; 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir if(nBytesToRead > 0) { // not all bytes read 214*cdf0e10cSrcweir byte tmp[] = new byte[aReadBytes[0].length - nBytesToRead]; 215*cdf0e10cSrcweir System.arraycopy(aReadBytes[0], 0, tmp, 0, tmp.length); 216*cdf0e10cSrcweir 217*cdf0e10cSrcweir aReadBytes[0] = tmp; 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir return aReadBytes[0].length; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir /** 224*cdf0e10cSrcweir * Write bytes. 225*cdf0e10cSrcweir * <p> 226*cdf0e10cSrcweir * @param aData the bytes to write 227*cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#write 228*cdf0e10cSrcweir */ 229*cdf0e10cSrcweir public void write(byte aData[]) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 230*cdf0e10cSrcweir _otherSide.receive(aData); 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir /** 234*cdf0e10cSrcweir * Flushes the buffer, notifies if necessary the other side that new data has arrived. 235*cdf0e10cSrcweir * <p> 236*cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#flush 237*cdf0e10cSrcweir */ 238*cdf0e10cSrcweir public void flush() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 239*cdf0e10cSrcweir synchronized(_otherSide) { 240*cdf0e10cSrcweir _otherSide.notify(); 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir /** 245*cdf0e10cSrcweir * Closes the pipe. 246*cdf0e10cSrcweir * <p> 247*cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#closed 248*cdf0e10cSrcweir */ 249*cdf0e10cSrcweir public synchronized void close() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException { 250*cdf0e10cSrcweir if(!_closed) { 251*cdf0e10cSrcweir _closed = true; 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir _otherSide.close(); 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir notify(); 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir /** 260*cdf0e10cSrcweir * Gives a description of this pipe. 261*cdf0e10cSrcweir * <p> 262*cdf0e10cSrcweir * @return the description 263*cdf0e10cSrcweir * @see com.sun.star.connections.XConnection#getDescription 264*cdf0e10cSrcweir */ 265*cdf0e10cSrcweir public String getDescription() throws com.sun.star.uno.RuntimeException { 266*cdf0e10cSrcweir return getClass().getName(); 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir 271