Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Core/3.3/StoreProxy.cs @ 2211

Last change on this file since 2211 was 2053, checked in by gkronber, 15 years ago

Removed lazy loading of problem data and made adds of datasets and models atomic. #656 (CEDMA server should handle only one data set (problem) at a time)

File size: 2.8 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using System.Xml;
28using System.ServiceModel;
29using System.ServiceModel.Description;
30using HeuristicLab.CEDMA.DB.Interfaces;
31using System.ServiceModel.Channels;
32
33namespace HeuristicLab.CEDMA.Core {
34  public class StoreProxy : IStore {
35    private string serverUri;
36    private IStore store;
37    private NetTcpBinding binding;
38
39    public StoreProxy(string serverUri) {
40      this.serverUri = serverUri;
41      Reset();
42      Reconnect();
43    }
44
45    private void Reset() {
46      binding = new NetTcpBinding();
47      binding.ReceiveTimeout = new TimeSpan(1, 0, 0);
48      binding.MaxReceivedMessageSize = int.MaxValue;
49      binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
50      binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
51    }
52
53    private void Reconnect() {
54      ChannelFactory<IStore> factory = new ChannelFactory<IStore>(binding);
55      store = factory.CreateChannel(new EndpointAddress(serverUri));
56    }
57
58    private T ExecuteSavely<T>(Func<T> a ) {
59      try {
60        return a();
61      }
62      catch (CommunicationException) {
63        Reconnect();
64        return a();
65      }
66      catch (TimeoutException) {
67        Reconnect();
68        return a();
69      }
70    }
71
72    #region IStore Members
73
74    public void Add(Statement statement) {
75      ExecuteSavely(() => { store.Add(statement); return 1.0; });
76    }
77
78    public void AddRange(ICollection<Statement> statements) {
79      ExecuteSavely(() => { store.AddRange(statements); return 1.0; });
80    }
81
82    public ICollection<VariableBindings> Query(ICollection<Statement> query, int page, int pageSize) {
83      return ExecuteSavely(() => store.Query(query, page, pageSize));
84    }
85
86    public ICollection<VariableBindings> Query(string query, int page, int pageSize) {
87      return ExecuteSavely(() => store.Query(query, page, pageSize));
88    }
89
90    #endregion
91  }
92}
Note: See TracBrowser for help on using the repository browser.