<commit_before>/*
 * Copyright (c) 2003 <NAME> (<EMAIL>)
 *                    <NAME> (<EMAIL>)
 *                    Massachusetts Institute of Technology
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 * See the HOWTO in eventgenerators/churneventgenerator.h .
 */

#include "churneventgenerator.h"
#include "p2psim/eventqueue.h"
#include "p2psim/network.h"
#include "events/p2pevent.h"
#include "events/simevent.h"
#include <math.h>
#include <time.h>
#include <list>
#include <stdlib.h>
#include <iostream>
using namespace std;

ChurnEventGenerator::ChurnEventGenerator(Args *args)
{
  if( (*args)["wkn"] == "" ) {
    _wkn_string = "1";
    _wkn = 1;
  } else {
    _wkn_string = (*args)["wkn"];
    _wkn = args->nget<IPAddress>("wkn", 1, 10);
  }

  _lifemean = args->nget( "lifemean", 100000, 10 ); //0 means no failure
  _deathmean = args->nget( "deathmean", _lifemean, 10 ); //0 means no failure
  _lookupmean = args->nget( "lookupmean", 10000, 10 );

  if( (*args)["exittime"] == "" ) {
    _exittime_string = "200000";
    _exittime = 200000;
  } else {
    _exittime_string = (*args)["exittime"];
    _exittime = args->nget( "exittime", 200000, 10 );
  }

  Node::set_collect_stat_time(args->nget("stattime",0,10));

  _ipkeys = args->nget("ipkeys", 0, 10);
  _ips = NULL;

  EventQueue::Instance()->registerObserver(this);
}

void
ChurnEventGenerator::run()
{
  // first register the exit event
  vector<string> simargs;
  simargs.push_back( _exittime_string );
  simargs.push_back( "exit" );
  SimEvent *se = New SimEvent( &simargs );
  add_event( se );

  // start all nodes at a random time between 1 and n (except the wkn, who
  // starts at 1)
  vector<IPAddress> *_ips = Network::Instance()->getallfirstips();


  IPAddress ip = 0;
  for(u_int xxx = 0; xxx < _ips->size(); xxx++){
    ip = (*_ips)[xxx];

    Args *a = New Args();
    (*a)["wellknown"] = _wkn_string;
    (*a)["first"] = _wkn_string; //a hack
    u_int jointime;
    if( ip == _wkn ) {
      jointime = 1;
    } else {
      // add one to the mod factor because typical 2^n network sizes
      // make really bad mod factors
      jointime = (random()%(_ips->size()+1)) + 1;
    }
    if( now() + jointime < _exittime ) {
      P2PEvent *e = New P2PEvent(now() + jointime, ip, "join", a);
      add_event(e);
    } else {
      delete a;
      a = NULL;
    }

    // also schedule their first lookup
    a = New Args();
    Time tolookup = next_exponential( _lookupmean );
    (*a)["key"] = get_lookup_key();
    if( now() + jointime + tolookup < _exittime ) {
      P2PEvent *e = New P2PEvent(now() + jointime + tolookup, ip, "lookup", a);
      add_event(e);
    } else {
      delete a;
    }

  }
  EventQueue::Instance()->go();
}


void
ChurnEventGenerator::kick(Observed *o, ObserverInfo *oi)
{
  assert( oi );

  Event *ev = (Event *) oi;
  assert( ev );

  if( ev->name() != "P2PEvent" ) {
    // not a p2p event, so we don't care
    return;
  }
  P2PEvent *p2p_observed = (P2PEvent *) ev;
  assert( p2p_observed );
<commit_msg>if lookupmean == 0, don't do any lookups<commit_after>/*
 * Copyright (c) 2003 <NAME> (<EMAIL>)
 *                    <NAME> (<EMAIL>)
 *                    Massachusetts Institute of Technology
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 *
 * See the HOWTO in eventgenerators/churneventgenerator.h .
 */

#include "churneventgenerator.h"
#include "p2psim/eventqueue.h"
#include "p2psim/network.h"
#include "events/p2pevent.h"
#include "events/simevent.h"
#include <math.h>
#include <time.h>
#include <list>
#include <stdlib.h>
#include <iostream>
using namespace std;

ChurnEventGenerator::ChurnEventGenerator(Args *args)
{
  if( (*args)["wkn"] == "" ) {
    _wkn_string = "1";
    _wkn = 1;
  } else {
    _wkn_string = (*args)["wkn"];
    _wkn = args->nget<IPAddress>("wkn", 1, 10);
  }

  _lifemean = args->nget( "lifemean", 100000, 10 ); //0 means no failure
  _deathmean = args->nget( "deathmean", _lifemean, 10 ); //0 means no failure
  _lookupmean = args->nget( "lookupmean", 10000, 10 );

  if( (*args)["exittime"] == "" ) {
    _exittime_string = "200000";
    _exittime = 200000;
  } else {
    _exittime_string = (*args)["exittime"];
    _exittime = args->nget( "exittime", 200000, 10 );
  }

  Node::set_collect_stat_time(args->nget("stattime",0,10));

  _ipkeys = args->nget("ipkeys", 0, 10);
  _ips = NULL;

  EventQueue::Instance()->registerObserver(this);
}

void
ChurnEventGenerator::run()
{
  // first register the exit event
  vector<string> simargs;
  simargs.push_back( _exittime_string );
  simargs.push_back( "exit" );
  SimEvent *se = New SimEvent( &simargs );
  add_event( se );

  // start all nodes at a random time between 1 and n (except the wkn, who
  // starts at 1)
  vector<IPAddress> *_ips = Network::Instance()->getallfirstips();


  IPAddress ip = 0;
  for(u_int xxx = 0; xxx < _ips->size(); xxx++){
    ip = (*_ips)[xxx];

    Args *a = New Args();
    (*a)["wellknown"] = _wkn_string;
    (*a)["first"] = _wkn_string; //a hack
    u_int jointime;
    if( ip == _wkn ) {
      jointime = 1;
    } else {
      // add one to the mod factor because typical 2^n network sizes
      // make really bad mod factors
      jointime = (random()%(_ips->size()+1)) + 1;
    }
    if( now() + jointime < _exittime ) {
      P2PEvent *e = New P2PEvent(now() + jointime, ip, "join", a);
      add_event(e);
    } else {
      delete a;
      a = NULL;
    }

    // also schedule their first lookup
    a = New Args();
    Time tolookup = next_exponential( _lookupmean );
    (*a)["key"] = get_lookup_key();
    if( _lookupmean > 0 && now() + jointime + tolookup < _exittime ) {
      P2PEvent *e = New P2PEvent(now() + jointime + tolookup, ip, "lookup", a);
      add_event(e);
    } else {
      delete a;
    }

  }
  EventQueue::Instance()->go();
}


void
ChurnEventGenerator::kick(Observed *o, ObserverInfo *oi)
{
  assert( oi );

  Event *ev = (Event *) oi;
  assert( ev );

  if( ev->name() != "P2PEvent" ) {
    // not a p2p event, so we don't care
    return;
  }
  P2PEvent *p2p_observed = (P2PEvent *) ev;
  assert( p2p_observed );

{'fraction_non_alphanumeric': 0.13521781693587862, 'fraction_numerical': 0.01860009789525208, 'mean_word_length': 3.28803777544596, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1257', 'n_tokens_mistral': 2895, 'n_tokens_neox': 2697, 'n_words': 874}