Free cookie consent management tool by TermsFeed Policy Generator

source: branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs @ 13918

Last change on this file since 13918 was 13918, checked in by thasling, 8 years ago

#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

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.ServiceModel;
5using DistributedGA.Core.Domain;
6using DistributedGA.Core.Interface;
7using System.Timers;
8
9namespace DistributedGA.Core.Implementation
10{
11    public class WcfPeerListManager : IPeerListManager
12    {
13        private string serverString = null;
14
15        private IContactService client = null;
16
17        private IContactService heartbeatClient = null;
18
19        private PeerInfo myself = null;
20
21        private Timer timer = null; //sends heartbeat to contact-server
22
23        public void Init(PeerInfo source, string contactServerUrl)
24        {
25            serverString = contactServerUrl;
26            client = CreateClient();
27            heartbeatClient = CreateClient();
28            myself = source;
29            client.RegisterPeer(source);
30            timer = new Timer(1000 ); //each 5 minutes
31            timer.Elapsed += SendHeartbeatToServer;
32            timer.Start();
33        }
34
35
36
37        public List<PeerInfo> GetPeerList()
38        {
39            var allPeers = client.GetPeerList(myself);
40            var peersForMessaging = ChoosePeersForMessaging(allPeers);
41            //return peersForMessaging;
42            return allPeers; //TODO: Enable 10% list communication
43        }
44
45        public void SendLogToServer(string msg)
46        {
47            client.MakeLog(myself, msg);
48        }
49
50        private IContactService CreateClient()
51        {
52            var binding = new NetTcpBinding();
53            var endpoint = new EndpointAddress(serverString);
54            var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
55
56            IContactService client = null;
57            client = myChannelFactory.CreateChannel();
58            return client;
59        }
60
61        private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers)
62        {
63            //communicate with 10% of the network
64            int noOfPeers = allPeers.Count / 10;
65            List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
66            List<PeerInfo> res = new List<PeerInfo>();
67            foreach (int index in indexList)
68            {
69                res.Add(allPeers.ElementAt(index));
70            }
71            return res;
72        }
73
74        private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue)
75        {
76            List<int> res = new List<int>();
77            Random rnd = new Random();
78            int tmp = -1;
79            while (res.Count < noOfItems)
80            {
81                tmp = rnd.Next(minValue, maxValue);
82                if (!res.Contains(tmp))
83                {
84                    res.Add(tmp);
85                }
86            }
87            return res;
88        }
89
90        private void SendHeartbeatToServer(object sender, ElapsedEventArgs e)
91        {
92            try
93            {
94                heartbeatClient.UpdateHeartbeat(myself);
95            }
96            catch { } //nothing to do, exception is raised when getting peer list
97        }
98
99
100
101        public void Dispose()
102        {
103            timer.Stop();
104            timer.Dispose();
105            timer = null;
106        }
107    }
108}
Note: See TracBrowser for help on using the repository browser.