SemiGlobals.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: SemiGlobals.cpp (S H A R E D) * 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 "SemiGlobals.hpp" 00051 00052 00053 00054 00055 00056 00057 00058 /***********************************************************************/ 00059 SemiDebug::SemiDebug(char *logfile) 00060 { 00061 stdOutEnabled = false; 00062 shouldFlush = false; 00063 this->logfile = logfile; 00064 if (logfile) outfile.open(logfile); 00065 writeTimestamp("Log started on on "); 00066 } 00067 00068 00069 /***********************************************************************/ 00070 SemiDebug::~SemiDebug() 00071 { 00072 writeTimestamp("\r\nLog ended on on "); 00073 writeLogfile(); 00074 } 00075 00076 /***********************************************************************/ 00077 void SemiDebug::doFlush() 00078 { 00079 cout.flush(); 00080 00081 if (logfile) { 00082 uint i = 0; 00083 while (i < log.size()) 00084 { 00085 outfile << log[i]; 00086 log.erase(log.begin()); 00087 } 00088 //force write of file 00089 outfile.close(); 00090 outfile.open(logfile, ios::app); 00091 } 00092 } 00093 00094 /***********************************************************************/ 00095 void SemiDebug::writeTimestamp(char *str) 00096 { 00097 if (logfile) { 00098 #ifndef WIN32 00099 struct timeval currTime; 00100 gettimeofday(&currTime, NULL); 00101 uint hour = (currTime.tv_sec / 3600) % 24; 00102 uint minute = (currTime.tv_sec / 60 ) % 60; 00103 uint second = currTime.tv_sec % 60; 00104 #else 00105 SYSTEMTIME currTime; 00106 GetLocalTime(&currTime); 00107 uint hour = currTime.wHour; 00108 uint minute = currTime.wMinute; 00109 uint second = currTime.wSecond; 00110 #endif 00111 ostringstream ostr; 00112 ostr << str << hour << ":" << minute << ":" << second; 00113 log.push_back(ostr.str()); 00114 if (shouldFlush) doFlush(); 00115 } 00116 } 00117 00118 /***********************************************************************/ 00119 void SemiDebug::writeLogfile() 00120 { 00121 if (logfile) { 00122 00123 if (log.size() > 0 && outfile) 00124 { 00125 00126 vector<string>::const_iterator iter = log.begin(); 00127 while (iter != log.end()) 00128 { 00129 outfile << *iter++; 00130 } 00131 } 00132 00133 if (outfile) 00134 { 00135 outfile.close(); 00136 Globals::sdebug << "SemiDebug::writeLogfile(): logfile written \r\n"; 00137 } 00138 } 00139 } 00140 00141 /***********************************************************************/ 00142 void SemiDebug::enableStdOut() 00143 { 00144 this->stdOutEnabled = true; 00145 } 00146 00147 /***********************************************************************/ 00148 void SemiDebug::enableFlush() 00149 { 00150 this->shouldFlush = true; 00151 } 00152 00153 /***********************************************************************/ 00154 SemiDebug& SemiDebug::operator << (unsigned int n) 00155 { 00156 if (logfile) log.push_back(semifunc::toString(n)); 00157 00158 if (stdOutEnabled) cout << n; 00159 if (shouldFlush) doFlush(); 00160 return *this; 00161 } 00162 00163 00164 /***********************************************************************/ 00165 SemiDebug& SemiDebug::operator << (int n) 00166 { 00167 if (logfile) log.push_back(semifunc::toString(n)); 00168 00169 if (stdOutEnabled) cout << n; 00170 if (shouldFlush) doFlush(); 00171 return *this; 00172 } 00173 00174 /***********************************************************************/ 00175 SemiDebug& SemiDebug::operator << (unsigned long n) 00176 { 00177 if (logfile) log.push_back(semifunc::toString(n)); 00178 00179 if (stdOutEnabled) cout << n; 00180 if (shouldFlush) doFlush(); 00181 return *this; 00182 } 00183 00184 00185 /***********************************************************************/ 00186 SemiDebug& SemiDebug::operator << (long n) 00187 { 00188 if (logfile) log.push_back(semifunc::toString(n)); 00189 00190 if (stdOutEnabled) cout << n; 00191 if (shouldFlush) doFlush(); 00192 return *this; 00193 } 00194 00195 00196 /***********************************************************************/ 00197 SemiDebug& SemiDebug::operator << (float f) 00198 { 00199 if (logfile) log.push_back(semifunc::toString(f)); 00200 00201 if (stdOutEnabled) cout << f; 00202 if (shouldFlush) doFlush(); 00203 return *this; 00204 } 00205 00206 00207 /***********************************************************************/ 00208 SemiDebug& SemiDebug::operator << (double d) 00209 { 00210 if (logfile) log.push_back(semifunc::toString(d)); 00211 00212 if (stdOutEnabled) cout << d; 00213 if (shouldFlush) doFlush(); 00214 return *this; 00215 } 00216 00217 00218 /***********************************************************************/ 00219 SemiDebug& SemiDebug::operator << (char c) 00220 { 00221 if (logfile) log.push_back(semifunc::toString(c)); 00222 00223 if (stdOutEnabled) cout << c; 00224 if (shouldFlush) doFlush(); 00225 return *this; 00226 } 00227 00228 00229 /***********************************************************************/ 00230 SemiDebug& SemiDebug::operator << (char *str) 00231 { 00232 string strg (str); 00233 semifunc::replaceStr(strg, "\r\n", "\r \n\t", true); 00234 if (logfile) log.push_back(strg); 00235 00236 if (stdOutEnabled) cout << strg; 00237 if (shouldFlush) doFlush(); 00238 return *this; 00239 } 00240 00241 /***********************************************************************/ 00242 SemiDebug& SemiDebug::operator << (const char *str) 00243 { 00244 return *this << (char*)str; 00245 } 00246 00247 00248 /***********************************************************************/ 00249 SemiDebug& SemiDebug::operator << (string &str) 00250 { 00251 string strg (str); 00252 semifunc::replaceStr(strg, "\r\n", "\r \n\t", true); 00253 if (logfile) log.push_back(strg); 00254 00255 if (stdOutEnabled) cout << strg; 00256 if (shouldFlush) doFlush(); 00257 return *this; 00258 } 00259 00260 00261 00262 00263 00264 //define static variables 00265 bool Globals::initialized = false; 00266 //SemiDebug Globals::sdebug ("./semilog.txt"); 00267 SemiDebug Globals::sdebug (NULL); 00268 map<char*,char*> Globals::config; 00269 map<char*, char*> Globals::configDesc; 00270 00271 00272 /***********************************************************************/ 00273 char *Globals::getConfigFilePath() 00274 { 00275 char *dest = new char[100]; 00276 if (getenv("HOME") != NULL) { 00277 strcpy(dest, getenv("HOME")); 00278 strcat(dest, "/.semirc"); 00279 } else { 00280 strcpy(dest, "/.semirc"); 00281 } 00282 return dest; 00283 } 00284 00285 /***********************************************************************/ 00286 void Globals::initialize() 00287 { 00288 loadConfiguration(getConfigFilePath()); 00289 initialized = true; 00290 } 00291 00292 /***********************************************************************/ 00293 string Globals::getCGIPath() 00294 { 00295 if (!initialized) initialize(); 00296 return config["CGIPath"]; 00297 } 00298 00299 /***********************************************************************/ 00300 char *Globals::getProxyComHost() 00301 { 00302 if (!initialized) initialize(); 00303 return config["ProxyComHost"]; 00304 } 00305 00306 /***********************************************************************/ 00307 int Globals::getProxyComPort() 00308 { 00309 if (!initialized) initialize(); 00310 return atoi(config["ProxyComPort"]); 00311 } 00312 00313 /***********************************************************************/ 00314 char *Globals::getMoviesFileName() 00315 { 00316 if (!initialized) initialize(); 00317 return config["MoviesFilePath"]; 00318 } 00319 00320 /***********************************************************************/ 00321 char *Globals::getAbsoluteImagesFilePath() 00322 { 00323 if (!initialized) initialize(); 00324 return config["AbsoluteImagesFilePath"]; 00325 } 00326 00327 /***********************************************************************/ 00328 char *Globals::getRelativeImagesFilePath() 00329 { 00330 if (!initialized) initialize(); 00331 return config["RelativeImagesFilePath"]; 00332 } 00333 00334 /***********************************************************************/ 00335 char *Globals::getMediaserverHttpHost() 00336 { 00337 if (!initialized) initialize(); 00338 return config["MediaserverHttpHost"]; 00339 } 00340 00341 /***********************************************************************/ 00342 int Globals::getMediaserverHttpPort() 00343 { 00344 if (!initialized) initialize(); 00345 return atoi(config["MediaserverHttpPort"]); 00346 } 00347 00348 /***********************************************************************/ 00349 char *Globals::getAbsoluteSDPFilePath() 00350 { 00351 if (!initialized) initialize(); 00352 return config["AbsoluteSDPFilePath"]; 00353 } 00354 00355 /***********************************************************************/ 00356 char *Globals::getRelativeSDPFilePath() 00357 { 00358 if (!initialized) initialize(); 00359 return config["RelativeSDPFilePath"]; 00360 } 00361 00362 uint Globals::getHoursToPreserveClosedSessions() 00363 { 00364 if (!initialized) initialize(); 00365 return atoi(config["HoursToPreserveClosedSessions"]); 00366 } 00367 00368 uint Globals::getPRMaxWaitSecs() 00369 { 00370 if (!initialized) initialize(); 00371 return atoi(config["PRMaxWaitSecs"]); 00372 } 00373 00374 00375 00376 /***********************************************************************/ 00377 void Globals::readConfigValues(char *buffer) 00378 { 00379 00380 uint i = 0, k; 00381 while (buffer[i] != 0) 00382 { 00383 //ignore users part 00384 if (strncmp(buffer+i, "############USERS start############", strlen("############USERS start############")) == 0) { 00385 uint oldi = i; 00386 while (strncmp(buffer+i, "############USERS end############", strlen("############USERS end############")) != 0) i++; 00387 sdebug << "\nusers part ignored (" << (i-oldi) << " bytes)"; 00388 } 00389 00390 //ignore profiles part 00391 if (strncmp(buffer+i, "############PROFILES start############", strlen("############PROFILES start############")) == 0) { 00392 uint oldi = i; 00393 while (strncmp(buffer+i, "############PROFILES end############", strlen("############PROFILES end############")) != 0) i++; 00394 sdebug << "\nprofiles part ignored (" << (i-oldi) << " bytes)"; 00395 } 00396 00397 //ignore comments 00398 if (buffer[i] == '#') 00399 { 00400 //look for end of line 00401 while (buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != 0) i++; 00402 if (buffer[i] == 0) break; 00403 } 00404 else if (buffer[i] == '\r' || buffer[i] == '\n') 00405 { 00406 //ignore empty lines 00407 i++; 00408 } 00409 else 00410 { 00411 //look for param 00412 k = i; 00413 while (buffer[i] != '=' && buffer[i] != 0) i++; 00414 k = i - k; 00415 if (k == 0) break; 00416 00417 char *param = new char[k + 1]; 00418 strncpy(param, &buffer[i-k], k); 00419 param[k] = 0; 00420 00421 //look for value 00422 i++; 00423 //look for end of line 00424 k = i; 00425 while (buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != 0) i++; 00426 k = i - k; 00427 if (k == 0) break; 00428 00429 char *val = new char[k + 1]; 00430 strncpy(val, &buffer[i-k], k); 00431 val[k] = 0; 00432 00433 sdebug << "\nGlobals::readConfigValues(): found param:" << param << "=" << val; 00434 00435 if (strcmp(param, "CGIPath") == 0) config["CGIPath"] = val; 00436 else if (strcmp(param, "ProxyComHost") == 0) config["ProxyComHost"] = val; 00437 else if (strcmp(param, "ProxyComPort") == 0) config["ProxyComPort"] = val; 00438 else if (strcmp(param, "MoviesFilePath") == 0) config["MoviesFilePath"] = val; 00439 else if (strcmp(param, "AbsoluteImagesFilePath") == 0) config["AbsoluteImagesFilePath"] = val; 00440 else if (strcmp(param, "RelativeImagesFilePath") == 0) config["RelativeImagesFilePath"] = val; 00441 else if (strcmp(param, "MediaserverHttpHost") == 0) config["MediaserverHttpHost"] = val; 00442 else if (strcmp(param, "MediaserverHttpPort") == 0) config["MediaserverHttpPort"] = val; 00443 else if (strcmp(param, "AbsoluteSDPFilePath") == 0) config["AbsoluteSDPFilePath"] = val; 00444 else if (strcmp(param, "RelativeSDPFilePath") == 0) config["RelativeSDPFilePath"] = val; 00445 else if (strcmp(param, "HoursToPreserveClosedSessions") == 0) config["HoursToPreserveClosedSessions"] = val; 00446 else if (strcmp(param, "PRMaxWaitSecs") == 0) config["PRMaxWaitSecs"] = val; 00447 } 00448 } 00449 } 00450 00451 /***********************************************************************/ 00452 void Globals::loadDefaultConfiguration() 00453 { 00454 00455 config["CGIPath"] = "/cgi-bin/"; 00456 config["ProxyComHost"] = "localhost"; 00457 config["ProxyComPort"] = "6556"; 00458 config["MoviesFilePath"] = "metadata/MovieList.xml"; 00459 config["AbsoluteImagesFilePath"] = "images/"; 00460 config["RelativeImagesFilePath"] = "/images/"; 00461 config["MediaserverHttpHost"] = "localhost"; 00462 config["MediaserverHttpPort"] = "8080"; 00463 config["AbsoluteSDPFilePath"] = "SDPs/"; 00464 config["RelativeSDPFilePath"] = "/SDPs/"; 00465 config["HoursToPreserveClosedSessions"] = "12"; 00466 config["PRMaxWaitSecs"] = "3"; 00467 00468 configDesc["CGIPath"] = "#path of cgi-files used by the semiserver"; 00469 configDesc["ProxyComHost"] = "#host running semiproxy (used from semiserver)"; 00470 configDesc["ProxyComPort"] = "#port of semiproxy used to communicate with semiserver programs"; 00471 configDesc["MoviesFilePath"] = "#filepath to movielist (which was requested from mediaserver)"; 00472 configDesc["AbsoluteImagesFilePath"] = "#absolute path of images (used from semiserver to store requested images)"; 00473 configDesc["RelativeImagesFilePath"] = "#relative path of images used from semiserver"; 00474 configDesc["MediaserverHttpHost"] = "#host running the http-server of the mediaserver"; 00475 configDesc["MediaserverHttpPort"] = "#port running the http-server of the mediaserver"; 00476 configDesc["AbsoluteSDPFilePath"] = "#absolute path of SDP-files (used from semiserver to store on-the-fly created SDP files)"; 00477 configDesc["RelativeSDPFilePath"] = "#relative path of SDP-files used from semiserver"; 00478 configDesc["HoursToPreserveClosedSessions"] = "#number of hours, a closed session should be remained in the list of started sessions"; 00479 configDesc["PRMaxWaitSecs"] = "#maximum number of seconds to wait for the response of a position request\r\n#(if this number is reached, the proxy will abort waiting for a response)"; 00480 } 00481 00482 /***********************************************************************/ 00483 void Globals::showCurrentConfiguration() 00484 { 00485 if (!initialized) initialize(); 00486 00487 typedef map<char*, char*>::const_iterator MI; 00488 00489 cout << "------------Current Configuration------------" << endl; 00490 for (MI c = config.begin(); c != config.end(); ++c) 00491 { 00492 cout << c->first << "=" << c->second << endl; 00493 } 00494 cout << "---------------------------------------------" << endl; 00495 } 00496 00497 /***********************************************************************/ 00498 void Globals::writeConfiguration(char *filename) 00499 { 00500 ofstream fout (filename); 00501 00502 typedef map<char*, char*>::const_iterator MI; 00503 00504 for (MI c = config.begin(); c != config.end(); ++c) 00505 { 00506 fout << configDesc[c->first] << endl; 00507 fout << c->first << "=" << c->second << endl << endl; 00508 } 00509 00510 //write default users to config file 00511 fout << "\n\n############USERS start############\n"; 00512 fout << "# This file is used for users.!\n"; 00513 fout << "# Each user starts with a name (enclosed by brackets)\n"; 00514 fout << "# and has the following properties:\n"; 00515 fout << "# - username (in brackets)\n"; 00516 fout << "# - userid\n"; 00517 fout << "# - password\n"; 00518 fout << "#\n"; 00519 fout << "# please do not use characters like öäüß...\n"; 00520 fout << "#\n"; 00521 fout << "[adam]\n"; 00522 fout << " uid=1\n"; 00523 fout << " pwd=adam\n\n"; 00524 fout << "[eve]\n"; 00525 fout << " uid=2\n"; 00526 fout << " pwd=eve\n\n"; 00527 fout << "[klaus]\n"; 00528 fout << " uid=3\n"; 00529 fout << " pwd=klaus\n\n"; 00530 fout << "############USERS end############\n"; 00531 00532 //write default profiles to config file 00533 fout << "\n\n############PROFILES start############\n"; 00534 fout << "# This file is used for profiles.!\n"; 00535 fout << "# Each profile starts with a name (enclosed by brackets) \n"; 00536 fout << "# and has the following properties:\n"; 00537 fout << "#\tprofileId\t\tID of profil\n"; 00538 fout << "#\t\t\t\tPLEASE USE CONSECUTIVE IDs!\n"; 00539 fout << "#\t- uids\t\t\tIDs of users which are allowed, to \n"; 00540 fout << "#\t\t\t\tto use this profile\n"; 00541 fout << "#\t- width\t\t\tWidth of display in pixel\n"; 00542 fout << "#\t- height\t\tHeight of display in pixel\n"; 00543 fout << "#\t- color\t\t\tIs display color-capable ('yes'/'no')\n"; 00544 fout << "#\t- bpp\t\t\tNumber of bits-per-pixel used for display\n"; 00545 fout << "#\t- bufferingDelay\t\tBuffering-delay of the used device/player\n"; 00546 fout << "#\t\t\t\tin milliseconds\n"; 00547 fout << "#\t\t\t\tTHIS VALUE WILL ONLY BE USED, IF THE \n"; 00548 fout << "#\t\t\t\tPLAYER DO NOT ANNOUNCE AN DELAY ITSELF!\n"; 00549 fout << "#\t- netkBps\t\tNetwork bandwidth (in kilo-Bytes per sec)\n"; 00550 fout << "#\t- refreshRate\t\tRefreshrate in Hertz (e.g. 100)\n"; 00551 fout << "#\n"; 00552 fout << "# please do not use characters like öäüß... \n"; 00553 fout << "#\n"; 00554 fout << "[Common (CIF)]\n"; 00555 fout << " profileId=1\n"; 00556 fout << " uids=1,2,3\n"; 00557 fout << " width=352\n"; 00558 fout << " height=288\n"; 00559 fout << " color=yes\n"; 00560 fout << " bpp=16\n"; 00561 fout << " bufferingDelay=8000\n"; 00562 fout << " netkBps=128\n"; 00563 fout << " refreshRate=100\n\n"; 00564 fout << "[Common Gray]\n"; 00565 fout << " profileId=2\n"; 00566 fout << " uids=1,2,3\n"; 00567 fout << " width=352\n"; 00568 fout << " height=288\n"; 00569 fout << " color=no\n"; 00570 fout << " bpp=16\n"; 00571 fout << " bufferingDelay=8000\n"; 00572 fout << " netkBps=128\n"; 00573 fout << " refreshRate=100\n\n"; 00574 fout << "[iPAQ]\n"; 00575 fout << " profileId=3\n"; 00576 fout << " uids=3\n"; 00577 fout << " width=192\n"; 00578 fout << " height=144\n"; 00579 fout << " color=yes\n"; 00580 fout << " bpp=16\n"; 00581 fout << " bufferingDelay=9000\n"; 00582 fout << " netkBps=64\n"; 00583 fout << " refreshRate=100\n\n"; 00584 fout << "[iPAQ Gray]\n"; 00585 fout << " profileId=4\n"; 00586 fout << " uids=3\n"; 00587 fout << " width=192\n"; 00588 fout << " height=144\n"; 00589 fout << " color=no\n"; 00590 fout << " bpp=16\n"; 00591 fout << " bufferingDelay=9000\n"; 00592 fout << " netkBps=64\n"; 00593 fout << " refreshRate=100\n\n"; 00594 fout << "[iPAQ small]\n"; 00595 fout << " profileId=5\n"; 00596 fout << " uids=3\n"; 00597 fout << " width=96\n"; 00598 fout << " height=72\n"; 00599 fout << " color=yes\n"; 00600 fout << " bpp=16\n"; 00601 fout << " bufferingDelay=9000\n"; 00602 fout << " netkBps=64\n"; 00603 fout << " refreshRate=100\n\n"; 00604 fout << "[iPAQ small gray]\n"; 00605 fout << " profileId=6\n"; 00606 fout << " uids=3\n"; 00607 fout << " width=96\n"; 00608 fout << " height=72\n"; 00609 fout << " color=no\n"; 00610 fout << " bpp=16\n"; 00611 fout << " bufferingDelay=9000\n"; 00612 fout << " netkBps=64\n"; 00613 fout << " refreshRate=100\n"; 00614 fout << "############PROFILES end############\n"; 00615 00616 fout.close(); 00617 } 00618 00619 /*********************************************************************** 00620 Loads the SeMi configuration from the specified config-file. 00621 If the file not exists, this function writes the default config-file. 00622 ***********************************************************************/ 00623 bool Globals::loadConfiguration(char *filename) 00624 { 00625 char buffer [CONFIGFILE_MAXLEN]; 00626 ifstream fin (filename); 00627 00628 //first load default configuration 00629 loadDefaultConfiguration(); 00630 00631 if (!fin) 00632 { 00633 cerr << "\nGlobals::loadConfiguration(): No SeMi-configfile detected!" << endl; 00634 writeConfiguration(filename); 00635 cout << "A default-configfile was written to:" << filename << endl; 00636 return false; 00637 } 00638 else 00639 { 00640 memset(buffer, 0, CONFIGFILE_MAXLEN); 00641 fin.read(buffer, CONFIGFILE_MAXLEN); 00642 fin.close(); 00643 00644 readConfigValues(buffer); 00645 00646 return true; 00647 } 00648 } 00649 00650 00651