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