Free cookie consent management tool by TermsFeed Policy Generator

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

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

changed code to remove compiler warnings

File size: 7.6 KB
RevLine 
[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
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;
[32]36using System.Net;
[2]37
38namespace 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 {
[228]63        ResetConnection();
[2]64        fetchOperationTimer.Start();
65        startButton.Enabled = false;
66        stopButton.Enabled = true;
67        statusTextBox.Text = "Waiting for engine";
[246]68        stopped = false;
[2]69
[219]70      } catch(CommunicationException ex) {
[2]71        MessageBox.Show("Exception while connecting to the server: " + ex.Message);
72        startButton.Enabled = true;
73        stopButton.Enabled = false;
74        fetchOperationTimer.Stop();
75      }
76    }
77
[228]78    private void ResetConnection() {
79      NetTcpBinding binding = new NetTcpBinding();
80      binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
81      binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
82      binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
83      binding.Security.Mode = SecurityMode.None;
84      factory = new ChannelFactory<IEngineStore>(binding);
85      engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
86    }
87
[2]88    private void stopButton_Click(object sender, EventArgs e) {
[246]89      stopped = true;
[2]90      fetchOperationTimer.Stop();
[244]91      if(currentEngine != null)
92        currentEngine.Abort();
93      lock(connectionLock) {
[246]94        if(factory.State == CommunicationState.Opened || factory.State == CommunicationState.Opening) {
95          IAsyncResult closeResult = factory.BeginClose(null, null);
96          factory.EndClose(closeResult);
97        }
[244]98      }
[2]99      statusTextBox.Text = "Stopped";
100      stopButton.Enabled = false;
101      startButton.Enabled = true;
102    }
103
104    private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
[251]105      byte[] engineXml = null;
106      // first stop the timer!
[219]107      fetchOperationTimer.Stop();
[251]108      bool gotEngine = false;
109      lock(connectionLock) {
110        if(stopped) return;
111        try {
112          gotEngine = engineStore.TryTakeEngine(out currentGuid, out engineXml);
[383]113        } catch(TimeoutException) {
[251]114          currentEngine = null;
115          currentGuid = Guid.Empty;
116          // timeout -> just start the timer again
117          fetchOperationTimer.Interval = 5000;
118          fetchOperationTimer.Start();
[383]119        } catch(CommunicationException) {
[251]120          // connection problem -> reset connection and start the timer again
121          ResetConnection();
122          currentEngine = null;
123          currentGuid = Guid.Empty;
124          fetchOperationTimer.Interval = 5000;
125          fetchOperationTimer.Start();
126        }
127      }
128      // got engine from server and user didn't press stop -> execute the engine
129      if(gotEngine && !stopped) {
130        currentEngine = RestoreEngine(engineXml);
131        if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
132        currentEngine.Finished += new EventHandler(currentEngine_Finished); // register event-handler that sends result back to server and restarts timer
133        currentEngine.Execute();
134      } else {
135        // ok we didn't get engine -> if the user didn't press stop this means that the server doesn't have engines for us
136        // if the user pressed stop we must not start the timer
137        if(!stopped) {
138          if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
139          // start the timer again
140          fetchOperationTimer.Interval = 5000;
141          fetchOperationTimer.Start();
142        }
143      }
144    }
145
146    void currentEngine_Finished(object sender, EventArgs e) {
147      IEngine engine = (IEngine)sender;
148      byte[] resultXml = SaveEngine(engine);
149      bool success = false;
150      int retries = 0;
151      do {
[244]152        lock(connectionLock) {
[251]153          if(!stopped) {
154            try {
155              engineStore.StoreResult(currentGuid, resultXml);
156              success = true;
[383]157            } catch(TimeoutException) {
[251]158              success = false;
159              retries++;
160              Thread.Sleep(TimeSpan.FromSeconds(CONNECTION_RETRY_TIMEOUT_SEC));
[383]161            } catch(CommunicationException) {
[251]162              ResetConnection();
163              success = false;
164              retries++;
165              Thread.Sleep(TimeSpan.FromSeconds(CONNECTION_RETRY_TIMEOUT_SEC));
[244]166            }
[246]167          }
[35]168        }
[252]169      } while(!stopped && !success && retries < MAX_RETRIES);
[251]170      // ok if we could store the result it's probable that the server can send us another engine use a small time-interval
171      if(success)
172        fetchOperationTimer.Interval = 100;
173      else fetchOperationTimer.Interval = CONNECTION_RETRY_TIMEOUT_SEC; // if there were problems -> sleep for a longer time
174      // clear state
175      currentEngine = null;
176      currentGuid = Guid.Empty;
177      // start the timer
178      fetchOperationTimer.Start();
[2]179    }
[251]180
[2]181    private ProcessingEngine RestoreEngine(byte[] engine) {
182      GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
183      return (ProcessingEngine)PersistenceManager.Load(stream);
184    }
[34]185    private byte[] SaveEngine(IEngine engine) {
[2]186      MemoryStream memStream = new MemoryStream();
187      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
[34]188      PersistenceManager.Save(engine, stream);
[2]189      stream.Close();
190      return memStream.ToArray();
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.