Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Haskell program to calculate statistics from a
  2. -- network simulation trace file, using point-free syntax,
  3. -- lazy evalutation, and the IO monad.
  4.  
  5. -- Ciaran Dunne, Heriot-Watt University 2017
  6.  
  7. import System.Environment
  8.  
  9. -- Main I/O procedure for analysing trace files
  10. -- The file path is read from the terminal using getArgs
  11. -- The entire string of the program is lazily read into 'file'
  12. -- The numbers required for the output are calculated using functions in the code
  13. -- Then printed as shown
  14. main = do
  15.  [path]  <- getArgs
  16.  file <- readFile path
  17.  let pkts          = createPacketList file
  18.  let totalReceived = (fromIntegral . length . receivedPackets) pkts
  19.  let totalDropped  = (length . droppedPackets) pkts
  20.  let totalSent     = (fromIntegral . length . sentPackets) pkts
  21.  let totalDequeued = (length . dequeuedPackets) pkts
  22.  let totalOverhead = (length . overhead) pkts
  23.  print ("Dropped Packets: " ++ (show totalDropped))
  24.  print ("Received Packets: " ++ (show totalReceived))
  25.  print ("Sent Packets: " ++ (show totalSent))
  26.  print ("Dequeued Packets: " ++ (show totalDequeued))
  27.  print ("Throughput: " ++ (show (totalReceived * 100 / totalSent)))
  28.  print ("Lost Packets: " ++ (show (totalSent - totalReceived)))
  29.  print ("Overhead: " ++ show totalOverhead)
  30.  
  31. -- Data type for a packet, in line with the formatting
  32. -- of the NS trace files. Deriving the Eq and Show typeclasses
  33. -- so that we can compare and print packets.
  34. data Packet = Packet
  35.     {event    :: Char,
  36.      time     :: Float,
  37.      source   :: Int,
  38.      dest     :: Int,
  39.      pktType  :: String,
  40.      size     :: Int,
  41.      flags    :: String,
  42.      fid      :: Int,
  43.      src_addr :: String,
  44.      dst_addr :: String,
  45.      seq_num  :: Int,
  46.      pkt_id   :: Int
  47.      }
  48.      deriving (Eq, Show)
  49.  
  50. -- A function that takes a list of strings and converts
  51. -- each element to it's corresponding datatype so they can
  52. -- be entries for the Packet data constructor.
  53. -- Takes lists such as:
  54. -- ["r","0.13","1","2","cbr","1000","-------","2","1.0","3.1","2","2"]
  55. toPacket :: [String] -> Packet
  56. toPacket pkt = Packet
  57.     { event    = head (pkt !! 0)
  58.     , time     = read (pkt !! 1) :: Float
  59.     , source   = read (pkt !! 2) :: Int
  60.     , dest     = read (pkt !! 3) :: Int
  61.     , pktType  = pkt !! 4
  62.     , size     = read (pkt !! 5) :: Int
  63.     , flags    = pkt !! 6
  64.     , fid      = read (pkt !! 7) :: Int
  65.     , src_addr = pkt !! 8
  66.     , dst_addr = pkt !! 9
  67.     , seq_num  = read (pkt !! 10) :: Int
  68.     , pkt_id   = read (pkt !! 11) :: Int
  69.     }
  70.  
  71. --Converts file first into lines, then splits at whitespace
  72. --then converts each element of each split line into a packet data structure
  73. --String -> [String] -> [[String]] -> [Packet]
  74. createPacketList :: String -> [Packet]
  75. createPacketList = (map toPacket) . (map words) . lines
  76.  
  77. -- Allows us to create a function to test the event of a packet
  78. -- For example:
  79. -- ev 'd' pkt = True    if the event of the pkt is 'd'
  80. ev :: Char -> Packet -> Bool
  81. ev = (. event) . (==)
  82.  
  83. -- Same as the event function, but for testing event type
  84. -- For example: ty "ack" pkt = True  
  85. -- if the type of the pkt is an acknowledgement
  86. ty :: String -> Packet -> Bool
  87. ty = (. pktType) . (==)
  88.  
  89. --Takes a list of packets, and keeps the packets
  90. --that have the 'd' event
  91. droppedPackets :: [Packet] -> [Packet]
  92. droppedPackets = filter (\p -> (ev 'd' p))
  93.  
  94. -- Keeps packets that have the 'r' event
  95. -- and aren't acknowledgements
  96. receivedPackets :: [Packet] -> [Packet]
  97. receivedPackets = filter (\p -> (ev 'r' p)&&(not (ty "ack" p)))
  98.  
  99. -- Keeps packets that have the '+' event and aren't acknowledgements
  100. sentPackets :: [Packet] -> [Packet]
  101. sentPackets = filter (\p -> (ev '+' p)&&(not (ty "ack" p)))
  102.  
  103. -- Keeps packets that have the '-' event
  104. dequeuedPackets :: [Packet] -> [Packet]
  105. dequeuedPackets = filter (ev '-')
  106.  
  107. -- Keeps packets that have the "ack" packet type
  108. overhead :: [Packet] -> [Packet]
  109. overhead = filter (ty "ack")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement