[1417] | 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.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using System.Xml;
|
---|
| 28 | using System.ServiceModel;
|
---|
| 29 | using System.ServiceModel.Description;
|
---|
| 30 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
| 31 | using System.ServiceModel.Channels;
|
---|
| 32 |
|
---|
| 33 | namespace 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 void ExecuteSavely(Action a) {
|
---|
| 59 | ExecuteSavely(a);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | private T ExecuteSavely<T>(Func<T> a ) {
|
---|
| 63 | try {
|
---|
| 64 | return a();
|
---|
| 65 | }
|
---|
| 66 | catch (CommunicationException) {
|
---|
| 67 | Reconnect();
|
---|
| 68 | return a();
|
---|
| 69 | }
|
---|
| 70 | catch (TimeoutException) {
|
---|
| 71 | Reconnect();
|
---|
| 72 | return a();
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | #region IStore Members
|
---|
| 77 |
|
---|
| 78 | public void Add(Statement statement) {
|
---|
| 79 | ExecuteSavely(() => store.Add(statement));
|
---|
| 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 | }
|
---|