Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Grid/3.2/GridServerProxy.cs @ 3578

Last change on this file since 3578 was 2079, checked in by gkronber, 15 years ago

Fixed a bug in the GridServerProxy (introduced with r2085) #642.

File size: 3.1 KB
RevLine 
[2058]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.Text;
25using System.ServiceModel;
26using HeuristicLab.Tracing;
27using System.Threading;
28using HeuristicLab.Core;
29
30namespace HeuristicLab.Grid {
31  public class GridServerProxy : IGridServer {
32    private const int MAX_CONNECTION_RETRIES = 10;
33    private const int RETRY_TIMEOUT_SEC = 60;
34    private string address;
35    private object connectionLock = new object();
36    private ChannelFactory<IGridServer> factory;
37    private IGridServer server;
38
39    public GridServerProxy(string address) {
40      this.address = address;
41    }
42
43    public JobState JobState(Guid guid) {
44      return SavelyExecute(() => server.JobState(guid));
45    }
46
47    public Guid BeginExecuteEngine(byte[] engine) {
48      return SavelyExecute(() => server.BeginExecuteEngine(engine));
49    }
50
51    public byte[] TryEndExecuteEngine(Guid guid) {
52      return SavelyExecute(() => server.TryEndExecuteEngine(guid));
53    }
54
55    private void ResetConnection() {
56      Logger.Info("Reset connection in JobManager");
57      lock (connectionLock) {
58        // open a new channel
59        NetTcpBinding binding = new NetTcpBinding();
60        binding.MaxReceivedMessageSize = 100000000; // 100Mbytes
61        binding.ReaderQuotas.MaxStringContentLength = 100000000; // also 100M chars
62        binding.ReaderQuotas.MaxArrayLength = 100000000; // also 100M elements;
63
64        factory = new ChannelFactory<IGridServer>(binding);
65        server = factory.CreateChannel(new EndpointAddress(address));
66      }
67    }
68
69    private TResult SavelyExecute<TResult>(Func<TResult> a) {
70      int retries = 0;
[2079]71      if (server == null) ResetConnection();
[2058]72      do {
73        try {
74          lock (connectionLock) {
75            return a();
76          }
77        }
78        catch (TimeoutException) {
79          retries++;
80          Thread.Sleep(TimeSpan.FromSeconds(RETRY_TIMEOUT_SEC));
81        }
82        catch (CommunicationException) {
83          ResetConnection();
84          retries++;
85          Thread.Sleep(TimeSpan.FromSeconds(RETRY_TIMEOUT_SEC));
86        }
87      } while (retries < MAX_CONNECTION_RETRIES);
88      Logger.Warn("Reached max connection retries");
89      return default(TResult);
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.