bi-connector.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
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 #include "packet.h"
00037 #include "bi-connector.h"
00038 
00039 static class BiConnectorClass : public TclClass {
00040 public:
00041     BiConnectorClass() : TclClass("BiConnector") {}
00042     TclObject* create(int, const char*const*) {
00043         return (new BiConnector);
00044     }
00045 } class_biconnector;
00046 
00047 BiConnector::BiConnector() : uptarget_(0), downtarget_(0), drop_(0)
00048 {}
00049 
00050 
00051 int BiConnector::command(int argc, const char*const* argv)
00052 {
00053     Tcl& tcl = Tcl::instance();
00054     /*XXX*/
00055     if (argc == 2) {
00056         if (strcmp(argv[1], "up-target") == 0) {
00057             if (uptarget_ != 0)
00058                 tcl.result(uptarget_->name());
00059             return (TCL_OK);
00060         }
00061         if (strcmp(argv[1], "down-target") == 0) {
00062             if (downtarget_ != 0)
00063                 tcl.result(downtarget_->name());
00064             return (TCL_OK);
00065         }
00066         if (strcmp(argv[1], "drop-target") == 0) {
00067             if (drop_ != 0)
00068                 tcl.resultf("%s", drop_->name());
00069             return (TCL_OK);
00070         }
00071         if (strcmp(argv[1], "isDynamic") == 0) {
00072             return TCL_OK;
00073         }
00074     }
00075 
00076     else if (argc == 3) {
00077         TclObject *obj;
00078         
00079         if( (obj = TclObject::lookup(argv[2])) == 0) {
00080             fprintf(stderr, "%s lookup failed\n", argv[1]);
00081             return TCL_ERROR;
00082         }
00083         if (strcmp(argv[1], "up-target") == 0) {
00084             if (*argv[2] == '0') {
00085                 uptarget_ = 0;
00086                 return (TCL_OK);
00087             }
00088             uptarget_ = (NsObject*) obj;
00089             if (uptarget_ == 0) {
00090                 tcl.resultf("no such object %s", argv[2]);
00091                 return (TCL_ERROR);
00092             }
00093             return (TCL_OK);
00094         }
00095         if (strcmp(argv[1], "down-target") == 0) {
00096             if (*argv[2] == '0') {
00097                 downtarget_ = 0;
00098                 return (TCL_OK);
00099             }
00100             downtarget_ = (NsObject*) obj;
00101             if (downtarget_ == 0) {
00102                 tcl.resultf("no such object %s", argv[2]);
00103                 return (TCL_ERROR);
00104             }
00105             return (TCL_OK);
00106         }
00107         if (strcmp(argv[1], "drop-target") == 0) {
00108             drop_ = (NsObject*) obj;
00109             if (drop_ == 0) {
00110                 tcl.resultf("no object %s", argv[2]);
00111                 return (TCL_ERROR);
00112             }
00113             return (TCL_OK);
00114         }
00115     }
00116     return (NsObject::command(argc, argv));
00117 }
00118 
00119 void BiConnector::recv(Packet* p, Handler* h)
00120 {
00121     hdr_cmn *ch = HDR_CMN(p);
00122     switch (ch->direction()) {
00123     case hdr_cmn::UP :
00124         sendUp(p, h);
00125         break;
00126     case hdr_cmn::DOWN :
00127         sendDown(p, h);
00128         break;
00129     default:
00130         printf("Error: Packet Direction not specified; Using default 'UP' direction\n\n");
00131         sendUp(p, h);
00132     }
00133 }
00134 
00135 void BiConnector::drop(Packet* p)
00136 {
00137     if (drop_ != 0)
00138         drop_->recv(p);
00139     else
00140         Packet::free(p);
00141 }
00142 
00143 
00144 void BiConnector::drop(Packet* p, const char *s)
00145 {
00146     if (drop_ != 0)
00147         drop_->recv(p, s);
00148     else
00149         Packet::free(p);
00150 }

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