basetrace.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8 -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  * 
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the MASH Research
00017  *  Group at the University of California Berkeley.
00018  * 4. Neither the name of the University nor of the Research Group may be
00019  *    used to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  * 
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  *
00034 
00035  */
00036 
00037 #include "basetrace.h"
00038 #include "tcp.h"
00039 
00040 class BaseTraceClass : public TclClass {
00041 public:
00042        BaseTraceClass() : TclClass("BaseTrace") { }
00043        TclObject* create(int argc, const char*const* argv) {
00044           return (new BaseTrace());
00045        }
00046 } basetrace_class;
00047 
00048 class EventTraceClass : public TclClass {
00049 public:
00050     EventTraceClass() : TclClass("BaseTrace/Event") { }
00051     TclObject* create(int argc, const char*const* argv) {
00052         return (new EventTrace());
00053     }
00054 } eventtrace_class;
00055 
00056 
00057 BaseTrace::BaseTrace() 
00058   : channel_(0), namChan_(0), tagged_(0) 
00059 {
00060   wrk_ = new char[1026];
00061   nwrk_ = new char[256];
00062 }
00063 
00064 BaseTrace::~BaseTrace()
00065 {
00066   delete wrk_;
00067   delete nwrk_;
00068 }
00069 
00070 void BaseTrace::dump()
00071 {
00072     int n = strlen(wrk_);
00073     if ((n > 0) && (channel_ != 0)) {
00074         /*
00075          * tack on a newline (temporarily) instead
00076          * of doing two writes
00077          */
00078         wrk_[n] = '\n';
00079         wrk_[n + 1] = 0;
00080  /* -NEW- */
00081         //printf("%s",wrk_);
00082         (void)Tcl_Write(channel_, wrk_, n + 1);
00083 
00084  /* END -NEW- */
00085         //Tcl_Flush(channel_);
00086         wrk_[n] = 0;
00087     }
00088 
00089     //  if (callback_) {
00090 //          Tcl& tcl = Tcl::instance();
00091 //          tcl.evalf("%s handle { %s }", name(), wrk_);
00092 //      }
00093 }
00094 
00095 void BaseTrace::namdump()
00096 {
00097     int n = 0;
00098 
00099     /* Otherwise nwrk_ isn't initialized */
00100     if (namChan_ != 0)
00101         n = strlen(nwrk_);
00102     if ((n > 0) && (namChan_ != 0)) {
00103         /*
00104          * tack on a newline (temporarily) instead
00105          * of doing two writes
00106          */
00107         nwrk_[n] = '\n';
00108         nwrk_[n + 1] = 0;
00109         (void)Tcl_Write(namChan_, nwrk_, n + 1);
00110         //Tcl_Flush(channel_);
00111         nwrk_[n] = 0;
00112     }
00113 }
00114 
00115 
00116 /*
00117  * $trace detach
00118  * $trace flush
00119  * $trace attach $fileID
00120  */
00121 int BaseTrace::command(int argc, const char*const* argv)
00122 {
00123     Tcl& tcl = Tcl::instance();
00124     if (argc == 2) {
00125         if (strcmp(argv[1], "detach") == 0) {
00126             channel_ = 0;
00127             namChan_ = 0;
00128             return (TCL_OK);
00129         }
00130         if (strcmp(argv[1], "flush") == 0) {
00131             if (channel_ != 0) 
00132                 Tcl_Flush(channel_);
00133             if (namChan_ != 0)
00134                 Tcl_Flush(namChan_);
00135             return (TCL_OK);
00136         }
00137         if (strcmp(argv[1], "tagged") == 0) {
00138             tcl.resultf("%d", tagged());
00139                         return (TCL_OK);
00140         }
00141     } else if (argc == 3) {
00142         if (strcmp(argv[1], "attach") == 0) {
00143             int mode;
00144             const char* id = argv[2];
00145             channel_ = Tcl_GetChannel(tcl.interp(), (char*)id,
00146                           &mode);
00147             if (channel_ == 0) {
00148                 tcl.resultf("trace: can't attach %s for writing", id);
00149                 return (TCL_ERROR);
00150             }
00151             return (TCL_OK);
00152         }
00153         if (strcmp(argv[1], "namattach") == 0) {
00154             int mode;
00155             const char* id = argv[2];
00156             namChan_ = Tcl_GetChannel(tcl.interp(), (char*)id,
00157                           &mode);
00158             if (namChan_ == 0) {
00159                 tcl.resultf("trace: can't attach %s for writing", id);
00160                 return (TCL_ERROR);
00161             }
00162             return (TCL_OK);
00163         }
00164         if (strcmp(argv[1], "tagged") == 0) {
00165             int tag;
00166             if (Tcl_GetBoolean(tcl.interp(),
00167                        (char*)argv[2], &tag) == TCL_OK) {
00168                 tagged(tag);
00169                 return (TCL_OK);
00170             } else return (TCL_ERROR);
00171         }
00172     }
00173     return (TclObject::command(argc, argv));
00174 }
00175 
00176 /* The eventtrace format is tentative for now; will be extended in future to include data like window size, rtt, segsize, segperack etc */
00177 
00178 void EventTrace::trace()
00179 {
00180   dump();
00181   namdump();
00182 }
00183 
00184 

Generated on Tue Mar 6 16:47:43 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6