84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
using NLog;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using DataClient.Struct;
|
|
using System.Net;
|
|
|
|
namespace DataClient
|
|
{
|
|
public class STPClient
|
|
{
|
|
Logger log = LogManager.GetCurrentClassLogger();
|
|
string confDir = Path.Combine(Directory.GetCurrentDirectory(), "Config", "config.json");
|
|
private List<server> servers = new List<server>();
|
|
int serverDefault = -1;
|
|
|
|
public STPClient()
|
|
{
|
|
log.Trace("Create instance class.");
|
|
GetConfig();
|
|
}
|
|
public STPClient(string confDir)
|
|
{
|
|
log.Trace("Create instance class.");
|
|
this.confDir = confDir;
|
|
}
|
|
public bool AddServer(string name, string ip, int port, string dir)
|
|
{
|
|
|
|
|
|
return true;
|
|
}
|
|
private void GetConfig()
|
|
{
|
|
log.Trace("Get config from: " + confDir);
|
|
string jsonString;
|
|
try
|
|
{
|
|
log.Trace("Read config file.");
|
|
jsonString = File.ReadAllText(confDir);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
log.Warn(e, "Can't read config file.");
|
|
return;
|
|
}
|
|
var conf = (JObject)JsonConvert.DeserializeObject(jsonString);
|
|
if (!conf.HasValues && !conf["servers"].HasValues && conf["servers"].Type != JTokenType.Array)
|
|
{
|
|
log.Warn("Doesn't exist \"servers\" object in config file.");
|
|
return;
|
|
}
|
|
var count = -1;
|
|
foreach (var s in conf["servers"])
|
|
{
|
|
count++;
|
|
var name = (s["name"].Type == JTokenType.String && ((string)s["name"]).Length > 0) ? (string)s["name"] : "default";
|
|
var ip = (s["ip"].Type == JTokenType.String && IPAddress.TryParse((string)s["ip"], out _)) ? (string)s["ip"] : "";
|
|
var port = (s["port"].Type == JTokenType.Integer) ? (int)s["port"] : -1;
|
|
var dir = (s["dir"].Type == JTokenType.String && Directory.Exists((string)s["dir"])) ? (string)s["dir"] : "";
|
|
if (ip == "")
|
|
{
|
|
log.Warn("servers[" + count.ToString() + "].ip - incorrect. Skip server.");
|
|
continue;
|
|
}
|
|
if (port < 0 && port > 65535)
|
|
{
|
|
log.Warn("servers[" + count.ToString() + "].port - incorrect. Skip server.");
|
|
continue;
|
|
}
|
|
if (dir == "")
|
|
{
|
|
log.Warn("servers[" + count.ToString() + "].dir - incorrect or not exist. Skip server.");
|
|
continue;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
} |