Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected void toLayer3(int callingEntity, Packet p)
- {
- nToLayer3++;
- int destination;
- double arrivalTime;
- Packet packet = new Packet(p);
- if (traceLevel > 2)
- {
- System.out.println("toLayer3: " + packet);
- }
- // Set our destination
- if (callingEntity == A)
- {
- destination = B;
- }
- else if (callingEntity == B)
- {
- destination = A;
- }
- else
- {
- System.out.println("toLayer3: Warning: invalid packet sender");
- return;
- }
- // Simulate losses
- if (rand.nextDouble() < lossProb)
- {
- nLost++;
- if (traceLevel > 0)
- {
- System.out.println("toLayer3: packet being lost");
- }
- return;
- }
- // Simulate corruption
- if (rand.nextDouble() < corruptProb)
- {
- nCorrupt++;
- if (traceLevel > 0)
- {
- System.out.println("toLayer3: packet being corrupted");
- }
- double x = rand.nextDouble();
- if (x < 0.75)
- {
- String payload = packet.getPayload();
- payload = "?" + payload.substring(payload.length() - 1);
- packet.setPayload(payload);
- }
- else if (x < 0.875)
- {
- packet.setSeqnum(Math.abs(rand.nextInt()));
- }
- else
- {
- packet.setAcknum(Math.abs(rand.nextInt()));
- }
- }
- // Decide when the packet will arrive. Since the medium cannot
- // reorder, the packet will arrive 1 to 10 time units after the
- // last packet sent by this sender
- arrivalTime = eventList.getLastPacketTime(destination);
- if (arrivalTime <= 0.0)
- {
- arrivalTime = time;
- }
- arrivalTime = arrivalTime + 1.0 + (rand.nextDouble() * 9.0);
- // Finally, create and schedule this event
- if (traceLevel > 2)
- {
- System.out.println("toLayer3: scheduling arrival on other side");
- }
- Event arrival = new Event(arrivalTime, FROMLAYER3, destination, packet);
- eventList.add(arrival);
- }
Add Comment
Please, Sign In to add comment