[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.ComponentModel;
|
---|
| 25 | using System.Data;
|
---|
| 26 | using System.Drawing;
|
---|
| 27 | using System.Linq;
|
---|
| 28 | using System.Text;
|
---|
| 29 | using System.Windows.Forms;
|
---|
| 30 | using System.ServiceModel;
|
---|
| 31 | using HeuristicLab.Core;
|
---|
| 32 | using System.Xml;
|
---|
| 33 | using System.Threading;
|
---|
| 34 | using System.IO;
|
---|
| 35 | using System.IO.Compression;
|
---|
[32] | 36 | using System.Net;
|
---|
[2] | 37 |
|
---|
| 38 | namespace HeuristicLab.Grid {
|
---|
[32] | 39 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
|
---|
| 40 | public partial class ClientForm : Form, IClient {
|
---|
[2] | 41 | private ChannelFactory<IEngineStore> factory;
|
---|
[32] | 42 | private ServiceHost clientHost;
|
---|
[2] | 43 | private System.Timers.Timer fetchOperationTimer;
|
---|
| 44 | private IEngineStore engineStore;
|
---|
[32] | 45 | private Guid currentGuid;
|
---|
| 46 | private ProcessingEngine currentEngine;
|
---|
| 47 | private string clientUrl;
|
---|
[35] | 48 | private object locker = new object();
|
---|
[2] | 49 |
|
---|
| 50 | public ClientForm() {
|
---|
| 51 | InitializeComponent();
|
---|
| 52 | fetchOperationTimer = new System.Timers.Timer();
|
---|
| 53 | fetchOperationTimer.Interval = 200;
|
---|
| 54 | fetchOperationTimer.Elapsed += new System.Timers.ElapsedEventHandler(fetchOperationTimer_Elapsed);
|
---|
| 55 | statusTextBox.Text = "Stopped";
|
---|
[32] | 56 | currentGuid = Guid.Empty;
|
---|
[2] | 57 | }
|
---|
| 58 |
|
---|
| 59 | private void startButton_Click(object sender, EventArgs e) {
|
---|
[114] | 60 | string hostname = Dns.GetHostName();
|
---|
| 61 | IPAddress[] addresses = Dns.GetHostAddresses(hostname);
|
---|
| 62 |
|
---|
[115] | 63 | // windows XP returns the external ip on index 0 while windows vista returns the external ip on index 2
|
---|
[114] | 64 | if (System.Environment.OSVersion.Version.Major >= 6) {
|
---|
| 65 | clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[2] + ":" + clientPort.Text +"/Grid/Client";
|
---|
| 66 | } else {
|
---|
| 67 | clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[0] + ":" + clientPort.Text +"/Grid/Client";
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[32] | 70 | clientHost = new ServiceHost(this, new Uri(clientUrl));
|
---|
[2] | 71 | try {
|
---|
| 72 | NetTcpBinding binding = new NetTcpBinding();
|
---|
| 73 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
| 74 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
| 75 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
[32] | 76 | binding.Security.Mode = SecurityMode.None;
|
---|
| 77 |
|
---|
| 78 | clientHost.AddServiceEndpoint(typeof(IClient), binding, clientUrl);
|
---|
| 79 | clientHost.Open();
|
---|
| 80 |
|
---|
[2] | 81 | factory = new ChannelFactory<IEngineStore>(binding);
|
---|
| 82 | engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
|
---|
| 83 |
|
---|
| 84 | fetchOperationTimer.Start();
|
---|
| 85 | startButton.Enabled = false;
|
---|
| 86 | stopButton.Enabled = true;
|
---|
| 87 | statusTextBox.Text = "Waiting for engine";
|
---|
| 88 |
|
---|
[32] | 89 | } catch (CommunicationException ex) {
|
---|
[2] | 90 | MessageBox.Show("Exception while connecting to the server: " + ex.Message);
|
---|
[32] | 91 | clientHost.Abort();
|
---|
[2] | 92 | startButton.Enabled = true;
|
---|
| 93 | stopButton.Enabled = false;
|
---|
| 94 | fetchOperationTimer.Stop();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
| 99 | fetchOperationTimer.Stop();
|
---|
| 100 | factory.Abort();
|
---|
[32] | 101 | clientHost.Close();
|
---|
[2] | 102 | statusTextBox.Text = "Stopped";
|
---|
| 103 | stopButton.Enabled = false;
|
---|
| 104 | startButton.Enabled = true;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
[35] | 108 | lock(locker) {
|
---|
| 109 | byte[] engineXml;
|
---|
| 110 | fetchOperationTimer.Stop();
|
---|
| 111 | if(engineStore.TryTakeEngine(clientUrl, out currentGuid, out engineXml)) {
|
---|
| 112 | currentEngine = RestoreEngine(engineXml);
|
---|
| 113 | if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
|
---|
| 114 | currentEngine.Finished += delegate(object src, EventArgs args) {
|
---|
| 115 | byte[] resultXml = SaveEngine(currentEngine);
|
---|
| 116 | engineStore.StoreResult(currentGuid, resultXml);
|
---|
| 117 | currentGuid = Guid.Empty;
|
---|
| 118 | currentEngine = null;
|
---|
| 119 | fetchOperationTimer.Interval = 100;
|
---|
| 120 | fetchOperationTimer.Start();
|
---|
| 121 | };
|
---|
| 122 | currentEngine.Execute();
|
---|
| 123 | } else {
|
---|
| 124 | if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
|
---|
| 125 | fetchOperationTimer.Interval = 5000;
|
---|
[2] | 126 | fetchOperationTimer.Start();
|
---|
[35] | 127 | }
|
---|
[2] | 128 | }
|
---|
| 129 | }
|
---|
[32] | 130 | public void Abort(Guid guid) {
|
---|
[35] | 131 | lock(locker) {
|
---|
| 132 | if(!IsRunningEngine(guid)) return;
|
---|
| 133 | currentEngine.Abort();
|
---|
| 134 | }
|
---|
[32] | 135 | }
|
---|
| 136 | public bool IsRunningEngine(Guid guid) {
|
---|
| 137 | return currentGuid == guid;
|
---|
| 138 | }
|
---|
[2] | 139 | private ProcessingEngine RestoreEngine(byte[] engine) {
|
---|
| 140 | GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
|
---|
| 141 | return (ProcessingEngine)PersistenceManager.Load(stream);
|
---|
| 142 | }
|
---|
[34] | 143 | private byte[] SaveEngine(IEngine engine) {
|
---|
[2] | 144 | MemoryStream memStream = new MemoryStream();
|
---|
| 145 | GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
|
---|
[34] | 146 | PersistenceManager.Save(engine, stream);
|
---|
[2] | 147 | stream.Close();
|
---|
| 148 | return memStream.ToArray();
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|