00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 #include <fcntl.h>
00006 #include <sys/stat.h>
00007 #include <sys/types.h>
00008 #include <tcl.h>
00009
00010 #include "proxytrace.h"
00011 #include "my-endian.h"
00012
00013 #ifdef PC
00014 #include <io.h>
00015 #endif
00016
00017 void PrintEntry_Text(FILE *out_file, TEntry *entry, int noURL);
00018 void PrintEntry_Squid(FILE *out_file, TEntry *entry, int swap);
00019
00020 enum { ifNone = 0, ifDECV1_0, ifDECV1_2 } InputFormat = ifNone;
00021 enum { ofNone = 0, ofDECText, ofSquid, ofSquidSwapped } OutputFormat = ofNone;
00022
00023 int getInputFormat(char* argv[]) {
00024
00025 if (strncmp("-i", argv[0], 3) != 0)
00026 return ifNone;
00027
00028 if (strncmp("v1.0", argv[1], 5) == 0)
00029 InputFormat = ifDECV1_0;
00030 else
00031 if (strncmp("v1.2", argv[1], 5) == 0)
00032 InputFormat = ifDECV1_2;
00033 else
00034 InputFormat = ifNone;
00035
00036 return InputFormat;
00037 }
00038
00039 int getOutputFormat(char* argv[]) {
00040
00041 if (strncmp("-o", argv[0], 3) != 0)
00042 return ofNone;
00043
00044 if (strncmp("txt", argv[1], 4) == 0)
00045 OutputFormat = ofDECText;
00046 else
00047 if (strncmp("squid", argv[1], 6) == 0)
00048 OutputFormat = ofSquid;
00049 else
00050 if (strncmp("squidsw", argv[1], 8) == 0)
00051 OutputFormat = ofSquidSwapped;
00052 else
00053 OutputFormat = ofNone;
00054
00055 return OutputFormat;
00056 }
00057
00058 extern FILE *cf, *sf;
00059 extern double initTime;
00060 extern double duration;
00061 extern double startTime;
00062 extern ReqLog* rlog;
00063 extern unsigned int num_rlog, sz_rlog;
00064 extern Tcl_HashTable cidHash, sidHash, urlHash;
00065
00066 double lf_analyze(TEntry& lfe);
00067 void sort_url();
00068 void sort_rlog();
00069
00070 int main (int argc, char* argv[])
00071 {
00072 int is_little_endian = 0;
00073 TEntry entry;
00074 int ret;
00075 double ctime;
00076
00077
00078 Tcl_Interp *interp = Tcl_CreateInterp();
00079 if (Tcl_Init(interp) == TCL_ERROR) {
00080 printf("%s\n", interp->result);
00081 abort();
00082 }
00083 Tcl_InitHashTable(&cidHash, TCL_ONE_WORD_KEYS);
00084 Tcl_InitHashTable(&sidHash, TCL_ONE_WORD_KEYS);
00085 Tcl_InitHashTable(&urlHash, TCL_ONE_WORD_KEYS);
00086
00087 if ((cf = fopen("reqlog", "w")) == NULL) {
00088 printf("cannot open request log.\n");
00089 exit(1);
00090 }
00091 if ((sf = fopen("pglog", "w")) == NULL) {
00092 printf("cannot open page log.\n");
00093 exit(1);
00094 }
00095
00096
00097 if ((argc < 2) || (argc > 4)) {
00098 printf("Usage: %s <trace size> [<time duration>] [<start_time>]\n", argv[0]);
00099 return 1;
00100 }
00101 if (argc >= 3) {
00102 duration = strtod(argv[2], NULL);
00103 if (argc == 4) {
00104 startTime = strtod(argv[3], NULL);
00105 printf("start time = %f\n", startTime);
00106 }
00107 }
00108
00109 sz_rlog = strtoul(argv[1], NULL, 10);
00110 rlog = new ReqLog[sz_rlog];
00111
00112
00113 is_little_endian = IsLittleEndian();
00114
00115
00116 ReadHeader(stdin, 0);
00117
00118
00119 while (ReadEntry(stdin, &entry)) {
00120 if ( !is_little_endian )
00121 ToOtherEndian(&entry);
00122
00123 ctime = lf_analyze(entry);
00124 if ((duration > 0) && (ctime > duration))
00125 break;
00126 }
00127
00128 Tcl_DeleteHashTable(&cidHash);
00129 Tcl_DeleteHashTable(&sidHash);
00130
00131 fprintf(stderr, "sort url\n");
00132 sort_url();
00133 fclose(sf);
00134
00135 fprintf(stderr, "sort requests\n");
00136 sort_rlog();
00137 fclose(cf);
00138
00139 return 0;
00140 }