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