Changeset 13918


Ignore:
Timestamp:
06/17/16 17:46:40 (3 years ago)
Author:
thasling
Message:

#2615: implemented Dispose()-Method in all classes
also sending a message with the wcf-sender class was rewritten, because of Dispose
added new log entries when server removes peers from its dicionary

Location:
branches/thasling/DistributedGA
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.ContactServer/ContactServiceImpl.cs

    r13905 r13918  
    9292        DateTime tmp;
    9393        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));
    9495      }
    9596    }
  • branches/thasling/DistributedGA/DistributedGA.Core.Host/Program.cs

    r13888 r13918  
    66using DistributedGA.Core.Interface;
    77
    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"];
     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"];
    1915
    20                 IMessageHandler h = new PeerNetworkMessageHandler();
    21                 h.Init(ipPrefix, serverUrl);
     16        IMessageHandler h = new PeerNetworkMessageHandler();
     17        h.Init(ipPrefix, serverUrl);
    2218
    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));
    5525        }
    5626
    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          }
    6239        }
     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      }
    6347
    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    }
    7049
    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) };
    7754    }
     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  }
    7868}
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs

    r13887 r13918  
    4545                readQueue = new ConcurrentQueue<byte[]>();
    4646
     47
    4748                host = new WcfMessageService();
    4849                ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
     
    6768            {
    6869                host.Dispose();
     70                sender.Dispose();
     71                peerListManager.Dispose();
    6972            }
    7073            catch (Exception ex)
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/TestPeerListManager.cs

    r13887 r13918  
    2828
    2929        }
     30
     31
     32        public void Dispose()
     33        {
     34           
     35        }
    3036    }
    3137}
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs

    r13887 r13918  
    44using DistributedGA.Core.Interface;
    55
    6 namespace DistributedGA.Core.Implementation
    7 {
    8     public class WcfMessageSender : IMessageSender
    9     {
    10         private PeerInfo myself;
     6namespace DistributedGA.Core.Implementation {
     7  public class WcfMessageSender : IMessageSender {
     8    private PeerInfo myself;
    119
    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...
    1525        }
     26      }
     27    }
    1628
    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() {
    3730
    3831    }
     32  }
    3933}
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs

    r13888 r13918  
    9797        }
    9898
     99
     100
     101        public void Dispose()
     102        {
     103            timer.Stop();
     104            timer.Dispose();
     105            timer = null;
     106        }
    99107    }
    100108}
  • branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageSender.cs

    r13887 r13918  
    1010
    1111        void SendData(PeerInfo destination, byte[][] data);
     12
     13        void Dispose();
    1214    }
    1315}
  • branches/thasling/DistributedGA/DistributedGA.Core/Interface/IPeerListManager.cs

    r13887 r13918  
    1313
    1414        void SendLogToServer(string msg);
     15
     16        void Dispose();
    1517    }
    1618}
Note: See TracChangeset for help on using the changeset viewer.