Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Xml;
- namespace Domain
- {
- class LRM
- {
- string name;
- public int outPort;
- string filename;
- string message1;
- string message2;
- private Socket _connectingSocket;
- List<Link> links = new List<Link>();
- OXForm form;
- public LRM(int index,int port,OXForm _form)
- {
- form = _form;
- name = "LRM" + index;
- outPort = port;
- }
- public void send(String message)
- {
- while (_connectingSocket == null || !_connectingSocket.Connected)
- {
- try
- {
- connectWithCloud();
- }
- catch { }
- }
- SendMessage sendMessage = new SendMessage(_connectingSocket);
- sendMessage.Send(Encoding.ASCII.GetBytes("MESSAGE " + message + ";Port= " + outPort));
- }
- public void connectWithCloud()
- {
- readConfigFile();
- while (_connectingSocket == null || !_connectingSocket.Connected)
- {
- try
- {
- _connectingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- _connectingSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234));
- SendMessage sendMessage = new SendMessage(_connectingSocket);
- sendMessage.Send(Encoding.ASCII.GetBytes($"HELLO {name}"));
- Task.Run(() => { recive(); });
- }
- catch { }
- Console.WriteLine("nawalam");
- }
- }
- public void recive()
- {
- while (true)
- {
- while (_connectingSocket == null || !_connectingSocket.Connected)
- {
- connectWithCloud();
- }
- try
- {
- var buffer = new byte[256];
- _connectingSocket.Receive(buffer);
- string data = Encoding.Default.GetString(buffer);
- // form.Data(DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond.ToString() + " Received: " + data);
- var splittedata = data.Split(' ');
- var splittedata2 = splittedata[1].Split(',');
- if(splittedata[0].Equals("SNPLinkConnectionRequest_req"))
- {
- XmlDocument doc = new XmlDocument();
- doc.Load("..\\..\\..\\Config\\rc2.xml");
- XmlNodeList aNodes = doc.SelectNodes("/Config/link");
- message1 = "READY";
- message2 = "READY";
- foreach (XmlNode aNode in aNodes)
- {
- string idAttribute = aNode.InnerXml;
- string[] split = idAttribute.Split(' ');
- // zle porownywanie jest
- if (split[4].Split('=').GetValue(1).Equals(splittedata2[1]) || split[3].Split('=').GetValue(1).Equals(splittedata2[1]))
- {
- if (Int32.Parse(split[6].Split('=').GetValue(1).ToString()) >= Int32.Parse(splittedata2[0]))
- {
- message1 = "READY";
- // aNode.InnerXml = idAttribute.Replace("NOTREADY", "READY");
- }
- else
- {
- message1 = "NOTREADY";
- if(split[5].Split('=').GetValue(1).Equals("READY"))
- aNode.InnerXml = idAttribute.Replace("READY", "NOTREADY");
- }
- doc.Save("..\\..\\..\\Config\\rc2.xml");
- }
- else if(split[4].Split('=').GetValue(1).Equals(splittedata2[2]) || split[3].Split('=').GetValue(1).Equals(splittedata2[2]))
- {
- if (Int32.Parse(split[6].Split('=').GetValue(1).ToString()) >= Int32.Parse(splittedata2[0]))
- {
- message2 = "READY";
- //aNode.InnerXml = idAttribute.Replace("NOTREADY", "READY");
- }
- else
- {
- message2 = "NOTREADY";
- if (split[5].Split('=').GetValue(1).Equals("READY"))
- aNode.InnerXml = idAttribute.Replace("READY", "NOTREADY");
- }
- doc.Save("..\\..\\..\\Config\\rc2.xml");
- }
- }
- Thread.Sleep(1000);
- this.send("SNPLinkConnectionRequest_resp " + message1+","+ message2 + " ;Port=" + outPort);
- form.Data(DateTime.Now.ToLongTimeString() + ":" + DateTime.Now.Millisecond.ToString() + " " + filename + " SNPLinkConnectionRequest_resp: (" + message1 + "," + message2 + "); Port:" + outPort);
- }
- }
- catch(Exception e)
- {
- Console.WriteLine("Listener exception");
- }
- }
- }
- public void readConfigFile()
- {
- XmlTextReader reader = null;
- try
- {
- var content = Environment.GetCommandLineArgs()[1];
- reader = new XmlTextReader(content);
- }
- catch
- {
- reader = new XmlTextReader("..\\..\\..\\Config\\rc2.xml");
- }
- // dorobic jak bedziemy exe odpalac
- while (reader.Read())
- {
- reader.Read();
- reader.Read();
- if (reader.Name.Equals("link"))
- {
- reader.Read();
- var split = reader.Value.ToString().Split(' ');
- 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()));
- links.Add(link);
- }
- }
- reader.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement