Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 86ba59ef00
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 196 lines (161 sloc) 6.522 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>

#include "ensemble_container.hpp"
#include "series.hpp"
#include "pinger.hpp"
#include "printer.hpp"
#include "buffer.hpp"
#include "throughput.hpp"
#include "delay.hpp"
#include "stochastic_loss.hpp"
#include "collector.hpp"
#include "screener.hpp"
#include "jitter.hpp"
#include "extractor.hpp"
#include "isender.hpp"
#include "pawn.hpp"
#include "utility.hpp"
#include "intermittent.hpp"
#include "squarewave.hpp"
#include "timequantize.hpp"

#include "series.cpp"
#include "ensemble_container.cpp"
#include "isender.cpp"
#include "diverter.cpp"
#include "value_iterator.cpp"

class TwoTerminalNetwork {
public:
  template <class SenderObject, class ReceiverObject, class BreakageObject>
  class DemoNet {
    typedef Series< Series<Series< Pinger, BreakageObject >,
SenderObject>,
Series< Buffer,
Series< Series< Throughput, StochasticLoss >,
Diverter< Series< TimeQuantize, ReceiverObject >,
Collector > > > > Channel;
  };

  typedef typename DemoNet<Pawn, Collector, Intermittent>::Channel SimulatedChannel;
  typedef ISender<SimulatedChannel> SmartSender;
  typedef typename DemoNet<SmartSender, SignallingCollector, SquareWave>::Channel RealChannel;
  
  template <class ChannelType, class SenderObject, class ReceiverObject>
  class Navigator
  {
  public:
    static ReceiverObject & get_collector( ChannelType *ch )
    {
      return ch->get_second().get_second().get_second().get_first().get_second();
    }

    static Collector & get_cross_traffic( ChannelType *ch )
    {
      return ch->get_second().get_second().get_second().get_second();
    }

    static SenderObject & get_sender( ChannelType *ch )
    {
      return ch->get_first().get_second();
    }

    static ChannelType * get_root( SenderObject *ch )
    {
      Channel *top = ch->get_container_channel()->get_container_channel();
      ChannelType *top_rc = dynamic_cast<ChannelType *>( top );
      assert( top_rc );
      return top_rc;
    }

    static ChannelType * get_root( ReceiverObject *ch )
    {
      Channel *top = ch->get_container_channel()->get_container_channel()->get_container_channel()->get_container_channel()->get_container_channel();
      ChannelType *top_rc = dynamic_cast<RealChannel *>( top );
      assert( top_rc );
      return top_rc;
    }

    static void reset( ChannelType *ch )
    {
      get_collector( ch ).reset();
      get_cross_traffic( ch ).reset();
    }
  };

public:
  class TheExtractor : public Extractor<SimulatedChannel>
  {
  private:
    Navigator<SimulatedChannel, Pawn, Collector> sim;
    Navigator<RealChannel, SmartSender, SignallingCollector> real;

  public:
    TheExtractor() : sim(), real() {}

    /* Let the true sender find its collector and cross traffic */
    SignallingCollector & get_collector( SmartSender *ch ) { return real.get_collector( real.get_root( ch ) ); }
    Collector & get_cross_traffic( SmartSender *ch ) { return real.get_cross_traffic( real.get_root( ch ) ); }

    /* Let him reset all collectors */
    void reset( SmartSender *ch ) { return real.reset( real.get_root( ch ) ); }

    /* Let an omniscient find the simulated collector, pawn and cross traffic */
    Collector & get_collector( SimulatedChannel &ch ) { return sim.get_collector( &ch ); }
    Collector & get_cross_traffic( SimulatedChannel &ch ) { return sim.get_cross_traffic( &ch ); }
    Pawn & get_pawn( SimulatedChannel &ch ) { return sim.get_sender( &ch ); }

    /* Let him reset all collectors */
    void reset( SimulatedChannel &ch ) { return sim.reset( &ch ); }

    /* Let us find the true collector and cross traffic */
    Collector & get_collector( RealChannel &ch ) { return real.get_collector( &ch ); }
    Collector & get_cross_traffic( RealChannel &ch ) { return real.get_cross_traffic( &ch ); }
  };

  class TheWaker : public Waker
  {
  private:
    Navigator<RealChannel, SmartSender, SignallingCollector> real;

  public:
    TheWaker() : real() {}
    void wakeup_smart_sender( SignallingCollector *ch, double time ) { real.get_root( ch )->get_first().sleep_until( time, 1, 99 ); }
  };

  TheExtractor extractor;
  TheWaker waker;

  TwoTerminalNetwork() : extractor(), waker() {}
};

int main( void )
{
  srand( 0 );

  TwoTerminalNetwork network;
  EnsembleContainer<typename TwoTerminalNetwork::RealChannel> truth;
  EnsembleContainer<typename TwoTerminalNetwork::SimulatedChannel> prior;

  truth.set_follow_all_forks( false );

  for ( double ping_interval = 0.2; ping_interval <= 0.9; ping_interval += 0.1 ) {
    for ( int bufsize = 12000*3; bufsize <= 12000*3; bufsize += 12000 ) {
      for ( int init = 0; init * 12000 <= 0 /* bufsize */; init++ ) {
        for ( int linkspeed = 12000*8; linkspeed <= 12000*10; linkspeed += 12000*2 ) {
for ( double lossrate = 0.2; lossrate <= 0.2; lossrate += 0.05 ) {
ping_interval = variable_round( ping_interval, 0.1 );
printf( "PING INTERVAL: %.30f\n", ping_interval );
prior.add( series( series( series( Pinger( ping_interval, 1, true ), Intermittent( .00069, .1 ) ),
Pawn() ),
series( Buffer( bufsize, init, 12000 ),
series( series( Throughput( linkspeed ), StochasticLoss( lossrate ) ),
diverter( series( TimeQuantize( 0.01 ), Collector() ),
Collector() ) ) ) ) );
}
        }
      }
    }
  }

  prior.normalize();

  double my_linkspeed = 12000*10;
  double my_bufsize = 12000*3;
  double my_lossrate = 0.2;
  double my_ping_interval = variable_round( 0.2, 0.1 );

  printf( "MY PING INTERVAL: %.30f\n", my_ping_interval );

  truth.add( series( series( series( Pinger( my_ping_interval, 1, true ), SquareWave( 200 ) ),
                             TwoTerminalNetwork::SmartSender( prior, &network.extractor ) ),
                     series( Buffer( my_bufsize ),
                             series( series( Throughput( my_linkspeed ), StochasticLoss( my_lossrate ) ),
                                     diverter( series( TimeQuantize( 0.01 ), SignallingCollector( &network.waker ) ),
                                               Collector() ) ) ) ) );

  truth.normalize();

  truth.set_printing( true );

  printf( "Starting with %d channels...\n", prior.size() );
  cout << prior.identify();
  fflush( NULL );

  while ( truth.tick() && (truth.time() < 350) ) {
    cout << "===" << endl;
    cout << "True channel at time " << truth.time() << ":" << endl;
    cout << truth.identify();
  }

  return 0;
}
Something went wrong with that request. Please try again.