00001 /* 00002 * Copyright (c) Xerox Corporation 1998. All rights reserved. 00003 * 00004 * This program is free software; you can redistribute it and/or modify it 00005 * under the terms of the GNU General Public License as published by the 00006 * Free Software Foundation; either version 2 of the License, or (at your 00007 * option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License along 00015 * with this program; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00017 * 00018 * Linking this file statically or dynamically with other modules is making 00019 * a combined work based on this file. Thus, the terms and conditions of 00020 * the GNU General Public License cover the whole combination. 00021 * 00022 * In addition, as a special exception, the copyright holders of this file 00023 * give you permission to combine this file with free software programs or 00024 * libraries that are released under the GNU LGPL and with code included in 00025 * the standard release of ns-2 under the Apache 2.0 license or under 00026 * otherwise-compatible licenses with advertising requirements (or modified 00027 * versions of such code, with unchanged license). You may copy and 00028 * distribute such a system following the terms of the GNU GPL for this 00029 * file and the licenses of the other code concerned, provided that you 00030 * include the source code of that other code when and as the GNU GPL 00031 * requires distribution of source code. 00032 * 00033 * Note that people who make modified versions of this file are not 00034 * obligated to grant this special exception for their modified versions; 00035 * it is their choice whether to do so. The GNU General Public License 00036 * gives permission to release a modified version without this exception; 00037 * this exception also makes it possible to release a modified version 00038 * which carries forward this exception. 00039 * 00040 * $Header: /nfs/jade/vint/CVSROOT/ns-2/webcache/inval-agent.cc,v 1.14 2005/08/26 05:05:31 tomh Exp $ 00041 */ 00042 00043 // 00044 // Agents used to send and receive invalidation records 00045 // 00046 00047 #include "inval-agent.h" 00048 #include "ip.h" 00049 #include "http.h" 00050 00051 // Implementation 1: Invalidation via multicast heartbeat 00052 int hdr_inval::offset_; 00053 static class HttpInvalHeaderClass : public PacketHeaderClass { 00054 public: 00055 HttpInvalHeaderClass() : PacketHeaderClass("PacketHeader/HttpInval", 00056 sizeof(hdr_inval)) { 00057 bind_offset(&hdr_inval::offset_); 00058 } 00059 } class_httpinvalhdr; 00060 00061 static class HttpInvalClass : public TclClass { 00062 public: 00063 HttpInvalClass() : TclClass("Agent/HttpInval") {} 00064 TclObject* create(int, const char*const*) { 00065 return (new HttpInvalAgent()); 00066 } 00067 } class_httpinval_agent; 00068 00069 HttpInvalAgent::HttpInvalAgent() : Agent(PT_INVAL) 00070 { 00071 // It should be initialized to the same as tcpip_base_hdr_size_ 00072 bind("inval_hdr_size_", &inval_hdr_size_); 00073 } 00074 00075 void HttpInvalAgent::recv(Packet *pkt, Handler*) 00076 { 00077 hdr_ip *ip = hdr_ip::access(pkt); 00078 if ((ip->saddr() == addr()) && (ip->sport() == port())) 00079 // XXX Why do we need this? 00080 return; 00081 if (app_ == 0) 00082 return; 00083 hdr_inval *ih = hdr_inval::access(pkt); 00084 ((HttpApp*)app_)->process_data(ih->size(), pkt->userdata()); 00085 Packet::free(pkt); 00086 } 00087 00088 // Send a list of invalidation records in user data area 00089 // realsize: the claimed size 00090 // datasize: the actual size of user data, used to allocate packet 00091 void HttpInvalAgent::send(int realsize, AppData* data) 00092 { 00093 Packet *pkt = allocpkt(data->size()); 00094 hdr_inval *ih = hdr_inval::access(pkt); 00095 ih->size() = data->size(); 00096 pkt->setdata(data); 00097 00098 // Set packet size proportional to the number of invalidations 00099 hdr_cmn *ch = hdr_cmn::access(pkt); 00100 ch->size() = inval_hdr_size_ + realsize; 00101 Agent::send(pkt, 0); 00102 } 00103 00104 00105 // Implementation 2: Invalidation via TCP. 00106 static class HttpUInvalClass : public TclClass { 00107 public: 00108 HttpUInvalClass() : TclClass("Application/TcpApp/HttpInval") {} 00109 TclObject* create(int argc, const char*const* argv) { 00110 if (argc != 5) 00111 return NULL; 00112 Agent *a = (Agent *)TclObject::lookup(argv[4]); 00113 a->set_pkttype(PT_INVAL); // It's TCP but used for invalidation 00114 if (a == NULL) 00115 return NULL; 00116 return (new HttpUInvalAgent(a)); 00117 } 00118 } class_httpuinval_agent; 00119 00120 void HttpUInvalAgent::process_data(int size, AppData* data) 00121 { 00122 target_->process_data(size, data); 00123 } 00124 00125 int HttpUInvalAgent::command(int argc, const char*const* argv) 00126 { 00127 if (strcmp(argv[1], "set-app") == 0) { 00128 // Compatibility interface 00129 HttpApp* c = 00130 (HttpApp*)TclObject::lookup(argv[2]); 00131 target_ = (Process *)c; 00132 return TCL_OK; 00133 } 00134 return TcpApp::command(argc, argv); 00135 }
1.4.6