using System; using System.Configuration; using System.Threading; using DistributedGA.Core.Domain; using DistributedGA.Core.Implementation; using DistributedGA.Core.Interface; namespace DistributedGA.Core.Host { class Program { static void Main(string[] args) { try { Console.WriteLine("Starting peer..."); string ipPrefix = ConfigurationManager.AppSettings["LanIpPrefix"]; string serverUrl = ConfigurationManager.AppSettings["ContactServerURL"]; IMessageHandler h = new PeerNetworkMessageHandler(); h.Init(ipPrefix, serverUrl, "TestProblem", 10000, 100); PeerInfo pi = h.GetPeerInfo(); Console.WriteLine(string.Format("Peer is hostet at IP: {0} and port: {1}", pi.IpAddress, pi.Port)); Thread.Sleep(1000 * 20); Console.WriteLine("Current peers within network:"); foreach (var item in h.GetCurrentNetwork()) { Console.WriteLine(string.Format("Peer at {0}:{1}", item.IpAddress, item.Port)); } int i = 0; while (i < 1000) { i++; Thread.Sleep(1000); var message = CreateMessage(pi, i); Console.WriteLine("Publishing messages..."); //h.PublishDataToNetwork(message); Console.WriteLine("Messages published."); Console.WriteLine("Recieved messages:"); foreach (var item in h.GetDataFromNetwork()) { //Console.WriteLine(string.Format("Message:{0}", GetString(item))); } } h.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine("press any key to continue..."); Console.ReadLine(); } } private static byte[][] CreateMessage(PeerInfo pi, int iterationNumber) { string msg1 = string.Concat("Message 1 from Peer ", pi.IpAddress, ":", pi.Port, " at iteration ", iterationNumber); string msg2 = string.Concat("Message 2 from Peer ", pi.IpAddress, ":", pi.Port, " at iteration ", iterationNumber); return new byte[][] { GetBytes(msg1), GetBytes(msg2) }; } static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } static string GetString(byte[] bytes) { char[] chars = new char[bytes.Length / sizeof(char)]; System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return new string(chars); } } }