Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/08/16 17:02:06 (8 years ago)
Author:
thasling
Message:

prepared protoype for next meeting

Location:
branches/thasling/DistributedGA/DistributedGA.Core.Host
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Core.Host/App.config

    r13557 r13887  
    66  <appSettings>
    77    <add key="ContactServerURL" value="net.tcp://localhost:9090/DistributedGA.ContactServer/ContactService"/>
    8     <add key="LanIpPrefix" value="10."/>
     8    <add key="LanIpPrefix" value="192."/>
     9    <!--<add key="LanIpPrefix" value="10."/>-->
    910  </appSettings>
    1011</configuration>
  • branches/thasling/DistributedGA/DistributedGA.Core.Host/Program.cs

    r13557 r13887  
    66using DistributedGA.Core.Interface;
    77
    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"];
     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"];
    1519
     20                IMessageHandler h = new PeerNetworkMessageHandler();
     21                h.Init(ipPrefix, serverUrl);
    1622
    17         IMessageHandler h = new PeerNetworkMessageHandler();
    18         h.Init(ipPrefix, serverUrl);
    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));
     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 (i < 10)
     34                {
     35                    i++;
     36                    Thread.Sleep(1000 * 10);
     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
    2555        }
    26         int i = 1;
    27         while (i < 10) {
    28           i++;
    29           Thread.Sleep(1000 * 10);
    30           var pop1 = CreatePopulation();
    31           Console.WriteLine("Publish population into network...");
    32           h.PublishDataToNetwork(pop1);
    33           Console.WriteLine("Population published.");
    34           Console.WriteLine("Recieved populations:");
    35           foreach (var item in h.GetDataFromNetwork()) {
    36             Console.WriteLine(string.Format("Population with Quality: {0:2} in Iteration {1}", item.Quality, item.IterationNumber));
    37           }
     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) };
    3862        }
    39       } catch (Exception ex) {
    40         Console.WriteLine(ex.Message);
    41         Console.WriteLine("press any key to continue...");
    42         Console.ReadLine();
    43       }
    4463
     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        }
    4577    }
    46 
    47     private static SolutionInfo[] CreatePopulation() {
    48       SolutionInfo si1 = new SolutionInfo() {
    49         IterationNumber = 1,
    50         Quality = 3.5f,
    51         Solution = new Solution() {
    52         }
    53       };
    54       SolutionInfo si2 = new SolutionInfo() {
    55         IterationNumber = 2,
    56         Quality = 3.5f,
    57         Solution = new Solution() {
    58         }
    59       };
    60       SolutionInfo si3 = new SolutionInfo() {
    61         IterationNumber = 3,
    62         Quality = 3.5f,
    63         Solution = new Solution() {
    64         }
    65       };
    66       return new SolutionInfo[] { si1, si2, si3 };
    67     }
    68   }
    6978}
Note: See TracChangeset for help on using the changeset viewer.