common/message.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) 1994-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 Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    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 #ifndef lint
00036 static const char rcsid[] =
00037     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/common/message.cc,v 1.13 1998/08/12 23:41:08 gnguyen Exp $ (LBL)";
00038 #endif
00039 
00040 #include "agent.h"
00041 #include "random.h"
00042 #include "message.h"
00043 
00044 int hdr_msg::offset_;
00045 
00046 static class MessageHeaderClass : public PacketHeaderClass {
00047 public:
00048     MessageHeaderClass() : PacketHeaderClass("PacketHeader/Message",
00049                          sizeof(hdr_msg)) {
00050         bind_offset(&hdr_msg::offset_);
00051     }
00052 } class_msghdr;
00053 
00054 class MessageAgent : public Agent {
00055  public:
00056     MessageAgent();
00057     int command(int argc, const char*const* argv);
00058     void recv(Packet*, Handler*);
00059 };
00060 
00061 static class MessageClass : public TclClass {
00062 public:
00063     MessageClass() : TclClass("Agent/Message") {}
00064     TclObject* create(int, const char*const*) {
00065         return (new MessageAgent());
00066     }
00067 } class_message;
00068 
00069 MessageAgent::MessageAgent() : Agent(PT_MESSAGE)
00070 {
00071     bind("packetSize_", &size_);
00072 }
00073 
00074 void MessageAgent::recv(Packet* pkt, Handler*)
00075 {
00076     hdr_msg* mh = hdr_msg::access(pkt);
00077     char wrk[128];/*XXX*/
00078     sprintf(wrk, "%s recv {%s}", name(), mh->msg());
00079 
00080     Tcl& tcl = Tcl::instance();
00081     tcl.eval(wrk);
00082 
00083     Packet::free(pkt);
00084 }
00085 
00086 /*
00087  * $proc handler $handler
00088  * $proc send $msg
00089  */
00090 int MessageAgent::command(int argc, const char*const* argv)
00091 {
00092     Tcl& tcl = Tcl::instance();
00093     if (argc == 3) {
00094         if (strcmp(argv[1], "send") == 0) {
00095             Packet* pkt = allocpkt();
00096             hdr_msg* mh = hdr_msg::access(pkt);
00097             const char* s = argv[2];
00098             int n = strlen(s);
00099             if (n >= mh->maxmsg()) {
00100                 tcl.result("message too big");
00101                 Packet::free(pkt);
00102                 return (TCL_ERROR);
00103             }
00104             strcpy(mh->msg(), s);
00105             send(pkt, 0);
00106             return (TCL_OK);
00107         }
00108     }
00109     return (Agent::command(argc, argv));
00110 }

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