1 6 7 package SOFA.SOFAnet.Transport.RMI; 8 9 import java.rmi.server.UnicastRemoteObject ; 10 import java.rmi.server.ServerNotActiveException ; 11 import java.rmi.RemoteException ; 12 import SOFA.SOFAnet.Core.NetOps; 13 import SOFA.SOFAnet.Core.ShareOps; 14 import SOFA.SOFAnet.Core.CoreException; 15 import SOFA.SOFAnet.Repository.NodeInfo; 16 import SOFA.SOFAnet.Transport.*; 17 18 25 public class RMITransportServer extends UnicastRemoteObject implements RMITransportInterface 26 { 27 private NetOps netOps; 28 private ShareOps shareOps; 29 30 31 public RMITransportServer(NetOps netOps, ShareOps shareOps) throws RemoteException  32 { 33 this.netOps = netOps; 34 this.shareOps = shareOps; 35 } 36 37 public boolean testPush(String nodeName, String bundleName, boolean offer) throws RMITransportException, RemoteException  38 { 39 testNodeName(nodeName); 40 41 boolean result; 42 try 43 { 44 IOParams ioParams = new IOParams(); 45 46 ioParams.setBundleName(bundleName); 47 ioParams.setOffer(offer); 48 ioParams.setSourceNodeName(nodeName); 49 50 result = netOps.isPushAcceptable(ioParams); 51 } 52 catch (CoreException e) 53 { 54 throw new RMITransportException(e.getMessage(), e); 55 } 56 57 return result; 58 } 59 60 public void push(String nodeName, String bundleName, byte[] bundle, LicenceRMI licenceRMI, boolean offer) throws RMITransportException, RemoteException  61 { 62 testNodeName(nodeName); 63 64 try 65 { 66 IOParams ioParams = new IOParams(); 67 68 ioParams.setBundleName(bundleName); 69 ioParams.setBundleData(new BundleData(bundle)); 70 ioParams.setOffer(offer); 71 ioParams.setSourceNodeName(nodeName); 72 if (licenceRMI == null) ioParams.setLicence(null); 73 else ioParams.setLicence(licenceRMI.getLicence()); 74 75 netOps.deliverBundlePush(ioParams); 76 } 77 catch (CoreException e) 78 { 79 throw new RMITransportException(e.getMessage(), e); 80 } 81 } 82 83 public boolean testPull(String nodeName, String bundleName, boolean sharing, String contractID) throws RMITransportException, RemoteException  84 { 85 testNodeName(nodeName); 86 87 boolean result; 88 try 89 { 90 IOParams ioParams = new IOParams(); 91 92 ioParams.setBundleName(bundleName); 93 ioParams.setSourceNodeName(nodeName); 94 95 if (sharing) result = shareOps.isAcquisitionAcceptable(ioParams); 96 else 97 { 98 ioParams.setContractID(contractID); 99 result = netOps.isPullAcceptable(ioParams); 100 } 101 } 102 catch (CoreException e) 103 { 104 throw new RMITransportException(e.getMessage(), e); 105 } 106 107 return result; 108 } 109 110 public RMIPullOutputHolder pull(String nodeName, String bundleName, boolean sharing, boolean licenceOnly, String contractID) throws RMITransportException, RemoteException  111 { 112 testNodeName(nodeName); 113 114 try 115 { 116 IOParams ioParams = new IOParams(); 117 118 ioParams.setBundleName(bundleName); 119 ioParams.setSourceNodeName(nodeName); 120 121 if (sharing) 122 { 123 ioParams.setLicenceOnly(licenceOnly); 124 shareOps.prepareBundleForAcquisition(ioParams); 125 } 126 else 127 { 128 ioParams.setContractID(contractID); 129 netOps.preparePulledBundle(ioParams); 130 } 131 132 RMIPullOutputHolder outputHolder = new RMIPullOutputHolder(); 133 outputHolder.errCode = ioParams.getErrCode(); 134 if (outputHolder.errCode == 0) 135 { 136 if (ioParams.isAddress() && ioParams.getAddressNodeName() != null && ioParams.getAddressNodeName().length() != 0) 137 { 138 outputHolder.address = ioParams.getAddressNodeName(); 139 } 140 else 141 { 142 BundleData bundleData = ioParams.getBundleData(); 143 if (bundleData != null) 144 { 145 outputHolder.bundle = bundleData.getData(); 146 bundleData.delete(); 147 if (outputHolder.bundle == null) throw new RMITransportException("I/O error when copying bundle data"); 148 } 149 else outputHolder.bundle = null; 150 } 151 152 if (ioParams.getLicence() == null) outputHolder.licenceRMI = null; 153 else outputHolder.licenceRMI = new LicenceRMI(ioParams.getLicence()); 154 } 155 156 return outputHolder; 157 } 158 catch (CoreException e) 159 { 160 throw new RMITransportException(e.getMessage(), e); 161 } 162 } 163 164 public boolean canReturnShared(String nodeName, String bundleName) throws RMITransportException, RemoteException  165 { 166 testNodeName(nodeName); 167 168 boolean result; 169 try 170 { 171 IOParams ioParams = new IOParams(); 172 173 ioParams.setBundleName(bundleName); 174 ioParams.setSourceNodeName(nodeName); 175 176 result = shareOps.canReturnSharedBundle(ioParams); 177 } 178 catch (CoreException e) 179 { 180 throw new RMITransportException(e.getMessage(), e); 181 } 182 183 return result; 184 } 185 186 public int returnShared(String nodeName, String bundleName, boolean address, String addressNodeName) throws RMITransportException, RemoteException  187 { 188 testNodeName(nodeName); 189 190 int result; 191 try 192 { 193 IOParams ioParams = new IOParams(); 194 195 ioParams.setBundleName(bundleName); 196 ioParams.setSourceNodeName(nodeName); 197 ioParams.setAddress(address); 198 ioParams.setAddressNodeName(addressNodeName); 199 200 result = shareOps.returnSharedBundle(ioParams); 201 } 202 catch (CoreException e) 203 { 204 throw new RMITransportException(e.getMessage(), e); 205 } 206 207 return result; 208 } 209 210 public int manualReturnShared(String nodeName, String bundleName) throws RMITransportException, RemoteException  211 { 212 testNodeName(nodeName); 213 214 int result; 215 try 216 { 217 IOParams ioParams = new IOParams(); 218 219 ioParams.setBundleName(bundleName); 220 ioParams.setSourceNodeName(nodeName); 221 222 result = shareOps.performManualReturnSharedBundle(ioParams); 223 } 224 catch (CoreException e) 225 { 226 throw new RMITransportException(e.getMessage(), e); 227 } 228 229 return result; 230 } 231 232 233 private NodeInfo testNodeName(String nodeName) throws RMITransportException 234 { 235 NodeInfo nodeInfo = new NodeInfo(); 236 try 237 { 238 nodeInfo.setNodeName(nodeName); 239 } 240 catch (NodeInfo.InvalidNodeNameException e) 241 { 242 throw new RMITransportException("Invalid name of SOFA node: " + nodeName, e); 243 } 244 245 String realHost = ""; 246 247 try 248 { 249 realHost = getClientHost(); 250 } 251 catch (ServerNotActiveException e) 252 { 253 throw new RMITransportException(e.getMessage(), e); 254 } 255 256 boolean ok = false; 257 try 258 { 259 ok = nodeInfo.isSameHost(realHost); 260 } 261 catch (NodeInfo.InvalidNodeNameException e) 262 { 263 throw new RMITransportException("Invalid RMI client host: " + nodeName, e); 264 } 265 266 if (!ok) throw new RMITransportException("Name of SOFA node (" + nodeName + ") and RMI client host (" + realHost + ") are incosistent"); 267 268 return nodeInfo; 269 } 270 } 271 | Popular Tags |