Free cookie consent management tool by TermsFeed Policy Generator

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

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

fixed #96 and #97

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      //clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[0] + ":8002/Grid/Client";
61      string hostname = Dns.GetHostName();
62      IPAddress[] addresses = Dns.GetHostAddresses(hostname);
63
64      // Thanks to Microsoft
65      if (System.Environment.OSVersion.Version.Major >= 6) {
66        clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[2] + ":" + clientPort.Text +"/Grid/Client";
67      } else {
68        clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[0] + ":" + clientPort.Text +"/Grid/Client";
69      }
70
71      clientHost = new ServiceHost(this, new Uri(clientUrl));
72      try {
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
79        clientHost.AddServiceEndpoint(typeof(IClient), binding, clientUrl);
80        clientHost.Open();
81
82        factory = new ChannelFactory<IEngineStore>(binding);
83        engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
84
85        fetchOperationTimer.Start();
86        startButton.Enabled = false;
87        stopButton.Enabled = true;
88        statusTextBox.Text = "Waiting for engine";
89
90      } catch (CommunicationException ex) {
91        MessageBox.Show("Exception while connecting to the server: " + ex.Message);
92        clientHost.Abort();
93        startButton.Enabled = true;
94        stopButton.Enabled = false;
95        fetchOperationTimer.Stop();
96      }
97    }
98
99    private void stopButton_Click(object sender, EventArgs e) {
100      fetchOperationTimer.Stop();
101      factory.Abort();
102      clientHost.Close();
103      statusTextBox.Text = "Stopped";
104      stopButton.Enabled = false;
105      startButton.Enabled = true;
106    }
107
108    private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
109      lock(locker) {
110        byte[] engineXml;
111        fetchOperationTimer.Stop();
112        if(engineStore.TryTakeEngine(clientUrl, out currentGuid, out engineXml)) {
113          currentEngine = RestoreEngine(engineXml);
114          if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
115          currentEngine.Finished += delegate(object src, EventArgs args) {
116            byte[] resultXml = SaveEngine(currentEngine);
117            engineStore.StoreResult(currentGuid, resultXml);
118            currentGuid = Guid.Empty;
119            currentEngine = null;
120            fetchOperationTimer.Interval = 100;
121            fetchOperationTimer.Start();
122          };
123          currentEngine.Execute();
124        } else {
125          if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
126          fetchOperationTimer.Interval = 5000;
127          fetchOperationTimer.Start();
128        }
129      }
130    }
131    public void Abort(Guid guid) {
132      lock(locker) {
133        if(!IsRunningEngine(guid)) return;
134        currentEngine.Abort();
135      }
136    }
137    public bool IsRunningEngine(Guid guid) {
138      return currentGuid == guid;
139    }
140    private ProcessingEngine RestoreEngine(byte[] engine) {
141      GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
142      return (ProcessingEngine)PersistenceManager.Load(stream);
143    }
144    private byte[] SaveEngine(IEngine engine) {
145      MemoryStream memStream = new MemoryStream();
146      GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
147      PersistenceManager.Save(engine, stream);
148      stream.Close();
149      return memStream.ToArray();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.