Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/HiveServiceLocatorWebManagerService.cs @ 13656

Last change on this file since 13656 was 13656, checked in by ascheibe, 8 years ago

#2582 created branch for Hive Web Job Manager

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