fork download
  1. using System;
  2. using System.Collections.Generic;
  3. public class TCP
  4. {
  5.  
  6. private class State
  7. {
  8. //TcpFSM state name
  9. public string Name { get; set; }
  10.  
  11. // list of trasitions for this state
  12. public Dictionary<string, State> Transitions { get; set; } = new Dictionary<string, State>();
  13.  
  14. public State GetNextState(string eventName)
  15. {
  16. if (Transitions.TryGetValue(eventName, out var result))
  17. {
  18. return result;
  19. }
  20. return null;
  21. }
  22. }
  23.  
  24.  
  25.  
  26. public static string TraverseStates(string[] events)
  27. {
  28. var state = "CLOSED"; // initial state, always
  29. // Your code here!
  30. var tcpStateMachine = Init();
  31. var currentState = tcpStateMachine;
  32. foreach (var e in events)
  33. {
  34. currentState = currentState.GetNextState(e);
  35. if (currentState is null)
  36. {
  37. state = "ERROR";
  38. break;
  39. }
  40. state = currentState.Name;
  41. }
  42.  
  43. return state;
  44. }
  45.  
  46. private static State Init()
  47. {
  48. State initialState = new State()
  49. {
  50. Name = "CLOSED"
  51. };
  52. State listen = new State()
  53. {
  54. Name = "LISTEN",
  55. };
  56. State synSent = new State()
  57. {
  58. Name = "SYN_SENT",
  59. };
  60. State synRcvd = new State()
  61. {
  62. Name = "SYN_RCVD",
  63. };
  64.  
  65. State established = new State()
  66. {
  67. Name = "ESTABLISHED",
  68. };
  69.  
  70. State closeWait = new State()
  71. {
  72. Name = "CLOSE_WAIT"
  73. };
  74. State lastAck = new State()
  75. {
  76. Name = "LAST_ACK"
  77. };
  78.  
  79.  
  80. State finWait1 = new State()
  81. {
  82. Name = "FIN_WAIT_1"
  83. };
  84. State finWait2 = new State()
  85. {
  86. Name = "FIN_WAIT_2"
  87. };
  88.  
  89. State timeWait = new State()
  90. {
  91. Name = "TIME_WAIT"
  92. };
  93.  
  94. State closing = new State()
  95. {
  96. Name = "CLOSING"
  97. };
  98.  
  99. initialState.Transitions["APP_PASSIVE_OPEN"] = listen;
  100. initialState.Transitions["APP_ACTIVE_OPEN"] = synSent;
  101.  
  102. listen.Transitions["RCV_SYN"] = synRcvd;
  103. listen.Transitions["APP_SEND"] = synSent;
  104. listen.Transitions["APP_CLOSE"] = initialState;
  105.  
  106. synRcvd.Transitions["APP_CLOSE"] = finWait1;
  107. synRcvd.Transitions["RCV_ACK"] = established;
  108.  
  109. synSent.Transitions["RCV_SYN"] = synRcvd;
  110. synSent.Transitions["RCV_SYN_ACK"] = established;
  111. synSent.Transitions["APP_CLOSE"] = initialState;
  112.  
  113. established.Transitions["APP_CLOSE"] = finWait1;
  114. established.Transitions["RCV_FIN"] = closeWait;
  115.  
  116. finWait1.Transitions["RCV_FIN"] = closing;
  117. finWait1.Transitions["RCV_FIN_ACK"] = timeWait;
  118. finWait1.Transitions["RCV_ACK"] = finWait2;
  119.  
  120. closing.Transitions["RCV_ACK"] = timeWait;
  121. finWait2.Transitions["RCV_FIN"] = timeWait;
  122.  
  123. timeWait.Transitions["APP_TIMEOUT"] = initialState;
  124.  
  125. closeWait.Transitions["APP_CLOSE"] = lastAck;
  126.  
  127. lastAck.Transitions["RCV_ACK"] = initialState;
  128.  
  129. return initialState;
  130. }
  131. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(35,34): error CS1644: Feature `pattern matching' cannot be used because it is not part of the C# 7.0 language specification
Compilation failed: 1 error(s), 0 warnings
stdout
Standard output is empty