iolog.cc

Go to the documentation of this file.
00001 //
00002 // iolog.cc      : IO Log Layer
00003 // Authors       : Fabio Silva and Yutaka Mori
00004 //
00005 // Copyright (C) 2000-2002 by the University of Southern California
00006 // $Id: iolog.cc,v 1.3 2005/09/13 04:53:47 tomh Exp $
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License,
00010 // version 2, as published by the Free Software Foundation.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License along
00018 // with this program; if not, write to the Free Software Foundation, Inc.,
00019 // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020 //
00021 // Linking this file statically or dynamically with other modules is making
00022 // a combined work based on this file.  Thus, the terms and conditions of
00023 // the GNU General Public License cover the whole combination.
00024 //
00025 // In addition, as a special exception, the copyright holders of this file
00026 // give you permission to combine this file with free software programs or
00027 // libraries that are released under the GNU LGPL and with code included in
00028 // the standard release of ns-2 under the Apache 2.0 license or under
00029 // otherwise-compatible licenses with advertising requirements (or modified
00030 // versions of such code, with unchanged license).  You may copy and
00031 // distribute such a system following the terms of the GNU GPL for this
00032 // file and the licenses of the other code concerned, provided that you
00033 // include the source code of that other code when and as the GNU GPL
00034 // requires distribution of source code.
00035 //
00036 // Note that people who make modified versions of this file are not
00037 // obligated to grant this special exception for their modified versions;
00038 // it is their choice whether to do so.  The GNU General Public License
00039 // gives permission to release a modified version without this exception;
00040 // this exception also makes it possible to release a modified version
00041 // which carries forward this exception.
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 
00046 #include "iolog.hh"
00047 
00048 IOLog::IOLog(int32_t id) : IOHook()
00049 {
00050   node_id_ = id;
00051   DiffPrint(DEBUG_IMPORTANT, "Initializing IOLog...\n");
00052 }
00053 
00054 DiffPacket IOLog::recvPacket(int fd)
00055 {
00056   DiffPacket incoming_packet;
00057   struct timeval tv;
00058   struct hdr_diff *dfh;
00059   u_int16_t data_len, packet_len;
00060   int32_t last_hop, next_hop;
00061   int msg_type;
00062   char *msg_name;
00063 
00064   // Receive the packet first
00065   incoming_packet = IOHook::recvPacket(fd);
00066 
00067   if (incoming_packet){
00068     // Log incoming packet
00069     dfh = HDR_DIFF(incoming_packet);
00070     last_hop = ntohl(LAST_HOP(dfh));
00071     next_hop = ntohl(NEXT_HOP(dfh));
00072     data_len = ntohs(DATA_LEN(dfh));
00073     msg_type = MSG_TYPE(dfh);
00074     packet_len = data_len + sizeof(hdr_diff);
00075 
00076     switch (msg_type){
00077     case INTEREST:
00078       msg_name = strdup("Interest");
00079       break;
00080     case DATA:
00081       msg_name = strdup("Data");
00082       break;
00083     case EXPLORATORY_DATA:
00084       msg_name = strdup("Exploratory Data");
00085       break;
00086     case PUSH_EXPLORATORY_DATA:
00087       msg_name = strdup("Push Exploratory Data");
00088       break;
00089     case POSITIVE_REINFORCEMENT:
00090       msg_name = strdup("Positive Reinforcement");
00091       break;
00092     case NEGATIVE_REINFORCEMENT:
00093       msg_name = strdup("Negative Reinforcement");
00094       break;
00095     default:
00096       msg_name = strdup("Unknown");
00097       break;
00098     }
00099 
00100     if (last_hop != LOCALHOST_ADDR){
00101       GetTime(&tv);
00102       if (next_hop == BROADCAST_ADDR){
00103     fprintf(stdout,
00104         "Diffusion Log: Time %ld.%06ld Node %d received broadcast %d bytes from node %d message %s\n",
00105         tv.tv_sec, tv.tv_usec, node_id_, packet_len, last_hop, msg_name);
00106       }
00107       else{
00108     fprintf(stdout,
00109         "Diffusion Log: Time %ld.%06ld Node %d received unicast %d bytes from node %d message %s\n",
00110         tv.tv_sec, tv.tv_usec, node_id_, packet_len, last_hop, msg_name);
00111       }
00112       fflush(NULL);
00113     }
00114 
00115     free(msg_name);
00116   }
00117 
00118   return incoming_packet;
00119 }
00120 
00121 void IOLog::sendPacket(DiffPacket pkt, int len, int dst)
00122 {
00123   struct timeval tv;
00124   struct hdr_diff *dfh;
00125   int msg_type;
00126   char *msg_name;
00127 
00128   // Log outgoing packet
00129   dfh = HDR_DIFF(pkt);
00130   msg_type = MSG_TYPE(dfh);
00131 
00132   switch (msg_type){
00133   case INTEREST:
00134     msg_name = strdup("Interest");
00135     break;
00136   case DATA:
00137     msg_name = strdup("Data");
00138     break;
00139   case EXPLORATORY_DATA:
00140     msg_name = strdup("Exploratory Data");
00141     break;
00142   case PUSH_EXPLORATORY_DATA:
00143     msg_name = strdup("Push Exploratory Data");
00144     break;
00145   case POSITIVE_REINFORCEMENT:
00146     msg_name = strdup("Positive Reinforcement");
00147     break;
00148   case NEGATIVE_REINFORCEMENT:
00149     msg_name = strdup("Negative Reinforcement");
00150     break;
00151   default:
00152     msg_name = strdup("Unknown");
00153     break;
00154   }
00155 
00156   // Get local time
00157   GetTime(&tv);
00158 
00159   if (dst == BROADCAST_ADDR){
00160     fprintf(stdout,
00161         "Diffusion Log: Time %ld.%06ld Node %d sending broadcast %d bytes to node %d message %s\n",
00162         tv.tv_sec, tv.tv_usec, node_id_, len, dst, msg_name);
00163   }
00164   else{
00165     fprintf(stdout,
00166         "Diffusion Log: Time %ld.%06ld Node %d sending unicast %d bytes to node %d message %s\n",
00167         tv.tv_sec, tv.tv_usec, node_id_, len, dst, msg_name);
00168   }
00169 
00170   fflush(NULL);
00171 
00172   free(msg_name);
00173 
00174   // Send packet to device
00175   IOHook::sendPacket(pkt, len, dst);
00176 }

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