1 | using HeuristicLab.Clients.Common;
|
---|
2 | using HeuristicLab.Clients.Common.Properties;
|
---|
3 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
4 | using Microsoft.AspNet.Mvc;
|
---|
5 | using System;
|
---|
6 | using System.Collections.Generic;
|
---|
7 | using System.Linq;
|
---|
8 | using System.ServiceModel;
|
---|
9 | using System.ServiceModel.Security;
|
---|
10 | using System.Threading.Tasks;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Clients.Hive.WebJobManager.Services
|
---|
13 | {
|
---|
14 | /// <summary>
|
---|
15 | /// Rewritten for Endpoint problems
|
---|
16 | /// </summary>
|
---|
17 | public class HiveServiceLocatorWeb : IHiveServiceLocator
|
---|
18 | {
|
---|
19 | private static IHiveServiceLocator instance = null;
|
---|
20 | private HiveServiceClient clientinst;
|
---|
21 | public static IHiveServiceLocator Instance
|
---|
22 | {
|
---|
23 | get
|
---|
24 | {
|
---|
25 | if (instance == null)
|
---|
26 | {
|
---|
27 | instance = new HiveServiceLocatorWeb();
|
---|
28 | }
|
---|
29 | return instance;
|
---|
30 | }
|
---|
31 | }
|
---|
32 | private string username;
|
---|
33 | public string Username
|
---|
34 | {
|
---|
35 | get { return username; }
|
---|
36 | set { username = value; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | private string password;
|
---|
40 | public string Password
|
---|
41 | {
|
---|
42 | get { return password; }
|
---|
43 | set { password = value; }
|
---|
44 | }
|
---|
45 | public int EndpointRetries { get; private set; }
|
---|
46 |
|
---|
47 | public string WorkingEndpoint { get; private set; }
|
---|
48 |
|
---|
49 | public static void clear()
|
---|
50 | {
|
---|
51 | instance = null;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #region #unknownCalls
|
---|
55 | public void CallHiveService(Action<IHiveService> call)
|
---|
56 | {
|
---|
57 | HiveServiceClient client = NewServiceClient();
|
---|
58 |
|
---|
59 | try
|
---|
60 | {
|
---|
61 | call(client);
|
---|
62 | }
|
---|
63 | finally
|
---|
64 | {
|
---|
65 | try
|
---|
66 | {
|
---|
67 | client.Close();
|
---|
68 | }
|
---|
69 | catch (Exception)
|
---|
70 | {
|
---|
71 | client.Abort();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public T CallHiveService<T>(Func<IHiveService, T> call)
|
---|
77 | {
|
---|
78 | HiveServiceClient client = NewServiceClient();
|
---|
79 |
|
---|
80 | try
|
---|
81 | {
|
---|
82 | return call(client);
|
---|
83 | }
|
---|
84 | finally
|
---|
85 | {
|
---|
86 | try
|
---|
87 | {
|
---|
88 | client.Close();
|
---|
89 | }
|
---|
90 | catch (Exception)
|
---|
91 | {
|
---|
92 | client.Abort();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | #endregion unknown
|
---|
97 |
|
---|
98 | /// <summary>
|
---|
99 | /// Gets current loaded HiveServiceClient
|
---|
100 | /// </summary>
|
---|
101 | /// <returns></returns>
|
---|
102 | public HiveServiceClient getHiveServiceClient()
|
---|
103 | {
|
---|
104 | if(clientinst == null || clientinst.State != CommunicationState.Opened)
|
---|
105 | {
|
---|
106 | clientinst = NewServiceClient();
|
---|
107 | }
|
---|
108 | return clientinst;
|
---|
109 | }
|
---|
110 | /// <summary>
|
---|
111 | /// Checks if login still works
|
---|
112 | /// </summary>
|
---|
113 | /// <returns></returns>
|
---|
114 | public bool CheckLogin()
|
---|
115 | {
|
---|
116 | if(clientinst != null)
|
---|
117 | {
|
---|
118 | try
|
---|
119 | {
|
---|
120 | clientinst.GetJobs();
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 | catch(Exception e)
|
---|
124 | {
|
---|
125 | if (e is MessageSecurityException || e is InvalidOperationException)
|
---|
126 | {
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 | throw;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 | public static void SetLoginErrorMessage()
|
---|
135 | {
|
---|
136 | LoginViewModelService.Instance.GetLoginViewModel().errorMessage = "Login timed out";
|
---|
137 | }
|
---|
138 |
|
---|
139 | public string GetEndpointInformation()
|
---|
140 | {
|
---|
141 | throw new NotImplementedException();
|
---|
142 | }
|
---|
143 | /// <summary>
|
---|
144 | /// Builds hard coded binding and gets a new HiveServiceClient
|
---|
145 | /// </summary>
|
---|
146 | /// <returns></returns>
|
---|
147 | private HiveServiceClient NewServiceClient()
|
---|
148 | {
|
---|
149 | //build config by code
|
---|
150 | WSHttpBinding binding = new WSHttpBinding();
|
---|
151 | // I think most (or all) of these are defaults--I just copied them from app.config:
|
---|
152 | binding.Name = "wsHttpBinding_Hive";
|
---|
153 | binding.CloseTimeout = TimeSpan.FromMinutes(1);
|
---|
154 | binding.OpenTimeout = TimeSpan.FromMinutes(1);
|
---|
155 | binding.ReceiveTimeout = TimeSpan.FromMinutes(35);
|
---|
156 | binding.SendTimeout = TimeSpan.FromMinutes(35.0);
|
---|
157 | binding.BypassProxyOnLocal = false;
|
---|
158 |
|
---|
159 | binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
|
---|
160 | binding.MaxBufferPoolSize = 2147483647;
|
---|
161 | binding.MaxReceivedMessageSize = 2147483647;
|
---|
162 | binding.MessageEncoding = WSMessageEncoding.Text;
|
---|
163 |
|
---|
164 | binding.TextEncoding = System.Text.Encoding.UTF8;
|
---|
165 | binding.UseDefaultWebProxy = true;
|
---|
166 | // binding.TransferMode = TransferMode.Buffered;
|
---|
167 | binding.AllowCookies = false;
|
---|
168 |
|
---|
169 | binding.ReaderQuotas.MaxDepth = 32;
|
---|
170 | binding.ReaderQuotas.MaxStringContentLength = 200000000;
|
---|
171 | binding.ReaderQuotas.MaxArrayLength = 200000000;
|
---|
172 | binding.ReaderQuotas.MaxBytesPerRead = 4096;
|
---|
173 | binding.ReaderQuotas.MaxNameTableCharCount = 16384;
|
---|
174 |
|
---|
175 | binding.ReliableSession.Ordered = true;
|
---|
176 | binding.ReliableSession.InactivityTimeout = TimeSpan.Parse("00:10:00");
|
---|
177 | binding.ReliableSession.Enabled = false;
|
---|
178 |
|
---|
179 |
|
---|
180 | binding.Security.Mode = SecurityMode.Message;
|
---|
181 | binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
|
---|
182 | binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 | EndpointAddress end = new EndpointAddress("http://services.heuristiclab.com/Hive-3.3/HiveService.svc");
|
---|
187 |
|
---|
188 | HiveServiceClient client = new HiveServiceClient(binding, end);
|
---|
189 | client.ClientCredentials.UserName.UserName = this.username;
|
---|
190 | client.ClientCredentials.UserName.Password = this.password;
|
---|
191 | client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
|
---|
192 |
|
---|
193 | return client;
|
---|
194 |
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void destroy()
|
---|
198 | {
|
---|
199 | clientinst = null;
|
---|
200 | instance = null;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|