| 1 | package dmg.cells.nucleus ; |
| 2 | |
| 3 | import dmg.util.*; |
| 4 | import java.io.Serializable ; |
| 5 | |
| 6 | /** |
| 7 | * |
| 8 | * |
| 9 | * @author Patrick Fuhrmann |
| 10 | * @version 0.1, 15 Feb 1998 |
| 11 | */ |
| 12 | /* |
| 13 | * route add -default <cell>[@<domain>] |
| 14 | * route add -domain <domain> <cell>[@<domain>] |
| 15 | * WARNING : This Class is designed to be imutual. |
| 16 | * All other class rely on that fact and |
| 17 | * a lot of things may fail at runtime |
| 18 | * if this design item is changed. |
| 19 | */ |
| 20 | public class CellRoute implements Cloneable , Serializable { |
| 21 | static final long serialVersionUID = 4566260400288960984L; |
| 22 | private String _destCell ; |
| 23 | private String _destDomain ; |
| 24 | private String _gateway ; |
| 25 | private int _type ; |
| 26 | |
| 27 | public static final int AUTO = 0 ; |
| 28 | public static final int EXACT = 1 ; |
| 29 | public static final int WELLKNOWN = 2 ; |
| 30 | public static final int DOMAIN = 3 ; |
| 31 | public static final int DEFAULT = 4 ; |
| 32 | public static final int DUMPSTER = 5 ; |
| 33 | public static final int ALIAS = 6 ; |
| 34 | |
| 35 | private final static String [] __typeNames = |
| 36 | { "Auto" , "Exact" , "Wellknown" , "Domain" , |
| 37 | "Default" , "Dumpster" , "Alias" } ; |
| 38 | |
| 39 | public CellRoute(){} |
| 40 | public CellRoute( Args args ) |
| 41 | throws IllegalArgumentException { |
| 42 | |
| 43 | String opt = args.optc() == 0 ? "-auto" : args.optv(0) ; |
| 44 | int type ; |
| 45 | |
| 46 | if( args.argc() == 0 ) |
| 47 | throw new IllegalArgumentException("Not enough arguments" ) ; |
| 48 | |
| 49 | type = AUTO ; |
| 50 | if( opt.equals( "-auto") ) type = AUTO ; |
| 51 | else if( opt.equals( "-domain" ))type = DOMAIN ; |
| 52 | else if( opt.equals( "-wellknown" ))type = WELLKNOWN ; |
| 53 | else if( opt.equals( "-exact" ))type = EXACT ; |
| 54 | else if( opt.equals( "-default" ))type = DEFAULT ; |
| 55 | else if( opt.equals( "-dumpster" ))type = DUMPSTER ; |
| 56 | else if( opt.equals( "-alias" ))type = ALIAS ; |
| 57 | |
| 58 | if( args.argc() == 1 ){ |
| 59 | if( ( type == DEFAULT ) || ( type == DUMPSTER ) ){ |
| 60 | _CellRoute( null , args.argv(0) , type ) ; |
| 61 | }else{ |
| 62 | throw new IllegalArgumentException("Not enough arguments" ) ; |
| 63 | } |
| 64 | }else if( args.argc() == 2 ){ |
| 65 | if( ( type == DEFAULT ) || ( type == DUMPSTER ) ){ |
| 66 | throw new IllegalArgumentException("Too many arguments" ) ; |
| 67 | }else{ |
| 68 | _CellRoute( args.argv(0) , args.argv(1) , type ) ; |
| 69 | } |
| 70 | }else |
| 71 | throw new IllegalArgumentException("Too many arguments" ) ; |
| 72 | |
| 73 | } |
| 74 | public CellRoute( String dest , String gateway , int type ) |
| 75 | throws IllegalArgumentException { |
| 76 | |
| 77 | _CellRoute( dest , gateway , type ) ; |
| 78 | } |
| 79 | public CellRoute( String dest , String gateway , String type ) |
| 80 | throws IllegalArgumentException { |
| 81 | |
| 82 | int i ; |
| 83 | for( i = 0 ; |
| 84 | ( i < __typeNames.length ) && |
| 85 | ( ! __typeNames[i].equals( type ) ) ; i++ ) ; |
| 86 | if( ( i == 0 ) || ( i == __typeNames.length ) ) |
| 87 | throw new IllegalArgumentException("Illegal Route Type "+type ) ; |
| 88 | _CellRoute( dest , gateway , i ) ; |
| 89 | } |
| 90 | public void _CellRoute( String dest , String gateway , int type ) |
| 91 | throws IllegalArgumentException { |
| 92 | splitDestination( dest ) ; |
| 93 | _gateway = gateway ; |
| 94 | _type = type ; |
| 95 | switch( _type ){ |
| 96 | case EXACT : |
| 97 | case ALIAS : |
| 98 | if( _destCell == null ) |
| 99 | throw new IllegalArgumentException("No destination cell spec." ) ; |
| 100 | if( _destDomain == null )_destDomain = "local" ; |
| 101 | break ; |
| 102 | case WELLKNOWN : |
| 103 | if( _destCell == null ) |
| 104 | throw new IllegalArgumentException("No destination cell spec." ) ; |
| 105 | if( _destDomain != null ) |
| 106 | throw new IllegalArgumentException("WELLKNOWN doesn't accept domain" ) ; |
| 107 | _destDomain = "*" ; |
| 108 | break ; |
| 109 | case DOMAIN : |
| 110 | if( _destDomain != null ) |
| 111 | throw new IllegalArgumentException("DOMAIN doesn't accept cell" ) ; |
| 112 | if( _destCell == null ) |
| 113 | throw new IllegalArgumentException("No destination domain spec." ) ; |
| 114 | _destDomain = _destCell ; |
| 115 | _destCell = "*" ; |
| 116 | break ; |
| 117 | case DUMPSTER : |
| 118 | if( _destCell != null ) |
| 119 | throw new IllegalArgumentException("DUMPSTER doesn't accept cell" ) ; |
| 120 | if( _destDomain != null ) |
| 121 | throw new IllegalArgumentException("DUMPSTER doesn't accept domain" ) ; |
| 122 | _destDomain = "*" ; |
| 123 | _destCell = "*" ; |
| 124 | break ; |
| 125 | case DEFAULT : |
| 126 | if( _destCell != null ) |
| 127 | throw new IllegalArgumentException("DEFAULT doesn't accept cell" ) ; |
| 128 | if( _destDomain != null ) |
| 129 | throw new IllegalArgumentException("DEFAULT doesn't accept domain" ) ; |
| 130 | _destDomain = "*" ; |
| 131 | _destCell = "*" ; |
| 132 | break ; |
| 133 | case AUTO : |
| 134 | if( ( _destCell != null ) && ( _destDomain != null ) ){ |
| 135 | if( _destCell.equals("*") && _destDomain.equals("*") ){ |
| 136 | _type = DEFAULT ; |
| 137 | }else if( _destCell.equals("*") ){ |
| 138 | _type = DOMAIN ; |
| 139 | }else if( _destDomain.equals("*") ){ |
| 140 | _type = WELLKNOWN ; |
| 141 | }else{ |
| 142 | _type = EXACT ; |
| 143 | } |
| 144 | }else if( _destCell == null ){ |
| 145 | _destCell = "*" ; |
| 146 | _type = DOMAIN ; |
| 147 | }else if( _destDomain == null ){ |
| 148 | _destDomain = "*" ; |
| 149 | _type = WELLKNOWN ; |
| 150 | }else{ |
| 151 | _destCell = "*" ; |
| 152 | _destDomain = "*" ; |
| 153 | _type = DEFAULT ; |
| 154 | } |
| 155 | break ; |
| 156 | default : |
| 157 | throw new IllegalArgumentException( "Unknown Route type" ) ; |
| 158 | |
| 159 | } |
| 160 | } |
| 161 | public String getCellName(){ return _destCell ; } |
| 162 | public String getDomainName(){ return _destDomain ; } |
| 163 | public String getTargetName(){ return _gateway ; } |
| 164 | |
| 165 | public int getRouteType(){ return _type ; } |
| 166 | public CellAddressCore getTarget(){ |
| 167 | |
| 168 | return new CellAddressCore( _gateway ) ; |
| 169 | } |
| 170 | public String getRouteTypeName(){ |
| 171 | return __typeNames[_type] ; |
| 172 | } |
| 173 | private void splitDestination( String dest ){ |
| 174 | if( ( dest == null ) || ( dest.equals("") ) ){ |
| 175 | _destCell = null ; |
| 176 | _destDomain = null ; |
| 177 | return ; |
| 178 | } |
| 179 | int ind = dest.indexOf( '@' ) ; |
| 180 | if( ind < 0 ){ |
| 181 | _destCell = dest ; |
| 182 | _destDomain = null ; |
| 183 | }else{ |
| 184 | _destCell = dest.substring( 0 , ind ) ; |
| 185 | if( ind == ( dest.length() -1 ) ) |
| 186 | _destDomain = null ; |
| 187 | else |
| 188 | _destDomain = dest.substring( ind+1 ) ; |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | public int hashCode(){ |
| 193 | return (_destCell+_destDomain+_gateway).hashCode() ; |
| 194 | } |
| 195 | public boolean equals( Object x ){ |
| 196 | |
| 197 | if( !(x instanceof CellRoute) ) return false; |
| 198 | |
| 199 | CellRoute route = (CellRoute)x ; |
| 200 | return ( route._destCell.equals( _destCell ) ) && |
| 201 | ( route._destDomain.equals( _destDomain ) ) ; |
| 202 | } |
| 203 | private static final int _destLength = 15 ; |
| 204 | private static final int _domainLength = 15 ; |
| 205 | private static final int _gateLength = 25 ; |
| 206 | public static String headerToString(){ |
| 207 | return Formats.field( "Dest Cell" , _destLength , Formats.CENTER ) + |
| 208 | Formats.field( "Dest Domain" , _domainLength , Formats.CENTER ) + |
| 209 | Formats.field( "Gateway" , _gateLength , Formats.CENTER ) + |
| 210 | Formats.field( "Type" , 10 , Formats.CENTER ); |
| 211 | } |
| 212 | public String toString(){ |
| 213 | return Formats.field( _destCell , _destLength , Formats.CENTER ) + |
| 214 | Formats.field( _destDomain , _domainLength , Formats.CENTER ) + |
| 215 | Formats.field( _gateway , _gateLength , Formats.CENTER ) + |
| 216 | Formats.field( __typeNames[_type] , 10 , Formats.LEFT ); |
| 217 | } |
| 218 | /* |
| 219 | CellRoute getClone(){ |
| 220 | try { |
| 221 | return (CellRoute)this.clone() ; |
| 222 | }catch( CloneNotSupportedException cnse ){ |
| 223 | return null ; |
| 224 | } |
| 225 | } |
| 226 | */ |
| 227 | CellRoute getClone(){ |
| 228 | CellRoute cr = new CellRoute() ; |
| 229 | cr._destCell = _destCell ; |
| 230 | cr._destDomain = _destDomain ; |
| 231 | cr._gateway = _gateway ; |
| 232 | cr._type = _type ; |
| 233 | return cr ; |
| 234 | } |
| 235 | protected Object clone(){ |
| 236 | CellRoute cr = new CellRoute() ; |
| 237 | cr._destCell = _destCell ; |
| 238 | cr._destDomain = _destDomain ; |
| 239 | cr._gateway = _gateway ; |
| 240 | cr._type = _type ; |
| 241 | return cr ; |
| 242 | } |
| 243 | public static void main( String [] args ){ |
| 244 | if( args.length < 3 )System.exit(4); |
| 245 | |
| 246 | try{ |
| 247 | CellRoute route ; |
| 248 | route = new CellRoute( args[0] , args[1] , args[2] ) ; |
| 249 | System.out.println( CellRoute.headerToString() ) ; |
| 250 | System.out.println( route.toString() ) ; |
| 251 | }catch( IllegalArgumentException iae ){ |
| 252 | System.out.println( "exception : "+iae ) ; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | } |
| 258 | |