Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Grid/ClientForm.cs @ 21

Last change on this file since 21 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Data;
26using System.Drawing;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using System.ServiceModel;
31using HeuristicLab.Core;
32using System.Xml;
33using System.Threading;
34using System.IO;
35using System.IO.Compression;
36
37namespace HeuristicLab.Grid {
38  public partial class ClientForm : Form {
39
40    private ChannelFactory<IEngineStore> factory;
41    private System.Timers.Timer fetchOperationTimer;
42    private IEngineStore engineStore;
43
44    public ClientForm() {
45      InitializeComponent();
46      fetchOperationTimer = new System.Timers.Timer();
47      fetchOperationTimer.Interval = 200;
48      fetchOperationTimer.Elapsed += new System.Timers.ElapsedEventHandler(fetchOperationTimer_Elapsed);
49      statusTextBox.Text = "Stopped";
50    }
51
52    private void startButton_Click(object sender, EventArgs e) {
53      try {
54        NetTcpBinding binding = new NetTcpBinding();
55        binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
56        binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
57        binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
58        factory = new ChannelFactory<IEngineStore>(binding);
59        engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
60
61        fetchOperationTimer.Start();
62        startButton.Enabled = false;
63        stopButton.Enabled = true;
64        statusTextBox.Text = "Waiting for engine";
65
66      } catch (Exception ex) {
67        MessageBox.Show("Exception while connecting to the server: " + ex.Message);
68        startButton.Enabled = true;
69        stopButton.Enabled = false;
70        fetchOperationTimer.Stop();
71      }
72    }
73
74    private void stopButton_Click(object sender, EventArgs e) {
75      fetchOperationTimer.Stop();
76      factory.Abort();
77      statusTextBox.Text = "Stopped";
78      stopButton.Enabled = false;
79      startButton.Enabled = true;
80    }
81
82    private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
83      byte[] engineXml;
84      Guid guid;
85      fetchOperationTimer.Stop();
86      if (engineStore.TryTakeEngine(out guid, out engineXml)) {
87        ProcessingEngine engine = RestoreEngine(engineXml);
88        if (InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
89        engine.Finished += delegate(object src, EventArgs args) {
90          byte[] resultScopeXml = SaveScope(engine.InitialOperation.Scope);
91          engineStore.StoreResult(guid, resultScopeXml);
92          fetchOperationTimer.Interval = 100;
93          fetchOperationTimer.Start();
94        };
95        engine.Execute();
96      } else {
97        if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
98        fetchOperationTimer.Interval = 5000;
99        fetchOperationTimer.Start();
100      }
101    }
102    private ProcessingEngine RestoreEngine(byte[] engine) {
103      GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
104      return (ProcessingEngine)PersistenceManager.Load(stream);
105    }
106    private byte[] SaveScope(IScope scope) {
107      MemoryStream memStream = new MemoryStream();
108      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
109      PersistenceManager.Save(scope, stream);
110      stream.Close();
111      return memStream.ToArray();
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.