Content-Based Networking Simulation Library Documentation (v. 1.2.1)

Node.h

00001 // -*- C++ -*-
00002 //
00003 //  This file is part of CBNSIM, the Content-Based Networking
00004 //  Simulation Library.
00005 //
00006 //  Authors: See the file AUTHORS for full details.
00007 //
00008 //  Copyright (C) 2002-2004 University of Colorado
00009 //
00010 //  This program is free software; you can redistribute it and/or
00011 //  modify it under the terms of the GNU General Public License
00012 //  as published by the Free Software Foundation; either version 2
00013 //  of the License, or (at your option) any later version.
00014 //
00015 //  This program is distributed in the hope that it will be useful,
00016 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //  GNU General Public License for more details.
00019 //
00020 //  You should have received a copy of the GNU General Public License
00021 //  along with this program; if not, write to the Free Software
00022 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
00023 //  USA, or send email to serl@cs.colorado.edu.
00024 //
00025 // $Id: Node.h,v 1.24 2011-08-25 12:32:38 carzanig Exp $
00026 //
00027 #ifndef CBNSIM_NODE_H
00028 #define CBNSIM_NODE_H
00029 
00030 #include <cassert>
00031 #include <map>
00032 #include <vector>
00033 
00034 #include <siena/types.h>
00035 #include <siena/ssim.h>
00036 
00037 #include "cbnsim/NodeEvent.h"
00038 #include "cbnsim/Packet.h"
00039 #include "cbnsim/types.h"
00040 
00041 namespace cbnsim
00042 {
00045   class Node : public ssim::ProcessWithPId
00046   {
00047   protected:
00048     Node( unsigned int id, unsigned int x, unsigned int y )
00049       : m_id( id ), m_x(x), m_y(y) { reset_packet_counters(); }
00050 
00051   public:
00052     virtual ~Node() { }
00053 
00057     virtual ssim::ProcessId activate();
00058 
00061     virtual void add_neighbor( ssim::ProcessId nid, ssim::Time length );
00062 
00070     bool broadcast_packet( const Packet *packet, ssim::Time lag = 0, bool count_all = false );
00071       
00075     virtual size_t bytesize() const = 0;
00076 
00080     unsigned int control_packets() const
00081     { return m_ctrl_packets; }
00082 
00086     unsigned int data_packets() const
00087     { return m_data_packets; }
00088 
00092     unsigned int id() const
00093     { return m_id; }
00094 
00098     ssim::ProcessId i2p( unsigned int ifid ) const
00099     { assert( ifid < m_if_table.size() ); return m_if_table[ifid]; }
00100 
00105     virtual void issue_packet( unsigned int ifid, const Packet* packet, ssim::Time lag = 0, bool count = true );
00106 
00109     virtual void issue_packet( ssim::ProcessId pid, const Packet* packet, ssim::Time lag = 0 );
00110 
00113     unsigned int neighbor_count() const
00114     { return m_if_table.size() - 1; }
00115 
00121     virtual void node_fail() {}
00122     virtual bool has_failed() const = 0;
00123 
00129     virtual void node_recover() {}
00130 
00134     unsigned int p2i( ssim::ProcessId pid ) const;
00135 
00140     void process_event( const ssim::Event* evt );
00141 
00148     virtual void process_packet( unsigned int ifid, const Packet& pkt ) = 0;
00149 
00152     void reset_packet_counters()
00153     { m_ctrl_packets = m_data_packets = 0; }
00154 
00162     virtual void send_message( MessageId mid, const Message& message ) {}
00163 
00169       virtual void set_predicate(const Predicate& predicate, unsigned int rate,
00170                                  bool pred_null) {}
00171 
00172       virtual void set_power(power_type p) { m_power_left = p; }
00173 
00174       virtual bool count_power() const = 0;
00175       virtual power_type power_left() { return (m_power_left > 0 ? m_power_left : 0); }
00176 
00179       unsigned int x_location() const { return m_x; }
00180 
00183       unsigned int y_location() const { return m_y; }
00184       
00185   private:
00186       static const unsigned long SEND_POWER_DRAIN = 20;  //nAh
00187       static const unsigned long RECV_POWER_DRAIN = 10;
00188 
00189       unsigned int                              m_ctrl_packets;
00190       unsigned int                              m_data_packets;
00191       std::vector<ssim::Time>                   m_delay_table;
00192       unsigned int                              m_id;
00193       std::vector<ssim::ProcessId>              m_if_table;
00194       std::map<ssim::ProcessId,unsigned int>    m_pid_map;
00195       unsigned int                              m_x, m_y;
00196       
00197       power_type                                m_power_left;
00198 
00199 #ifdef COLLISION_DETECTION_ON
00200       static CC2420RadioModel                   m_rm;
00201       static CollisionField                     m_collisions;
00202 #endif
00203 
00204   };
00205 };
00206 
00207 #endif