00001 /* BEGIN_COPYRIGHT */ 00002 /* */ 00003 /* Open Diameter: Open-source software for the Diameter and */ 00004 /* Diameter related protocols */ 00005 /* */ 00006 /* Copyright (C) 2002-2004 Open Diameter Project */ 00007 /* */ 00008 /* This library is free software; you can redistribute it and/or modify */ 00009 /* it under the terms of the GNU Lesser General Public License as */ 00010 /* published by the Free Software Foundation; either version 2.1 of the */ 00011 /* License, or (at your option) any later version. */ 00012 /* */ 00013 /* This library is distributed in the hope that it will be useful, */ 00014 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 00015 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ 00016 /* Lesser General Public License for more details. */ 00017 /* */ 00018 /* You should have received a copy of the GNU Lesser General Public */ 00019 /* License along with this library; if not, write to the Free Software */ 00020 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 */ 00021 /* USA. */ 00022 /* */ 00023 /* In addition, when you copy and redistribute some or the entire part of */ 00024 /* the source code of this software with or without modification, you */ 00025 /* MUST include this copyright notice in each copy. */ 00026 /* */ 00027 /* If you make any changes that are appeared to be useful, please send */ 00028 /* sources that include the changed part to */ 00029 /* diameter-developers@lists.sourceforge.net so that we can reflect your */ 00030 /* changes to one unified version of this software. */ 00031 /* */ 00032 /* END_COPYRIGHT */ 00033 00034 #ifndef __PANA_UTL_H__ 00035 #define __PANA_UTL_H__ 00036 00037 #include "ace/Synch.h" 00038 #include "ace/Singleton.h" 00039 00040 /* 00041 * Auxilliary class for generating session id's 00042 */ 00043 class PANA_SessionIdGenerator 00044 { 00045 public: 00046 void Seed(std::string &id) { m_Identity = id; } 00047 void Generate(std::string &newSessionId) { 00048 if (m_Identity.size() == 0) { 00049 char hostname[128]; 00050 ACE_INET_Addr base; 00051 if (base.get_host_name(hostname, sizeof(hostname)) == 0) { 00052 m_Identity = hostname; 00053 } 00054 else { 00055 throw (PANA_Exception(PANA_Exception::SESSIONID_ERROR, 00056 "Unable to retrieve local hostname")); 00057 } 00058 } 00059 ACE_Time_Value tv = ACE_OS::gettimeofday(); 00060 m_Incrementor ++; 00061 00062 char id[256]; 00063 ACE_OS::sprintf(id, "%s;%d;%d", 00064 m_Identity.data(), 00065 tv.sec(), 00066 m_Incrementor); 00067 newSessionId = id; 00068 } 00069 00070 private: 00071 std::string m_Identity; 00072 long m_Incrementor; 00073 }; 00074 00075 typedef ACE_Singleton<PANA_SessionIdGenerator, ACE_Null_Mutex> PANA_SessionIdGenerator_S; 00076 00077 #endif /* __PANA_UTL_H__ */ 00078