Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. using Manager;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12.  
  13. namespace Manager
  14. {
  15. class StateObject
  16. {
  17. // Client socket
  18. public Socket WorkSocket { get; set; } = null;
  19. // Size of receive buffer
  20. public const int BufferSize = 1024;
  21. // Receive buffer
  22. public byte[] Buffer { get; set; } = new byte[BufferSize];
  23. // Received data string
  24. public StringBuilder sb { get; set; } = new StringBuilder();
  25. }
  26. public class Manager
  27. {
  28. Config config;
  29. private ManualResetEvent AllDone = new ManualResetEvent(false);
  30. private const int Backlog = 100;
  31. private IPAddress ManagementSystemAddress = IPAddress.Parse("127.0.1.1");
  32. private Socket Server;
  33. private const int ManagementSystemPort = 123;
  34. private ConcurrentDictionary<string, Socket> RouterNameToSocket;
  35. public ConcurrentDictionary<Socket, string> SocketToRouterName { get; set; }
  36. // public Dictionary<string, List<FtnTableRow>> RouterNameToFTN_Table { get; set; } = new Dictionary<string, List<FtnTableRow>>();
  37. // public Dictionary<string, List<MplsFibTableRow>> RouterNameToMPLS_FIB_Table { get; set; } = new Dictionary<string, List<MplsFibTableRow>>();
  38. // public Dictionary<string, List<IpFibTableRow>> RouterNameToIP_FIB_Table { get; set; } = new Dictionary<string, List<IpFibTableRow>>();
  39. // public Dictionary<string, List<IlmTableRow>> RouterNameToILMTable { get; set; } = new Dictionary<string, List<IlmTableRow>>();
  40. // public Dictionary<string, List<NHLFETableRow>> RouterNameToNHLFE_Table { get; set; } = new Dictionary<string, List<NHLFETableRow>>();
  41.  
  42. public Manager()
  43. {
  44. RouterNameToSocket = new ConcurrentDictionary<string, Socket>();
  45. SocketToRouterName = new ConcurrentDictionary<Socket, string>();
  46. }
  47. public Manager(Config cof)
  48. {
  49. RouterNameToSocket = new ConcurrentDictionary<string, Socket>();
  50. SocketToRouterName = new ConcurrentDictionary<Socket, string>();
  51. config = cof;
  52.  
  53. }
  54.  
  55. public void Start()
  56. {
  57. Task.Run(action: () => StartServer());
  58. }
  59.  
  60. private void StartServer()
  61. {
  62. Server = new Socket(ManagementSystemAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  63. try
  64. {
  65. Server.Bind(new IPEndPoint(ManagementSystemAddress, ManagementSystemPort));
  66. Server.Listen(Backlog);
  67. while (true)
  68. {
  69. AllDone.Reset();
  70. Server.BeginAccept(new AsyncCallback(AcceptCallback), Server);
  71. AllDone.WaitOne();
  72. }
  73. }
  74. catch (Exception e)
  75. {
  76. }
  77. }
  78.  
  79. private void AcceptCallback(IAsyncResult ar)
  80. {
  81. // Signal the main thread to continue.
  82. AllDone.Set();
  83.  
  84. // Get the socket that handles the client request.
  85. Socket listener = (Socket)ar.AsyncState;
  86. Socket handler = listener.EndAccept(ar);
  87.  
  88. // Create the state object.
  89. StateObject state = new StateObject();
  90. state.WorkSocket = handler;
  91. handler.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state);
  92. }
  93.  
  94. private void ReadCallback(IAsyncResult ar)
  95. {
  96. // Retrieve the state object and the handler socket
  97. // from the asynchronous state object.
  98. StateObject state = (StateObject)ar.AsyncState;
  99. Socket handler = state.WorkSocket;
  100. // Read data from the client socket.
  101. int bytesRead;
  102. try
  103. {
  104. bytesRead = handler.EndReceive(ar);
  105. }
  106. catch (Exception)
  107. {
  108. Socket outSocket;
  109. string outString;
  110.  
  111. var routerName = SocketToRouterName[handler];
  112. RouterNameToSocket.TryRemove(routerName, out outSocket);
  113. SocketToRouterName.TryRemove(handler, out outString);
  114. // if the client has been shutdown, then close the connection
  115. handler.Shutdown(SocketShutdown.Both);
  116. handler.Close();
  117. return;
  118. }
  119. state.sb.Append(Encoding.ASCII.GetString(state.Buffer, 0, bytesRead));
  120. var content = state.sb.ToString().Split(' ');
  121. if (content[0].Equals("HELLO"))
  122. {
  123. var routerName = content[1];
  124.  
  125. while (true)
  126. {
  127. var success = RouterNameToSocket.TryAdd(routerName, handler);
  128. if (success)
  129. {
  130. break;
  131. }
  132. Thread.Sleep(100);
  133. }
  134.  
  135. while (true)
  136. {
  137. var success = SocketToRouterName.TryAdd(handler, routerName);
  138. if (success)
  139. {
  140. break;
  141. }
  142. Thread.Sleep(100);
  143. }
  144. SendResponse(routerName, handler);
  145. }
  146. else
  147. {
  148.  
  149. return;
  150. }
  151. state.sb.Clear();
  152. handler.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
  153. }
  154.  
  155. private void SendResponse(string routerName, Socket handler)
  156. {
  157. byte[] responseMessage = Encoding.ASCII.GetBytes("HELLO");
  158. handler.Send(responseMessage);
  159.  
  160. SendTable(routerName);
  161.  
  162. }
  163. public void SendTable(string routerName)
  164. {
  165. var socket = RouterNameToSocket[routerName];
  166. StringBuilder sb = new StringBuilder();
  167. for (int i = 0; i < config.configs.Count; i++)
  168. {
  169. if (config.configs[i].routerName.Equals(routerName))
  170. {
  171. sb.Append(config.configs[i].inLabel + " " + config.configs[i].outLabel + " " + config.configs[i].inPort + " " + config.configs[i].outPort + " " + config.configs[i].routerName + " " + config.configs[i].newLabel + " " + config.configs[i].labelAction + " " + config.configs[i].operationID + " ");
  172. }
  173. }
  174. byte[] data = Encoding.ASCII.GetBytes(sb.ToString());
  175. socket.Send(data);
  176. }
  177. public void SendNewTable(string routerName,string mess)
  178. {
  179. var socket = RouterNameToSocket[routerName];
  180. byte[] data = Encoding.ASCII.GetBytes(mess);
  181. socket.Send(data);
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement