Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 246 was 246, checked in by gkronber, 16 years ago

minor bugfixes (related to #149)

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