inval-agent.cc
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:5k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * Copyright (c) Xerox Corporation 1998. All rights reserved.
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify it
  5.  * under the terms of the GNU General Public License as published by the
  6.  * Free Software Foundation; either version 2 of the License, or (at your
  7.  * option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful, but
  10.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.  * General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program; if not, write to the Free Software Foundation, Inc.,
  16.  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17.  *
  18.  * Linking this file statically or dynamically with other modules is making
  19.  * a combined work based on this file.  Thus, the terms and conditions of
  20.  * the GNU General Public License cover the whole combination.
  21.  *
  22.  * In addition, as a special exception, the copyright holders of this file
  23.  * give you permission to combine this file with free software programs or
  24.  * libraries that are released under the GNU LGPL and with code included in
  25.  * the standard release of ns-2 under the Apache 2.0 license or under
  26.  * otherwise-compatible licenses with advertising requirements (or modified
  27.  * versions of such code, with unchanged license).  You may copy and
  28.  * distribute such a system following the terms of the GNU GPL for this
  29.  * file and the licenses of the other code concerned, provided that you
  30.  * include the source code of that other code when and as the GNU GPL
  31.  * requires distribution of source code.
  32.  *
  33.  * Note that people who make modified versions of this file are not
  34.  * obligated to grant this special exception for their modified versions;
  35.  * it is their choice whether to do so.  The GNU General Public License
  36.  * gives permission to release a modified version without this exception;
  37.  * this exception also makes it possible to release a modified version
  38.  * which carries forward this exception.
  39.  *
  40.  * $Header: /cvsroot/nsnam/ns-2/webcache/inval-agent.cc,v 1.14 2005/08/26 05:05:31 tomh Exp $
  41.  */
  42. //
  43. // Agents used to send and receive invalidation records
  44. // 
  45. #include "inval-agent.h"
  46. #include "ip.h"
  47. #include "http.h"
  48. // Implementation 1: Invalidation via multicast heartbeat
  49. int hdr_inval::offset_;
  50. static class HttpInvalHeaderClass : public PacketHeaderClass {
  51. public:
  52.         HttpInvalHeaderClass() : PacketHeaderClass("PacketHeader/HttpInval",
  53.    sizeof(hdr_inval)) {
  54. bind_offset(&hdr_inval::offset_);
  55. }
  56. } class_httpinvalhdr;
  57. static class HttpInvalClass : public TclClass {
  58. public:
  59. HttpInvalClass() : TclClass("Agent/HttpInval") {}
  60. TclObject* create(int, const char*const*) {
  61. return (new HttpInvalAgent());
  62. }
  63. } class_httpinval_agent;
  64. HttpInvalAgent::HttpInvalAgent() : Agent(PT_INVAL)
  65. {
  66. // It should be initialized to the same as tcpip_base_hdr_size_
  67. bind("inval_hdr_size_", &inval_hdr_size_);
  68. }
  69. void HttpInvalAgent::recv(Packet *pkt, Handler*)
  70. {
  71. hdr_ip *ip = hdr_ip::access(pkt);
  72. if ((ip->saddr() == addr()) && (ip->sport() == port()))
  73. // XXX Why do we need this?
  74. return;
  75. if (app_ == 0) 
  76. return;
  77. hdr_inval *ih = hdr_inval::access(pkt);
  78. ((HttpApp*)app_)->process_data(ih->size(), pkt->userdata());
  79. Packet::free(pkt);
  80. }
  81. // Send a list of invalidation records in user data area
  82. // realsize: the claimed size
  83. // datasize: the actual size of user data, used to allocate packet
  84. void HttpInvalAgent::send(int realsize, AppData* data)
  85. {
  86. Packet *pkt = allocpkt(data->size());
  87. hdr_inval *ih = hdr_inval::access(pkt);
  88. ih->size() = data->size();
  89. pkt->setdata(data);
  90. // Set packet size proportional to the number of invalidations
  91. hdr_cmn *ch = hdr_cmn::access(pkt);
  92. ch->size() = inval_hdr_size_ + realsize;
  93. Agent::send(pkt, 0);
  94. }
  95. // Implementation 2: Invalidation via TCP. 
  96. static class HttpUInvalClass : public TclClass {
  97. public:
  98. HttpUInvalClass() : TclClass("Application/TcpApp/HttpInval") {}
  99. TclObject* create(int argc, const char*const* argv) {
  100. if (argc != 5)
  101. return NULL;
  102. Agent *a = (Agent *)TclObject::lookup(argv[4]);
  103. a->set_pkttype(PT_INVAL); // It's TCP but used for invalidation
  104. if (a == NULL)
  105. return NULL;
  106. return (new HttpUInvalAgent(a));
  107. }
  108. } class_httpuinval_agent;
  109. void HttpUInvalAgent::process_data(int size, AppData* data) 
  110. {
  111. target_->process_data(size, data);
  112. }
  113. int HttpUInvalAgent::command(int argc, const char*const* argv)
  114. {
  115. if (strcmp(argv[1], "set-app") == 0) {
  116. // Compatibility interface
  117. HttpApp* c = 
  118. (HttpApp*)TclObject::lookup(argv[2]);
  119. target_ = (Process *)c;
  120. return TCL_OK;
  121. }
  122. return TcpApp::command(argc, argv);
  123. }