Changeset 13918 for branches/thasling/DistributedGA
- Timestamp:
- 06/17/16 17:46:40 (8 years ago)
- Location:
- branches/thasling/DistributedGA
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.ContactServer/ContactServiceImpl.cs
r13905 r13918 92 92 DateTime tmp; 93 93 allPeers.TryRemove(pi, out tmp); 94 Console.WriteLine(string.Format("Removed peer {0}:{1} from dictionary because last access was: {2}", pi.IpAddress, pi.Port, tmp)); 94 95 } 95 96 } -
branches/thasling/DistributedGA/DistributedGA.Core.Host/Program.cs
r13888 r13918 6 6 using DistributedGA.Core.Interface; 7 7 8 namespace 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"]; 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"]; 19 15 20 21 16 IMessageHandler h = new PeerNetworkMessageHandler(); 17 h.Init(ipPrefix, serverUrl); 22 18 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 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)); 55 25 } 56 26 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) }; 27 int i = 0; 28 while (i < 20) { 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 } 62 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 } 63 47 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 } 48 } 70 49 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 } 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) }; 77 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 } 78 68 } -
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs
r13887 r13918 45 45 readQueue = new ConcurrentQueue<byte[]>(); 46 46 47 47 48 host = new WcfMessageService(); 48 49 ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet … … 67 68 { 68 69 host.Dispose(); 70 sender.Dispose(); 71 peerListManager.Dispose(); 69 72 } 70 73 catch (Exception ex) -
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/TestPeerListManager.cs
r13887 r13918 28 28 29 29 } 30 31 32 public void Dispose() 33 { 34 35 } 30 36 } 31 37 } -
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs
r13887 r13918 4 4 using DistributedGA.Core.Interface; 5 5 6 namespace DistributedGA.Core.Implementation 7 { 8 public class WcfMessageSender : IMessageSender 9 { 10 private PeerInfo myself; 6 namespace DistributedGA.Core.Implementation { 7 public class WcfMessageSender : IMessageSender { 8 private PeerInfo myself; 11 9 12 public void Init(PeerInfo source) 13 { 14 myself = source; 10 public void Init(PeerInfo source) { 11 myself = source; 12 } 13 14 public void SendData(PeerInfo destination, byte[][] data) { 15 16 var serviceUrl = "DistributedGA.svc"; 17 var baseUri = new Uri(string.Concat("net.tcp://", destination.IpAddress, ":", destination.Port, "/DistributedGA")); 18 var serviceUri = new Uri(baseUri, serviceUrl); 19 20 var binding = new NetTcpBinding(); 21 var endpoint = new EndpointAddress(serviceUri); 22 using (var myChannelFactory = new ChannelFactory<IMessageContract>(binding, endpoint)) { 23 using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) { 24 ((IMessageContract)client).SendData(myself, data); //maybe timout exception... 15 25 } 26 } 27 } 16 28 17 public void SendData(PeerInfo destination, byte[][] data) 18 { 19 var client = CreateServerClient(destination.IpAddress, destination.Port); 20 client.SendData(myself, data); //maybe timout exception... 21 } 22 23 private IMessageContract CreateServerClient(string ip, int port) 24 { 25 var serviceUrl = "DistributedGA.svc"; 26 var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA")); 27 var serviceUri = new Uri(baseUri, serviceUrl); 28 29 var binding = new NetTcpBinding(); 30 var endpoint = new EndpointAddress(serviceUri); 31 var myChannelFactory = new ChannelFactory<IMessageContract>(binding, endpoint); 32 33 IMessageContract client = null; 34 client = myChannelFactory.CreateChannel(); 35 return client; 36 } 29 public void Dispose() { 37 30 38 31 } 32 } 39 33 } -
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs
r13888 r13918 97 97 } 98 98 99 100 101 public void Dispose() 102 { 103 timer.Stop(); 104 timer.Dispose(); 105 timer = null; 106 } 99 107 } 100 108 } -
branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageSender.cs
r13887 r13918 10 10 11 11 void SendData(PeerInfo destination, byte[][] data); 12 13 void Dispose(); 12 14 } 13 15 } -
branches/thasling/DistributedGA/DistributedGA.Core/Interface/IPeerListManager.cs
r13887 r13918 13 13 14 14 void SendLogToServer(string msg); 15 16 void Dispose(); 15 17 } 16 18 }
Note: See TracChangeset
for help on using the changeset viewer.