Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Grid/ClientForm.cs @ 142

Last change on this file since 142 was 115, checked in by gkronber, 17 years ago

improved the comment of the OS.version check

File size: 6.0 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, IClient {
41    private ChannelFactory<IEngineStore> factory;
42    private ServiceHost clientHost;
43    private System.Timers.Timer fetchOperationTimer;
44    private IEngineStore engineStore;
45    private Guid currentGuid;
46    private ProcessingEngine currentEngine;
47    private string clientUrl;
48    private object locker = new object();
49
50    public ClientForm() {
51      InitializeComponent();
52      fetchOperationTimer = new System.Timers.Timer();
53      fetchOperationTimer.Interval = 200;
54      fetchOperationTimer.Elapsed += new System.Timers.ElapsedEventHandler(fetchOperationTimer_Elapsed);
55      statusTextBox.Text = "Stopped";
56      currentGuid = Guid.Empty;
57    }
58
59    private void startButton_Click(object sender, EventArgs e) {
60      string hostname = Dns.GetHostName();
61      IPAddress[] addresses = Dns.GetHostAddresses(hostname);
62
63      // windows XP returns the external ip on index 0 while windows vista returns the external ip on index 2
64      if (System.Environment.OSVersion.Version.Major >= 6) {
65        clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[2] + ":" + clientPort.Text +"/Grid/Client";
66      } else {
67        clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[0] + ":" + clientPort.Text +"/Grid/Client";
68      }
69
70      clientHost = new ServiceHost(this, new Uri(clientUrl));
71      try {
72        NetTcpBinding binding = new NetTcpBinding();
73        binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
74        binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
75        binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
76        binding.Security.Mode = SecurityMode.None;
77
78        clientHost.AddServiceEndpoint(typeof(IClient), binding, clientUrl);
79        clientHost.Open();
80
81        factory = new ChannelFactory<IEngineStore>(binding);
82        engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
83
84        fetchOperationTimer.Start();
85        startButton.Enabled = false;
86        stopButton.Enabled = true;
87        statusTextBox.Text = "Waiting for engine";
88
89      } catch (CommunicationException ex) {
90        MessageBox.Show("Exception while connecting to the server: " + ex.Message);
91        clientHost.Abort();
92        startButton.Enabled = true;
93        stopButton.Enabled = false;
94        fetchOperationTimer.Stop();
95      }
96    }
97
98    private void stopButton_Click(object sender, EventArgs e) {
99      fetchOperationTimer.Stop();
100      factory.Abort();
101      clientHost.Close();
102      statusTextBox.Text = "Stopped";
103      stopButton.Enabled = false;
104      startButton.Enabled = true;
105    }
106
107    private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
108      lock(locker) {
109        byte[] engineXml;
110        fetchOperationTimer.Stop();
111        if(engineStore.TryTakeEngine(clientUrl, out currentGuid, out engineXml)) {
112          currentEngine = RestoreEngine(engineXml);
113          if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
114          currentEngine.Finished += delegate(object src, EventArgs args) {
115            byte[] resultXml = SaveEngine(currentEngine);
116            engineStore.StoreResult(currentGuid, resultXml);
117            currentGuid = Guid.Empty;
118            currentEngine = null;
119            fetchOperationTimer.Interval = 100;
120            fetchOperationTimer.Start();
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;
126          fetchOperationTimer.Start();
127        }
128      }
129    }
130    public void Abort(Guid guid) {
131      lock(locker) {
132        if(!IsRunningEngine(guid)) return;
133        currentEngine.Abort();
134      }
135    }
136    public bool IsRunningEngine(Guid guid) {
137      return currentGuid == guid;
138    }
139    private ProcessingEngine RestoreEngine(byte[] engine) {
140      GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
141      return (ProcessingEngine)PersistenceManager.Load(stream);
142    }
143    private byte[] SaveEngine(IEngine engine) {
144      MemoryStream memStream = new MemoryStream();
145      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
146      PersistenceManager.Save(engine, stream);
147      stream.Close();
148      return memStream.ToArray();
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.