Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10.  
  11. namespace Domain
  12. {
  13. class LRM
  14. {
  15. string name;
  16. public int outPort;
  17. string filename;
  18. string message1;
  19. string message2;
  20. private Socket _connectingSocket;
  21. List<Link> links = new List<Link>();
  22. OXForm form;
  23. public LRM(int index,int port,OXForm _form)
  24. {
  25. form = _form;
  26. name = "LRM" + index;
  27. outPort = port;
  28. }
  29.  
  30. public void send(String message)
  31. {
  32. while (_connectingSocket == null || !_connectingSocket.Connected)
  33. {
  34. try
  35. {
  36. connectWithCloud();
  37. }
  38. catch { }
  39. }
  40. SendMessage sendMessage = new SendMessage(_connectingSocket);
  41. sendMessage.Send(Encoding.ASCII.GetBytes("MESSAGE " + message + ";Port= " + outPort));
  42. }
  43.  
  44. public void connectWithCloud()
  45. {
  46. readConfigFile();
  47. while (_connectingSocket == null || !_connectingSocket.Connected)
  48. {
  49. try
  50. {
  51. _connectingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  52. _connectingSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234));
  53. SendMessage sendMessage = new SendMessage(_connectingSocket);
  54. sendMessage.Send(Encoding.ASCII.GetBytes($"HELLO {name}"));
  55. Task.Run(() => { recive(); });
  56. }
  57. catch { }
  58. Console.WriteLine("nawalam");
  59. }
  60. }
  61.  
  62. public void recive()
  63. {
  64. while (true)
  65. {
  66. while (_connectingSocket == null || !_connectingSocket.Connected)
  67. {
  68. connectWithCloud();
  69. }
  70. try
  71. {
  72. var buffer = new byte[256];
  73. _connectingSocket.Receive(buffer);
  74. string data = Encoding.Default.GetString(buffer);
  75. // form.Data(DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond.ToString() + " Received: " + data);
  76. var splittedata = data.Split(' ');
  77. var splittedata2 = splittedata[1].Split(',');
  78. if(splittedata[0].Equals("SNPLinkConnectionRequest_req"))
  79. {
  80. XmlDocument doc = new XmlDocument();
  81. doc.Load("..\\..\\..\\Config\\rc2.xml");
  82. XmlNodeList aNodes = doc.SelectNodes("/Config/link");
  83.  
  84.  
  85. message1 = "READY";
  86. message2 = "READY";
  87. foreach (XmlNode aNode in aNodes)
  88. {
  89.  
  90. string idAttribute = aNode.InnerXml;
  91. string[] split = idAttribute.Split(' ');
  92.  
  93.  
  94. // zle porownywanie jest
  95.  
  96.  
  97. if (split[4].Split('=').GetValue(1).Equals(splittedata2[1]) || split[3].Split('=').GetValue(1).Equals(splittedata2[1]))
  98. {
  99. if (Int32.Parse(split[6].Split('=').GetValue(1).ToString()) >= Int32.Parse(splittedata2[0]))
  100. {
  101. message1 = "READY";
  102. // aNode.InnerXml = idAttribute.Replace("NOTREADY", "READY");
  103. }
  104. else
  105. {
  106.  
  107. message1 = "NOTREADY";
  108. if(split[5].Split('=').GetValue(1).Equals("READY"))
  109. aNode.InnerXml = idAttribute.Replace("READY", "NOTREADY");
  110.  
  111. }
  112. doc.Save("..\\..\\..\\Config\\rc2.xml");
  113. }
  114. else if(split[4].Split('=').GetValue(1).Equals(splittedata2[2]) || split[3].Split('=').GetValue(1).Equals(splittedata2[2]))
  115. {
  116. if (Int32.Parse(split[6].Split('=').GetValue(1).ToString()) >= Int32.Parse(splittedata2[0]))
  117. {
  118. message2 = "READY";
  119. //aNode.InnerXml = idAttribute.Replace("NOTREADY", "READY");
  120. }
  121. else
  122. {
  123.  
  124. message2 = "NOTREADY";
  125. if (split[5].Split('=').GetValue(1).Equals("READY"))
  126. aNode.InnerXml = idAttribute.Replace("READY", "NOTREADY");
  127.  
  128. }
  129. doc.Save("..\\..\\..\\Config\\rc2.xml");
  130. }
  131.  
  132. }
  133.  
  134. Thread.Sleep(1000);
  135. this.send("SNPLinkConnectionRequest_resp " + message1+","+ message2 + " ;Port=" + outPort);
  136. form.Data(DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond.ToString() + " " + filename + " SNPLinkConnectionRequest_resp: (" + message1 + "," + message2 + "); Port:" + outPort);
  137.  
  138. }
  139.  
  140. }
  141. catch(Exception e)
  142. {
  143. Console.WriteLine("Listener exception");
  144. }
  145. }
  146. }
  147. public void readConfigFile()
  148. {
  149. XmlTextReader reader = null;
  150. try
  151. {
  152. var content = Environment.GetCommandLineArgs()[1];
  153. reader = new XmlTextReader(content);
  154. }
  155. catch
  156. {
  157. reader = new XmlTextReader("..\\..\\..\\Config\\rc2.xml");
  158. }
  159.  
  160.  
  161.  
  162. // dorobic jak bedziemy exe odpalac
  163.  
  164.  
  165.  
  166. while (reader.Read())
  167. {
  168. reader.Read();
  169. reader.Read();
  170. if (reader.Name.Equals("link"))
  171. {
  172. reader.Read();
  173. var split = reader.Value.ToString().Split(' ');
  174. Link link = new Link(split[0].Split('=').GetValue(1).ToString(), split[1].Split('=').GetValue(1).ToString(), Int32.Parse(split[2].Split('=').GetValue(1).ToString()), split[3].Split('=').GetValue(1).ToString(), split[4].Split('=').GetValue(1).ToString(), split[5].Split('=').GetValue(1).ToString(), Int32.Parse(split[6].Split('=').GetValue(1).ToString()));
  175. links.Add(link);
  176. }
  177.  
  178. }
  179. reader.Close();
  180.  
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement