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

org.coos.messaging.routing.RouterChannel Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/**
 * 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.ConnectingException;
import org.coos.messaging.Connectable;
import org.coos.messaging.Message;
import org.coos.messaging.impl.DefaultChannel;
import org.coos.messaging.impl.DefaultMessage;
import org.coos.messaging.impl.DefaultProcessor;

/**
 * @author Knut Eilif Husa, Tellu AS 
 * 
 * Router Channel class. Contains one inlink
 * and one outlink and a transport This Channel type connects COOS
 * instances
 */
public class RouterChannel extends DefaultChannel {
	
	private String routingAlgorithm;

	public RouterChannel() {
		protocols.addElement("coos");
	}

	public String getRoutingAlgorithm() {
		return routingAlgorithm;
	}

	public void setRoutingAlgorithmName(String routingAlgorithm) {
		this.routingAlgorithm = routingAlgorithm;
	}

	public String getConnectingPartyUuid() {
		return connectingPartyUuid;
	}

	public void setConnectingPartyUuid(String connectingPartyUuid) {
		this.connectingPartyUuid = connectingPartyUuid;
	}

	/**
	 * Method that connects the channel if the Channel is init property set to
	 * true
	 */
	public void connect(Connectable linkManager) throws ConnectingException {
		this.connectable = linkManager;
		final Connectable linkManager1 = linkManager;
		if (!isInit()) {
			return;
		}
		try {

			transport.setChainedProcessor(new DefaultProcessor() {
				public void processMessage(Message msg) {
					if (msg.getHeader(Message.MESSAGE_NAME).equals(CONNECT_ACK)) {
						String conUuid = msg.getHeader(CONNECT_UUID);
						if (conUuid != null) {
							outLink.setDestinationUuid(conUuid);
							outLink.setChainedProcessor(transport);
							try {
								linkManager1.addLink(conUuid, outLink);
							} catch (Exception e) {
								e.printStackTrace();
							}
							inLink.setChainedProcessor(linkManager1.getDefaultProcessor());
							inLink.setDestinationUuid(connectingPartyUuid);
							transport.setChainedProcessor(inLink);
							connected = true;
						} else {
							System.out.println("Can not process msg: " + msg);

						}
					}
				}
			});

			Message msg = new DefaultMessage(CONNECT);
			msg.setHeader(CONNECT_UUID, connectingPartyUuid);
			msg.setHeader(ROUTING_ALGORITHM, routingAlgorithm);
			transport.start();
			outLink.start();
			inLink.start();
			transport.processMessage(msg);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Method that disconnects the Channel
	 */
	public void disconnect() {
		// todo disconnect protocol is missing towards the peer. Will only
		// disconnect on this channel side
		connected = false;
		try {
			if (connectable != null) {
				connectable.removeLinkById(outLink.getLinkId());
			}
			outLink.stop();
			inLink.stop();
			if (transport != null) {
				transport.stop();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Makes a copy of the channel. Based on the shared property of the
	 * processors the Channel will be populated with unique or shared instances
	 * 
	 * @return - the copied channel
	 */
	public DefaultChannel copy() {
		RouterChannel ch = (RouterChannel) super.copy();
		ch.setConnectingPartyUuid(connectingPartyUuid);
		return ch;
	}

	public boolean isConnected() {
		return connected;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy