/***************************************************************************
                          onlineusers.cpp  -  description
                             -------------------
    begin                : Mon Nov 24 2003
    copyright            : (C) 2003 by Wei Zheng
    email                : v_zheng@yahoo.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "onlineusers.h"

onlineusers::onlineusers(){
  default_head = NULL;
  headuserpp = &default_head;
  current_user = NULL;
}

onlineusers::~onlineusers(){
}

int onlineusers::myrandom()
{
	int min;
	float max;
	min=1000;
	max=1000.000;
	return min+(int)(max*random()/(RAND_MAX));
}

void onlineusers::eliminate_online_queue()
{
	online_userinfo * current;
	online_userinfo * previous;
	current = * headuserpp;

  while(current!=NULL)
  {
    if(current->userinfop!=NULL)
    {
      while(shmdt(current->userinfop) == -1);
      while(shmctl(current->userinfoid, IPC_RMID, 0)==-1);
    }
		previous = current;
		current = current->next;
		free(previous);
	}
  headuserpp = &default_head;
}

void onlineusers::list_online_user()
{
	online_userinfo * current;
	current= * headuserpp;
	
  while(current!=NULL)
	{
		printf("userinfoid:%d hotmail:%s nickname:%s ip:%s port:%s\n",current->userinfoid,current->userinfop->hotmail,current->userinfop->nickname,current->userinfop->ip,current->userinfop->port);
		current=current->next;
	}
}

online_userinfo * onlineusers::search_online_user(char * hotmail)
{
	online_userinfo * current;
	current= * headuserpp;
	
  while(current!=NULL)
	{
		if(strcmp(current->userinfop->hotmail,hotmail)==0)
			break;
		else
			current=current->next;
	}
	return current;
}

void onlineusers::add_online_user(char * hotmail, char * nickname)
{

	online_userinfo * head;
	online_userinfo * current;
	int shmid,ran;
	head = * headuserpp;

	if(*headuserpp==NULL)
	{
		*headuserpp=(online_userinfo *)malloc(sizeof(online_userinfo));
		memset(*headuserpp,'\0',sizeof(*headuserpp));
		(*headuserpp)->next=NULL;

		do{
			ran = myrandom();
		}while((shmid = shmget((key_t)ran, sizeof(userinfo), 0666 | IPC_CREAT))==-1);

		(*headuserpp)->userinfoid = shmid;
		(*headuserpp)->userinfop = (userinfo *) shmat(shmid, (void *)0, 0);
    memset((*headuserpp)->userinfop,0,sizeof(userinfo));
    strcat((*headuserpp)->userinfop->hotmail,hotmail);
    strncat((*headuserpp)->userinfop->nickname,nickname,sizeof((*headuserpp)->userinfop->nickname));
	}
	else
	{
		current=head;
		while(1)
		{
			if(current->next==NULL)
			{
				current->next=(online_userinfo *)malloc(sizeof(online_userinfo));
				memset(current->next,'\0',sizeof(current->next));
				current=current->next;
				current->next = NULL;
				do{
					ran= myrandom();
				}while((shmid = shmget((key_t)ran, sizeof(userinfo), 0666 | IPC_CREAT))==-1);

				current->userinfoid = shmid;
				current->userinfop = (userinfo *) shmat(shmid, (void *)0, 0);
				memset(current->userinfop,0,sizeof(userinfo));
				strcat(current->userinfop->hotmail,hotmail);
				strncat(current->userinfop->nickname,nickname,sizeof(current->userinfop->nickname));
				break;
			}
			else
			{
				if(strcmp(current->userinfop->hotmail,hotmail)==0)
				{
					break;
				}
				else
					current=current->next;
			}
		}
	}
  list_online_user();
}

pid_t onlineusers::del_online_user(char * hotmail)
{
	online_userinfo * head;
	online_userinfo * prev;
	online_userinfo * current;
	online_userinfo * next;

	pid_t pidtmp;

	head = *headuserpp;
	if(strcmp((*headuserpp)->userinfop->hotmail,hotmail)==0)
	{
		printf("hotmail:%s nickname:%s ip:%s port:%s\n",(*headuserpp)->userinfop->hotmail,(*headuserpp)->userinfop->nickname,(*headuserpp)->userinfop->ip,(*headuserpp)->userinfop->port);

		pidtmp=(*headuserpp)->pid;
		if((*headuserpp)->userinfop!=NULL)
			while(shmdt((*headuserpp)->userinfop) == -1);

		if((*headuserpp)->userinfoid!=0)
	    	while(shmctl((*headuserpp)->userinfoid, IPC_RMID, 0) == -1);

		prev = (*headuserpp);
		(*headuserpp)=(*headuserpp)->next;
		free(prev);
		return pidtmp;
	}
	else
	{
		prev=head;
		current=head->next;
		next=current->next;
		while(1)
		{
			if(strcmp(current->userinfop->hotmail,hotmail)==0)
			{
				printf("hotmail:%s nickname:%s ip:%s port:%s\n",current->userinfop->hotmail,current->userinfop->nickname,current->userinfop->ip,current->userinfop->port);
				pidtmp=current->pid;
				if(current->userinfop!=NULL)
					while(shmdt(current->userinfop) == -1);

				if(current->userinfoid!=0)
	    			while(shmctl(current->userinfoid, IPC_RMID, 0) == -1);

				prev->next=next;
				free(current);
				return pidtmp;
			}
			else
			{
				if(next!=NULL)
				{
					prev=current;
					current=next;
					next=current->next;
				}
				else
				{
					return -1;
				}
			}
		}
	}
}

void onlineusers::save_hostinfo_conf(char * hostinfo1, char * hostinfo2)
{
	char * delim1;
	char * delim2;
	delim1 = strchr(hostinfo1,':');
	delim2 = strchr(hostinfo2,':');
	/*memset(destination.host1name,0,strlen(destination.host1name));
	memset(destination.host1port,0,strlen(destination.host1port));
	memset(destination.host2name,0,strlen(destination.host2name));
	memset(destination.host2port,0,strlen(destination.host2port));

	strncat(destination.host1name,hostinfo1,delim1-hostinfo1);
	strcat(destination.host1port,delim1+sizeof(char));

	strncat(destination.host2name,hostinfo2,delim2-hostinfo2);
	strcat(destination.host2port,delim2+sizeof(char));*/
}