moved files out of trunk/ in prep to track this proj primarily on github
[javacyc.git] / UnixDomainSocket.java
blobd7849aa02e2d4398ca9b5f4077c46a5772a33403
1 // UnixDomainSocket.java
2 // J-BUDS version 1.0
3 // Copyright (c) 2001; Echogent Systems, Inc.
4 // See COPYRIGHT file for license details
6 // Modified on 05/29/2003 by Thomas Yan to replace deprecated thread code
8 import java.io.*;
10 /**
11 * This class provides a means of connecting to a unix domain socket server.
13 * @author Robert Morgan
16 public class UnixDomainSocket
18 static
20 // Load the Unix Domain Socket C library
21 System.out.println("Reading library file: " +
22 System.mapLibraryName("unixdomainsocket"));
23 System.loadLibrary("unixdomainsocket");
26 // Input and output streams
27 private UnixDomainSocketInputStream in;
28 private UnixDomainSocketOutputStream out;
30 // Socket read timeout
31 private int timeout;
33 // Native methods, implemented in the Unix Domain Socket C library
34 private native static int nativeOpen(String socketFile);
35 private native static int nativeRead(int nativeSocketFileHandle);
36 private native static int nativeWrite(int nativeSocketFileHandle, int data);
37 private native static void nativeClose(int nativeSocketFileHandle);
38 private native static void nativeCloseInput(int nativeSocketFileHandle);
39 private native static void nativeCloseOutput(int nativeSocketFileHandle);
41 // Handle for the native Unix Domain Socket
42 private int nativeSocketFileHandle;
44 /**
45 * Creates a unix domain socket and connects it to the server specified by the socket file.
47 * @param socketFile Name of the socket file
49 * @throws IOException If unable to construct the socket
52 public UnixDomainSocket(String socketFile)
53 throws IOException
55 // Create the native socket, and connect using the specified socket file
56 if( (nativeSocketFileHandle = nativeOpen(socketFile)) < 0)
58 throw new IOException("Unable to open Unix Domain Socket");
61 // Initialise the socket input and output streams
62 in = new UnixDomainSocketInputStream();
63 out = new UnixDomainSocketOutputStream();
66 /**
67 * Returns an input stream for this socket.
69 * @return An input stream for reading bytes from this socket
72 public InputStream getInputStream()
74 return (InputStream)in;
77 /**
78 * Returns an output stream for this socket.
80 * @return An output stream for writing bytes to this socket
83 public OutputStream getOutputStream()
85 return (OutputStream)out;
88 /**
89 * Sets the read timeout for the socket. If a read call blocks for the specified amount of time
90 * it will be cancelled, and a java.io.InterruptedIOException will be thrown. A timeout of zero
91 * is interpreted as an infinite timeout.
93 * @param timeout The specified timeout, in milliseconds.
95 public void setTimeout(int timeout)
97 this.timeout = timeout;
101 * Closes the socket.
103 public void close()
105 nativeClose(nativeSocketFileHandle);
108 private class UnixDomainSocketInputStream extends InputStream
110 // Reads a byte of data from the socket input stream
111 public int read()
112 throws IOException
114 int data;
116 // If a timeout is set, then use a read thread
117 if(timeout>0)
119 // Create a thread to read the byte
120 UnixDomainSocketReadThread thread = new UnixDomainSocketReadThread();
121 thread.setDaemon(true);
122 thread.start();
126 // Wait up until the specified timeout for the thread to complete
127 thread.join(timeout);
129 catch(InterruptedException e)
132 // If the thread is still alive, then the read() call has blocked longer than
133 // the specified timeout
134 if(thread.isAlive())
136 //thread.stop(); This has been deprecated!
137 //Just leave thread alone until OS timeout occurs
138 throw new InterruptedIOException("Unix Domain Socket read() call timed out");
140 else
142 data = thread.getData();
146 else
148 data = nativeRead(nativeSocketFileHandle);
151 return data;
155 // Closes the socket input stream
156 public void close()
157 throws IOException
159 nativeCloseInput(nativeSocketFileHandle);
163 private class UnixDomainSocketOutputStream extends OutputStream
165 // Write a byte of data to the socket output stream
166 public void write(int data)
167 throws IOException
169 if((nativeWrite(nativeSocketFileHandle, data))<0)
171 throw new IOException("Unable to write to Unix Domain Socket");
175 // Closes the socket output stream
176 public void close()
177 throws IOException
179 nativeCloseOutput(nativeSocketFileHandle);
183 // Thread class reads a byte of data from the socket. Used for enforcing timeouts.
184 private class UnixDomainSocketReadThread extends Thread
186 private int data;
188 public void run()
190 data = nativeRead(nativeSocketFileHandle);
193 public int getData()
195 return data;