001/* jpvmTaskId.java
002 *
003 * Implements the unique jpvm task identifier class.
004 *
005 * Adam J Ferrari
006 * Sat 05-25-1996
007 *
008 * Copyright (C) 1996  Adam J Ferrari
009 * 
010 * This library is free software; you can redistribute it and/or
011 * modify it under the terms of the GNU Library General Public
012 * License as published by the Free Software Foundation; either
013 * version 2 of the License, or (at your option) any later version.
014 * 
015 * This library is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018 * Library General Public License for more details.
019 * 
020 * You should have received a copy of the GNU Library General Public
021 * License along with this library; if not, write to the
022 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
023 * MA 02139, USA.
024 */
025
026package jpvm;
027import java.net.*;
028import java.io.*;
029
030public
031class jpvmTaskId implements Serializable {
032        private String          taskHost;
033        private int             taskConnectPort;
034
035        public jpvmTaskId() {
036                taskHost        = null;
037                taskConnectPort = 0;
038        };
039
040        public jpvmTaskId(int my_port) {
041                taskHost        = null;
042                taskConnectPort = 0;
043                try {
044                        InetAddress taskAddr = InetAddress.getLocalHost();
045                        taskHost = taskAddr.getHostName();
046                        taskConnectPort = my_port;
047                }
048                catch (UnknownHostException uhe) {
049                        jpvmDebug.error("jpvmTaskId, unknown host exception");
050                }
051        }
052
053        public jpvmTaskId(String host, int port) {
054                taskHost        = new String(host);
055                taskConnectPort = port;
056        }
057
058        public String getHost() {
059                return taskHost;
060        }
061
062        public int getPort() {
063                return taskConnectPort;
064        }
065
066        public String toString() {
067                return ((taskHost!=null ? taskHost : "(null)") +
068                        ", port "+taskConnectPort);
069        }
070
071        public boolean equals(jpvmTaskId tid) {
072                if(tid == null)
073                        return false;
074                if(taskConnectPort != tid.taskConnectPort)
075                        return false;
076                if(tid.taskHost==null)
077                        return false;
078                boolean ret = tid.taskHost.equalsIgnoreCase(taskHost);
079                return ret;
080        }
081
082        public void send(DataOutputStream strm) throws jpvmException {
083            int i;
084            try {
085                int len = 0;
086                if(taskHost!=null) {
087                        len = taskHost.length();
088                        strm.writeInt(len);
089                        char hname[] = new char[len];
090                        taskHost.getChars(0,len,hname,0);
091                        for(i=0;i<len;i++) {
092                                strm.writeChar(hname[i]);
093                        }
094                }
095                else {
096                        strm.writeInt(len);
097                }
098                strm.writeInt(taskConnectPort);
099            }
100            catch (IOException ioe) {
101                jpvmDebug.note("jpvmTaskId, send - i/o exception");
102                throw new jpvmException("jpvmTaskId, send - i/o exception");
103            }
104        }
105
106        public void recv(DataInputStream strm) throws jpvmException {
107            int i;
108            try {
109                int len = strm.readInt();
110                if(len>0) {
111                        char hname[] = new char[len];
112                        for(i=0;i<len;i++) {
113                                hname[i] = strm.readChar();
114                        }
115                        taskHost = new String(hname);
116                }
117                else {
118                        taskHost = null;
119                }
120                taskConnectPort = strm.readInt();
121            }
122            catch (IOException ioe) {
123                jpvmDebug.note("jpvmTaskId, recv - i/o exception");
124                throw new jpvmException("jpvmTaskId, recv - i/o exception");
125            }
126        }
127};