#include "sessionIPC.h"
sessionIPC::sessionIPC(): sessionList() {
// key_t key;
// int shmid, semid;
if (debug) cerr << "START sessionIPC ctor START " << endl;
key=ftok(FKEY,PROJ);
semid=getSemID(key);
if(semid<0)
cerr << "fatal error in sessionLIstIPC constructor" << endl;
shmid=getShmID(key);
if(shmid<0)
cerr << "fatal error in sessionLIstIPC constructor" << endl;
first=(node *)shmat(shmid,NULL,0);
buf={0,-1,0};
if(semop(semid,&buf,1)==-1) {
perror("semop");
if (debug) cerr << "semop returned -1" ;
}
if (debug) cerr << "END sessionIPC ctor END " ;
}
sessionIPC::~sessionIPC() {
if (debug) cerr << "START sessionIPC dtor START " ;
if (first != 0) {
node * currentPtr = first, *tempPtr;
while (currentPtr != 0) {
tempPtr = currentPtr;
currentPtr = currentPtr->getNext();
delete tempPtr;
}
}
shmdt(first);
unlockSession(semid);
if (debug) cerr << "END sessionIPC dtor END " ;
}
// does insert regardless of session with that _id does(n't) exist
// has inline isEmpty()
// returns true on addition
bool sessionIPC::addSession (session & session_in) {
if (debug) cerr << "START sessionIPC::addSession START " ;
node * newnode;
session currentSession;
std::string session_in_id = session_in.get_id();
std::string currentSid;
if (first == 0) {// list is empty -- start the list
first = new node (session_in); // create a node object
if (debug) cerr << "END sessionIPC::addSession END " ;
return true;
}
node * current, * previous;
previous = current = first; // start at head of list
while (current) { // need check if at end of list so we don't SIGSEV
currentSession = current->getSession();
currentSid = currentSession.get_id();
if (session_in_id == currentSid) {
numsessions++;
currentSession = session_in;
if (debug) cerr << "END sessionIPC::addSession END " ;
return true;
}
previous=current;
current=current->getNext();
}
// insert at end
newnode = new node(session_in); // create a node object
// in preparation for insert
if (!newnode) {
std::cout << "\nCannot create node." << std::endl;
if (debug) cerr << "END sessionIPC::addSession END " ;
return false;
}
// set new node to point to following node
previous->setNext (newnode);//only make previous point to something in_ide loop
numsessions++;
if (debug) cerr << "END sessionIPC::addSession END " ;
return true;
}
// returns true: found session
// returns false: did not find session
bool sessionIPC::getSession(session & session_out, const string & _id) {
if (debug) cerr << "START sessionIPC::getSession START " ;
node * current = first;
session current_session;
string current_id;
while(current) {
current_session = current->getSession();
current_id = current_session.get_id();
if(_id == current_id) {
session_out = current_session;
// unlockSession(semid);
if (debug) cerr << "END sessionIPC::getSession END " ;
return true;
}
current = current->getNext();
}
if (debug) cerr << "END sessionIPC::getSession END " ;
return false;
}
// returns true: found session
// returns false: did not find session
bool sessionIPC::findSession(const string & _id_in) {
if (debug) cerr << "START sessionIPC::findSession START " ;
string _id = _id_in;
node * current = first;
session current_session;
string current_id;
// lockSession(semid);
while(current) {
current_session = current->getSession();
current_id = current_session.get_id();
if(_id == current_id) {
// unlockSession(semid);
if (debug) cerr << "END sessionIPC::findSession END " ;
return true;
}
current = current->getNext();
}
// unlockSession(semid);
if (debug) cerr << "END sessionIPC::findSession END " ;
return false;
}
void sessionIPC::cleanUp() {
if (debug) cerr << "START sessionIPC::cleanUp() START " ;
fprintf(stderr,"cleaning up ...\n");
node * current = first;
node * temp = new node();
node * previous = new node();
session current_session;
time_t current_time;
time_t now = time(NULL);
while (current) {
current_session = current->getSession();
current_time = current_session.getTime();
if((now - current_time)>EXPIRE) {
// pointer arithmetic
temp = current->getNext();
previous->setNext(temp);
delete current;
//
numsessions--;
}
previous = current;
current=current->getNext();
current_time = current_session.getTime();
}
if (debug) cerr << "END sessionIPC::cleanUp() END " ;
return;
}
bool sessionIPC::removeSession(session & session_in) {
if (debug) cerr << "START sessionIPC::removeSession() START " ;
node * current = first;
node * temp = new node();
node * previous = new node();
session current_session;
string _id_to_remove = session_in.get_id();
string current_id;
while (current) {
current_session = current->getSession();
current_id = current_session.get_id();
if(_id_to_remove == current_id) {
// pointer arithmetic
temp = current->getNext();
previous->setNext(temp);
delete current;
//
numsessions--;
if (debug) cerr << "END sessionIPC::removeSession() END " ;
return true;
}
current=current->getNext();
}
if (debug) cerr << "END sessionIPC::removeSession() END " ;
return false;
}
std::ostream& operator << (std::ostream & lhs, sessionIPC& rhs ) {
// declarations
if (debug) cerr << "START sessionIPC::operator<<() START " ;
session theSession;
node * current = rhs.getFirst();
int i = 0;
string _id;
time_t current_time;
char * data = new char[1024];
lhs << std::setiosflags(std::ios::left);
lhs << "++++++++++++++++++++++ BEGIN printSessions() ++++++++++++++++++++++++++++++ " << endl;
//
while (current) {
theSession=current->getSession();
lhs << theSession.get_id() << " | ";
lhs << theSession.getSessionKeysAndValues();
current_time = theSession.getTime();
lhs << " | " ;
lhs << current_time << endl;
current=current->getNext();
} // end while current
lhs << "++++++++++++++++++++++ END printSessions() ++++++++++++++++++++++++++++++ " << endl;
delete data;
if (debug) cerr << "END sessionIPC::operator<<() END " ;
return lhs;
}
bool sessionIPC::destroy() {
if (debug) cerr << "START sessionIPC::destroy() START " ;
if(semid>=0) {
shmid=getShmID(key);
if(shmid) {
if(!semctl(semid,0,IPC_RMID,0) && !shmctl(shmid,IPC_RMID,0)) {
numsessions=0;
if (debug) cerr << "END sessionIPC::destroy() END " ;
return true;
}
}
}
if (debug) cerr << "END sessionIPC::destroy() END " ;
return false;
}
bool sessionIPC::removeSessions() {
if (debug) cerr << "START sessionIPC::removeSessions() START " ;
shmid=getShmID(key);
if(shmid<0) return -1;
first=(node *)shmat(shmid,NULL,0);
numsessions=0;
fprintf(stderr,"sessions removed\n");
shmdt(first);
if (debug) cerr << "END sessionIPC::removeSessions() END " ;
return true;
}
int sessionIPC::getSemID(key_t key) {
if (debug) cerr << "START sessionIPC::getSemiD() START " ;
int semid;
if((semid=semget(key,1,0))==-1) {
if(errno==ENOENT) {
if((semid=semget(key,1,IPC_CREAT | 0755))==-1) {
perror("semget");
if (debug) cerr << "END sessionIPC::getSemiD() END " ;
return -1;
} else {
struct sembuf buf={0,1,0};
if(semop(semid,&buf,1)==-1) {
perror("semop");
if (debug) cerr << "END sessionIPC::getSemiD() END " ;
return -1;
}
if (debug) cerr << "END sessionIPC::getSemiD() END " ;
return semid;
}
} else {
perror("semget");
if (debug) cerr << "END sessionIPC::getSemiD() END " ;
return -1;
}
}
if (debug) cerr << "END sessionIPC::getSemiD() END " ;
return semid;
}
int sessionIPC::getShmID(key_t key) {
if (debug) cerr << "START sessionIPC::getShmiD() START " ;
int shmid;
if((shmid=shmget(key,ALLSIZE,0))==-1) {
if(errno==ENOENT) {
if((shmid=shmget(key,ALLSIZE,IPC_CREAT | 0755))==-1) {
perror("shmget");
if (debug) cerr << "END sessionIPC::getShmiD() END " ;
return -1;
} else {
first=(node *)shmat(shmid,NULL,0);
numsessions=0;
shmdt(first);
if (debug) cerr << "END sessionIPC::getShmiD() END " ;
return shmid;
}
} else {
perror("shmget");
if (debug) cerr << "END sessionIPC::getShmiD() END " ;
return -1;
}
}
if (debug) cerr << "END sessionIPC::getShmiD() END " ;
return shmid;
}
int sessionIPC::lockSession(int semid) {
if (debug) cerr << "START sessionIPC::lockSession() START " ;
struct sembuf buf={0,-1,0};
if(semop(semid,&buf,1)==-1) {
perror("semop");
if (debug) cerr << "END sessionIPC::lockSession() END " ;
return -1;
}
if (debug) cerr << "END sessionIPC::lockSession() END " ;
return 0;
}
int sessionIPC::unlockSession(int semid) {
if (debug) cerr << "START sessionIPC::unlockSession() START " ;
struct sembuf buf={0,1,0};
if(semop(semid,&buf,1)==-1) {
perror("semop");
if (debug) cerr << "END sessionIPC::unlockSession() END " ;
return -1;
}
if (debug) cerr << "END sessionIPC::unlockSession() END " ;
return 0;
}
string sessionIPC::get_id_from_key(const string key, const string value) {
if (debug) cerr << "START sessionIPC::get_id_from_key START " ;
map<string,string> dataMap;
map<string,string>::iterator iter;
node * current = first;
session current_session;
// lockSession(semid);
while(current) {
current_session = current->getSession();
dataMap = current_session.getDataMap();
// iterate over sessionObj's dataMap
string found = dataMap[key];
if (found == value) {
// unlockSession(semid);
if (debug) cerr << "END sessionIPC::get_id_from_key END " ;
return current_session.get_id();
}
current = current->getNext();
}
// unlockSession(semid);
if (debug) cerr << "END sessionIPC::get_id_from_key END " ;
return "";
}
return to top