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); } }
@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); }
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 void scheduleMessageToBeSent() { scheduleMessageToBeSent( env.getTimeSource().currentTimeMillis() + rand.nextInt(1999) + 1, true); }
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(); }
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); }