|
@@ -15,46 +15,43 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace InABox.Clients
|
|
|
{
|
|
|
+ public static class StaticRestClients
|
|
|
+ {
|
|
|
+ public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
|
|
|
+
|
|
|
+ public static RestClient GetClient(string url)
|
|
|
+ {
|
|
|
+ var uri = new Uri(url);
|
|
|
+ if (!Clients.TryGetValue(url, out var cli))
|
|
|
+ {
|
|
|
+ cli = new RestClient(uri);
|
|
|
+ Clients.Add(url, cli);
|
|
|
+ }
|
|
|
+ return cli;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public class RestClient<TEntity> : BaseClient<TEntity> where TEntity : Entity, new()
|
|
|
{
|
|
|
private bool _simpleencryption;
|
|
|
private string _server;
|
|
|
private bool _compression;
|
|
|
private BinarySerializationSettings _binarysettings;
|
|
|
- private static RestClient cli = null;
|
|
|
|
|
|
- public RestClient(string server, bool simpleencryption, bool compression, BinarySerializationSettings binarySerializationSettings)
|
|
|
+ public RestClient(string server, bool useSimpleEncryption, bool compression, BinarySerializationSettings binarySerializationSettings)
|
|
|
{
|
|
|
_server = server;
|
|
|
- _simpleencryption = simpleencryption;
|
|
|
+ _simpleencryption = useSimpleEncryption;
|
|
|
_compression = compression;
|
|
|
_binarysettings = binarySerializationSettings;
|
|
|
|
|
|
RestClientCache.Check(server);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- public static class StaticRestClients
|
|
|
+ public RestClient(string server, bool simpleencryption, bool compression) : this(server, simpleencryption, compression, BinarySerializationSettings.Latest)
|
|
|
{
|
|
|
- public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
|
|
|
-
|
|
|
- public static void SetClient(string url)
|
|
|
- {
|
|
|
- var uri = new Uri(url);
|
|
|
- if (Clients.ContainsKey(url))
|
|
|
- cli = Clients[url];
|
|
|
- else
|
|
|
- {
|
|
|
- cli = new RestClient(uri);
|
|
|
- Clients.Add(url, cli);
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- public RestClient(string server, bool simpleencryption, bool compression) : this(server, simpleencryption, false, BinarySerializationSettings.Latest)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
public RestClient(string server, bool simpleencryption) : this(server, simpleencryption, false)
|
|
|
{
|
|
|
}
|
|
@@ -63,17 +60,17 @@ namespace InABox.Clients
|
|
|
{
|
|
|
}
|
|
|
|
|
|
- public static string Ping(String[] urls, out DatabaseInfo info)
|
|
|
+ public static string Ping(string[] urls, out DatabaseInfo info)
|
|
|
{
|
|
|
- String result = "";
|
|
|
+ var result = "";
|
|
|
info = new DatabaseInfo();
|
|
|
- List<Task<Tuple<String,DatabaseInfo>>> pings = urls.Select(x => Task.Run(
|
|
|
- () => new Tuple<String,DatabaseInfo>(x,new RestClient<User>(x).Info())
|
|
|
+ List<Task<Tuple<string,DatabaseInfo>>> pings = urls.Select(x => Task.Run(
|
|
|
+ () => new Tuple<string, DatabaseInfo>(x, new RestClient<User>(x).Info())
|
|
|
)).ToList();
|
|
|
while (pings.Count > 0)
|
|
|
{
|
|
|
var ping = Task.WhenAny(pings).Result;
|
|
|
- if (ping.Status == TaskStatus.RanToCompletion && !String.IsNullOrWhiteSpace(ping.Result.Item2.Version))
|
|
|
+ if (ping.Status == TaskStatus.RanToCompletion && !string.IsNullOrWhiteSpace(ping.Result.Item2.Version))
|
|
|
{
|
|
|
result = ping.Result.Item1;
|
|
|
info = ping.Result.Item2;
|
|
@@ -197,8 +194,7 @@ namespace InABox.Clients
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- if (cli == null)
|
|
|
- StaticRestClients.SetClient(url);
|
|
|
+ var cli = StaticRestClients.GetClient(url);
|
|
|
|
|
|
var cmd = string.Format(
|
|
|
"{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}",
|
|
@@ -320,13 +316,8 @@ namespace InABox.Clients
|
|
|
result.Messages.Add("- " + err.InnerException.Message);
|
|
|
}
|
|
|
|
|
|
- req = null;
|
|
|
- //double elapsed = (DateTime.Now - now).TotalMilliseconds;
|
|
|
- //Log(" * {0}{1}() completed in {2:F0}ms", Action, typeof(TEntity).Name, elapsed);
|
|
|
return result;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+ }
|
|
|
|
|
|
#region Query Data
|
|
|
|
|
@@ -567,8 +558,8 @@ namespace InABox.Clients
|
|
|
|
|
|
protected override bool DoPing()
|
|
|
{
|
|
|
- var cli = new RestClient(new Uri(RestClientCache.URL(_server)));
|
|
|
- var req = new RestRequest("/info", Method.Get) { Timeout = 20000 };
|
|
|
+ var cli = StaticRestClients.GetClient(RestClientCache.URL(_server));
|
|
|
+ var req = new RestRequest("/ping", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|
|
|
{
|
|
@@ -589,10 +580,7 @@ namespace InABox.Clients
|
|
|
{
|
|
|
var result = new List<string>();
|
|
|
|
|
|
- var url = RestClientCache.URL(_server);
|
|
|
-
|
|
|
- var uri = new Uri(url);
|
|
|
- var cli = new RestClient(uri);
|
|
|
+ var cli = StaticRestClients.GetClient(RestClientCache.URL(_server));
|
|
|
var req = new RestRequest("/classes", Method.Get) { Timeout = 20000 };
|
|
|
|
|
|
try
|