[13537] | 1 | using System;
|
---|
[13557] | 2 | using System.Configuration;
|
---|
[13537] | 3 | using System.Threading;
|
---|
| 4 | using DistributedGA.Core.Domain;
|
---|
[13524] | 5 | using DistributedGA.Core.Implementation;
|
---|
| 6 | using DistributedGA.Core.Interface;
|
---|
| 7 |
|
---|
[13918] | 8 | namespace DistributedGA.Core.Host {
|
---|
| 9 | class Program {
|
---|
| 10 | static void Main(string[] args) {
|
---|
| 11 | try {
|
---|
| 12 | Console.WriteLine("Starting peer...");
|
---|
| 13 | string ipPrefix = ConfigurationManager.AppSettings["LanIpPrefix"];
|
---|
| 14 | string serverUrl = ConfigurationManager.AppSettings["ContactServerURL"];
|
---|
[13557] | 15 |
|
---|
[13918] | 16 | IMessageHandler h = new PeerNetworkMessageHandler();
|
---|
[13956] | 17 | h.Init(ipPrefix, serverUrl, "TestProblem", 10000, 100);
|
---|
[13557] | 18 |
|
---|
[13918] | 19 | PeerInfo pi = h.GetPeerInfo();
|
---|
| 20 | Console.WriteLine(string.Format("Peer is hostet at IP: {0} and port: {1}", pi.IpAddress, pi.Port));
|
---|
| 21 | Thread.Sleep(1000 * 20);
|
---|
| 22 | Console.WriteLine("Current peers within network:");
|
---|
| 23 | foreach (var item in h.GetCurrentNetwork()) {
|
---|
| 24 | Console.WriteLine(string.Format("Peer at {0}:{1}", item.IpAddress, item.Port));
|
---|
[13524] | 25 | }
|
---|
[13887] | 26 |
|
---|
[13918] | 27 | int i = 0;
|
---|
[13919] | 28 | while (i < 1000) {
|
---|
[13918] | 29 | i++;
|
---|
| 30 | Thread.Sleep(1000);
|
---|
| 31 | var message = CreateMessage(pi, i);
|
---|
| 32 | Console.WriteLine("Publishing messages...");
|
---|
[13972] | 33 | //h.PublishDataToNetwork(message);
|
---|
[13918] | 34 | Console.WriteLine("Messages published.");
|
---|
| 35 | Console.WriteLine("Recieved messages:");
|
---|
| 36 | foreach (var item in h.GetDataFromNetwork()) {
|
---|
[13972] | 37 | //Console.WriteLine(string.Format("Message:{0}", GetString(item)));
|
---|
[13918] | 38 | }
|
---|
[13537] | 39 | }
|
---|
[13918] | 40 | h.Dispose();
|
---|
| 41 | }
|
---|
| 42 | catch (Exception ex) {
|
---|
| 43 | Console.WriteLine(ex.Message);
|
---|
| 44 | Console.WriteLine("press any key to continue...");
|
---|
| 45 | Console.ReadLine();
|
---|
| 46 | }
|
---|
[13524] | 47 |
|
---|
[13918] | 48 | }
|
---|
[13537] | 49 |
|
---|
[13918] | 50 | private static byte[][] CreateMessage(PeerInfo pi, int iterationNumber) {
|
---|
| 51 | string msg1 = string.Concat("Message 1 from Peer ", pi.IpAddress, ":", pi.Port, " at iteration ", iterationNumber);
|
---|
| 52 | string msg2 = string.Concat("Message 2 from Peer ", pi.IpAddress, ":", pi.Port, " at iteration ", iterationNumber);
|
---|
| 53 | return new byte[][] { GetBytes(msg1), GetBytes(msg2) };
|
---|
[13524] | 54 | }
|
---|
[13918] | 55 |
|
---|
| 56 | static byte[] GetBytes(string str) {
|
---|
| 57 | byte[] bytes = new byte[str.Length * sizeof(char)];
|
---|
| 58 | System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
|
---|
| 59 | return bytes;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | static string GetString(byte[] bytes) {
|
---|
| 63 | char[] chars = new char[bytes.Length / sizeof(char)];
|
---|
| 64 | System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
|
---|
| 65 | return new string(chars);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
[13524] | 68 | }
|
---|