/*
 * Author:
 *         Kendy Kutzner <kutzner@ira.uka.de>
 *
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * $Id: transport.cc,v 1.7 2006/01/05 12:49:01 kutzner Exp $
 *
 */

#include <string>
#include <sstream>
using namespace std;

#include "transport.h"

transport::transport(){
	_host.clear();
	_port = -1;
	_empty = true;
	_port_set = _host_set = false;
}

transport::transport (string s){
	parsetransport(s);
}

void
transport::parsetransport(string s){
	int colpos = s.find(':');
	// XXX do some checking
	_host = s.substr(0,colpos);
	_port = atoi(s.substr(colpos+1, s.size()).c_str());
	_empty = false;
	_port_set = _host_set = true;
}

string 
transport::tostring() const {
	stringstream s;
	if (!_empty){
		s << _host;
		s << ':';
		s << _port;
	} else {
		s << "--EMPTY-TRANSPORT--";
	}
	return s.str();
}

string 
transport::host() const {
	return _host;
}

int 
transport::port() const {
	return _port;
}

void 
transport::sethost(string h){
	///\todo can we check the hostname here? Should we?
	_host=h;
	_host_set = true;
	if (_port_set) {
		_empty = false;
	}
}

void 
transport::setport(int p){
	///\todo do some checking on valid ports
	_port = p;
	_port_set = true;
	if (_host_set) {
		_empty = false;
	}
}

bool 
transport::operator==(transport & other) const {
	/// \todo do some more checking on different representations of hostname
	if ( (_host == other._host) && (_port == other._port)) {
		return true;
	}
	return false;
}

