00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00025 package cast.core;
00026
00027 import java.lang.reflect.Method;
00028 import java.util.Arrays;
00029 import java.util.HashMap;
00030 import java.util.Hashtable;
00031
00032 import Ice.Communicator;
00033 import Ice.Current;
00034 import Ice.Identity;
00035 import Ice.ObjectAdapter;
00036 import Ice.ObjectPrx;
00037 import cast.CASTException;
00038 import cast.ComponentCreationException;
00039 import cast.architecture.ManagedComponent;
00040 import cast.architecture.SubarchitectureTaskManager;
00041 import cast.architecture.SubarchitectureWorkingMemory;
00042 import cast.architecture.UnmanagedComponent;
00043 import cast.cdl.CASTRELEASESTRING;
00044 import cast.cdl.CASTTime;
00045 import cast.cdl.CPPSERVERPORT;
00046 import cast.cdl.ComponentLanguage;
00047 import cast.cdl.FilterRestriction;
00048 import cast.cdl.JAVASERVERPORT;
00049 import cast.cdl.PYTHONSERVERPORT;
00050 import cast.cdl.WorkingMemoryAddress;
00051 import cast.cdl.WorkingMemoryChange;
00052 import cast.cdl.WorkingMemoryChangeFilter;
00053 import cast.cdl.WorkingMemoryOperation;
00054 import cast.cdl.WorkingMemoryPermissions;
00055 import cast.interfaces.CASTComponentPrx;
00056 import cast.interfaces.CASTComponentPrxHelper;
00057 import cast.interfaces.ComponentFactoryPrx;
00058 import cast.interfaces.ComponentFactoryPrxHelper;
00059 import cast.interfaces.ComponentManagerPrx;
00060 import cast.interfaces.ManagedComponentPrx;
00061 import cast.interfaces.TaskManagerPrx;
00062 import cast.interfaces.TimeServerPrx;
00063 import cast.interfaces.TimeServerPrxHelper;
00064 import cast.interfaces.UnmanagedComponentPrx;
00065 import cast.interfaces.WorkingMemoryPrx;
00066 import cast.interfaces._CASTComponentTie;
00067 import cast.interfaces._ComponentManagerTie;
00068 import cast.interfaces._ManagedComponentTie;
00069 import cast.interfaces._TaskManagerTie;
00070 import cast.interfaces._UnmanagedComponentTie;
00071 import cast.interfaces._WorkingMemoryTie;
00072 import cast.server.CASTComponentManager;
00073
00077 public class CASTUtils {
00078
00079 private static HashMap<String, ComponentFactoryPrx> m_factories;
00080 private static Hashtable<Class<? extends Ice.Object>, String> m_typeNames;
00081
00082 static {
00083 m_factories = new HashMap<String, ComponentFactoryPrx>();
00084 m_typeNames = new Hashtable<Class<? extends Ice.Object>, String>();
00085 }
00086
00087 private static TimeServerPrx m_staticTimeServer;
00088
00096 boolean lessThan(CASTTime _ct1, CASTTime _ct2) {
00097 if (_ct1.s < _ct2.s) {
00098 return true;
00099 } else if (_ct1.s == _ct2.s) {
00100 return (_ct1.us < _ct2.us);
00101 } else {
00102 return false;
00103 }
00104 }
00105
00127 public static <Type extends Ice.Object, TypePrx extends Ice.ObjectPrx> TypePrx getCASTIceServer(
00128 String _componentID, String _componentHost,
00129 ComponentLanguage _componentLanguage, Class<Type> _cls,
00130 Class<TypePrx> _prxCls) throws CASTException {
00131 return getCASTIceServer(_componentID, _componentHost,
00132 _componentLanguage, _cls, _prxCls, Ice.Util.initialize());
00133 }
00134
00145 static <Type extends Ice.Object, TypePrx extends Ice.ObjectPrx> TypePrx getCASTIceServer(
00146 String _componentID, String _componentHost,
00147 ComponentLanguage _componentLanguage, Class<Type> _cls,
00148 Class<TypePrx> _prxCls, Communicator _communicator)
00149 throws CASTException {
00150
00151 int port = CASTUtils.languageToPort(_componentLanguage);
00152 ObjectPrx prx = CASTUtils.getIceServer(_componentID,
00153 CASTUtils.toServantCategory(_cls), _componentHost, port,
00154 _communicator);
00155
00156
00157 String helperClassName = _prxCls.getCanonicalName() + "Helper";
00158 Class<?> helperClass;
00159 try {
00160 helperClass = Class.forName(helperClassName);
00161 Method checkedCastMethod = helperClass.getMethod("checkedCast",
00162 Ice.ObjectPrx.class);
00163 TypePrx checkedPrx = _prxCls.cast(checkedCastMethod.invoke(null,
00164 prx));
00165 if (checkedPrx == null) {
00166 throw new CASTException("Unable to cast returned proxy to "
00167 + _prxCls);
00168 }
00169 return checkedPrx;
00170 } catch (Exception e) {
00171 CASTException ce = new CASTException(
00172 "Error processing interface class");
00173 ce.initCause(e);
00174 throw ce;
00175 }
00176 }
00177
00188 public static ObjectPrx getIceServer(String _componentID, String _category,
00189 String _host, int _port, Communicator _communicator) {
00190 Identity id = new Identity(_componentID, _category);
00191
00192 StringBuilder details = new StringBuilder(
00193 _communicator.identityToString(id));
00194 details.append(":default -h ");
00195 details.append(_host);
00196 details.append(" -p ");
00197 details.append(_port);
00198
00199 ObjectPrx prx = _communicator.stringToProxy(details.toString());
00200 return prx;
00201 }
00202
00209 public static final String concatenate(Object... _strings) {
00210 StringBuilder sb = new StringBuilder(_strings[0].toString());
00211 for (int i = 1; i < _strings.length; i++) {
00212 sb.append(_strings[i]);
00213 }
00214 return sb.toString();
00215 }
00216
00225 public static final <Type extends Ice.Object> String toServantCategory(
00226 Class<Type> _cls) {
00227
00228 return _cls.getCanonicalName();
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243 }
00244
00245 public static final TimeServerPrx getTimeServer() {
00246 if (m_staticTimeServer == null) {
00247 String host = "localhost";
00248 int port = CPPSERVERPORT.value;
00249 Communicator ic = Ice.Util.initialize();
00250 Identity id = new Identity("TimeServer", "TimeServer");
00251 ObjectPrx base = ic.stringToProxy(ic.identityToString(id)
00252 + ":default -h " + host + " -p " + port);
00253 m_staticTimeServer = TimeServerPrxHelper.checkedCast(base);
00254 }
00255 return m_staticTimeServer;
00256 }
00257
00258 private static ComponentFactoryPrx getComponentFactory(Communicator _ic,
00259 String _host, int _port) throws ComponentCreationException {
00260 String hash = _host + _port;
00261 ComponentFactoryPrx factory = m_factories.get(hash);
00262 if (factory == null) {
00263 Identity _id = new Identity("ComponentFactory", "ComponentFactory");
00264 ObjectPrx base = _ic.stringToProxy(_ic.identityToString(_id)
00265 + ":default -h " + _host + " -p " + _port);
00266 factory = ComponentFactoryPrxHelper.checkedCast(base);
00267
00268
00269 String factoryVersion = factory.getVersionString();
00270 if (!factoryVersion.equals(CASTRELEASESTRING.value)) {
00271 throw new ComponentCreationException(
00272 "CAST version mismatch. Client version string: \""
00273 + CASTRELEASESTRING.value
00274 + "\" but server reports \"" + factoryVersion
00275 + "\"");
00276 }
00277
00278 m_factories.put(hash, factory);
00279 }
00280 return factory;
00281 }
00282
00283 private static java.util.Hashtable<String, ComponentFactoryPrx> m_id2factory = new java.util.Hashtable<String, ComponentFactoryPrx>();
00284
00285 private static ComponentFactoryPrx getComponentFactoryForID(Identity _id,
00286 Communicator _ic, String _host, int _port)
00287 throws ComponentCreationException {
00288 ComponentFactoryPrx factory = getComponentFactory(_ic, _host, _port);
00289 m_id2factory.put(_id.name, factory);
00290 return factory;
00291 }
00292
00293 public static ComponentFactoryPrx getComponentFactoryForID(String _id) {
00294 return m_id2factory.get(_id);
00295 }
00296
00297 public static WorkingMemoryPrx createWorkingMemory(Identity _id,
00298 Communicator _ic, String _host, int _port)
00299 throws ComponentCreationException {
00300 ComponentFactoryPrx factory = getComponentFactoryForID(_id, _ic, _host,
00301 _port);
00302 logCreation(_id, _ic, _host, _port);
00303 return factory.newWorkingMemory(_id.name, _id.category, false);
00304 }
00305
00306 public static TaskManagerPrx createTaskManager(Identity _id,
00307 Communicator _ic, String _host, int _port)
00308 throws ComponentCreationException {
00309 ComponentFactoryPrx factory = getComponentFactoryForID(_id, _ic, _host,
00310 _port);
00311 logCreation(_id, _ic, _host, _port);
00312 return factory.newTaskManager(_id.name, _id.category, false);
00313 }
00314
00315 public static ManagedComponentPrx createManagedComponent(Identity _id,
00316 Communicator _ic, String _host, int _port)
00317 throws ComponentCreationException {
00318 ComponentFactoryPrx factory = getComponentFactoryForID(_id, _ic, _host,
00319 _port);
00320
00321 logCreation(_id, _ic, _host, _port);
00322 return factory.newManagedComponent(_id.name, _id.category, false);
00323 }
00324
00325 private static void logCreation(Identity _id, Communicator _ic,
00326 String _host, int _port) {
00327 System.out.println(concatenate("[creating ", _id.name, " at ", _host,
00328 ":", _port, "]"));
00329 }
00330
00331 public static UnmanagedComponentPrx createUnmanagedComponent(Identity _id,
00332 Communicator _ic, String _host, int _port)
00333 throws ComponentCreationException {
00334 ComponentFactoryPrx factory = getComponentFactoryForID(_id, _ic, _host,
00335 _port);
00336 logCreation(_id, _ic, _host, _port);
00337 return factory.newUnmanagedComponent(_id.name, _id.category, false);
00338 }
00339
00340 public static CASTComponentPrx createCASTComponent(Identity _id,
00341 Communicator _ic, String _host, int _port)
00342 throws ComponentCreationException {
00343 ComponentFactoryPrx factory = getComponentFactoryForID(_id, _ic, _host,
00344 _port);
00345 assert (factory != null);
00346 logCreation(_id, _ic, _host, _port);
00347 return factory.newComponent(_id.name, _id.category, false);
00348 }
00349
00350 public static CASTComponentPrx getComponent(Identity _id, Communicator _ic,
00351 String _host, int _port) throws CASTException {
00352 ObjectPrx base = _ic.stringToProxy(_ic.identityToString(_id)
00353 + ":default -h " + _host + " -p " + _port);
00354
00355 if (base == null) {
00356 throw new CASTException("Cannot create proxy");
00357 }
00358
00359 CASTComponentPrx prx = CASTComponentPrxHelper.checkedCast(base);
00360
00361 prx.ice_ping();
00362
00363
00364 assert (prx.getID().equals(_id.name));
00365
00366 return prx;
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00446 public static String toString(WorkingMemoryAddress _wma) {
00447 StringBuffer buf = new StringBuffer("[WMA id = ");
00448 buf.append(_wma.id);
00449 buf.append(" : sa = ");
00450 buf.append(_wma.subarchitecture);
00451 buf.append("]");
00452 return buf.toString();
00453 }
00454
00459 public static String toString(WorkingMemoryChange _wmc) {
00460 StringBuffer buf = new StringBuffer("[WMC ");
00461 buf.append(_wmc.src);
00462 buf.append(" ");
00463 buf.append(_wmc.type);
00464 buf.append(" ");
00465 buf.append(Arrays.toString(_wmc.superTypes));
00466 buf.append(" ");
00467 buf.append(toString(_wmc.address));
00468 buf.append(" ");
00469 buf.append(toString(_wmc.operation));
00470 buf.append("]");
00471 return buf.toString();
00472 }
00473
00478 public static String toString(WorkingMemoryChangeFilter _filter) {
00479 StringBuffer buf = new StringBuffer("[WMCF: ");
00480 buf.append(toString(_filter.operation));
00481 buf.append(" ");
00482 buf.append(_filter.src);
00483 buf.append(" ");
00484 buf.append(toString(_filter.address));
00485 buf.append(" ");
00486 buf.append(_filter.type);
00487 buf.append(" ");
00488 buf.append(toString(_filter.restriction));
00489 buf.append(" (");
00490 buf.append(_filter.origin);
00491 buf.append(")]");
00492 return buf.toString();
00493 }
00494
00495 public static String toString(FilterRestriction _restriction) {
00496 switch (_restriction) {
00497 case ALLSA:
00498 return "ALL_SA";
00499 case LOCALSA:
00500 return "LOCAL_SA";
00501 }
00502 throw new RuntimeException("Unknown enum value: " + _restriction);
00503
00504 }
00505
00506 public static String toString(WorkingMemoryOperation _op) {
00507 switch (_op) {
00508 case ADD:
00509 return "ADD";
00510 case OVERWRITE:
00511 return "OVERWRITE";
00512 case DELETE:
00513 return "DELETE";
00514 case GET:
00515 return "GET";
00516 default:
00517 return "UNKNOWN";
00518 }
00519 }
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00563 public static final <Type extends Ice.Object> String typeName(Type _obj) {
00564 return _obj.ice_id();
00565 }
00566
00574 public static final <Type extends Ice.Object> String typeName(
00575 Class<Type> _cls) {
00576 String typeName = m_typeNames.get(_cls);
00577 if (typeName == null) {
00578 try {
00579 typeName = typeName(_cls.newInstance());
00580 assert (typeName != null);
00581 m_typeNames.put(_cls, typeName);
00582 } catch (Exception e) {
00583 throw new RuntimeException("can't derive is type from class", e);
00584 }
00585 }
00586 return typeName;
00587 }
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00699 public final static boolean readAllowed(
00700 WorkingMemoryPermissions _permissions) {
00701 return _permissions == WorkingMemoryPermissions.UNLOCKED
00702 || _permissions == WorkingMemoryPermissions.LOCKEDO
00703 || _permissions == WorkingMemoryPermissions.LOCKEDOD;
00704 }
00705
00712 public final static boolean deleteAllowed(
00713 WorkingMemoryPermissions _permissions) {
00714 return _permissions == WorkingMemoryPermissions.UNLOCKED
00715 || _permissions == WorkingMemoryPermissions.LOCKEDO;
00716 }
00717
00724 public final static boolean overwriteAllowed(
00725 WorkingMemoryPermissions _permissions) {
00726 return _permissions == WorkingMemoryPermissions.UNLOCKED;
00727 }
00728
00729 public static Ice.Object createServant(Identity _id, ObjectAdapter _adapter)
00730 throws ClassNotFoundException, InstantiationException,
00731 IllegalAccessException {
00732
00733 Class<?> componentClass = Class.forName(_id.category);
00734
00735
00736
00737 CASTComponent component = (CASTComponent) componentClass.newInstance();
00738 assert (component != null);
00739 component.setID(_id.name, new Current());
00740 component.setIceIdentity(_id);
00741 component.setObjectAdapter(_adapter);
00742
00743
00744 if (ManagedComponent.class.isAssignableFrom(componentClass)) {
00745 _ManagedComponentTie tie = new _ManagedComponentTie();
00746 tie.ice_delegate(component);
00747 return tie;
00748 } else if (UnmanagedComponent.class.isAssignableFrom(componentClass)) {
00749 _UnmanagedComponentTie tie = new _UnmanagedComponentTie();
00750 tie.ice_delegate(component);
00751 return tie;
00752 } else if (SubarchitectureWorkingMemory.class
00753 .isAssignableFrom(componentClass)) {
00754 _WorkingMemoryTie tie = new _WorkingMemoryTie();
00755 tie.ice_delegate(component);
00756 return tie;
00757 } else if (SubarchitectureTaskManager.class
00758 .isAssignableFrom(componentClass)) {
00759 _TaskManagerTie tie = new _TaskManagerTie();
00760 tie.ice_delegate(component);
00761 return tie;
00762 } else if (CASTComponentManager.class.isAssignableFrom(componentClass)) {
00763 _ComponentManagerTie tie = new _ComponentManagerTie();
00764 tie.ice_delegate(component);
00765 return tie;
00766 } else {
00767 _CASTComponentTie tie = new _CASTComponentTie();
00768 tie.ice_delegate(component);
00769 return tie;
00770 }
00771
00772 }
00773
00774 public static String toString(CASTTime _time) {
00775 StringBuilder sb = new StringBuilder("[CASTTime: ");
00776 sb.append(_time.s);
00777 sb.append(":");
00778 sb.append(_time.us);
00779 sb.append("]");
00780 return sb.toString();
00781 }
00782
00783 public final static int languageToPort(ComponentLanguage _language) {
00784 if (_language == ComponentLanguage.CPP) {
00785 return CPPSERVERPORT.value;
00786 } else if (_language == ComponentLanguage.JAVA) {
00787 return JAVASERVERPORT.value;
00788 } else if (_language == ComponentLanguage.PYTHON) {
00789 return PYTHONSERVERPORT.value;
00790 } else {
00791 throw new RuntimeException("Unknown language: " + _language);
00792 }
00793 }
00794
00795 private static ComponentManagerPrx m_manager;
00796
00797 public static void setComponentManager(ComponentManagerPrx _man) {
00798 m_manager = _man;
00799 }
00800
00801 public static ComponentManagerPrx getComponentManager() {
00802
00803 return m_manager;
00804 }
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851 }