Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/19/16 23:32:37 (9 years ago)
Author:
thasling
Message:

#2615:
delete unneeded project, appended code style

Location:
branches/thasling/DistributedGA/DistributedGA.Core/Implementation
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified branches/thasling/DistributedGA/DistributedGA.Core/Implementation/MessageContractImpl.cs

    r13887 r13924  
    44using DistributedGA.Core.Interface;
    55
    6 namespace DistributedGA.Core.Implementation
    7 {
    8     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    9     public class MessageContractImpl : IMessageContract
    10     {
    11         public void SendData(PeerInfo sender, byte[][] data)
    12         {
    13             MessageRecieveEventArgs args = new MessageRecieveEventArgs() { Sender = sender, data = data };
     6namespace DistributedGA.Core.Implementation {
     7  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
     8  public class MessageContractImpl : IMessageContract {
     9    public void SendData(PeerInfo sender, byte[][] data) {
     10      MessageRecieveEventArgs args = new MessageRecieveEventArgs() { Sender = sender, data = data };
    1411
    15             if (MessageRecieved != null)
    16             {
    17                 MessageRecieved(this, args);
    18             }
    19         }
     12      if (MessageRecieved != null) {
     13        MessageRecieved(this, args);
     14      }
     15    }
    2016
    21         public event EventHandler<MessageRecieveEventArgs> MessageRecieved;
     17    public event EventHandler<MessageRecieveEventArgs> MessageRecieved;
    2218
    23     }
     19  }
    2420}
  • TabularUnified branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs

    r13918 r13924  
    77using DistributedGA.Core.Interface;
    88
    9 namespace DistributedGA.Core.Implementation
    10 {
    11     public class PeerNetworkMessageHandler : IMessageHandler
    12     {
    13         //own peer-instance Information
    14         private PeerInfo ownInstance = null;
     9namespace DistributedGA.Core.Implementation {
     10  public class PeerNetworkMessageHandler : IMessageHandler {
     11    //own peer-instance Information
     12    private PeerInfo ownInstance = null;
    1513
    16         //uses peer-list from IPeerListManager to decide which peers to contact
    17         private IPeerListManager peerListManager;
     14    //uses peer-list from IPeerListManager to decide which peers to contact
     15    private IPeerListManager peerListManager;
    1816
    19         //uses IMessageSender to send populations to peers
    20         private IMessageSender sender = null;
     17    //uses IMessageSender to send populations to peers
     18    private IMessageSender sender = null;
    2119
    22         //provides current population for the higher layer IMigrationOperator
    23         //to queues are used to gather and and provide population more efficiently
    24         private object activeQueueLocker = new object();
    25         private ConcurrentQueue<byte[]> writeQueue;
    26         private ConcurrentQueue<byte[]> readQueue;
     20    //provides current population for the higher layer IMigrationOperator
     21    //to queues are used to gather and and provide population more efficiently
     22    private object activeQueueLocker = new object();
     23    private ConcurrentQueue<byte[]> writeQueue;
     24    private ConcurrentQueue<byte[]> readQueue;
    2725
    28         //uses IMessageService for recieving population from one peer at once
    29         private IMessageService host = null;
     26    //uses IMessageService for recieving population from one peer at once
     27    private IMessageService host = null;
    3028
    3129
    3230
    33         public void Init(string lanIpPrefix, string contactServerUrl)
    34         {
    35             try
    36             {
    37                 ownInstance = new PeerInfo()
    38                 {
    39                     IpAddress = GetInternalIpAddress(lanIpPrefix),
    40                     Port = 0,
    41                     ProblemInstance = "TestProblem"
    42                 }; // TODO: get own peerinfo
     31    public void Init(string lanIpPrefix, string contactServerUrl) {
     32      try {
     33        ownInstance = new PeerInfo() {
     34          IpAddress = GetInternalIpAddress(lanIpPrefix),
     35          Port = 0,
     36          ProblemInstance = "TestProblem"
     37        }; // TODO: get own peerinfo
    4338
    44                 writeQueue = new ConcurrentQueue<byte[]>();
    45                 readQueue = new ConcurrentQueue<byte[]>();
     39        writeQueue = new ConcurrentQueue<byte[]>();
     40        readQueue = new ConcurrentQueue<byte[]>();
    4641
    4742
    48                 host = new WcfMessageService();
    49                 ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
    50                 host.OnDataRecieved += new EventHandler<MessageRecieveEventArgs>(OnPopulationRecieved);
     43        host = new WcfMessageService();
     44        ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
     45        host.OnDataRecieved += new EventHandler<MessageRecieveEventArgs>(OnPopulationRecieved);
    5146
    52                 peerListManager = new WcfPeerListManager();
    53                 peerListManager.Init(ownInstance, contactServerUrl);
     47        peerListManager = new WcfPeerListManager();
     48        peerListManager.Init(ownInstance, contactServerUrl);
    5449
    55                 sender = new WcfMessageSender();
    56                 sender.Init(ownInstance);
     50        sender = new WcfMessageSender();
     51        sender.Init(ownInstance);
    5752
    58             }
    59             catch (Exception ex)
    60             {
    61                 AddError("PeerNetworkMessageHandler.Init", ex);
    62             }
     53      }
     54      catch (Exception ex) {
     55        AddError("PeerNetworkMessageHandler.Init", ex);
     56      }
     57    }
     58
     59    public void Dispose() {
     60      try {
     61        host.Dispose();
     62        sender.Dispose();
     63        peerListManager.Dispose();
     64      }
     65      catch (Exception ex) {
     66        AddError("PeerNetworkMessageHandler.Dispose", ex);
     67      }
     68    }
     69
     70    public void PublishDataToNetwork(byte[][] data) {
     71      try {
     72        foreach (PeerInfo peer in peerListManager.GetPeerList()) {
     73          //maybe something will go wrong during network communication...
     74          try {
     75            sender.SendData(peer, data);
     76          }
     77          catch (Exception ex) {
     78            AddError("PeerNetworkMessageHandler.PublishDataToNetwork(during sending to one peer!)", ex);
     79          }
     80        }
     81      }
     82      catch (Exception ex) {
     83        AddError("PeerNetworkMessageHandler.PublishDataToNetwork", ex);
     84      }
     85    }
     86
     87    public byte[][] GetDataFromNetwork() {
     88      try {
     89        List<byte[]> res = new List<byte[]>();
     90        byte[] item = null;
     91        lock (activeQueueLocker) {
     92          var tmp = readQueue;
     93          readQueue = writeQueue;
     94          writeQueue = tmp;
    6395        }
    6496
    65         public void Dispose()
    66         {
    67             try
    68             {
    69                 host.Dispose();
    70                 sender.Dispose();
    71                 peerListManager.Dispose();
    72             }
    73             catch (Exception ex)
    74             {
    75                 AddError("PeerNetworkMessageHandler.Dispose", ex);
    76             }
     97        //creating resultset
     98        while (!readQueue.IsEmpty) {
     99          if (readQueue.TryDequeue(out item)) {
     100            res.Add(item);
     101          }
    77102        }
     103        return res.ToArray();
     104      }
     105      catch (Exception ex) {
     106        AddError("PeerNetworkMessageHandler.GetDataFromNetwork", ex);
     107        return null;
     108      }
     109    }
    78110
    79         public void PublishDataToNetwork(byte[][] data)
    80         {
    81             try
    82             {
    83                 foreach (PeerInfo peer in peerListManager.GetPeerList())
    84                 {
    85                     //maybe something will go wrong during network communication...
    86                     try
    87                     {
    88                         sender.SendData(peer, data);
    89                     }
    90                     catch (Exception ex)
    91                     {
    92                         AddError("PeerNetworkMessageHandler.PublishDataToNetwork(during sending to one peer!)", ex);
    93                     }
    94                 }
    95             }
    96             catch (Exception ex)
    97             {
    98                 AddError("PeerNetworkMessageHandler.PublishDataToNetwork", ex);
    99             }
     111    public PeerInfo GetPeerInfo() {
     112      return ownInstance;
     113    }
     114
     115    public List<PeerInfo> GetCurrentNetwork() {
     116      return peerListManager.GetPeerList();
     117    }
     118
     119    private string GetInternalIpAddress(string ipPrefix) {
     120      try {
     121        var strHostName = Dns.GetHostName();
     122        // Then using host name, get the IP address list..
     123        IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
     124        IPAddress[] addr = ipEntry.AddressList;
     125
     126        return addr
     127          .Select(ip => ip.ToString())
     128          .First(str => str.StartsWith(ipPrefix));
     129      }
     130      catch { return null; }
     131    }
     132
     133    private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e) {
     134      if (e != null) {
     135        lock (activeQueueLocker) {
     136          foreach (byte[] item in e.data) {
     137            writeQueue.Enqueue(item);
     138          }
    100139        }
     140      }
     141    }
    101142
    102         public byte[][] GetDataFromNetwork()
    103         {
    104             try
    105             {
    106                 List<byte[]> res = new List<byte[]>();
    107                 byte[] item = null;
    108                 lock (activeQueueLocker)
    109                 {
    110                     var tmp = readQueue;
    111                     readQueue = writeQueue;
    112                     writeQueue = tmp;
    113                 }
     143    private void AddError(string source, Exception ex) {
     144      if (peerListManager != null) {
     145        try {
     146          peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
     147        }
     148        catch { }
     149      }
     150    }
    114151
    115                 //creating resultset
    116                 while (!readQueue.IsEmpty)
    117                 {
    118                     if (readQueue.TryDequeue(out item))
    119                     {
    120                         res.Add(item);
    121                     }
    122                 }
    123                 return res.ToArray();
    124             }
    125             catch (Exception ex)
    126             {
    127                 AddError("PeerNetworkMessageHandler.GetDataFromNetwork", ex);
    128                 return null;
    129             }
    130         }
    131 
    132         public PeerInfo GetPeerInfo()
    133         {
    134             return ownInstance;
    135         }
    136 
    137         public List<PeerInfo> GetCurrentNetwork()
    138         {
    139             return peerListManager.GetPeerList();
    140         }
    141 
    142         private string GetInternalIpAddress(string ipPrefix)
    143         {
    144             try
    145             {
    146                 var strHostName = Dns.GetHostName();
    147                 // Then using host name, get the IP address list..
    148                 IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
    149                 IPAddress[] addr = ipEntry.AddressList;
    150 
    151                 return addr
    152                   .Select(ip => ip.ToString())
    153                   .First(str => str.StartsWith(ipPrefix));
    154             }
    155             catch { return null; }
    156         }
    157 
    158         private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e)
    159         {
    160             if (e != null)
    161             {
    162                 lock (activeQueueLocker)
    163                 {
    164                     foreach (byte[] item in e.data)
    165                     {
    166                         writeQueue.Enqueue(item);
    167                     }
    168                 }
    169             }
    170         }
    171 
    172         private void AddError(string source, Exception ex)
    173         {
    174             if (peerListManager != null)
    175             {
    176                 try
    177                 {
    178                     peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
    179                 }
    180                 catch { }
    181             }
    182         }
    183 
    184     }
     152  }
    185153}
  • TabularUnified branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageService.cs

    r13923 r13924  
    77using DistributedGA.Core.Interface;
    88
    9 namespace DistributedGA.Core.Implementation
    10 {
    11     public class WcfMessageService : IMessageService
    12     {
    13         public event EventHandler<MessageRecieveEventArgs> OnDataRecieved;
     9namespace DistributedGA.Core.Implementation {
     10  public class WcfMessageService : IMessageService {
     11    public event EventHandler<MessageRecieveEventArgs> OnDataRecieved;
    1412
    15         private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
     13    private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
    1614
    17         private IMessageContract messageContract = null;
     15    private IMessageContract messageContract = null;
    1816
    19         public int Init(string ip)
    20         {
    21             int port = 0;
    22             port = FreeTcpPort();
     17    public int Init(string ip) {
     18      int port = 0;
     19      port = FreeTcpPort();
    2320
    24             messageContract = new MessageContractImpl();
    25             messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
     21      messageContract = new MessageContractImpl();
     22      messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
    2623
    27             var serviceUrl = "DistributedGA.svc";
    28             new Thread(() =>
    29             {
    30                 var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
    31                 var serviceUri = new Uri(baseUri, serviceUrl);
    32                 NetTcpBinding binding = new NetTcpBinding();
     24      var serviceUrl = "DistributedGA.svc";
     25      new Thread(() => {
     26        var baseUri = new Uri(string.Concat("net.tcp://", ip, ":", port, "/DistributedGA"));
     27        var serviceUri = new Uri(baseUri, serviceUrl);
     28        NetTcpBinding binding = new NetTcpBinding();
    3329
    34                 using (var host = new ServiceHost(messageContract, serviceUri))
    35                 {
    36                     host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
     30        using (var host = new ServiceHost(messageContract, serviceUri)) {
     31          host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
    3732
    38                     host.Open();
     33          host.Open();
    3934
    40                     _ResetEvent.WaitOne();
    41                 }
    42             }).Start();
    43             return port;
     35          _ResetEvent.WaitOne();
    4436        }
     37      }).Start();
     38      return port;
     39    }
    4540
    46         public void Dispose()
    47         {
    48             _ResetEvent.Set();
    49         }
     41    public void Dispose() {
     42      _ResetEvent.Set();
     43    }
    5044
    51         private int FreeTcpPort()
    52         {
    53             TcpListener l = new TcpListener(IPAddress.Loopback, 0);
    54             l.Start();
    55             int port = ((IPEndPoint)l.LocalEndpoint).Port;
    56             l.Stop();
    57             return port;
    58         }
     45    private int FreeTcpPort() {
     46      TcpListener l = new TcpListener(IPAddress.Loopback, 0);
     47      l.Start();
     48      int port = ((IPEndPoint)l.LocalEndpoint).Port;
     49      l.Stop();
     50      return port;
     51    }
    5952
    60         private void OnMessageRecieved(object sender, MessageRecieveEventArgs e)
    61         {
    62             if (OnDataRecieved != null)
    63             {
    64                 OnDataRecieved(this, e);
    65             }
    66         }
     53    private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
     54      if (OnDataRecieved != null) {
     55        OnDataRecieved(this, e);
     56      }
    6757    }
     58  }
    6859}
Note: See TracChangeset for help on using the changeset viewer.