Guest User

Untitled

a guest
Jul 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. protected void toLayer3(int callingEntity, Packet p)
  2. {
  3. nToLayer3++;
  4.  
  5. int destination;
  6. double arrivalTime;
  7. Packet packet = new Packet(p);
  8.  
  9. if (traceLevel > 2)
  10. {
  11. System.out.println("toLayer3: " + packet);
  12. }
  13.  
  14. // Set our destination
  15. if (callingEntity == A)
  16. {
  17. destination = B;
  18. }
  19. else if (callingEntity == B)
  20. {
  21. destination = A;
  22. }
  23. else
  24. {
  25. System.out.println("toLayer3: Warning: invalid packet sender");
  26. return;
  27. }
  28.  
  29. // Simulate losses
  30. if (rand.nextDouble() < lossProb)
  31. {
  32. nLost++;
  33.  
  34. if (traceLevel > 0)
  35. {
  36. System.out.println("toLayer3: packet being lost");
  37. }
  38.  
  39. return;
  40. }
  41.  
  42. // Simulate corruption
  43. if (rand.nextDouble() < corruptProb)
  44. {
  45. nCorrupt++;
  46.  
  47. if (traceLevel > 0)
  48. {
  49. System.out.println("toLayer3: packet being corrupted");
  50. }
  51.  
  52. double x = rand.nextDouble();
  53. if (x < 0.75)
  54. {
  55. String payload = packet.getPayload();
  56.  
  57. payload = "?" + payload.substring(payload.length() - 1);
  58.  
  59. packet.setPayload(payload);
  60. }
  61. else if (x < 0.875)
  62. {
  63. packet.setSeqnum(Math.abs(rand.nextInt()));
  64. }
  65. else
  66. {
  67. packet.setAcknum(Math.abs(rand.nextInt()));
  68. }
  69. }
  70.  
  71. // Decide when the packet will arrive. Since the medium cannot
  72. // reorder, the packet will arrive 1 to 10 time units after the
  73. // last packet sent by this sender
  74. arrivalTime = eventList.getLastPacketTime(destination);
  75.  
  76. if (arrivalTime <= 0.0)
  77. {
  78. arrivalTime = time;
  79. }
  80.  
  81. arrivalTime = arrivalTime + 1.0 + (rand.nextDouble() * 9.0);
  82.  
  83. // Finally, create and schedule this event
  84. if (traceLevel > 2)
  85. {
  86. System.out.println("toLayer3: scheduling arrival on other side");
  87. }
  88. Event arrival = new Event(arrivalTime, FROMLAYER3, destination, packet);
  89. eventList.add(arrival);
  90. }
Add Comment
Please, Sign In to add comment