001/* jpvmSendConnection.java
002 *
003 * The jpvmSendConnection class implements objects that represent
004 * connections to remote jpvm processes to which messages may be
005 * sent.
006 *
007 * Adam J Ferrari
008 * Sun 05-26-1996
009 *
010 * Copyright (C) 1996  Adam J Ferrari
011 * 
012 * This library is free software; you can redistribute it and/or
013 * modify it under the terms of the GNU Library General Public
014 * License as published by the Free Software Foundation; either
015 * version 2 of the License, or (at your option) any later version.
016 * 
017 * This library is distributed in the hope that it will be useful,
018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
020 * Library General Public License for more details.
021 * 
022 * You should have received a copy of the GNU Library General Public
023 * License along with this library; if not, write to the
024 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
025 * MA 02139, USA.
026 */
027
028package jpvm;
029
030import java.net.*;
031import java.io.*;
032import jpvm.jpvmTaskId;
033
034public
035class jpvmSendConnection {
036        public DataOutputStream strm;
037        public jpvmTaskId       tid;
038        
039        public jpvmSendConnection() {
040                strm = null;
041                tid   = null;
042        }
043
044        public jpvmSendConnection(Socket sock, jpvmTaskId t) {
045                if(sock==null || t==null) return;
046                tid = t;
047                try {
048                        OutputStream outstrm = sock.getOutputStream();
049                        outstrm = new BufferedOutputStream(outstrm);
050                        strm = new DataOutputStream(outstrm);
051                }
052                catch (IOException ioe) {
053                        strm = null;
054                        tid  = null;
055                        jpvmDebug.error("jpvmSendConnection, i/o exception");
056                }
057        }
058
059        public static jpvmSendConnection connect(jpvmTaskId t, jpvmTaskId f) 
060            throws jpvmException {
061                jpvmSendConnection ret = null;
062                try {
063                        jpvmDebug.note("jpvmSendConnection, "+ 
064                                "connecting to "+t.toString());
065
066                        // Make the new connection...
067                        Socket sock = new Socket(t.getHost(),t.getPort());
068                        ret = new jpvmSendConnection(sock,t);
069
070                        // Send my identity to the newly connected task...
071                        f.send(ret.strm);
072                        ret.strm.flush();
073                }
074                catch (IOException ioe) {
075                        jpvmDebug.note("jpvmSendConnection, connect - " +
076                                " i/o exception");
077                        throw new jpvmException("jpvmSendConnection, connect - "
078                                + " i/o exception: \"" + ioe + "\"");
079                }
080                return ret;
081        }
082};