Free cookie consent management tool by TermsFeed Policy Generator

source: branches/thasling/DistributedGA/DistributedGA.Core.Host/Program.cs @ 14061

Last change on this file since 14061 was 13972, checked in by thasling, 8 years ago

#2615:
improved log
made changes in data structure

File size: 2.5 KB
Line 
1using System;
2using System.Configuration;
3using System.Threading;
4using DistributedGA.Core.Domain;
5using DistributedGA.Core.Implementation;
6using DistributedGA.Core.Interface;
7
8namespace 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"];
15
16        IMessageHandler h = new PeerNetworkMessageHandler();
17        h.Init(ipPrefix, serverUrl, "TestProblem", 10000, 100);
18
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));
25        }
26
27        int i = 0;
28        while (i < 1000) {
29          i++;
30          Thread.Sleep(1000);
31          var message = CreateMessage(pi, i);
32          Console.WriteLine("Publishing messages...");
33          //h.PublishDataToNetwork(message);
34          Console.WriteLine("Messages published.");
35          Console.WriteLine("Recieved messages:");
36          foreach (var item in h.GetDataFromNetwork()) {
37            //Console.WriteLine(string.Format("Message:{0}", GetString(item)));
38          }
39        }
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      }
47
48    }
49
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) };
54    }
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  }
68}
Note: See TracBrowser for help on using the repository browser.