SessionMgmt.cpp

00001 /*********************************************************************** 00002 * * 00003 * ViTooKi * 00004 * * 00005 * title: SessionMgmt.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 "SessionMgmt.hpp" 00051 00052 vector<SSession*> SessionMgmt::list; 00053 00054 /***********************************************************************/ 00055 SessionMgmt::SessionMgmt(int port) 00056 { 00057 this->port = port; 00058 } 00059 00060 /***********************************************************************/ 00061 SessionMgmt::~SessionMgmt() 00062 { 00063 Globals::sdebug << "\nSessionMgmt::~ SessionMgmt() cleanup..."; 00064 vector<SSession*>::iterator iter = list.begin(); 00065 while (iter != list.end()) 00066 { 00067 SSession *sess = *iter; 00068 delete sess; 00069 iter++; 00070 } 00071 list.clear(); 00072 Globals::sdebug << "\nSessionMgmt::~ SessionMgmt() done!"; 00073 } 00074 00075 /***********************************************************************/ 00076 void SessionMgmt::addSession(SSession *sess) 00077 { 00078 list.push_back(sess); 00079 } 00080 00081 /***********************************************************************/ 00082 void SessionMgmt::removeSession(SSession *remove) 00083 { 00084 if (remove == NULL) return; 00085 00086 vector<SSession*>::iterator iter = list.begin(); 00087 while (iter != list.end()) 00088 { 00089 SSession *sess = *iter; 00090 if (sess->getRtspSessionId() == remove->getRtspSessionId()) 00091 { 00092 Globals::sdebug << "\nSM::removeSession: session " << sess->getRtspSessionId() << " REMOVED !!!"; 00093 iter = list.erase(iter); 00094 } 00095 else 00096 { 00097 iter++; 00098 } 00099 } 00100 00101 } 00102 00103 /***********************************************************************/ 00104 SSession* SessionMgmt::getSession(uint RtspSessionId) 00105 { 00106 00107 vector<SSession*>::const_iterator iter = list.begin(); 00108 while (iter != list.end()) 00109 { 00110 SSession *sess = *iter; 00111 if (sess->getRtspSessionId() == RtspSessionId) 00112 { 00113 return sess; 00114 } 00115 iter++; 00116 } 00117 00118 //not found 00119 return NULL; 00120 00121 } 00122 00123 /***********************************************************************/ 00124 void SessionMgmt::listSessions() 00125 { 00126 00127 Globals::sdebug << "\nSM::listSessions(): Active Sessions:"; 00128 vector<SSession*>::const_iterator iter = list.begin(); 00129 while (iter != list.end()) 00130 { 00131 SSession *sess = *iter; 00132 00133 cout << endl << " - session: " << sess->getRtspSessionId(); 00134 cout << endl << " " << sess->getUrl(); 00135 cout << endl << " " << sess->getClientAdr(); 00136 cout << endl << " " << sess->getSemiMovieId(); 00137 cout << endl << " " << sess->getOriginRtspSessionId(); 00138 cout << endl << " " << sess->getElapsedMsecs(); 00139 cout << endl << " " << sess->getBufferingDelay()<< endl; 00140 iter++; 00141 } 00142 00143 } 00144 00145 /***********************************************************************/ 00146 int SessionMgmt::listenForServerRequests(int port) 00147 { 00148 struct sockaddr_in sin, caller; 00149 int sock, newsocket; 00150 socklen_t len; 00151 00152 memset((char*) &sin, 0, sizeof(sin)); 00153 sin.sin_family = AF_INET; 00154 sin.sin_addr.s_addr = INADDR_ANY; 00155 sin.sin_port = htons(port); 00156 00157 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) 00158 { 00159 cerr << "\nSM::openSocket(): Error: Socket-Create"; 00160 return 1; 00161 } 00162 00163 const int reuse = 1; 00164 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse, sizeof(reuse)) < 0) 00165 { 00166 cerr << "\nSM::openSocket(): Error: Socket-SetSockOpt"; 00167 return 1; 00168 } 00169 00170 if (bind(sock, (struct sockaddr*) &sin, sizeof(sin)) < 0) 00171 { 00172 cerr << "\nSM::openSocket(): Error: Socket-Bind"; 00173 return 1; 00174 } 00175 00176 //listen for new connections on this socket.. 00177 listen(sock, MAX_PENDING); 00178 00179 cout << "\nSM::openSocket(): accepting connections on port " << port <<" ..."; 00180 while (SemiProxy::isSemiProxyRunning()) 00181 { 00182 //accept connections 00183 if ((newsocket = accept(sock, (struct sockaddr*) &caller, &len)) < 0) 00184 { 00185 cerr << "\nSM::openSocket(): Error: Socket-Accept"; 00186 return 1; 00187 } 00188 00189 Globals::sdebug << "\nSM::openSocket(): socket connected with " << inet_ntoa(caller.sin_addr) << " on port " << caller.sin_port; 00190 00191 //create a server-communication object and send all active 00192 //streaming-sessions through this socket... 00193 ServerCommunication *serverComm = new ServerCommunication(newsocket, &caller, this); 00194 serverComm->sendSessionsToServer(); 00195 delete serverComm; 00196 serverComm = NULL; 00197 00198 //sleep 20 microseconds 00199 #ifndef WIN32 00200 usleep(20); 00201 #else 00202 Sleep(20); 00203 #endif 00204 } 00205 00206 return 0; //success; 00207 } 00208 00209 /***********************************************************************/ 00210 int SessionMgmt::getListSize() 00211 { 00212 return list.size(); 00213 } 00214 00215 /***********************************************************************/ 00216 vector<SSession*>* SessionMgmt::getVector() 00217 { 00218 return &list; 00219 } 00220 00221 /***********************************************************************/ 00222 void SessionMgmt::run() 00223 { 00224 listenForServerRequests(port); 00225 this->exit(); 00226 00227 } 00228 00229