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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Data;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using System.ServiceModel;
|
---|
31 | using HeuristicLab.Core;
|
---|
32 | using System.Xml;
|
---|
33 | using System.Threading;
|
---|
34 | using System.IO;
|
---|
35 | using System.IO.Compression;
|
---|
36 | using System.Net;
|
---|
37 |
|
---|
38 | namespace 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 |
|
---|
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 | currentGuid = Guid.Empty;
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void startButton_Click(object sender, EventArgs e) {
|
---|
59 | clientUrl = "net.tcp://" + Dns.GetHostAddresses(Dns.GetHostName())[0] + ":8002/Grid/Client";
|
---|
60 | clientHost = new ServiceHost(this, new Uri(clientUrl));
|
---|
61 | try {
|
---|
62 | NetTcpBinding binding = new NetTcpBinding();
|
---|
63 | binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
|
---|
64 | binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
|
---|
65 | binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
|
---|
66 | binding.Security.Mode = SecurityMode.None;
|
---|
67 |
|
---|
68 | clientHost.AddServiceEndpoint(typeof(IClient), binding, clientUrl);
|
---|
69 | clientHost.Open();
|
---|
70 |
|
---|
71 | factory = new ChannelFactory<IEngineStore>(binding);
|
---|
72 | engineStore = factory.CreateChannel(new EndpointAddress(addressTextBox.Text));
|
---|
73 |
|
---|
74 | fetchOperationTimer.Start();
|
---|
75 | startButton.Enabled = false;
|
---|
76 | stopButton.Enabled = true;
|
---|
77 | statusTextBox.Text = "Waiting for engine";
|
---|
78 |
|
---|
79 | } catch (CommunicationException ex) {
|
---|
80 | MessageBox.Show("Exception while connecting to the server: " + ex.Message);
|
---|
81 | clientHost.Abort();
|
---|
82 | startButton.Enabled = true;
|
---|
83 | stopButton.Enabled = false;
|
---|
84 | fetchOperationTimer.Stop();
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void stopButton_Click(object sender, EventArgs e) {
|
---|
89 | fetchOperationTimer.Stop();
|
---|
90 | factory.Abort();
|
---|
91 | clientHost.Close();
|
---|
92 | statusTextBox.Text = "Stopped";
|
---|
93 | stopButton.Enabled = false;
|
---|
94 | startButton.Enabled = true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | private void fetchOperationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
98 | byte[] engineXml;
|
---|
99 | fetchOperationTimer.Stop();
|
---|
100 | if (engineStore.TryTakeEngine(clientUrl, out currentGuid, out engineXml)) {
|
---|
101 | currentEngine = RestoreEngine(engineXml);
|
---|
102 | if (InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Executing engine"; }); } else statusTextBox.Text = "Executing engine";
|
---|
103 | currentEngine.Finished += delegate(object src, EventArgs args) {
|
---|
104 | byte[] resultScopeXml = SaveScope(currentEngine.InitialOperation.Scope);
|
---|
105 | engineStore.StoreResult(currentGuid, resultScopeXml);
|
---|
106 | currentGuid = Guid.Empty;
|
---|
107 | currentEngine = null;
|
---|
108 | fetchOperationTimer.Interval = 100;
|
---|
109 | fetchOperationTimer.Start();
|
---|
110 | };
|
---|
111 | currentEngine.Execute();
|
---|
112 | } else {
|
---|
113 | if(InvokeRequired) { Invoke((MethodInvoker)delegate() { statusTextBox.Text = "Waiting for engine"; }); } else statusTextBox.Text = "Waiting for engine";
|
---|
114 | fetchOperationTimer.Interval = 5000;
|
---|
115 | fetchOperationTimer.Start();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | public void Abort(Guid guid) {
|
---|
119 | if(!IsRunningEngine(guid)) return;
|
---|
120 | currentEngine.Abort();
|
---|
121 | }
|
---|
122 | public bool IsRunningEngine(Guid guid) {
|
---|
123 | return currentGuid == guid;
|
---|
124 | }
|
---|
125 | private ProcessingEngine RestoreEngine(byte[] engine) {
|
---|
126 | GZipStream stream = new GZipStream(new MemoryStream(engine), CompressionMode.Decompress);
|
---|
127 | return (ProcessingEngine)PersistenceManager.Load(stream);
|
---|
128 | }
|
---|
129 | private byte[] SaveScope(IScope scope) {
|
---|
130 | MemoryStream memStream = new MemoryStream();
|
---|
131 | GZipStream stream = new GZipStream(memStream, CompressionMode.Compress, true);
|
---|
132 | PersistenceManager.Save(scope, stream);
|
---|
133 | stream.Close();
|
---|
134 | return memStream.ToArray();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|