Exemplo n.º 1
0
  public static void replayNode(
      final Id id,
      final InetSocketAddress addr,
      InetSocketAddress bootaddress,
      final long startTime,
      final long randSeed)
      throws Exception {
    Environment env = new Environment();
    if (env.getParameters()
        .getBoolean("org.mpisws.p2p.testing.transportlayer.replay.Replayer_printlog"))
      printLog("0x" + id.toStringFull().substring(0, 6), env);

    //  Environment env = Environment.directEnvironment();
    System.out.println(
        id.toStringFull()
            + " "
            + addr.getAddress().getHostAddress()
            + " "
            + addr.getPort()
            + " "
            + bootaddress.getPort()
            + " "
            + startTime
            + " "
            + randSeed);
    env.destroy();
    new Replayer(id, addr, bootaddress, startTime, randSeed);
  }
    public Player(HandleImpl localHandle, HandleImpl dstHandle, final Environment env2)
        throws Exception {
      super();
      this.destHandle = dstHandle;
      //      this.id = id;
      env = cloneEnvironment(env2, localHandle.name, localHandle.id.id);

      this.logger = env.getLogManager().getLogger(Player.class, null);

      File f = new File(localHandle.name);
      if (f.exists()) {
        File f2 = new File(f, "peers");
        File[] foo = f2.listFiles();
        if (foo != null) {
          for (int c = 0; c < foo.length; c++) {
            foo[c].delete();
          }
        }

        foo = f.listFiles();
        if (foo != null) {
          for (int c = 0; c < foo.length; c++) {
            foo[c].delete();
          }
        }

        //        System.out.println("Delete "+f+","+f.delete());
      }
      //      File f = new File(name+".data");
      //      if (f.exists()) f.delete();
      //      f = new File(name+".index");
      //      if (f.exists()) f.delete();

      this.localHandle = localHandle;
      playerTable.put(localHandle, this);

      pair = keyPairGen.generateKeyPair();
      cert = caTool.sign(localHandle.name, pair.getPublic());

      t1 = getTL();

      transport = getIdTransport();

      idTLTable.put(localHandle, transport);
      pr = getPeerReview(this, transport, env);
      app = getApp();
      pr.setApp(app);
      env.getSelectorManager()
          .invoke(
              new Runnable() {
                public void run() {
                  try {
                    pr.init(Player.this.localHandle.name);
                  } catch (IOException ioe) {
                    ioe.printStackTrace();
                    env2.destroy();
                  }
                }
              });
    }
  /**
   * @param millisToFinish Call finish() after this amount of time
   * @throws Exception
   */
  public PRRegressionTest(int millisToFinish) throws Exception {

    env = RecordLayer.generateEnvironment(); // new Environment();
    env.getSelectorManager()
        .schedule(
            new TimerTask() {

              @Override
              public void run() {
                finish();
              }
            },
            millisToFinish);

    logger = env.getLogManager().getLogger(PRRegressionTest.class, null);
    setLoggingParams(env);

    buildCryptoMaterial(env);

    env.getSelectorManager()
        .invoke(
            new Runnable() {

              public void run() {
                try {
                  buildPlayers(env);

                  setupWitnesses();
                } catch (Exception ioe) {
                  env.destroy();
                  throw new RuntimeException(ioe);
                }
              }
            });
  }
 public void init() {
   //      logger.log("init()");
   rand = new Random();
   if (player.localHandle.id.id == 1) {
     // need to give the other nodes a second to boot, otherwise this message will go nowhere
     scheduleMessageToBeSent(env.getTimeSource().currentTimeMillis() + 1000, true);
   }
 }
    public void scheduleMessageToBeSent(long time, final boolean reschedule) {
      nextSendTime = time;
      if (logger.level <= Logger.FINE) logger.log("scheduling message to be sent at:" + time);
      env.getSelectorManager()
          .schedule(
              new TimerTask() {
                public String toString() {
                  return "SendMessageTask " + scheduledExecutionTime();
                }

                @Override
                public void run() {
                  sendMessage();
                  if (reschedule) scheduleMessageToBeSent();
                }
              },
              nextSendTime - env.getTimeSource().currentTimeMillis());
    }
 public BogusApp(Player player, TransportLayer<HandleImpl, ByteBuffer> tl, Environment env) {
   super();
   this.player = player;
   this.tl = tl;
   this.env = env;
   this.logger = env.getLogManager().getLogger(BogusApp.class, null);
   logger.log("new bogus app " + player);
   dest = player.destHandle;
   tl.setCallback(this);
 }
Exemplo n.º 7
0
  private static void doIinitstuff(String args[], Environment env) {
    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-verbosity") && i + 1 < args.length) {
        int num = Integer.parseInt(args[i + 1]);
        env.getParameters().setInt("loglevel", num * 100);
        break;
      }
    }

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-silent") && i + 1 < args.length) {
        env.getParameters().setInt("loglevel", Logger.SEVERE);
        break;
      }
    }

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-verbose") && i + 1 < args.length) {
        env.getParameters().setInt("loglevel", Logger.ALL);
        break;
      }
    }

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-nodes") && i + 1 < args.length) numnodes = Integer.parseInt(args[i + 1]);

      if (args[i].equals("-msgs") && i + 1 < args.length) nummsgs = Integer.parseInt(args[i + 1]);

      if (args[i].equals("-simultaneous_joins")) simultaneous_joins = true;

      if (args[i].equals("-simultaneous_msgs")) simultaneous_msgs = true;

      if (args[i].equals("-help")) {
        System.out.println(
            "Usage: HelloWorld [-msgs m] [-nodes n] [-verbose|-silent|-verbosity v]");
        System.out.println("                  [-simultaneous_joins] [-simultaneous_msgs] [-help]");
        System.out.println(
            "  Default verbosity is 8, -verbose is 1, and -silent is 10 (error msgs only).");
        System.exit(1);
      }
    }
  }
Exemplo n.º 8
0
  // This is so gross and nasty. Need to fix this later
  public void initPastryEnvironment(InetAddress inetAddress, int port) {
    LOG.debug(String.format("initPastryEnvironment(%s, %d)", inetAddress.getHostAddress(), port));
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    TimeSource timeSource = Environment.generateDefaultTimeSource();
    RandomSource randomSource = Environment.generateDefaultRandomSource(parameters, logManager);
    Processor proc = Environment.generateDefaultProcessor();

    selectorManager.setInetAddress(inetAddress);
    selectorManager.setPort(port);
    selectorManager.doPostInitializationTasks();
    environment =
        new Environment(
            selectorManager,
            proc,
            randomSource,
            timeSource,
            logManager,
            parameters,
            exceptionStrategy);

    // setup Log4j.xml watcher
    DOMConfigurator.configureAndWatch(log4jFile.getAbsolutePath(), LOG4J_WATCHER_INTERVAL);
  }
Exemplo n.º 9
0
  /**
   * Usage: HelloWorld [-msgs m] [-nodes n] [-verbose|-silent|-verbosity v] [-simultaneous_joins]
   * [-simultaneous_msgs] [-help]
   */
  public static void main(String args[]) {
    Environment env = Environment.directEnvironment();

    //    env.getParameters().setInt("loglevel", 800);
    doIinitstuff(args, env);

    HelloWorld driver = new HelloWorld(env);

    for (int i = 0; i < numnodes; i++) {
      driver.makePastryNode(i);
      if (simultaneous_joins == false) while (driver.simulate()) ;
    }
    if (simultaneous_joins) {
      System.out.println("let the joins begin!");
      while (driver.simulate()) ;
    }

    System.out.println(numnodes + " nodes constructed");

    driver.printLeafSets();

    for (int i = 0; i < nummsgs; i++) {
      driver.sendRandomMessage();
      if (simultaneous_msgs == false) while (driver.simulate()) ;
    }

    if (simultaneous_msgs) {
      System.out.println("let the msgs begin!");
      while (driver.simulate()) ;
    }
    try {
      Thread.sleep(5000);
    } catch (InterruptedException ie) {
    }
    env.destroy();
  }
Exemplo n.º 10
0
  @Before
  public void before() {
    id = Id.build("CAFEBEEF");
    healthContent = "yellow";
    typeStr = "String";
    mockPast = mock(Past.class);
    mockNodeHandle = mock(NodeHandle.class);
    headers = new HashMap<String, String>();
    mockTimeSource = mock(TimeSource.class);
    mockEnvironment = mock(Environment.class);
    when(mockEnvironment.getTimeSource()).thenReturn(mockTimeSource);

    content = new KoalaPiEntityContent(id, healthContent, headers);
    content.setVersion(0);
  }
Exemplo n.º 11
0
 private void receiveMessage(final HandleImpl i, final ByteBuffer m) {
   //      System.out.println(Thread.currentThread()+" invoking onto "+env.getSelectorManager());
   env.getSelectorManager()
       .invoke(
           new Runnable() {
             public void run() {
               try {
                 callback.messageReceived(i, m, null);
               } catch (IOException ioe) {
                 ioe.printStackTrace();
                 System.exit(0);
               }
             }
           });
   //      System.out.println(Thread.currentThread()+" invoked onto "+env.getSelectorManager());
 }
  /**
   * @param wtl
   * @param env
   * @param errorHandler call me when there is a problem
   * @param header the header to expect from a socket/packet
   * @param timeOut how long to wait before killing a socket that is not sending (milliseconds)
   */
  public MagicNumberTransportLayer(
      TransportLayer<Identifier, ByteBuffer> wtl,
      Environment env,
      ErrorHandler<Identifier> errorHandler,
      byte[] header,
      int timeOut) {
    this.logger = env.getLogManager().getLogger(MagicNumberTransportLayer.class, null);
    this.environment = env;
    this.wire = wtl;
    this.HEADER = header;
    this.SOCKET_TIMEOUT = timeOut;

    this.errorHandler = errorHandler;

    this.callback = new DefaultCallback<Identifier, ByteBuffer>(logger);

    if (this.errorHandler == null) {
      this.errorHandler = new DefaultErrorHandler<Identifier>(logger);
    }

    wire.setCallback(this);
  }
Exemplo n.º 13
0
 /**
  * Invoke a HelloWorldApp method called sendRndMsg. First choose a random application from
  * helloClients.
  */
 private void sendRandomMessage() {
   int n = helloClients.size();
   int client = environment.getRandomSource().nextInt(n);
   HelloWorldApp app = (HelloWorldApp) helloClients.get(client);
   app.sendRndMsg(environment.getRandomSource());
 }
Exemplo n.º 14
0
  public void init() {
    invokedInsert = false;
    messageId = Id.build("1111567890123456789012345678901234560023");

    content =
        new KoalaPiEntityContent(
            messageId, "what", false, "type", 4, NodeScope.AVAILABILITY_ZONE, "url", 1);
    continuation = mock(Continuation.class);

    builder = mock(MessageBuilder.class);
    when(builder.buildMessage())
        .thenReturn(new GCInsertMessage(-1, content, 0L, nodeHandle, Id.build("dest")));

    Logger logger = mock(Logger.class);

    LogManager logManager = mock(LogManager.class);
    when(logManager.getLogger(isA(Class.class), eq(instance))).thenReturn(logger);

    Parameters parameters = mock(Parameters.class);

    RandomSource randomSource = mock(RandomSource.class);

    Environment environment = mock(Environment.class);
    when(environment.getLogManager()).thenReturn(logManager);
    when(environment.getParameters()).thenReturn(parameters);
    when(environment.getRandomSource()).thenReturn(randomSource);
    TimeSource timesource = mock(TimeSource.class);
    when(timesource.currentTimeMillis()).thenReturn(System.currentTimeMillis());
    when(environment.getTimeSource()).thenReturn(timesource);

    Endpoint endpoint = mock(Endpoint.class);
    doAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                koalaDeserializer = (MessageDeserializer) invocation.getArguments()[0];
                return null;
              }
            })
        .when(endpoint)
        .setDeserializer(isA(MessageDeserializer.class));

    koalaIdFactory = mock(KoalaIdFactory.class);
    when(koalaIdFactory.buildIdFromToString(anyString()))
        .thenAnswer(
            new Answer<Id>() {
              @Override
              public Id answer(InvocationOnMock invocation) throws Throwable {
                return Id.build((String) invocation.getArguments()[0]);
              }
            });

    when(koalaIdFactory.buildId(anyString()))
        .thenAnswer(
            new Answer<Id>() {
              @Override
              public Id answer(InvocationOnMock invocation) throws Throwable {
                return Id.build((String) invocation.getArguments()[0]);
              }
            });

    node = mock(PastryNode.class);
    when(node.getEnvironment()).thenReturn(environment);
    when(node.buildEndpoint(isA(Application.class), eq(instance))).thenReturn(endpoint);
    when(node.getIdFactory()).thenReturn(koalaIdFactory);

    sentBackupContent = new ArrayList<KoalaMutableContent>();
    backupIds = new HashSet<rice.p2p.commonapi.Id>();

    storageManager = mock(StorageManager.class);
    koalaPiEntityFactory = mock(KoalaPiEntityFactory.class);
  }
Exemplo n.º 15
0
 public void setLoggingParams(Environment env) {
   env.getParameters()
       .setInt("org.mpisws.p2p.testing.transportlayer.peerreview_loglevel", Logger.INFO);
   //    env.getParameters().setInt("org.mpisws.p2p.transport.peerreview.audit_loglevel",
   // Logger.FINEST);
 }
Exemplo n.º 16
0
 // to be overridden
 public Environment cloneEnvironment(Environment env2, String name, int id) {
   return env2.cloneEnvironment(name);
 }
Exemplo n.º 17
0
  public Replayer(
      final Id id,
      final InetSocketAddress addr,
      InetSocketAddress bootaddress,
      final long startTime,
      final long randSeed)
      throws Exception {
    this.bootaddress = bootaddress;
    Environment env = ReplayLayer.generateEnvironment(id.toString(), startTime, randSeed, null);

    final Parameters params = env.getParameters();

    params.setInt(
        "pastry_socket_scm_max_open_sockets",
        params.getInt(
            "org.mpisws.p2p.testing.transportlayer.replay_pastry_socket_scm_max_open_sockets"));

    params.setBoolean("pastry_socket_use_own_random", false);
    //    env.getParameters().setInt("rice.environment.random_loglevel", Logger.FINER);

    logger = env.getLogManager().getLogger(Replayer.class, null);

    //    env.getParameters().setInt("org.mpisws.p2p.transport.peerreview.replay_loglevel",
    // Logger.FINER);

    final Logger simLogger = env.getLogManager().getLogger(EventSimulator.class, null);

    final List<ReplayLayer<InetSocketAddress>> replayers =
        new ArrayList<ReplayLayer<InetSocketAddress>>();

    SocketPastryNodeFactory factory =
        new SocketPastryNodeFactory(
            new NodeIdFactory() {
              public Id generateNodeId() {
                return id;
              }
            },
            addr.getPort(),
            env) {

          //      @Override
          //      protected TransportLayer<MultiInetSocketAddress, ByteBuffer>
          // getPriorityTransportLayer(TransportLayer<MultiInetSocketAddress, ByteBuffer> trans,
          // LivenessProvider<MultiInetSocketAddress> liveness,
          // ProximityProvider<MultiInetSocketAddress> prox, PastryNode pn) {
          //        // get rid of the priorityLayer
          //        if
          // (params.getBoolean("org.mpisws.p2p.testing.transportlayer.replay.use_priority")) {
          //          return super.getPriorityTransportLayer(trans, liveness, prox, pn);
          //        } else {
          //          return trans;
          //        }
          //      }

          @Override
          public NodeHandle getLocalHandle(PastryNode pn, NodeHandleFactory nhf) {
            SocketNodeHandle ret = (SocketNodeHandle) super.getLocalHandle(pn, nhf);
            logger.log(ret.toStringFull());
            return ret;
          }

          @Override
          protected RandomSource cloneRandomSource(
              Environment rootEnvironment, Id nodeId, LogManager lman) {
            return rootEnvironment.getRandomSource();
          }

          @Override
          protected TransportLayer<InetSocketAddress, ByteBuffer> getWireTransportLayer(
              InetSocketAddress innermostAddress, PastryNode pn) throws IOException {
            Serializer<InetSocketAddress> serializer = new InetSocketAddressSerializer();

            HashProvider hashProv = new NullHashProvider();
            SecureHistoryFactory shFactory =
                new SecureHistoryFactoryImpl(hashProv, pn.getEnvironment());
            String logName = "0x" + id.toStringFull().substring(0, 6);
            SecureHistory hist = shFactory.open(logName, "r");

            ReplayLayer<InetSocketAddress> replay =
                new ReplayLayer<InetSocketAddress>(
                    serializer, hashProv, hist, addr, pn.getEnvironment());
            replay.registerEvent(Replayer.this, EVT_BOOT, EVT_SUBSCRIBE, EVT_PUBLISH);
            replayers.add(replay);
            return replay;
          }
        };

    // construct a node, passing the null boothandle on the first loop will
    // cause the node to start its own ring
    node = (PastryNode) factory.newNode();
    app = new MyScribeClient(node);

    ReplaySM sim = (ReplaySM) env.getSelectorManager();
    ReplayLayer<InetSocketAddress> replay = replayers.get(0);
    replay.makeProgress(); // get rid of INIT event
    sim.setVerifier(replay);

    sim.start();

    //    // this is an example of th enew way
    //    //PastryNode node = factory.newNode(nidFactory.generateNodeId());
    //    //node.getBootstrapper().boot(Collections.singleton(bootaddress));
    //
    //    // the node may require sending several messages to fully boot into the ring
    //    synchronized(node) {
    //      while(!node.isReady() && !node.joinFailed()) {
    //        // delay so we don't busy-wait
    //        node.wait(500);
    //
    //        // abort if can't join
    //        if (node.joinFailed()) {
    //          throw new IOException("Could not join the FreePastry ring.
    // Reason:"+node.joinFailedReason());
    //        }
    //      }
    //    }
    //
    //    System.out.println("Finished creating new node: " + node);
    //
    //    // construct a new scribe application
    //    MyScribeClient app = new MyScribeClient(node);
    //
    //    // for all the rest just subscribe
    //    app.subscribe();

    // now, print the tree
    //    env.getTimeSource().sleep(5000);

    try {
      env.getTimeSource().sleep(55000);
    } catch (InterruptedException ie) {
      return;
    }
    env.destroy();
  }
Exemplo n.º 18
0
 public void scheduleMessageToBeSent() {
   scheduleMessageToBeSent(
       env.getTimeSource().currentTimeMillis() + rand.nextInt(1999) + 1, true);
 }