00001 // 00002 // tools.cc : Implements a few utility functions 00003 // authors : Fabio Silva 00004 // 00005 // Copyright (C) 2000-2002 by the University of Southern California 00006 // $Id: tools.cc,v 1.16 2005/10/05 15:16:02 sfloyd Exp $ 00007 // 00008 // This program is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU General Public License, 00010 // version 2, as published by the Free Software Foundation. 00011 // 00012 // This program is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License along 00018 // with this program; if not, write to the Free Software Foundation, Inc., 00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 00020 // 00021 // Linking this file statically or dynamically with other modules is making 00022 // a combined work based on this file. Thus, the terms and conditions of 00023 // the GNU General Public License cover the whole combination. 00024 // 00025 // In addition, as a special exception, the copyright holders of this file 00026 // give you permission to combine this file with free software programs or 00027 // libraries that are released under the GNU LGPL and with code included in 00028 // the standard release of ns-2 under the Apache 2.0 license or under 00029 // otherwise-compatible licenses with advertising requirements (or modified 00030 // versions of such code, with unchanged license). You may copy and 00031 // distribute such a system following the terms of the GNU GPL for this 00032 // file and the licenses of the other code concerned, provided that you 00033 // include the source code of that other code when and as the GNU GPL 00034 // requires distribution of source code. 00035 // 00036 // Note that people who make modified versions of this file are not 00037 // obligated to grant this special exception for their modified versions; 00038 // it is their choice whether to do so. The GNU General Public License 00039 // gives permission to release a modified version without this exception; 00040 // this exception also makes it possible to release a modified version 00041 // which carries forward this exception. 00042 00043 #include <math.h> 00044 00045 #include "tools.hh" 00046 00047 #ifdef NS_DIFFUSION 00048 #include "scheduler.h" 00049 #include "rng.h" 00050 #include "random.h" 00051 #endif // NS_DIFFUSION 00052 00053 int global_debug_level = DEBUG_DEFAULT; 00054 00055 void GetTime(struct timeval *tv) 00056 { 00057 #ifdef NS_DIFFUSION 00058 double time; 00059 long sec, usec; 00060 00061 time = Scheduler::instance().clock(); 00062 // sec = lrint (time); 00063 sec = (long) rint (time); 00064 // usec = lrint ((time - sec) * 1000000); 00065 usec = (long) rint ((time - sec) * 1000000); 00066 tv->tv_sec = sec; 00067 tv->tv_usec = usec; 00068 DiffPrint(DEBUG_NEVER, "tv->sec = %ld, tv->usec = %ld\n", tv->tv_sec, tv->tv_usec); 00069 #else 00070 gettimeofday(tv, NULL); 00071 #endif // NS_DIFFUSION 00072 } 00073 00074 void SetSeed(struct timeval *tv) 00075 { 00076 #ifdef NS_DIFFUSION 00077 // Don't need to do anything since NS's RNG is seeded using 00078 // otcl proc ns-random <seed> 00079 #else 00080 srand(tv->tv_usec); 00081 #endif // NS_DIFFUSION 00082 } 00083 00084 int GetRand() 00085 { 00086 #ifdef NS_DIFFUSION 00087 return (Random::random()); 00088 #else 00089 return (rand()); 00090 #endif // NS_DIFFUSION 00091 } 00092 00093 void DiffPrint(int msg_debug_level, const char *fmt, ...) 00094 { 00095 va_list ap; 00096 00097 va_start(ap, fmt); 00098 00099 if (global_debug_level >= msg_debug_level){ 00100 // Print message 00101 vfprintf(stderr, fmt, ap); 00102 fflush(NULL); 00103 } 00104 00105 va_end(ap); 00106 } 00107 00108 void DiffPrintWithTime(int msg_debug_level, const char *fmt, ...) 00109 { 00110 struct timeval tv; 00111 va_list ap; 00112 00113 va_start(ap, fmt); 00114 00115 if (global_debug_level >= msg_debug_level){ 00116 // Get time 00117 GetTime(&tv); 00118 00119 // Print Time 00120 fprintf(stderr, "%ld.%06ld : ", tv.tv_sec, tv.tv_usec); 00121 // Print message 00122 vfprintf(stderr, fmt, ap); 00123 fflush(NULL); 00124 } 00125 00126 va_end(ap); 00127 }
1.4.6