1 | using System;
|
---|
2 | using System.Configuration;
|
---|
3 | using System.Threading;
|
---|
4 | using DistributedGA.Core.Domain;
|
---|
5 | using DistributedGA.Core.Implementation;
|
---|
6 | using DistributedGA.Core.Interface;
|
---|
7 |
|
---|
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"];
|
---|
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 | }
|
---|