001    /*
002     * $Id: SocketChannel.java 2771 2009-08-05 20:10:49Z kredel $
003     */
004    
005    //package edu.unima.ky.parallel;
006    package edu.jas.util;
007    
008    
009    import java.io.IOException;
010    import java.io.ObjectOutputStream;
011    import java.io.ObjectInputStream;
012    import java.net.Socket;
013    
014    
015    /**
016     * SocketChannel provides a communication channel for Java objects using TCP/IP
017     * sockets. Refactored for java.util.concurrent.
018     * @author Akitoshi Yoshida
019     * @author Heinz Kredel.
020     */
021    public class SocketChannel {
022    
023    
024        /*
025         * Input stream from the socket.
026         */
027        private final ObjectInputStream in;
028    
029    
030        /*
031         * Output stream to the socket.
032         */
033        private final ObjectOutputStream out;
034    
035    
036        /*
037         * Underlying socket.
038         */
039        private final Socket soc;
040    
041    
042        /**
043         * Constructs a socket channel on the given socket s.
044         * @param s A socket object.
045         */
046        public SocketChannel(Socket s) throws IOException {
047            soc = s;
048            out = new ObjectOutputStream(s.getOutputStream());
049            out.flush();
050            in = new ObjectInputStream(s.getInputStream());
051        }
052    
053    
054        /**
055         * Get the Socket
056         */
057        public Socket getSocket() {
058            return soc;
059        }
060    
061    
062        /**
063         * Sends an object
064         */
065        public void send(Object v) throws IOException {
066            synchronized (out) {
067                out.writeObject(v);
068                out.flush();
069            }
070        }
071    
072    
073        /**
074         * Receives an object
075         */
076        public Object receive() throws IOException, ClassNotFoundException {
077            Object v = null;
078            synchronized (in) {
079                v = in.readObject();
080            }
081            return v;
082        }
083    
084    
085        /**
086         * Closes the channel.
087         */
088        public void close() {
089            if (in != null) {
090                try {
091                    in.close();
092                } catch (IOException e) {
093                }
094            }
095            if (out != null) {
096                try {
097                    out.close();
098                } catch (IOException e) {
099                }
100            }
101            if (soc != null) {
102                try {
103                    soc.close();
104                } catch (IOException e) {
105                }
106            }
107        }
108    
109    
110        /**
111         * to string
112         */
113        @Override
114        public String toString() {
115            return "socketChannel(" + soc + ")";
116        }
117    
118    }