Connection.java Source File (original) (raw)

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 package org.hebe.mps; 00036 00037 import java.io.; 00038 import java.net.; 00039 import java.util.*; 00040 import org.hebe.mps.naming.Binding; 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 class Connection { 00054 00055 private String resolvedName; 00056 00057 00058 private Socket connection; 00059 00060 00061 private String method; 00062 00063 private String hostname; 00064 00065 private int portnumber; 00066 00067 private int oid; 00068 00069 00070 00071 00072 00073 00074 00075 00076 public Connection(String name) throws MPSException { 00077 resolvedName = name; 00078 connection = null; 00079 parseResolvedName(); 00080 } 00081 00082 00083 00084 00085 00086 00087 00088 public String toString() { 00089 return "MPSConnection(" + resolvedName + ")"; 00090 } 00091 00092 00093 00094 00095 00096 00097 private void parseResolvedName() throws MPSException { 00098 try { 00099 StringTokenizer st = new StringTokenizer(resolvedName, ":"); 00100 st.nextToken();
00101 method = st.nextToken(); 00102 hostname = st.nextToken(); 00103 portnumber = Integer.parseInt(st.nextToken()); 00104 oid = Integer.parseInt(st.nextToken()); 00105 } catch (Exception e) { 00106 throw new MPSException("Invalid object name " + toString()); 00107 } 00108 00109 if (!method.equals("inet")) { 00110 throw new MPSException("Invalid transport name " + method); 00111 } 00112 } 00113 00114 00115 00116 00117 00118 00119 00120 00121 public void open() throws MPSException { 00122 if (connection != null) 00123 return; 00124 00125 parseResolvedName(); 00126 00127 try { 00128 connection = new Socket(hostname, portnumber); 00129 } catch (Exception e) { 00130 throw new MPSException("Could not connect to object " + toString()); 00131 } 00132 } 00133 00134 00135 00136 00137 00138 00139 public void close() throws MPSException { 00140 try { 00141 connection.close(); 00142 } catch (IOException e) { 00143 throw new MPSException("Error closing connection " + toString()); 00144 } finally { 00145 connection = null; 00146 } 00147 } 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 public java.io.InputStream getInputStream() throws MPSException { 00158 try { 00159 open(); 00160 return connection.getInputStream(); 00161 } catch (IOException e) { 00162 throw new MPSException("Could not get input stream for " + toString()); 00163 } 00164 } 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 public java.io.OutputStream getOutputStream() throws MPSException { 00175 try { 00176 open(); 00177 return connection.getOutputStream(); 00178 } catch (IOException e) { 00179 throw new MPSException("Could not get output stream for " + toString()); 00180 } 00181 } 00182 00183 00184 00185 00186 00187 public String getResolvedName() { 00188 return resolvedName; 00189 } 00190 00191 00192 00193 00194 00195 int getOid() { 00196 return oid; 00197 } 00198 }