All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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;
	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, Map properties) {
		this.router = router;
		this.routerUuid = routerUuid;
		this.properties = properties;
		segment = UuidHelper.getSegment(routerUuid);
		links = router.getLinks();
		QoSClasses = router.getQoSClasses();
		routingTables = router.getRoutingTables();
		aliasTable = router.getAliasTable();
		this.router.addRoutingAlgorithm(routerUuid, this);
	}

	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 = (String) 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 = (String) keys.next();
			writer.write("'" + alias + "' --> '" + aliasTable.get(alias) + "'\n");
		}

		writer.write("-------------------------\n");

		logger.debug(writer.toString());
	}

	public void setLinks(Map links) {
		this.links = links;
	}

    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);
        
    }
	
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy