Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13903 was 13888, checked in by thasling, 8 years ago

increased frequences of all timers, threads, etc... for testing purposes

File size: 2.9 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{
10    class Program
11    {
12        static void Main(string[] args)
13        {
14            try
15            {
16                Console.WriteLine("Starting peer...");
17                string ipPrefix = ConfigurationManager.AppSettings["LanIpPrefix"];
18                string serverUrl = ConfigurationManager.AppSettings["ContactServerURL"];
19
20                IMessageHandler h = new PeerNetworkMessageHandler();
21                h.Init(ipPrefix, serverUrl);
22
23                PeerInfo pi = h.GetPeerInfo();
24                Console.WriteLine(string.Format("Peer is hostet at IP: {0} and port: {1}", pi.IpAddress, pi.Port));
25                Thread.Sleep(1000 * 20);
26                Console.WriteLine("Current peers within network:");
27                foreach (var item in h.GetCurrentNetwork())
28                {
29                    Console.WriteLine(string.Format("Peer at {0}:{1}", item.IpAddress, item.Port));
30                }
31
32                int i = 0;
33                while (true)
34                {
35                    i++;
36                    Thread.Sleep(1000);
37                    var message = CreateMessage(pi, i);
38                    Console.WriteLine("Publishing messages...");
39                    h.PublishDataToNetwork(message);
40                    Console.WriteLine("Messages published.");
41                    Console.WriteLine("Recieved messages:");
42                    foreach (var item in h.GetDataFromNetwork())
43                    {
44                        Console.WriteLine(string.Format("Message:{0}", GetString(item)));
45                    }
46                }
47            }
48            catch (Exception ex)
49            {
50                Console.WriteLine(ex.Message);
51                Console.WriteLine("press any key to continue...");
52                Console.ReadLine();
53            }
54
55        }
56
57        private static byte[][] CreateMessage(PeerInfo pi, int iterationNumber)
58        {
59            string msg1 = string.Concat("Message 1 from Peer ", pi.IpAddress, ":", pi.Port, " at iteration ", iterationNumber);
60            string msg2 = string.Concat("Message 2 from Peer ", pi.IpAddress, ":", pi.Port, " at iteration ", iterationNumber);
61            return new byte[][] { GetBytes(msg1), GetBytes(msg2) };
62        }
63
64        static byte[] GetBytes(string str)
65        {
66            byte[] bytes = new byte[str.Length * sizeof(char)];
67            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
68            return bytes;
69        }
70
71        static string GetString(byte[] bytes)
72        {
73            char[] chars = new char[bytes.Length / sizeof(char)];
74            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
75            return new string(chars);
76        }
77    }
78}
Note: See TracBrowser for help on using the repository browser.