RoutingEngine.java

Go to the documentation of this file.
00001 /*
00002  * $Id: RoutingEngine.java 2 2006-11-14 22:37:20Z vingarzan $
00003  *
00004  * Copyright (C) 2004-2006 FhG Fokus
00005  *
00006  * This file is part of Open IMS Core - an open source IMS CSCFs & HSS
00007  * implementation
00008  *
00009  * Open IMS Core is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * For a license to use the Open IMS Core software under conditions
00015  * other than those described here, or to purchase support for this
00016  * software, please contact Fraunhofer FOKUS by e-mail at the following
00017  * addresses:
00018  *     info@open-ims.org
00019  *
00020  * Open IMS Core is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  */
00030 
00031 package de.fhg.fokus.diameter.DiameterPeer.routing;
00032 
00033 import java.util.TreeMap;
00034 import java.util.Vector;
00035 
00036 import de.fhg.fokus.diameter.DiameterPeer.data.AVP;
00037 import de.fhg.fokus.diameter.DiameterPeer.data.DiameterMessage;
00038 import de.fhg.fokus.diameter.DiameterPeer.peer.Peer;
00039 import de.fhg.fokus.diameter.DiameterPeer.peer.PeerManager;
00040 import de.fhg.fokus.diameter.DiameterPeer.peer.StateMachine;
00041 
00042 public class RoutingEngine {
00043      
00044     Vector<RoutingEntry> defaultRoutes;
00045     TreeMap<String,RoutingRealm> realms;
00046 
00047     public RoutingEngine()
00048     {
00049         defaultRoutes = new Vector<RoutingEntry>();
00050         realms = new TreeMap<String, RoutingRealm>();
00051     }
00052     
00053     public void addDefaultRoute(String FQDN,int metric){        
00054         RoutingEntry re = new RoutingEntry(FQDN,metric);
00055         for (RoutingEntry i : defaultRoutes) 
00056             if (i.metric<=metric){
00057                 defaultRoutes.add(defaultRoutes.indexOf(i), re);
00058                 break;
00059             }
00060     }
00061     
00062     public void addRealmRoute(String realm, String FQDN, int metric)
00063     {
00064         RoutingRealm rr;
00065         rr = realms.get(realm);
00066         if (rr==null){
00067             rr = new RoutingRealm(realm);
00068             this.realms.put(realm, rr);
00069         }
00070         rr.addRoute(FQDN,metric);
00071     }
00072 
00073     public Peer getRoute(DiameterMessage msg, PeerManager peerManager) {
00074         String destinationHost=null,destinationRealm=null;
00075         AVP avp;
00076         Peer p;
00077         avp = msg.findAVP(AVP.Destination_Host);
00078         if (avp!=null) destinationHost = new String(avp.getData());
00079         if (destinationHost!=null && destinationHost.length()>0){
00080             p = peerManager.getPeerByFQDN(destinationHost);
00081             if (p!=null && (p.state==StateMachine.I_Open || p.state!=StateMachine.R_Open))
00082                 return p;
00083         }
00084         avp = msg.findAVP(AVP.Destination_Realm);
00085         if (avp!=null) destinationRealm = new String(avp.getData());
00086         if (destinationRealm!=null && destinationRealm.length()>0){
00087             RoutingRealm rr = realms.get(destinationRealm);
00088             if (rr!=null){
00089                 for (RoutingEntry i : rr.routes) {
00090                     p = peerManager.getPeerByFQDN(i.FQDN);
00091                     if (p!=null && (p.state==StateMachine.I_Open || p.state!=StateMachine.R_Open))
00092                         return p;                   
00093                 }
00094             }
00095         }
00096         for (RoutingEntry i : defaultRoutes) {
00097             p = peerManager.getPeerByFQDN(i.FQDN);
00098             if (p!=null && (p.state==StateMachine.I_Open || p.state!=StateMachine.R_Open))
00099                 return p;                   
00100         }
00101         return null;
00102     }
00103     
00104     
00105     
00106     
00107 }
00108 
00109 class RoutingEntry {
00110     String FQDN;
00111     int metric;
00112     public RoutingEntry(String FQDN, int metric) {
00113         this.FQDN = FQDN;
00114         this.metric = metric;
00115     }
00116 }
00117 
00118 class RoutingRealm {
00119     String realm;
00120     Vector<RoutingEntry> routes;
00121     public RoutingRealm(String realm) {
00122         this.realm = realm;
00123         this.routes = new Vector<RoutingEntry>();
00124     }
00125     public void addRoute(String FQDN,int metric){       
00126         RoutingEntry re = new RoutingEntry(FQDN,metric);
00127         for (RoutingEntry i : routes) 
00128             if (i.metric<=metric){
00129                 routes.add(routes.indexOf(i), re);
00130                 break;
00131             }
00132     }
00133 }

Generated on Thu Oct 23 04:07:36 2008 for Open IMS Core JavaDiameterPeer by  doxygen 1.5.2