[13553] | 1 | using System;
|
---|
[13923] | 2 | using System.Collections.Concurrent;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[13553] | 4 | using System.ServiceModel;
|
---|
[13923] | 5 | using System.Threading.Tasks;
|
---|
| 6 | using System.Timers;
|
---|
[13553] | 7 | using DistributedGA.Core.Domain;
|
---|
[13524] | 8 | using DistributedGA.Core.Interface;
|
---|
| 9 |
|
---|
[13918] | 10 | namespace DistributedGA.Core.Implementation {
|
---|
| 11 | public class WcfMessageSender : IMessageSender {
|
---|
[13923] | 12 |
|
---|
[13918] | 13 | private PeerInfo myself;
|
---|
[13524] | 14 |
|
---|
[13923] | 15 | private ConcurrentQueue<KeyValuePair<PeerInfo, byte[][]>> bufferedMessages;
|
---|
| 16 |
|
---|
| 17 | private Timer timer; //sends cached messages to network in background
|
---|
| 18 |
|
---|
[13918] | 19 | public void Init(PeerInfo source) {
|
---|
| 20 | myself = source;
|
---|
[13923] | 21 | bufferedMessages = new ConcurrentQueue<KeyValuePair<PeerInfo, byte[][]>>();
|
---|
| 22 | timer = new Timer(1000 * 60); //each 5 minutes
|
---|
| 23 | timer.Elapsed += GenerateSendingTasks;
|
---|
| 24 | timer.Start();
|
---|
[13918] | 25 | }
|
---|
[13524] | 26 |
|
---|
[13918] | 27 | public void SendData(PeerInfo destination, byte[][] data) {
|
---|
[13923] | 28 | bufferedMessages.Enqueue(new KeyValuePair<PeerInfo, byte[][]>(destination, data));
|
---|
| 29 | }
|
---|
[13524] | 30 |
|
---|
[13923] | 31 | public void Dispose() {
|
---|
| 32 | timer.Stop();
|
---|
| 33 | timer.Dispose();
|
---|
| 34 | timer = null;
|
---|
| 35 | }
|
---|
[13524] | 36 |
|
---|
[13923] | 37 | private void GenerateSendingTasks(object sender, ElapsedEventArgs e) {
|
---|
| 38 | while (!bufferedMessages.IsEmpty) {
|
---|
| 39 | KeyValuePair<PeerInfo, byte[][]> message;
|
---|
| 40 | if (bufferedMessages.TryDequeue(out message)) {
|
---|
| 41 | Task.Factory.StartNew(() => SendDataFromQueue(message.Key, message.Value), TaskCreationOptions.LongRunning);
|
---|
[13887] | 42 | }
|
---|
[13918] | 43 | }
|
---|
| 44 | }
|
---|
[13887] | 45 |
|
---|
[13923] | 46 | /// <summary>
|
---|
| 47 | /// sends data to another peer, is used by a single thread to run in background
|
---|
| 48 | /// </summary>
|
---|
| 49 | /// <param name="destination">destination peer</param>
|
---|
| 50 | /// <param name="data">data to send</param>
|
---|
| 51 | private void SendDataFromQueue(PeerInfo destination, byte[][] data) {
|
---|
| 52 | try {
|
---|
| 53 | Console.WriteLine(string.Format("Sending data to {0}:{1} in the background...", destination.IpAddress, destination.Port));
|
---|
| 54 | var serviceUrl = "DistributedGA.svc";
|
---|
| 55 | var baseUri = new Uri(string.Concat("net.tcp://", destination.IpAddress, ":", destination.Port, "/DistributedGA"));
|
---|
| 56 | var serviceUri = new Uri(baseUri, serviceUrl);
|
---|
[13918] | 57 |
|
---|
[13923] | 58 | var binding = new NetTcpBinding();
|
---|
| 59 | var endpoint = new EndpointAddress(serviceUri);
|
---|
| 60 | using (var myChannelFactory = new ChannelFactory<IMessageContract>(binding, endpoint)) {
|
---|
| 61 | using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
|
---|
| 62 | ((IMessageContract)client).SendData(myself, data); //maybe timout exception...
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
|
---|
[13553] | 67 | }
|
---|
[13918] | 68 | }
|
---|
[13524] | 69 | }
|
---|