org.coos.messaging.routing.DefaultRoutingAlgorithm Maven / Gradle / Ivy
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.messaging.routing;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;
import org.coos.messaging.Link;
import org.coos.messaging.util.UuidHelper;
import java.io.StringWriter;
import java.util.*;
/**
* This router contains a set of methods that must be implemented by all routing algorithms
*
* @author Knut Eilif Husa, Tellu AS
*/
public abstract class DefaultRoutingAlgorithm implements RoutingAlgorithm {
protected Map> routingTables;
protected Map aliasTable;
protected Map properties = new Hashtable();
protected Map links;
boolean loggingEnabled = false;
protected Router router;
protected String routerUuid;
protected String segment;
protected final Log logger = LogFactory.getLog(this.getClass().getName());
protected Collection QoSClasses;
public void init(String routerUuid, Router router) {
this.router = router;
this.routerUuid = routerUuid;
segment = UuidHelper.getSegmentFromEndpointNameOrEndpointUuid(routerUuid);
links = router.getLinks();
QoSClasses = router.getQoSClasses();
routingTables = router.getRoutingTables();
aliasTable = router.getAliasTable();
this.router.addRoutingAlgorithm(routerUuid, this);
}
@SuppressWarnings("unchecked")
public void setRoutingTables(Map routingTables) {
this.routingTables = routingTables;
}
public String getRouterUuid() {
return routerUuid;
}
public void setLoggingEnabled(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
}
protected static synchronized void printRoutingTable(String routerUuid, String qos, Map routingTable, Log logger) {
StringWriter writer = new StringWriter();
writer.write("-------------Routing table for QoS:" + qos + " in router: " + routerUuid + "------------\n");
Iterator keys = routingTable.keySet().iterator();
while (keys.hasNext()) {
String uuid = keys.next();
writer.write("'" + uuid + "' --> '" + routingTable.get(uuid) + "'\n");
}
writer.write("-------------------------\n");
logger.debug(writer.toString());
}
protected static synchronized void printAliasTable(String routerUuid, Map aliasTable, Log logger) {
StringWriter writer = new StringWriter();
writer.write("-------------Alias table for router: " + routerUuid + "------------\n");
Iterator keys = aliasTable.keySet().iterator();
while (keys.hasNext()) {
String alias = keys.next();
writer.write("'" + alias + "' --> '" + aliasTable.get(alias) + "'\n");
}
writer.write("-------------------------\n");
logger.debug(writer.toString());
}
public void setLinks(Map links) {
this.links = links;
}
@SuppressWarnings("unchecked")
public void setProperties(Hashtable properties) {
this.properties = properties;
}
public Hashtable getProperties() {
return new Hashtable(properties);
}
public String getProperty(String key) {
return properties.get(key);
}
public void setProperty(String key, String value) {
properties.put(key, value);
}
public RoutingAlgorithm copy() {
RoutingAlgorithm algorithm = null;
try {
algorithm = this.getClass().newInstance();
algorithm.setProperties(new Hashtable(properties));
} catch (InstantiationException e) {
logger.error("InstantiationException ignored.", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException ignored.", e);
}
return algorithm;
}
}