xref: /AOO41X/main/jurt/com/sun/star/lib/connections/socket/SocketConnection.java (revision 2be432768a66cc90838f6a32e76ec156f587e741)
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 package com.sun.star.lib.connections.socket;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.BufferedInputStream;
27cdf0e10cSrcweir import java.io.BufferedOutputStream;
28cdf0e10cSrcweir import java.io.InputStream;
29cdf0e10cSrcweir import java.io.IOException;
30cdf0e10cSrcweir import java.io.OutputStream;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir import java.net.Socket;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import java.util.Enumeration;
35cdf0e10cSrcweir import java.util.Vector;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir import com.sun.star.io.XStreamListener;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir import com.sun.star.connection.XConnection;
41cdf0e10cSrcweir import com.sun.star.connection.XConnectionBroadcaster;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir /**
44cdf0e10cSrcweir  * The SocketConnection implements the <code>XConnection</code> interface
45cdf0e10cSrcweir  * and is uses by the <code>SocketConnector</code> and the <code>SocketAcceptor</code>.
46cdf0e10cSrcweir  * This class is not part of the provided <code>api</code>.
47cdf0e10cSrcweir  * <p>
48cdf0e10cSrcweir  * @version     $Revision: 1.6 $ $ $Date: 2008-04-11 11:14:31 $
49cdf0e10cSrcweir  * @author      Kay Ramme
50cdf0e10cSrcweir  * @see         com.sun.star.comp.connections.SocketAcceptor
51cdf0e10cSrcweir  * @see         com.sun.star.comp.connections.SocketConnector
52cdf0e10cSrcweir  * @see         com.sun.star.connections.XConnection
53cdf0e10cSrcweir  * @since       UDK1.0
54cdf0e10cSrcweir  */
55cdf0e10cSrcweir public class SocketConnection implements XConnection, XConnectionBroadcaster {
56cdf0e10cSrcweir     /**
57cdf0e10cSrcweir      * When set to true, enables various debugging output.
58cdf0e10cSrcweir      */
59cdf0e10cSrcweir     static public final boolean DEBUG = false;
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     protected String       _description;
62cdf0e10cSrcweir     protected Socket       _socket;
63cdf0e10cSrcweir     protected InputStream  _inputStream;
64cdf0e10cSrcweir     protected OutputStream _outputStream;
65cdf0e10cSrcweir     protected Vector       _listeners;
66cdf0e10cSrcweir     protected boolean      _firstRead;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     /**
69cdf0e10cSrcweir      * Constructs a new <code>SocketConnection</code>.
70cdf0e10cSrcweir      * <p>
71cdf0e10cSrcweir      * @param  description   the description of the connection
72cdf0e10cSrcweir      * @param  socket        the socket of the connection
73cdf0e10cSrcweir      */
SocketConnection(String description, Socket socket)74cdf0e10cSrcweir     public SocketConnection(String description, Socket socket) throws IOException {
75cdf0e10cSrcweir         if (DEBUG) System.err.println("##### " + getClass().getName() + " - instantiated " + description + " " + socket);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         _description = description
78cdf0e10cSrcweir             + ",localHost=" + socket.getLocalAddress().getHostName()
79cdf0e10cSrcweir             + ",localPort=" + socket.getLocalPort()
80cdf0e10cSrcweir             + ",peerHost=" + socket.getInetAddress().getHostName()
81cdf0e10cSrcweir             + ",peerPort=" + socket.getPort();
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         _socket = socket;
84cdf0e10cSrcweir         _inputStream = new BufferedInputStream(socket.getInputStream());
85cdf0e10cSrcweir         _outputStream = new BufferedOutputStream(socket.getOutputStream());
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         _listeners = new Vector();
88cdf0e10cSrcweir         _firstRead = true;
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
addStreamListener(XStreamListener aListener )94cdf0e10cSrcweir     public void addStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException {
95cdf0e10cSrcweir         _listeners.addElement(aListener);
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
removeStreamListener(XStreamListener aListener )98cdf0e10cSrcweir     public void removeStreamListener(XStreamListener aListener ) throws com.sun.star.uno.RuntimeException {
99cdf0e10cSrcweir         _listeners.removeElement(aListener);
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir 
notifyListeners_open()102cdf0e10cSrcweir     private void notifyListeners_open() {
103cdf0e10cSrcweir         Enumeration elements = _listeners.elements();
104cdf0e10cSrcweir         while(elements.hasMoreElements()) {
105cdf0e10cSrcweir             XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
106cdf0e10cSrcweir             xStreamListener.started();
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
notifyListeners_close()110cdf0e10cSrcweir     private void notifyListeners_close() {
111cdf0e10cSrcweir         Enumeration elements = _listeners.elements();
112cdf0e10cSrcweir         while(elements.hasMoreElements()) {
113cdf0e10cSrcweir             XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
114cdf0e10cSrcweir             xStreamListener.closed();
115cdf0e10cSrcweir         }
116cdf0e10cSrcweir     }
117cdf0e10cSrcweir 
notifyListeners_error(com.sun.star.uno.Exception exception)118cdf0e10cSrcweir     private void notifyListeners_error(com.sun.star.uno.Exception exception) {
119cdf0e10cSrcweir         Enumeration elements = _listeners.elements();
120cdf0e10cSrcweir         while(elements.hasMoreElements()) {
121cdf0e10cSrcweir             XStreamListener xStreamListener = (XStreamListener)elements.nextElement();
122cdf0e10cSrcweir             xStreamListener.error(exception);
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     /**
128cdf0e10cSrcweir      * Read the required number of bytes.
129cdf0e10cSrcweir      * <p>
130cdf0e10cSrcweir      * @return   the number of bytes read
131cdf0e10cSrcweir      * @param    aReadBytes   the outparameter, where the bytes have to be placed
132cdf0e10cSrcweir      * @param    nBytesToRead the number of bytes to read
133cdf0e10cSrcweir      * @see       com.sun.star.connections.XConnection#read
134cdf0e10cSrcweir      */
read( byte[][] bytes, int nBytesToRead)135cdf0e10cSrcweir     public int read(/*OUT*/byte[][] bytes, int nBytesToRead) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
136cdf0e10cSrcweir         if(_firstRead) {
137cdf0e10cSrcweir             _firstRead = false;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir             notifyListeners_open();
140cdf0e10cSrcweir         }
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         String errMessage = null;
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         int read_bytes = 0;
145cdf0e10cSrcweir         bytes[0] = new byte[nBytesToRead];
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         try {
148cdf0e10cSrcweir             int count  ;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             do {
151cdf0e10cSrcweir                 count = _inputStream.read(bytes[0], read_bytes, nBytesToRead - read_bytes);
152cdf0e10cSrcweir                 if(count == -1)
153cdf0e10cSrcweir                     errMessage = "EOF reached - " + getDescription();
154cdf0e10cSrcweir 
155cdf0e10cSrcweir                 read_bytes += count;
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir             while(read_bytes >= 0 && read_bytes < nBytesToRead && count >= 0);
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir         catch(IOException ioException) {
160cdf0e10cSrcweir             if(DEBUG) {
161cdf0e10cSrcweir                 System.err.println("##### " + getClass().getName() + ".read - exception occurred:" + ioException);
162cdf0e10cSrcweir                 ioException.printStackTrace();
163cdf0e10cSrcweir             }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir             errMessage = ioException.toString();
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         if(errMessage != null) {
169cdf0e10cSrcweir             com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(errMessage);
170cdf0e10cSrcweir             notifyListeners_error(unoIOException);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir             throw unoIOException;
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         if (DEBUG) System.err.println("##### " + getClass().getName() + " - read byte:" + read_bytes + " " + bytes[0]);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir         return read_bytes;
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     /**
181cdf0e10cSrcweir      * Write bytes.
182cdf0e10cSrcweir      * <p>
183cdf0e10cSrcweir      * @param    aData the bytes to write
184cdf0e10cSrcweir      * @see       com.sun.star.connections.XConnection#write
185cdf0e10cSrcweir      */
write(byte aData[])186cdf0e10cSrcweir     public void write(byte aData[]) throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
187cdf0e10cSrcweir         try {
188cdf0e10cSrcweir             _outputStream.write(aData);
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir         catch(IOException ioException) {
191cdf0e10cSrcweir             com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(ioException.toString());
192cdf0e10cSrcweir             notifyListeners_error(unoIOException);
193cdf0e10cSrcweir 
194cdf0e10cSrcweir             throw unoIOException;
195cdf0e10cSrcweir         }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir         if (DEBUG) System.err.println("##### " + getClass().getName() + " - written bytes:" + aData + " " + aData.length);
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     /**
201cdf0e10cSrcweir      * Flushes the buffer.
202cdf0e10cSrcweir      * <p>
203cdf0e10cSrcweir      * @see       com.sun.star.connections.XConnection#flush
204cdf0e10cSrcweir      */
flush()205cdf0e10cSrcweir     public void flush() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
206cdf0e10cSrcweir         try {
207cdf0e10cSrcweir             _outputStream.flush();
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir         catch(IOException ioException) {
210cdf0e10cSrcweir             com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(ioException.toString());
211cdf0e10cSrcweir             notifyListeners_error(unoIOException);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir             throw unoIOException;
214cdf0e10cSrcweir         }
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir     /**
218cdf0e10cSrcweir      * Closes the connection.
219cdf0e10cSrcweir      * <p>
220cdf0e10cSrcweir      * @see       com.sun.star.connections.XConnection#close
221cdf0e10cSrcweir      */
close()222cdf0e10cSrcweir     public void close() throws com.sun.star.io.IOException, com.sun.star.uno.RuntimeException {
223cdf0e10cSrcweir         try {
224cdf0e10cSrcweir             _socket.close();
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir         catch(IOException ioException) {
227cdf0e10cSrcweir             com.sun.star.io.IOException unoIOException = new com.sun.star.io.IOException(ioException.toString());
228cdf0e10cSrcweir             notifyListeners_error(unoIOException);
229cdf0e10cSrcweir 
230cdf0e10cSrcweir             throw unoIOException;
231cdf0e10cSrcweir         }
232cdf0e10cSrcweir         if (DEBUG) System.err.println("##### " + getClass().getName() + " - socket closed");
233cdf0e10cSrcweir 
234cdf0e10cSrcweir         notifyListeners_close();
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir     /**
238cdf0e10cSrcweir      * Gives a description of the connection.
239cdf0e10cSrcweir      * <p>
240cdf0e10cSrcweir      * @return  the description
241cdf0e10cSrcweir      * @see       com.sun.star.connections.XConnection#getDescription
242cdf0e10cSrcweir      */
getDescription()243cdf0e10cSrcweir     public String getDescription() throws com.sun.star.uno.RuntimeException {
244cdf0e10cSrcweir         return _description;
245cdf0e10cSrcweir     }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir }
248cdf0e10cSrcweir 
249