Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2615:
improved log
made changes in data structure

File size: 2.5 KB
RevLine 
[13537]1using System;
[13557]2using System.Configuration;
[13537]3using System.Threading;
4using DistributedGA.Core.Domain;
[13524]5using DistributedGA.Core.Implementation;
6using DistributedGA.Core.Interface;
7
[13918]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"];
[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}
Note: See TracBrowser for help on using the repository browser.