Update of /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/servlet
In directory sc8-pr-cvs1:/tmp/cvs-serv26010/src/server/http/servlet
Modified Files:
HttpSession.h HttpSession.cpp HttpServletRequest.cpp
Added Files:
SessionManager.h SessionManager.cpp
Log Message:
31/12/2003 Mikael Barbeaux
* Implemented a Session manager.
* Implemented HttpSession code source.
--- NEW FILE: SessionManager.h ---
/*
* This file is part of webInterface.
* Copyright (C) 2003 Mikael Barbeaux <mik...@us...>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "HttpSession.h"
#include <map>
#include <vector>
/**
* Defines a manager for Session objects.
* Used to retrieve sessions for servlet request, and
* to manage them ( time life, creation, destruction... )
* Implements the singleton design pattern.
*/
class SessionManager{
private:
// unique instance of SessionManager
static SessionManager *instance;
// sessions managed
map<string, HttpSession*> sessions;
protected:
/**
* Creates a SessionManager object.
*/
SessionManager();
public:
/**
* Destructor for SessionManager.
*/
~SessionManager();
/**
* Returns the unique instance of SessionManager
*
* @return SessionManager*
*/
static SessionManager *getInstance();
/**
* Returns the session associated to this id,
* or create a new one if the id is empty.
*
* @param session_id
* @return HttpSession*
*/
HttpSession *getSession(string session_id);
/**
* Returns all the ids defined.
*
* @return vector<string>
*/
vector<string> getIds();
};
--- NEW FILE: SessionManager.cpp ---
/*
* This file is part of webInterface.
* Copyright (C) 2003 Mikael Barbeaux <mik...@us...>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SessionManager.h"
SessionManager *SessionManager::instance = 0;
/**
* Creates a SessionManager objet.
*/
SessionManager::SessionManager() {
}
/**
* Destructor for SessionManager.
* Deletes all Session included in the manager.
*/
SessionManager::~SessionManager() {
}
/**
* Returns the unique instance of SessionManager.
*/
SessionManager *SessionManager::getInstance() {
if(instance == 0)
instance = new SessionManager();
return instance;
}
/**
* Returns the session associated to this id,
* or create a new one if the id is empty.
*/
HttpSession *SessionManager::getSession(string session_id) {
map<string, HttpSession*>::iterator it = sessions.find(session_id);
if(it != sessions.end())
return (*it).second;
else {
// create a session
return 0;
}
}
/**
* Returns all the ids defined.
*/
vector<string> SessionManager::getIds() {
map<string, HttpSession*>::iterator it = sessions.begin();
vector<string> ids(sessions.size());
int i=0;
while(it != sessions.end()) {
ids[i] = (*it).first;
i++; it++;
}
return ids;
}
Index: HttpSession.h
===================================================================
RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/servlet/HttpSession.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- HttpSession.h 30 Dec 2003 17:11:59 -0000 1.1
+++ HttpSession.h 31 Dec 2003 10:43:58 -0000 1.2
@@ -20,6 +20,7 @@
#include <time.h>
#include <vector>
#include <string>
+#include <map>
using namespace std;
/**
@@ -27,13 +28,26 @@
* into servlets.
*/
class HttpSession {
+
+ private:
+
+ // its id
+ string session_id;
+ // attributes bounded to this session.
+ map<string, void*> attributes;
+ // session valid ?
+ bool valid;
+ // creation time
+ time_t creation_time;
+ // last accessed time
+ time_t last_accessed;
public:
/**
* Creates a HttpSession object.
*/
- HttpSession();
+ HttpSession(string id);
/**
* Destructor for HttpSession.
Index: HttpSession.cpp
===================================================================
RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/servlet/HttpSession.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- HttpSession.cpp 30 Dec 2003 17:11:59 -0000 1.1
+++ HttpSession.cpp 31 Dec 2003 10:43:58 -0000 1.2
@@ -22,62 +22,85 @@
/**
* Creates a HttpSession object.
*/
-HttpSession::HttpSession() {
+HttpSession::HttpSession(string id) {
+ session_id = id;
+ valid = true;
+ creation_time = time(NULL);
}
/**
* Destructor for HttpSession.
*/
HttpSession::~HttpSession() {
+ if(valid)
+ invalidate();
}
/**
* Returns the value of a given attribute.
- *
*/
void *HttpSession::getAttribute(string name) {
- return 0;
+ map<string, void*>::iterator it = attributes.find(name);
+ if(it == attributes.end())
+ return 0;
+ else return (*it).second;
}
/**
* Returns a vector of all attribute names.
*/
vector<string> HttpSession::getAttributeNames() {
- vector<string> v;
- return v;
+ map<string, void*>::iterator it = attributes.begin();
+ vector<string> names(attributes.size());
+ int i=0;
+ while(it != attributes.end()) {
+ names[i] = (*it).first;
+ i++; it++;
+ }
+ return names;
}
/**
* Returns the creation time of this session.
*/
time_t HttpSession::getCreationTime() {
- return 0;
+ return creation_time;
}
/**
* Returns the identifiant of this session.
*/
string HttpSession::getId() {
- return "";
+ return session_id;
}
/**
* Returns the last accessed time to this servlet.
*/
time_t HttpSession::getLastAccessedTime() {
- return 0;
+ return last_accessed;
}
/**
* Invalidates this session.
*/
void HttpSession::invalidate() {
+ valid = false;
+ // Unbound any attributes
+ map<string, void*>::iterator it = attributes.begin();
+ while(it != attributes.end()) {
+ attributes.erase(it);
+ it++;
+ }
}
/**
* Removes the given attribute from the session.
*/
void HttpSession::removeAttribute(string name) {
+ map<string, void*>::iterator it = attributes.find(name);
+ if(it != attributes.end())
+ attributes.erase(it);
}
/**
@@ -85,5 +108,6 @@
* name and value.
*/
void HttpSession::setAttribute(string name, void *value) {
+ attributes[name] = value;
}
Index: HttpServletRequest.cpp
===================================================================
RCS file: /cvsroot/sharedaemon/sharedaemon-ui-web/src/server/http/servlet/HttpServletRequest.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- HttpServletRequest.cpp 30 Dec 2003 17:11:59 -0000 1.2
+++ HttpServletRequest.cpp 31 Dec 2003 10:43:58 -0000 1.3
@@ -22,7 +22,7 @@
/**
* Creates a HttpServletRequest object.
*/
-HttpServletRequest::HttpServletRequest() {
+HttpServletRequest::HttpServletRequest() : HttpRequest() {
}
/**
|