1 | using DistributedGA.Core.Domain;
|
---|
2 | using DistributedGA.Core.Interface;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using System.ServiceModel;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 |
|
---|
10 | namespace DistributedGA.Core.Implementation
|
---|
11 | {
|
---|
12 | public class WcfPeerListManager : IPeerListManager
|
---|
13 | {
|
---|
14 | //private const string SERVER_STRING = "http://localhost:9002/DistributedGA.ContactServer/ContactService";
|
---|
15 | private const string SERVER_STRING = "http://localhost:9090/DistributedGA.ContactServer/ContactService";
|
---|
16 |
|
---|
17 | private IContactService client = null;
|
---|
18 |
|
---|
19 | private PeerInfo myself = null;
|
---|
20 |
|
---|
21 | List<PeerInfo> allPeers = null;
|
---|
22 | List<PeerInfo> peersForMessaging = null;
|
---|
23 |
|
---|
24 | public void Init(PeerInfo source)
|
---|
25 | {
|
---|
26 | client = CreateClient();
|
---|
27 | myself = source;
|
---|
28 | client.RegisterPeer(source);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public List<PeerInfo> GetPeerList()
|
---|
32 | {
|
---|
33 | if(allPeers == null)
|
---|
34 | {
|
---|
35 | allPeers = client.GetPeerList(myself);
|
---|
36 | peersForMessaging = ChoosePeersForMessaging(allPeers);
|
---|
37 | }
|
---|
38 | //TODO: maybe always request a new list from the contact server so that he can refresh the heartbeat...
|
---|
39 | return peersForMessaging;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void SendLogToServer(string msg)
|
---|
43 | {
|
---|
44 | client.MakeLog(myself, msg);
|
---|
45 | }
|
---|
46 |
|
---|
47 | private IContactService CreateClient()
|
---|
48 | {
|
---|
49 | var binding = new BasicHttpBinding();
|
---|
50 | var endpoint = new EndpointAddress(SERVER_STRING);
|
---|
51 | var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
|
---|
52 |
|
---|
53 | IContactService client = null;
|
---|
54 | client = myChannelFactory.CreateChannel();
|
---|
55 | return client;
|
---|
56 | }
|
---|
57 |
|
---|
58 | private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers)
|
---|
59 | {
|
---|
60 | //TODO: strategy: 10% of list random choosen
|
---|
61 | int noOfPeers = allPeers.Count / 10; //communicate with 10% of the network
|
---|
62 | List<PeerInfo> res = new List<PeerInfo>();
|
---|
63 |
|
---|
64 | return allPeers;
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|
68 | }
|
---|