summaryrefslogtreecommitdiff
path: root/ridljar/com/sun/star/comp/connections/PipedConnection.java
blob: 631cfae40ee0bdbb25e5707e96ed2792a3246f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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 .
 */

package com.sun.star.comp.connections;


import com.sun.star.comp.loader.FactoryHelper;
import com.sun.star.connection.XConnection;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.lang.XSingleServiceFactory;
import com.sun.star.registry.XRegistryKey;

/**
 * The <code>PipedConnection</code> is a component that implements the
 * <code>XConnection</code> Interface.
 * <p>It is useful for <code>Thread</code> communication in one Process.</p>
 *
 * @see         com.sun.star.connection.XConnection
 * @see         com.sun.star.comp.loader.JavaLoader
 * @since       UDK1.0
 */
public class PipedConnection implements XConnection {
    /**
     * When set to true, enables various debugging output.
     */
    public static final boolean DEBUG = false;

    /**
     * The name of the service, the <code>JavaLoader</code> accesses this through
     * reflection.
     */
    private static final String __serviceName = "com.sun.star.connection.PipedConnection";

    /**
     * Gives 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 desired.
     * @param   multiFactory the service manager to be uses if needed.
     * @param   regKey       the registryKey.
     * @return  returns a <code>XSingleServiceFactory</code> for creating the component.
     *
     * @see                  com.sun.star.comp.loader.JavaLoader
     */
    public static XSingleServiceFactory __getServiceFactory(String implName,
                                                          XMultiServiceFactory multiFactory,
                                                          XRegistryKey regKey)
    {
        XSingleServiceFactory xSingleServiceFactory = null;

        if (implName.equals(PipedConnection.class.getName()) )
            xSingleServiceFactory = FactoryHelper.getServiceFactory(PipedConnection.class,
                                                                    __serviceName,
                                                                    multiFactory,
                                                                    regKey);

        return xSingleServiceFactory;
    }

    /**
     * The amount of time in milliseconds, to wait to see check the buffers.
     */
    protected static final int __waitTime = 10000;

    protected byte             _buffer[] = new byte[4096];
    protected int              _in,
                               _out;
    protected boolean          _closed;
    protected PipedConnection  _otherSide;

    /**
     * Constructs a new <code>PipedConnection</code>, sees if there is another
     * side, which it should be connected to.
     *
     * @param    args   Another side could be in index 0.
     */
    public PipedConnection(Object args[]) throws com.sun.star.uno.RuntimeException {
        if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated");

        _otherSide = (args.length == 1) ? (PipedConnection)args[0] : null;
        if(_otherSide != null) {
            if(_otherSide == this)
                throw new RuntimeException("can not connect to myself");

            _otherSide._otherSide = this;
        }
    }

    /**
     * This is a private method, used to communicate internal in the pipe.
     */
    private synchronized void receive(byte aData[]) throws com.sun.star.io.IOException {
        int bytesWritten = 0;

        if(DEBUG) System.err.println("##### PipedConnection.receive - bytes:" + aData.length + " at:" + _out);

        while(bytesWritten < aData.length) {
            // wait until it is not full anymore
            while(_out == (_in - 1) || (_in == 0 && _out == _buffer.length - 1)) {
                try {
                    notify(); // the buffer is full, signal it

                    wait(__waitTime);
                }
                catch(InterruptedException interruptedException) {
                    throw new com.sun.star.io.IOException(interruptedException);
                }
            }

            if(_closed) throw new com.sun.star.io.IOException("connection has been closed");

            int bytes  ;

            if(_out < _in) {
                bytes = Math.min(aData.length - bytesWritten, _in - _out - 1);

                System.arraycopy(aData, bytesWritten, _buffer, _out, bytes);
            }
            else {
                if(_in > 0){
                    bytes = Math.min(aData.length - bytesWritten, _buffer.length - _out);
                }
                else {
                    bytes = Math.min(aData.length - bytesWritten, _buffer.length - _out - 1);
                }

                System.arraycopy(aData, bytesWritten, _buffer, _out, bytes);
            }

            bytesWritten += bytes;
            _out += bytes;
            if(_out >= _buffer.length)
                _out = 0;
        }
    }

    /**
     * Read the required number of bytes.
     *
     * @param   aReadBytes   the out parameter, where the bytes have to be placed.
     * @param   nBytesToRead the number of bytes to read.
     * @return  the number of bytes read.
     *
     * @see     com.sun.star.connection.XConnection#read
     */
    public synchronized int read(/*OUT*/byte[][] aReadBytes, int nBytesToRead) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
        aReadBytes[0] = new byte[nBytesToRead];

        if(DEBUG) System.err.println("##### PipedConnection.read - bytes:" + nBytesToRead + " at:" + _in);

        // loop while not all bytes read or when closed but there is still data
        while(nBytesToRead > 0 && (_in != _out || !_closed)) {
            while(_in == _out && !_closed) {
                try {
                    notify(); // the buffer is empty, signal it

                    wait(__waitTime); // we wait for data or for the pipe to be closed
                }
                catch(InterruptedException interruptedException) {
                    throw new com.sun.star.io.IOException(interruptedException);
                }
            }

            if(_in < _out) {
                int bytes = Math.min(nBytesToRead, _out - _in);

                System.arraycopy(_buffer, _in, aReadBytes[0], aReadBytes[0].length - nBytesToRead, bytes);

                nBytesToRead -= bytes;
                _in += bytes;
            }
            else if(_in > _out) {
                int bytes = Math.min(nBytesToRead, _buffer.length - _in);

                System.arraycopy(_buffer, _in, aReadBytes[0], aReadBytes[0].length - nBytesToRead, bytes);

                nBytesToRead -= bytes;
                _in += bytes;
                if(_in >= _buffer.length)
                    _in = 0;
            }
        }

        if(nBytesToRead > 0) { // not all bytes read
            byte tmp[] = new byte[aReadBytes[0].length - nBytesToRead];
            System.arraycopy(aReadBytes[0], 0, tmp, 0, tmp.length);

            aReadBytes[0] = tmp;
        }

        return aReadBytes[0].length;
    }

    /**
     * Write bytes.
     *
     * @param   aData the bytes to write.
     * @see     com.sun.star.connection.XConnection#write
     */
    public void write(byte aData[]) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
        _otherSide.receive(aData);
    }

    /**
     * Flushes the buffer, notifies if necessary the other side that new data has
     * arrived.
     *
     * @see     com.sun.star.connection.XConnection#flush
     */
    public void flush() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
        synchronized(_otherSide) {
            _otherSide.notify();
        }
    }

    /**
     * Closes the pipe.
     *
     * @see     com.sun.star.connection.XConnection#close()
     */
    public synchronized void close() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
        if(!_closed) {
            _closed = true;

            _otherSide.close();

            notify();
        }
    }

    /**
     * Gives a description of this pipe.
     *
     * @return  the description.
     * @see     com.sun.star.connection.XConnection#getDescription
     */
    public String getDescription() throws com.sun.star.uno.RuntimeException {
        return getClass().getName();
    }

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */