1 | package dmg.cells.nucleus ; |
2 | |
3 | import java.io.BufferedReader; |
4 | import java.io.IOException; |
5 | import java.io.InputStreamReader; |
6 | import java.io.Serializable; |
7 | import java.util.HashMap; |
8 | import java.util.Map; |
9 | |
10 | import dmg.util.Args; |
11 | |
12 | /** |
13 | * |
14 | * |
15 | * @author Patrick Fuhrmann |
16 | * @version 0.1, 15 Feb 1998 |
17 | */ |
18 | public class CellRoutingTable implements Serializable { |
19 | |
20 | static final long serialVersionUID = -1456280129622980563L; |
21 | |
22 | private final Map<String, CellRoute> _wellknown = new HashMap<String, CellRoute>() ; |
23 | private final Map<String, CellRoute> _domain = new HashMap<String, CellRoute>() ; |
24 | private final Map<String, CellRoute> _exact = new HashMap<String, CellRoute>() ; |
25 | private CellRoute _dumpster = null ; |
26 | private CellRoute _default = null ; |
27 | |
28 | public synchronized void add( CellRoute route ) |
29 | throws IllegalArgumentException { |
30 | |
31 | int type = route.getRouteType() ; |
32 | String dest ; |
33 | switch( type ){ |
34 | case CellRoute.EXACT : |
35 | case CellRoute.ALIAS : |
36 | dest = route.getCellName()+"@"+route.getDomainName() ; |
37 | if( _exact.get( dest ) != null ) |
38 | throw new IllegalArgumentException( "Duplicated route Entry for : " + dest ) ; |
39 | _exact.put( dest , route ) ; |
40 | break ; |
41 | case CellRoute.WELLKNOWN : |
42 | dest = route.getCellName() ; |
43 | if( _wellknown.get( dest ) != null ) |
44 | throw new IllegalArgumentException( "Duplicated route Entry for : " + dest ) ; |
45 | _wellknown.put( dest , route ) ; |
46 | break ; |
47 | case CellRoute.DOMAIN : |
48 | dest = route.getDomainName() ; |
49 | if( _domain.get( dest ) != null ) |
50 | throw new IllegalArgumentException( "Duplicated route Entry for : " + dest ) ; |
51 | _domain.put( dest , route ) ; |
52 | break ; |
53 | case CellRoute.DEFAULT : |
54 | if( _default != null ) |
55 | throw new IllegalArgumentException( "Duplicated route Entry for default." ) ; |
56 | _default = route ; |
57 | break ; |
58 | case CellRoute.DUMPSTER : |
59 | if( _dumpster != null ) |
60 | throw new IllegalArgumentException( "Duplicated route Entry for dumpster" ) ; |
61 | _dumpster = route ; |
62 | break ; |
63 | |
64 | } |
65 | |
66 | } |
67 | public synchronized void delete( CellRoute route ) |
68 | throws IllegalArgumentException { |
69 | |
70 | int type = route.getRouteType() ; |
71 | String dest ; |
72 | switch( type ){ |
73 | case CellRoute.EXACT : |
74 | case CellRoute.ALIAS : |
75 | dest = route.getCellName()+"@"+route.getDomainName() ; |
76 | if( _exact.remove( dest ) == null ) |
77 | throw new IllegalArgumentException( "Route Entry Not Found for : " + dest ) ; |
78 | break ; |
79 | case CellRoute.WELLKNOWN : |
80 | dest = route.getCellName() ; |
81 | if( _wellknown.remove( dest ) == null ) |
82 | throw new IllegalArgumentException( "Route Entry Not Found for : " + dest ) ; |
83 | break ; |
84 | case CellRoute.DOMAIN : |
85 | dest = route.getDomainName() ; |
86 | if( _domain.remove( dest ) == null ) |
87 | throw new IllegalArgumentException( "Route Entry Not Found for : " + dest ) ; |
88 | break ; |
89 | case CellRoute.DEFAULT : |
90 | if( _default == null ) |
91 | throw new IllegalArgumentException( "Route Entry Not Found for default" ) ; |
92 | _default = null ; |
93 | break ; |
94 | case CellRoute.DUMPSTER : |
95 | if( _dumpster == null ) |
96 | throw new IllegalArgumentException( "Route Entry Not Found dumpster" ) ; |
97 | _dumpster = null ; |
98 | break ; |
99 | |
100 | } |
101 | |
102 | } |
103 | public synchronized CellRoute find( CellAddressCore addr ){ |
104 | String cellName = addr.getCellName() ; |
105 | String domainName = addr.getCellDomainName(); |
106 | CellRoute route = null ; |
107 | if( domainName.equals("local") ){ |
108 | // |
109 | // this is not really local but wellknown |
110 | // we checked for local before we called this. |
111 | // |
112 | route = _wellknown.get( cellName ) ; |
113 | if( route != null )return route ; |
114 | }else{ |
115 | route = _exact.get(cellName+"@"+domainName) ; |
116 | if( route != null )return route ; |
117 | route = _domain.get( domainName ) ; |
118 | if( route != null )return route ; |
119 | } |
120 | route = _exact.get( cellName+"@"+domainName ) ; |
121 | return route == null ? _default : route ; |
122 | |
123 | } |
124 | public String toString(){ |
125 | |
126 | StringBuffer sb = new StringBuffer() ; |
127 | sb.append( CellRoute.headerToString()+"\n" ) ; |
128 | for( CellRoute route: _exact.values() ) |
129 | sb.append( route.toString()+"\n" ) ; |
130 | for( CellRoute route: _wellknown.values() ) |
131 | sb.append( route.toString()+"\n" ) ; |
132 | for( CellRoute route: _domain.values() ) |
133 | sb.append( route.toString()+"\n" ) ; |
134 | if( _default != null ) |
135 | sb.append( _default.toString()+"\n" ) ; |
136 | if( _dumpster != null ) |
137 | sb.append( _dumpster.toString()+"\n" ) ; |
138 | |
139 | return sb.toString(); |
140 | } |
141 | public synchronized CellRoute [] getRoutingList(){ |
142 | int total = _exact.size() + |
143 | _wellknown.size() + |
144 | _domain.size() + |
145 | ( _default != null ? 1 : 0 ) + |
146 | ( _dumpster != null ? 1 : 0 ) ; |
147 | |
148 | CellRoute [] routes = new CellRoute[total] ; |
149 | int i = 0 ; |
150 | |
151 | for( CellRoute route: _exact.values() ) |
152 | routes[i++] = route ; |
153 | for( CellRoute route: _wellknown.values() ) |
154 | routes[i++] = route; |
155 | for( CellRoute route: _domain.values() ) |
156 | routes[i++] = route; |
157 | if( _default != null ) |
158 | routes[i++] = _default ; |
159 | if( _dumpster != null ) |
160 | routes[i++] = _dumpster ; |
161 | |
162 | return routes ; |
163 | } |
164 | public static void main( String [] argsxx ){ |
165 | CellRoutingTable table = new CellRoutingTable() ; |
166 | String line ; |
167 | BufferedReader reader = |
168 | new BufferedReader( new InputStreamReader( System.in ) ) ; |
169 | try{ |
170 | while( ( line = reader.readLine() ) != null ){ |
171 | Args args = new Args( line ) ; |
172 | if( args.argc() < 1 )continue ; |
173 | String com = args.argv(0) ; |
174 | args.shift() ; |
175 | if( com.equals( "add" ) ){ |
176 | c_add( table , args ) ; |
177 | }else if( com.equals( "show" ) ){ |
178 | System.out.println( table.toString() ) ; |
179 | }else if( com.equals( "delete" ) ){ |
180 | c_delete( table , args ) ; |
181 | }else if( com.equals( "find" ) ){ |
182 | c_find( table , args ) ; |
183 | } |
184 | } |
185 | }catch( IOException ioe ){ |
186 | System.err.println( " Exception "+ioe ) ; |
187 | } |
188 | } |
189 | public static void c_delete( CellRoutingTable table , Args args ){ |
190 | try{ |
191 | table.delete( new CellRoute( args ) ); |
192 | }catch( Exception e ){ |
193 | System.err.println( " Exception "+e ) ; |
194 | } |
195 | } |
196 | public static void c_add( CellRoutingTable table , Args args ){ |
197 | // route add type destination target |
198 | try{ |
199 | table.add( new CellRoute( args ) ); |
200 | }catch( Exception e ){ |
201 | System.err.println( " Exception "+e ) ; |
202 | } |
203 | } |
204 | public static void c_find( CellRoutingTable table , Args args ){ |
205 | // route add type destination target |
206 | if( args.argc() == 0 ){ |
207 | System.out.println( "Cell Argument missing" ) ; |
208 | return ; |
209 | } |
210 | try{ |
211 | CellAddressCore addr = new CellAddressCore( args.argv(0) ) ; |
212 | System.out.println( " Searching : "+addr.toString() ); |
213 | CellRoute route = table.find( addr ); |
214 | if( route != null )System.out.println( " Found route : "+route.toString() ); |
215 | else System.out.println( "Not found" ) ; |
216 | }catch( Exception e ){ |
217 | System.err.println( " Exception "+e ) ; |
218 | } |
219 | } |
220 | |
221 | } |