1 5 package SOFA.SOFAnode.InOut.Impl; 6 7 import java.util.HashMap ; 8 9 import SOFA.Connector.ConnectorException; 10 import SOFA.Connector.Reference; 11 import SOFA.SOFAnode.InOut.Bundle; 12 import SOFA.SOFAnode.InOut.InOut2Client; 13 import SOFA.SOFAnode.InOut.InOut2InOut; 14 import SOFA.SOFAnode.InOut.InOutException; 15 import SOFA.SOFAnode.InOut.Connector.InOut2InOutConnector; 16 import SOFA.SOFAnode.TR.ComponentInfo; 17 import SOFA.SOFAnode.TR.TR2InOut; 18 import SOFA.SOFAnode.TR.Connector.TR2InOutConnector; 19 import SOFA.SOFAnode.TR.Impl.ComponentInfoImpl; 20 21 25 public class InOutImpl implements InOut2InOut, InOut2Client { 26 27 30 private TR2InOut tr; 31 32 36 private HashMap inOutConn; 37 38 42 public InOutImpl (Reference ref) { 43 try { 44 tr = (TR2InOut) TR2InOutConnector.createClt(ref); 45 } catch (ConnectorException e) { 46 e.printStackTrace(); 47 System.exit(-1); 48 } 49 inOutConn = new HashMap (); 50 } 51 52 61 public Bundle pullBundle (ComponentInfo[] descs, ComponentInfo[] comps, boolean inferiors) throws InOutException { 62 return tr.getBundle(descs, comps, inferiors); 63 } 64 65 70 public void pushBundle (Bundle bundle) throws InOutException { 71 tr.storeBundle(bundle); 72 } 73 74 81 private InOut2InOut getConnection (String sofaNode) { 82 InOut2InOut inOut; 83 inOut = (InOut2InOut) inOutConn.get(sofaNode); if (inOut == null) { try { 86 inOut = (InOut2InOut) InOut2InOutConnector.createClt(Reference.fromString(sofaNode)); } catch (ConnectorException e) { 88 throw (new InOutException("Could not connect to " + sofaNode + "\n", e)); 89 } 90 inOutConn.put(sofaNode, inOut); } 92 return inOut; 93 } 94 95 101 private void distributeComponent (Bundle bundle, String sofaNode) throws InOutException { 102 InOut2InOut inOut = getConnection(sofaNode); 103 inOut.pushBundle(bundle); 104 } 105 106 112 private ComponentInfoImpl[] fullNamesToComponentInfos (String [] fullNames) { 113 ComponentInfoImpl[] comps = null; 114 if (fullNames != null) { 115 comps = new ComponentInfoImpl[fullNames.length]; 116 for (int i = 0; i < comps.length; i++) { 117 comps[i] = ComponentInfoImpl.fromString(fullNames[i]); 118 } 119 } 120 return comps; 121 } 122 123 136 private void _obtain (String [] offers, String [] components, String sofaNode, boolean inferiors) throws InOutException { 137 if (sofaNode == null) { 138 throw new InOutException("The SOFA node where components are to be distributed from must not be null!"); 139 } 140 141 InOut2InOut inOut = getConnection(sofaNode); 142 143 ComponentInfoImpl[] descs = fullNamesToComponentInfos(offers); 144 ComponentInfoImpl[] comps = fullNamesToComponentInfos(components); 145 146 Bundle bundle = inOut.pullBundle(descs, comps, inferiors); 147 tr.storeBundle(bundle); 148 } 149 150 164 private void _distribute (String [] offers, String [] components, String [] sofaNodes, boolean inferiors) throws InOutException { 165 166 if (sofaNodes == null || sofaNodes.length == 0) { 167 throw new InOutException("The list of SOFA nodes where components are to be distributed to must not be null nor empty!"); 168 } 169 170 ComponentInfoImpl[] descs = fullNamesToComponentInfos(offers); 171 ComponentInfoImpl[] comps = fullNamesToComponentInfos(components); 172 173 Bundle bundle = tr.getBundle(descs, comps, inferiors); 174 for (int i = 0; i < sofaNodes.length; i++) { 175 String sofaNode = sofaNodes[i]; 176 distributeComponent(bundle, sofaNode); 177 } 178 } 179 180 191 public void obtain (String [] offers, String [] components, String sofaNode) throws InOutException { 192 _obtain(offers, components, sofaNode, false); 193 } 194 195 206 public void distribute (String [] offers, String [] components, String [] sofaNodes) throws InOutException { 207 _distribute(offers, components, sofaNodes, false); 208 } 209 210 218 public void obtainOffers (String [] offers, String sofaNode) throws InOutException { 219 _obtain(offers, null, sofaNode, true); 220 } 221 222 230 public void distributeOffers (String [] offers, String [] sofaNodes) throws InOutException { 231 _distribute(offers, null, sofaNodes, true); 232 } 233 234 242 public void obtainComponents (String [] components, String sofaNode) throws InOutException { 243 _obtain(null, components, sofaNode, true); 244 } 245 246 254 public void distributeComponents (String [] components, String [] sofaNodes) throws InOutException { 255 _distribute(null, components, sofaNodes, true); 256 } 257 258 262 public Bundle list () { 263 return tr.list(); 264 } 265 266 } 267 | Popular Tags |