[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)]
|
---|
[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();
|
---|
[2] | 47 |
|
---|
| 48 | public ClientForm() {
|
---|
| 49 | InitializeComponent();
|
---|
| 50 | fetchOperationTimer = new System.Timers.Timer();
|
---|
| 51 | fetchOperationTimer.Interval = 200;
|
---|
| 52 | fetchOperationTimer.Elapsed += new System.Timers.ElapsedEventHandler(fetchOperationTimer_Elapsed);
|
---|
| 53 | statusTextBox.Text = "Stopped";
|
---|
[32] | 54 | currentGuid = Guid.Empty;
|
---|
[2] | 55 | }
|
---|
| 56 |
|
---|
| 57 | private void startButton_Click(object sender, EventArgs e) {
|
---|
| 58 | try {
|
---|
[228] | 59 | ResetConnection();
|
---|
[2] | 60 | fetchOperationTimer.Start();
|
---|
| 61 | startButton.Enabled = false;
|
---|
| 62 | stopButton.Enabled = true;
|
---|
| 63 | statusTextBox.Text = "Waiting for engine";
|
---|
| 64 |
|
---|
[219] | 65 | } catch(CommunicationException ex) {
|
---|
[2] | 66 | MessageBox.Show("Exception while connecting to the server: " + ex.Message);
|
---|
| 67 | startButton.Enabled = true;
|
---|
| 68 | stopButton.Enabled = false;
|
---|
| 69 | fetchOperationTimer.Stop();
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[228] | 73 | private void ResetConnection() {
|
---|
| 74 | NetTcpBinding binding = new NetTcpBinding();
|
---|
| 75 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
| 76 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
| 77 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
| 78 | binding.Security.Mode = SecurityMode.None;
|
---|
| 79 | factory = new ChannelFactory<IEngineStore>(binding);
|
---|
| 80 | engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[2] | 83 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
| 84 | fetchOperationTimer.Stop();
|
---|
[244] | 85 | if(currentEngine != null)
|
---|
| 86 | currentEngine.Abort();
|
---|
| 87 | lock(connectionLock) {
|
---|
| 88 | IAsyncResult closeResult = factory.BeginClose(null, null);
|
---|
| 89 | factory.EndClose(closeResult);
|
---|
| 90 | }
|
---|
[2] | 91 | statusTextBox.Text = "Stopped";
|
---|
| 92 | stopButton.Enabled = false;
|
---|
| 93 | startButton.Enabled = true;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
[219] | 97 | byte[] engineXml;
|
---|
| 98 | fetchOperationTimer.Stop();
|
---|
| 99 | try {
|
---|
[244] | 100 | bool success;
|
---|
| 101 | lock(connectionLock) {
|
---|
| 102 | if(factory.State != CommunicationState.Opened) {
|
---|
| 103 | ResetConnection();
|
---|
| 104 | }
|
---|
| 105 | success = engineStore.TryTakeEngine(out currentGuid, out engineXml);
|
---|
[228] | 106 | }
|
---|
[244] | 107 | if(success) {
|
---|
[35] | 108 | currentEngine = RestoreEngine(engineXml);
|
---|
| 109 | if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
|
---|
| 110 | currentEngine.Finished += delegate(object src, EventArgs args) {
|
---|
[244] | 111 | if(factory.State == CommunicationState.Opened && !currentEngine.Canceled) {
|
---|
| 112 | byte[] resultXml = SaveEngine(currentEngine);
|
---|
| 113 | lock(connectionLock) {
|
---|
| 114 | engineStore.StoreResult(currentGuid, resultXml);
|
---|
| 115 | }
|
---|
| 116 | currentGuid = Guid.Empty;
|
---|
| 117 | currentEngine = null;
|
---|
| 118 | fetchOperationTimer.Interval = 100;
|
---|
| 119 | fetchOperationTimer.Start();
|
---|
| 120 | }
|
---|
[35] | 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 | }
|
---|
[244] | 128 | } catch(TimeoutException timeoutException) {
|
---|
[219] | 129 | currentEngine = null;
|
---|
| 130 | currentGuid = Guid.Empty;
|
---|
| 131 | fetchOperationTimer.Interval = 5000;
|
---|
| 132 | fetchOperationTimer.Start();
|
---|
[244] | 133 | } catch(CommunicationException communicationException) {
|
---|
| 134 | currentEngine = null;
|
---|
| 135 | currentGuid = Guid.Empty;
|
---|
| 136 | fetchOperationTimer.Interval = 5000;
|
---|
| 137 | fetchOperationTimer.Start();
|
---|
[2] | 138 | }
|
---|
| 139 | }
|
---|
| 140 | private ProcessingEngine RestoreEngine(byte[] engine) {
|
---|
| 141 | GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
|
---|
| 142 | return (ProcessingEngine)PersistenceManager.Load(stream);
|
---|
| 143 | }
|
---|
[34] | 144 | private byte[] SaveEngine(IEngine engine) {
|
---|
[2] | 145 | MemoryStream memStream = new MemoryStream();
|
---|
| 146 | GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
|
---|
[34] | 147 | PersistenceManager.Save(engine, stream);
|
---|
[2] | 148 | stream.Close();
|
---|
| 149 | return memStream.ToArray();
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|