Advertisement
niposch

Untitled

Nov 17th, 2023
790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.39 KB | None | 0 0
  1. Public Class TCP
  2.     Public Shared Function TraverseStates(ByVal r As String()) As String
  3.         Dim dict = ParseStates()
  4.        
  5.         Dim currentState = "CLOSED"
  6.         For Each el As String In r
  7.             If Not dict.ContainsKey(currentState)Then Return "ERROR"
  8.             Dim currentDict = dict(currentState)
  9.             If Not currentDict.ContainsKey(el) Then Return "ERROR"
  10.             currentState = currentDict(el)
  11.         Next
  12.        
  13.         Return currentState
  14.        
  15.     End Function
  16.    
  17.    
  18.    
  19.     '                                           init state            event       new_state
  20.     Public Shared Function ParseStates() As Dictionary(Of String, Dictionary(Of String, String))
  21.         Dim parsedStates = states.Split(Environment.NewLine).
  22.             Select(Function(s) ParseStateLine(s)).
  23.             ToList()
  24.            
  25.         Dim results = New Dictionary(Of String, Dictionary(Of String, String))
  26.        
  27.         For Each line As Tuple(Of String, String, String) In parsedStates
  28.             If Not results.ContainsKey(line.Item1) Then results.Add(line.Item1, New Dictionary(Of String, String))
  29.             Dim subdict = results(line.Item1)
  30.             subdict(line.Item2) = line.Item3
  31.         Next
  32.        
  33.         Return results
  34.     End Function   
  35.  
  36.     Public Shared Function ParseStateLine(line As String) As Tuple(Of String, String, String)
  37.         Dim parts = line.Split(":")
  38.         Dim initialState = parts(0).Trim()
  39.         parts = parts(1).Split("->")
  40.         Dim [event] = parts(0).Trim()
  41.         Dim resultingState = parts(1).Trim()
  42.        
  43.         Return New Tuple(Of String, String, String)(initialState, [event], resultingState)
  44.        
  45.     End Function
  46.  
  47.     Class State
  48.         Public InitialState As String
  49.         Public EventName As String
  50.         Public NewState As String
  51.     End Class
  52.  
  53.     Dim Shared states As String = "CLOSED: APP_PASSIVE_OPEN -> LISTEN
  54.     CLOSED: APP_ACTIVE_OPEN  -> SYN_SENT
  55.     LISTEN: RCV_SYN          -> SYN_RCVD
  56.     LISTEN: APP_SEND         -> SYN_SENT
  57.     LISTEN: APP_CLOSE        -> CLOSED
  58.     SYN_RCVD: APP_CLOSE      -> FIN_WAIT_1
  59.     SYN_RCVD: RCV_ACK        -> ESTABLISHED
  60.     SYN_SENT: RCV_SYN        -> SYN_RCVD
  61.     SYN_SENT: RCV_SYN_ACK    -> ESTABLISHED
  62.     SYN_SENT: APP_CLOSE      -> CLOSED
  63.     ESTABLISHED: APP_CLOSE   -> FIN_WAIT_1
  64.     ESTABLISHED: RCV_FIN     -> CLOSE_WAIT
  65.     FIN_WAIT_1: RCV_FIN      -> CLOSING
  66.     FIN_WAIT_1: RCV_FIN_ACK  -> TIME_WAIT
  67.     FIN_WAIT_1: RCV_ACK      -> FIN_WAIT_2
  68.     CLOSING: RCV_ACK         -> TIME_WAIT
  69.     FIN_WAIT_2: RCV_FIN      -> TIME_WAIT
  70.     TIME_WAIT: APP_TIMEOUT   -> CLOSED
  71.     CLOSE_WAIT: APP_CLOSE    -> LAST_ACK
  72.     LAST_ACK: RCV_ACK        -> CLOSED"
  73. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement