[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;
|
---|
[32] | 35 | using System.Net;
|
---|
[402] | 36 | using System.Diagnostics;
|
---|
[2] | 37 |
|
---|
| 38 | namespace HeuristicLab.Grid {
|
---|
[32] | 39 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
|
---|
[244] | 40 | public partial class ClientForm : Form {
|
---|
[2] | 41 | private ChannelFactory<IEngineStore> factory;
|
---|
| 42 | private System.Timers.Timer fetchOperationTimer;
|
---|
| 43 | private IEngineStore engineStore;
|
---|
[32] | 44 | private Guid currentGuid;
|
---|
| 45 | private ProcessingEngine currentEngine;
|
---|
[244] | 46 | private object connectionLock = new object();
|
---|
[246] | 47 | private bool stopped;
|
---|
[251] | 48 | private const int CONNECTION_RETRY_TIMEOUT_SEC = 10;
|
---|
| 49 | private const int MAX_RETRIES = 10;
|
---|
[2] | 50 |
|
---|
| 51 | public ClientForm() {
|
---|
| 52 | InitializeComponent();
|
---|
| 53 | fetchOperationTimer = new System.Timers.Timer();
|
---|
| 54 | fetchOperationTimer.Interval = 200;
|
---|
| 55 | fetchOperationTimer.Elapsed += new System.Timers.ElapsedEventHandler(fetchOperationTimer_Elapsed);
|
---|
| 56 | statusTextBox.Text = "Stopped";
|
---|
[246] | 57 | stopped = true;
|
---|
[32] | 58 | currentGuid = Guid.Empty;
|
---|
[2] | 59 | }
|
---|
| 60 |
|
---|
| 61 | private void startButton_Click(object sender, EventArgs e) {
|
---|
| 62 | try {
|
---|
[402] | 63 | Trace.Listeners.Clear();
|
---|
| 64 | Trace.Listeners.Add(new EventLogTraceListener("HeuristicLab.Grid"));
|
---|
| 65 |
|
---|
[228] | 66 | ResetConnection();
|
---|
[2] | 67 | fetchOperationTimer.Start();
|
---|
| 68 | startButton.Enabled = false;
|
---|
| 69 | stopButton.Enabled = true;
|
---|
| 70 | statusTextBox.Text = "Waiting for engine";
|
---|
[246] | 71 | stopped = false;
|
---|
[2] | 72 |
|
---|
[219] | 73 | } catch(CommunicationException ex) {
|
---|
[2] | 74 | MessageBox.Show("Exception while connecting to the server: " + ex.Message);
|
---|
| 75 | startButton.Enabled = true;
|
---|
| 76 | stopButton.Enabled = false;
|
---|
| 77 | fetchOperationTimer.Stop();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[228] | 81 | private void ResetConnection() {
|
---|
[402] | 82 | Trace.TraceInformation("Reset connection in GridClient");
|
---|
[228] | 83 | NetTcpBinding binding = new NetTcpBinding();
|
---|
| 84 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
| 85 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
| 86 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
| 87 | binding.Security.Mode = SecurityMode.None;
|
---|
| 88 | factory = new ChannelFactory<IEngineStore>(binding);
|
---|
| 89 | engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[2] | 92 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
[246] | 93 | stopped = true;
|
---|
[2] | 94 | fetchOperationTimer.Stop();
|
---|
[244] | 95 | if(currentEngine != null)
|
---|
| 96 | currentEngine.Abort();
|
---|
| 97 | lock(connectionLock) {
|
---|
[246] | 98 | if(factory.State == CommunicationState.Opened || factory.State == CommunicationState.Opening) {
|
---|
| 99 | IAsyncResult closeResult = factory.BeginClose(null, null);
|
---|
| 100 | factory.EndClose(closeResult);
|
---|
| 101 | }
|
---|
[244] | 102 | }
|
---|
[2] | 103 | statusTextBox.Text = "Stopped";
|
---|
| 104 | stopButton.Enabled = false;
|
---|
| 105 | startButton.Enabled = true;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
[251] | 109 | byte[] engineXml = null;
|
---|
| 110 | // first stop the timer!
|
---|
[219] | 111 | fetchOperationTimer.Stop();
|
---|
[251] | 112 | bool gotEngine = false;
|
---|
| 113 | lock(connectionLock) {
|
---|
| 114 | if(stopped) return;
|
---|
| 115 | try {
|
---|
| 116 | gotEngine = engineStore.TryTakeEngine(out currentGuid, out engineXml);
|
---|
[383] | 117 | } catch(TimeoutException) {
|
---|
[402] | 118 | Trace.TraceWarning("TimeoutException while trying to get an engine");
|
---|
[251] | 119 | currentEngine = null;
|
---|
| 120 | currentGuid = Guid.Empty;
|
---|
| 121 | // timeout -> just start the timer again
|
---|
| 122 | fetchOperationTimer.Interval = 5000;
|
---|
| 123 | fetchOperationTimer.Start();
|
---|
[383] | 124 | } catch(CommunicationException) {
|
---|
[402] | 125 | Trace.TraceWarning("CommunicationException while trying to get an engine");
|
---|
[251] | 126 | // connection problem -> reset connection and start the timer again
|
---|
| 127 | ResetConnection();
|
---|
| 128 | currentEngine = null;
|
---|
| 129 | currentGuid = Guid.Empty;
|
---|
| 130 | fetchOperationTimer.Interval = 5000;
|
---|
| 131 | fetchOperationTimer.Start();
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | // got engine from server and user didn't press stop -> execute the engine
|
---|
| 135 | if(gotEngine && !stopped) {
|
---|
| 136 | currentEngine = RestoreEngine(engineXml);
|
---|
| 137 | if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
|
---|
| 138 | currentEngine.Finished += new EventHandler(currentEngine_Finished); // register event-handler that sends result back to server and restarts timer
|
---|
| 139 | currentEngine.Execute();
|
---|
| 140 | } else {
|
---|
| 141 | // ok we didn't get engine -> if the user didn't press stop this means that the server doesn't have engines for us
|
---|
| 142 | // if the user pressed stop we must not start the timer
|
---|
| 143 | if(!stopped) {
|
---|
| 144 | if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
|
---|
| 145 | // start the timer again
|
---|
| 146 | fetchOperationTimer.Interval = 5000;
|
---|
| 147 | fetchOperationTimer.Start();
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | void currentEngine_Finished(object sender, EventArgs e) {
|
---|
| 153 | IEngine engine = (IEngine)sender;
|
---|
| 154 | byte[] resultXml = SaveEngine(engine);
|
---|
| 155 | bool success = false;
|
---|
| 156 | int retries = 0;
|
---|
| 157 | do {
|
---|
[244] | 158 | lock(connectionLock) {
|
---|
[251] | 159 | if(!stopped) {
|
---|
| 160 | try {
|
---|
| 161 | engineStore.StoreResult(currentGuid, resultXml);
|
---|
| 162 | success = true;
|
---|
[383] | 163 | } catch(TimeoutException) {
|
---|
[402] | 164 | Trace.TraceWarning("TimeoutException while trying to store the result of an engine");
|
---|
[251] | 165 | success = false;
|
---|
| 166 | retries++;
|
---|
| 167 | Thread.Sleep(TimeSpan.FromSeconds(CONNECTION_RETRY_TIMEOUT_SEC));
|
---|
[383] | 168 | } catch(CommunicationException) {
|
---|
[402] | 169 | Trace.TraceWarning("CommunicationException while trying to store the result of an engine");
|
---|
[251] | 170 | ResetConnection();
|
---|
| 171 | success = false;
|
---|
| 172 | retries++;
|
---|
| 173 | Thread.Sleep(TimeSpan.FromSeconds(CONNECTION_RETRY_TIMEOUT_SEC));
|
---|
[244] | 174 | }
|
---|
[246] | 175 | }
|
---|
[35] | 176 | }
|
---|
[252] | 177 | } while(!stopped && !success && retries < MAX_RETRIES);
|
---|
[251] | 178 | // ok if we could store the result it's probable that the server can send us another engine use a small time-interval
|
---|
| 179 | if(success)
|
---|
| 180 | fetchOperationTimer.Interval = 100;
|
---|
| 181 | else fetchOperationTimer.Interval = CONNECTION_RETRY_TIMEOUT_SEC; // if there were problems -> sleep for a longer time
|
---|
| 182 | // clear state
|
---|
| 183 | currentEngine = null;
|
---|
| 184 | currentGuid = Guid.Empty;
|
---|
| 185 | // start the timer
|
---|
| 186 | fetchOperationTimer.Start();
|
---|
[2] | 187 | }
|
---|
[251] | 188 |
|
---|
[2] | 189 | private ProcessingEngine RestoreEngine(byte[] engine) {
|
---|
[402] | 190 | return (ProcessingEngine)PersistenceManager.RestoreFromGZip(engine);
|
---|
[2] | 191 | }
|
---|
[34] | 192 | private byte[] SaveEngine(IEngine engine) {
|
---|
[402] | 193 | return PersistenceManager.SaveToGZip(engine);
|
---|
[2] | 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|