MovieListRequest.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: MovieListRequest.cpp * 00006 * * 00007 * * 00008 * * 00009 * ITEC institute of the University of Klagenfurt (Austria) * 00010 * http://www.itec.uni-klu.ac.at * 00011 * * 00012 * * 00013 * For more information visit the ViTooKi homepage: * 00014 * http://ViTooKi.sourceforge.net * 00015 * vitooki-user@lists.sourceforge.net * 00016 * vitooki-devel@lists.sourceforge.net * 00017 * * 00018 * This file is part of ViTooKi, a free video toolkit. * 00019 * ViTooKi is free software; you can redistribute it and/or * 00020 * modify it under the terms of the GNU General Public License * 00021 * as published by the Free Software Foundation; either version 2 * 00022 * of the License, or (at your option) any later version. * 00023 * * 00024 * This program is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU General Public License * 00030 * along with this program; if not, write to the Free Software * 00031 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * 00032 * MA 02111-1307, USA. * 00033 * * 00034 ***********************************************************************/ 00035 00036 /*********************************************************************** 00037 * * 00038 * REVISION HISTORY: * 00039 * * 00040 * * 00041 * * 00042 ***********************************************************************/ 00043 00044 /*********************************************************************** 00045 * Video Session Migration System, 2004 * 00046 * Author: Klaus Schoeffmann * 00047 ************************************************************************/ 00048 00049 00050 #include "MovieListRequest.hpp" 00051 00052 /***********************************************************************/ 00053 MovieListRequest::MovieListRequest() 00054 {} 00055 00056 /***********************************************************************/ 00057 MovieListRequest::~MovieListRequest() 00058 {} 00059 00060 /***********************************************************************/ 00061 int MovieListRequest::createSocket(const char *host, int port) 00062 { 00063 struct hostent *hp; 00064 struct sockaddr_in sin; 00065 int sock; 00066 00067 if (!(hp = gethostbyname(host))) 00068 { 00069 cerr << "\nMovieListRequest::createSocket: Cannot resolve host '" << host << "'"; 00070 return 0; 00071 } 00072 00073 sin.sin_family = AF_INET; 00074 bcopy (hp->h_addr, (char*) &sin.sin_addr, hp->h_length); 00075 sin.sin_port = htons(port); 00076 00077 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) 00078 { 00079 cerr << "\nMovieListRequest::createSocket: Error on socket create (" << host << "/" << port << ")"; 00080 return 0; 00081 } 00082 00083 if (connect(sock, (struct sockaddr*) &sin, sizeof(sin)) < 0) 00084 { 00085 cerr << "\nMovieListRequest::createSocket: Error on socket connect (" << host << "/" << port << ")"; 00086 close(sock); 00087 return 0; 00088 } 00089 00090 return sock; 00091 } 00092 00093 00094 /***********************************************************************/ 00095 char* MovieListRequest::makeHttpRequest(const char *hostname, int port, string msg, char *recbuffer, uint &contentLength) 00096 { 00097 int sock = createSocket(hostname, port); 00098 00099 if (sock > 0) 00100 { 00101 //send request to the server 00102 send(sock, (char *) msg.c_str(), sizeof(char) * msg.length(), 0); 00103 00104 //wait for response 00105 memset(recbuffer, 0, MAX_RECBUFFER_SIZE); 00106 00107 //read until no more bytes (because of MTU) 00108 uint bytesRead = 0, bytesReadCum = 0; 00109 do 00110 { 00111 bytesRead = recv(sock, recbuffer + bytesReadCum, sizeof(char) * MAX_RECBUFFER_SIZE, 0); 00112 bytesReadCum += bytesRead; 00113 } 00114 while (bytesRead != 0); 00115 00116 if (strstr(recbuffer, "HTTP") != NULL && strstr(recbuffer, "200 OK") != NULL) 00117 { 00118 00119 //look for start of real content 00120 char *content = strstr(recbuffer, "Content-Length: "); 00121 if (content != NULL) 00122 { 00123 contentLength = atoi(content+16); 00124 content = strstr(content, "\r\n\r\n"); 00125 if (content != NULL) 00126 { 00127 return content+4; 00128 } 00129 } 00130 } 00131 } 00132 00133 return NULL; 00134 } 00135 00136 /***********************************************************************/ 00137 void MovieListRequest::addIfNotExists(list<char*> &imgList, char *str) 00138 { 00139 list<char*>::iterator iter = imgList.begin(); 00140 while (iter != imgList.end()) 00141 { 00142 if (strcmp(str, *iter) == 0) return; 00143 iter++; 00144 } 00145 00146 imgList.push_back(str); 00147 } 00148 00149 /***********************************************************************/ 00150 void MovieListRequest::writeToFile(const char *filename, char *buffer, uint len) 00151 { 00152 //create outputfilestream and write bytes to 00153 Globals::sdebug << "MovieListRequest::writeToFile(): trying '" << filename << "'"; 00154 ofstream fileout(filename, ios::out); 00155 uint i = 0; 00156 while (i < len) fileout.put(*(buffer + (i++))); 00157 fileout.close(); 00158 Globals::sdebug << "MovieListRequest::writeToFile(): '" << filename << "' successfully created!"; 00159 } 00160 00161 00162 /***********************************************************************/ 00163 bool MovieListRequest::requestMovieList(const char *hostname, int port) 00164 { 00165 00166 //find out IP-Address of specified host 00167 // hostent *host = gethostbyname(hostname); 00168 // char *address = inet_ntoa(*(reinterpret_cast<in_addr*>(host->h_addr))); 00169 00170 //buffer used for receiving 00171 char *buffer = new char[MAX_RECBUFFER_SIZE]; 00172 char *bufferStart = buffer; 00173 uint contentLength; 00174 00175 //send HTTP GET message for movielist 00176 ostringstream httpMsg; 00177 //httpMsg << "GET http://" << address << "/MovieList.xml HTTP/1.1\r\n\r\n"; httpMsg.flush(); 00178 httpMsg << "GET /MovieList.xml HTTP/1.1\r\n\r\n"; httpMsg.flush(); 00179 if ((buffer = makeHttpRequest(hostname, port, httpMsg.str(), buffer, contentLength))) 00180 { 00181 00182 //write this content to a file, where the semiserver has access to 00183 writeToFile(Globals::getMoviesFileName(), buffer, contentLength); 00184 00185 //search for images 00186 list<char*> imgList; 00187 char *imgStart, *imgEnd; //37 00188 while ((imgStart = strstr(buffer, "<Resource mimeType=\"image/jpeg\" ref=\"")) != NULL) 00189 { 00190 imgEnd = strstr(imgStart+strlen("<Resource mimeType=\"image/jpeg\" ref=\""), "\""); 00191 if (imgEnd != NULL) 00192 { 00193 char *foundImg = semifunc::newStrCpy( 00194 imgStart+strlen("<Resource mimeType=\"image/jpeg\" ref=\""), 00195 imgEnd-(imgStart+strlen("<Resource mimeType=\"image/jpeg\" ref=\""))); 00196 Globals::sdebug << "\nFound Image: " << foundImg; 00197 //add this image to the list, if not already there 00198 addIfNotExists(imgList, foundImg); 00199 } 00200 buffer = imgEnd; 00201 } 00202 00203 //now request all images 00204 list<char*>::iterator iter = imgList.begin(); 00205 while (iter != imgList.end()) 00206 { 00207 char *img = *iter; 00208 ostringstream httpMsgImg; 00209 //httpMsgImg << "GET http://" << address << "/" << img << " HTTP/1.1\r\n\r\n"; 00210 httpMsgImg << "GET /" << img << " HTTP/1.1\r\n\r\n"; 00211 Globals::sdebug << "\n" << httpMsgImg.str().c_str(); 00212 if ((buffer = makeHttpRequest(hostname, port, httpMsgImg.str(), bufferStart, contentLength))) 00213 { 00214 ostringstream filename; 00215 filename << Globals::getAbsoluteImagesFilePath() << img; 00216 writeToFile(filename.str().c_str(), buffer, contentLength); 00217 } 00218 00219 iter++; 00220 } 00221 imgList.clear(); //clear list 00222 00223 delete bufferStart; 00224 bufferStart = NULL; 00225 return true; 00226 00227 } 00228 00229 00230 return false; 00231 } 00232