00001
00002
00003
00004
00005 #include "connectionDescription.h"
00006
00007 #include <sstream>
00008
00009 using namespace std;
00010
00011 namespace eq
00012 {
00013 namespace net
00014 {
00015
00016 #define SEPARATOR '#'
00017
00018 string ConnectionDescription::toString() const
00019 {
00020 ostringstream description;
00021 serialize( description );
00022 return description.str();
00023 }
00024
00025 void ConnectionDescription::serialize( std::ostream& os ) const
00026 {
00027 switch( type )
00028 {
00029 case CONNECTIONTYPE_TCPIP:
00030 os << "TCPIP";
00031 break;
00032
00033 case CONNECTIONTYPE_SDP:
00034 os << "SDP";
00035 break;
00036
00037 case CONNECTIONTYPE_PIPE:
00038 os << "PIPE";
00039 break;
00040 }
00041
00042 os << SEPARATOR << bandwidth << SEPARATOR << _launchCommand
00043 << SEPARATOR << static_cast<int>( launchCommandQuote )
00044 << SEPARATOR << launchTimeout << SEPARATOR << _hostname ;
00045
00046 switch( type )
00047 {
00048 case CONNECTIONTYPE_TCPIP:
00049 case CONNECTIONTYPE_SDP:
00050 os << SEPARATOR << TCPIP.port;
00051 break;
00052
00053 default:
00054 break;
00055 }
00056 os << SEPARATOR;
00057 }
00058
00059 bool ConnectionDescription::fromString( std::string& data )
00060 {
00061 {
00062 size_t nextPos = data.find( SEPARATOR );
00063
00064 if( nextPos == string::npos )
00065 {
00066 type = CONNECTIONTYPE_TCPIP;
00067 nextPos = data.find( ':' );
00068 if( nextPos == string::npos )
00069 {
00070 _hostname = data;
00071 data.clear();
00072 return true;
00073 }
00074
00075 _hostname = data.substr( 0, nextPos );
00076 data = data.substr( nextPos + 1 );
00077
00078 while( nextPos != string::npos )
00079 {
00080 nextPos = data.find( ':' );
00081 const string token = data.substr( 0, nextPos );
00082 data = data.substr( nextPos + 1 );
00083
00084 if( !token.empty() && isdigit( token[0] ))
00085 TCPIP.port = atoi( token.c_str( ));
00086 else if( token == "TCPIP" )
00087 type = CONNECTIONTYPE_TCPIP;
00088 else if( token == "SDP" )
00089 type = CONNECTIONTYPE_SDP;
00090 else if( token == "PIPE" )
00091 type = CONNECTIONTYPE_PIPE;
00092 else
00093 goto error;
00094 }
00095
00096 data.clear();
00097 return true;
00098 }
00099
00100 const string typeStr = data.substr( 0, nextPos );
00101 data = data.substr( nextPos + 1 );
00102
00103 if( typeStr == "TCPIP" )
00104 type = CONNECTIONTYPE_TCPIP;
00105 else if( typeStr == "SDP" )
00106 type = CONNECTIONTYPE_SDP;
00107 else if( typeStr == "PIPE" )
00108 type = CONNECTIONTYPE_PIPE;
00109 else
00110 goto error;
00111
00112 nextPos = data.find( SEPARATOR );
00113 if( nextPos == string::npos )
00114 goto error;
00115
00116 const string bandwidthStr = data.substr( 0, nextPos );
00117 data = data.substr( nextPos + 1 );
00118 bandwidth = atoi( bandwidthStr.c_str( ));
00119
00120 nextPos = data.find( SEPARATOR );
00121 if( nextPos == string::npos )
00122 goto error;
00123 _launchCommand = data.substr( 0, nextPos );
00124 data = data.substr( nextPos + 1 );
00125
00126 nextPos = data.find( SEPARATOR );
00127 if( nextPos == string::npos )
00128 goto error;
00129 const string quoteStr = data.substr( 0, nextPos );
00130 launchCommandQuote = static_cast< char >( atoi( quoteStr.c_str( )));
00131 data = data.substr( nextPos + 1 );
00132
00133 nextPos = data.find( SEPARATOR );
00134 if( nextPos == string::npos )
00135 goto error;
00136
00137 const string launchTimeoutStr = data.substr( 0, nextPos );
00138 data = data.substr( nextPos + 1 );
00139 launchTimeout = atoi( launchTimeoutStr.c_str( ));
00140
00141 nextPos = data.find( SEPARATOR );
00142 if( nextPos == string::npos )
00143 goto error;
00144
00145 _hostname = data.substr( 0, nextPos );
00146 data = data.substr( nextPos + 1 );
00147
00148 switch( this->type )
00149 {
00150 case CONNECTIONTYPE_TCPIP:
00151 case CONNECTIONTYPE_SDP:
00152 {
00153 nextPos = data.find( SEPARATOR );
00154 if( nextPos == string::npos )
00155 goto error;
00156
00157 const string port = data.substr( 0, nextPos );
00158 data = data.substr( nextPos + 1 );
00159 TCPIP.port = atoi( port.c_str( ));
00160 break;
00161 }
00162
00163 default:
00164 break;
00165 }
00166 }
00167 return true;
00168
00169 error:
00170 EQWARN << "Could not parse connection description: " << data << endl;
00171 return false;
00172 }
00173
00174 void ConnectionDescription::setHostname( const std::string& hostname )
00175 {
00176 _hostname = hostname;
00177 }
00178
00179 const string& ConnectionDescription::getHostname() const
00180 {
00181 return _hostname;
00182 }
00183
00184 void ConnectionDescription::setLaunchCommand( const std::string& launchCommand )
00185 {
00186 _launchCommand = launchCommand;
00187 }
00188
00189 const string& ConnectionDescription::getLaunchCommand() const
00190 {
00191 return _launchCommand;
00192 }
00193
00194 std::ostream& operator << ( std::ostream& os, const ConnectionDescription* desc)
00195 {
00196 if( !desc )
00197 {
00198 os << "NULL connection description";
00199 return os;
00200 }
00201
00202 os << "connection " << ( desc->type == net::CONNECTIONTYPE_TCPIP ? "tcp/ip":
00203 desc->type == net::CONNECTIONTYPE_SDP ? "sdp" :
00204 desc->type == net::CONNECTIONTYPE_PIPE ? "pipe" :
00205 "ERROR" )
00206 << ' ' << desc->getHostname() << ':' << desc->TCPIP.port;
00207 return os;
00208 }
00209
00210
00211 }
00212 }