Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

ShoutCast.cpp

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------------
00002 
00003    Copyright (c) 2000 Tyrell Corporation. All rights reserved.
00004 
00005    Tyrell DarkIce
00006 
00007    File     : ShoutCast.cpp
00008    Version  : $Revision: 1.3 $
00009    Author   : $Author: darkeye $
00010    Location : $Source: /cvsroot/darkice/darkice/src/ShoutCast.cpp,v $
00011    
00012    Copyright notice:
00013 
00014     This program is free software; you can redistribute it and/or
00015     modify it under the terms of the GNU General Public License  
00016     as published by the Free Software Foundation; either version 2
00017     of the License, or (at your option) any later version.
00018    
00019     This program is distributed in the hope that it will be useful,
00020     but WITHOUT ANY WARRANTY; without even the implied warranty of 
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
00022     GNU General Public License for more details.
00023    
00024     You should have received a copy of the GNU General Public License
00025     along with this program; if not, write to the Free Software
00026     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ------------------------------------------------------------------------------*/
00029 
00030 /* ============================================================ include files */
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 
00036 #ifdef HAVE_STDIO_H
00037 #include <stdio.h>
00038 #else
00039 #error need stdio.h
00040 #endif
00041 
00042 #ifdef HAVE_STRING_H
00043 #include <string.h>
00044 #else
00045 #error need string.h
00046 #endif
00047 
00048 #ifdef HAVE_MATH_H
00049 #include <math.h>
00050 #else
00051 #error need math.h
00052 #endif
00053 
00054 
00055 #include "Exception.h"
00056 #include "Source.h"
00057 #include "Sink.h"
00058 #include "Util.h"
00059 #include "ShoutCast.h"
00060 
00061 
00062 /* ===================================================  local data structures */
00063 
00064 
00065 /* ================================================  local constants & macros */
00066 
00067 /*------------------------------------------------------------------------------
00068  *  File identity
00069  *----------------------------------------------------------------------------*/
00070 static const char fileid[] = "$Id: ShoutCast.cpp,v 1.3 2002/05/28 12:35:41 darkeye Exp $";
00071 
00072 
00073 /*------------------------------------------------------------------------------
00074  *  Size of string conversion buffer
00075  *----------------------------------------------------------------------------*/
00076 #define STRBUF_SIZE         32
00077 
00078 
00079 /* ===============================================  local function prototypes */
00080 
00081 
00082 /* =============================================================  module code */
00083 
00084 /*------------------------------------------------------------------------------
00085  *  Initialize the object
00086  *----------------------------------------------------------------------------*/
00087 void
00088 ShoutCast :: init ( const char            * irc,
00089                     const char            * aim,
00090                     const char            * icq )
00091                                                         throw ( Exception )
00092 {
00093     this->irc    = irc   ? Util::strDup( irc) : 0;
00094     this->aim    = aim   ? Util::strDup( aim) : 0;
00095     this->icq    = icq   ? Util::strDup( icq) : 0;
00096 }
00097 
00098 
00099 /*------------------------------------------------------------------------------
00100  *  De-initialize the object
00101  *----------------------------------------------------------------------------*/
00102 void
00103 ShoutCast :: strip ( void )                             throw ( Exception )
00104 {
00105     if ( irc ) {
00106         delete[] irc;
00107     }
00108     if ( aim ) {
00109         delete[] aim;
00110     }
00111     if ( icq ) {
00112         delete[] icq;
00113     }
00114 }
00115 
00116 
00117 /*------------------------------------------------------------------------------
00118  *  Log in to the ShoutCast server using the icy login scheme
00119  *----------------------------------------------------------------------------*/
00120 bool
00121 ShoutCast :: sendLogin ( void )                           throw ( Exception )
00122 {
00123     Sink          * sink   = getSink();
00124     Source        * source = getSocket();
00125     const char    * str;
00126     char            resp[STRBUF_SIZE];
00127     unsigned int    len;
00128 
00129     if ( !source->isOpen() ) {
00130         return false;
00131     }
00132     if ( !sink->isOpen() ) {
00133         return false;
00134     }
00135 
00136     /* first line is the password in itself */
00137     str = getPassword();
00138     sink->write( str, strlen( str));
00139     str = "\n";
00140     sink->write( str, strlen( str));
00141     sink->flush();
00142 
00143     /* send the icy headers */
00144     if ( getName() ) {
00145         str = "icy-name:";
00146         sink->write( str, strlen( str));
00147         str = getName();
00148         sink->write( str, strlen( str));
00149     }
00150 
00151     if ( getUrl() ) {
00152         str = "\nicy-url:";
00153         sink->write( str, strlen( str));
00154         str = getUrl();
00155         sink->write( str, strlen( str));
00156     }
00157 
00158     if ( getGenre() ) {
00159         str = "\nicy-genre:";
00160         sink->write( str, strlen( str));
00161         str = getGenre();
00162         sink->write( str, strlen( str));
00163     }
00164 
00165     if ( getIrc() ) {
00166         str = "\nicy-irc:";
00167         sink->write( str, strlen( str));
00168         str = getIrc();
00169         sink->write( str, strlen( str));
00170     }
00171 
00172     if ( getAim() ) {
00173         str = "\nicy-aim:";
00174         sink->write( str, strlen( str));
00175         str = getAim();
00176         sink->write( str, strlen( str));
00177     }
00178 
00179     if ( getIcq() ) {
00180         str = "\nicy-icq:";
00181         sink->write( str, strlen( str));
00182         str = getIcq();
00183         sink->write( str, strlen( str));
00184     }
00185 
00186     str = "\nicy-br:";
00187     sink->write( str, strlen( str));
00188     if ( log10(getBitRate()) >= (STRBUF_SIZE-2) ) {
00189         throw Exception( __FILE__, __LINE__,
00190                          "bitrate does not fit string buffer", getBitRate());
00191     }
00192     sprintf( resp, "%d", getBitRate());
00193     sink->write( resp, strlen( resp));
00194 
00195     str = "\nicy-pub:";
00196     sink->write( str, strlen( str));
00197     str = getIsPublic() ? "1" : "0";
00198     sink->write( str, strlen( str));
00199 
00200     str = "\n\n";
00201     sink->write( str, strlen( str));
00202     sink->flush();
00203 
00204     /* read the anticipated response: "OK" */
00205     len = source->read( resp, STRBUF_SIZE);
00206     if ( len < 2 || resp[0] != 'O' || resp[1] != 'K' ) {
00207         return false;
00208     }
00209 
00210     /* suck anything that the other side has to say */
00211     while ( source->canRead( 0, 0) && 
00212            (len = source->read( resp, STRBUF_SIZE)) ) {
00213         ;
00214     }
00215 
00216     return true;
00217 }
00218 
00219 
00220 
00221 /*------------------------------------------------------------------------------
00222  
00223   $Source: /cvsroot/darkice/darkice/src/ShoutCast.cpp,v $
00224 
00225   $Log: ShoutCast.cpp,v $
00226   Revision 1.3  2002/05/28 12:35:41  darkeye
00227   code cleanup: compiles under gcc-c++ 3.1, using -pedantic option
00228 
00229   Revision 1.2  2001/11/20 09:06:18  darkeye
00230   fixed public stream reporting
00231 
00232   Revision 1.1  2001/09/09 11:27:31  darkeye
00233   added support for ShoutCast servers
00234 
00235 
00236   
00237 ------------------------------------------------------------------------------*/
00238 

Generated on Sat Oct 22 13:17:05 2005 for DarkIce by  doxygen 1.4.4