-
Notifications
You must be signed in to change notification settings - Fork 41
/
Cat.cs
193 lines (153 loc) · 5.67 KB
/
Cat.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Com.Dianping.Cat.Configuration;
using Com.Dianping.Cat.Message.Spi;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Linq;
using Com.Dianping.Cat.Message.Spi.Internals;
using Com.Dianping.Cat.Util;
namespace Com.Dianping.Cat
{
public class Cat
{
private static readonly Cat Instance = new Cat();
private bool _mInitialized;
private IMessageManager _mManager;
private IMessageProducer _mProducer;
private Cat()
{
}
public static IMessageManager GetManager()
{
return Instance._mManager;
}
public static IMessageProducer GetProducer()
{
return Instance._mProducer;
}
public static void Initialize(string configFile)
{
if (Instance._mInitialized)
{
Logger.Warn("Cat can't initialize again with config file(%s), IGNORED!", configFile);
return;
}
Logger.Info("Initializing Cat .Net Client ...");
DefaultMessageManager manager = new DefaultMessageManager();
ClientConfig clientConfig = LoadClientConfig(configFile);
manager.InitializeClient(clientConfig);
Instance._mProducer = new DefaultMessageProducer(manager);
Instance._mManager = manager;
Instance._mInitialized = true;
Logger.Info("Cat .Net Client initialized.");
}
public static bool IsInitialized()
{
return Instance._mInitialized;
}
private static ClientConfig LoadClientConfig(string configFile)
{
ClientConfig config = new ClientConfig();
if (File.Exists(configFile))
{
Logger.Info("Use config file({0}).", configFile);
XmlDocument doc = new XmlDocument();
doc.Load(configFile);
XmlElement root = doc.DocumentElement;
if (root != null)
{
config.Domain = BuildDomain(root.GetElementsByTagName("domain"));
IEnumerable<Server> servers = BuildServers(root.GetElementsByTagName("servers"));
//NOTE: 只添加Enabled的
foreach (Server server in servers.Where(server => server.Enabled))
{
config.Servers.Add(server);
Logger.Info("CAT server configured: {0}:{1}", server.Ip, server.Port);
}
}
}
else
{
Logger.Warn("Config file({0}) not found, using localhost:2280 instead.", configFile);
config.Domain = BuildDomain(null);
config.Servers.Add(new Server("localhost", 2280));
}
return config;
}
private static Domain BuildDomain(XmlNodeList nodes)
{
if (nodes == null || nodes.Count == 0)
{
return new Domain();
}
XmlElement node = (XmlElement) nodes[0];
return new Domain
{
Id = GetStringProperty(node, "id", "Unknown"),
//Ip = GetStringProperty(node, "ip", null),
Enabled = GetBooleanProperty(node, "enabled", true)
};
}
private static IEnumerable<Server> BuildServers(XmlNodeList nodes)
{
List<Server> servers = new List<Server>();
if (nodes != null && nodes.Count > 0)
{
XmlElement first = (XmlElement) nodes[0];
XmlNodeList serverNodes = first.GetElementsByTagName("server");
foreach (XmlNode node in serverNodes)
{
XmlElement serverNode = (XmlElement) node;
string ip = GetStringProperty(serverNode, "ip", "localhost");
int port = GetIntProperty(serverNode, "port", 2280);
Server server = new Server(ip, port) {Enabled = GetBooleanProperty(serverNode, "enabled", true)};
servers.Add(server);
}
}
if (servers.Count == 0)
{
Logger.Warn("No server configured, use localhost:2280 instead.");
servers.Add(new Server("localhost", 2280));
}
return servers;
}
private static string GetStringProperty(XmlElement element, string name, string defaultValue)
{
if (element != null)
{
string value = element.GetAttribute(name);
if (value.Length > 0)
{
return value;
}
}
return defaultValue;
}
private static bool GetBooleanProperty(XmlElement element, string name, bool defaultValue)
{
if (element != null)
{
string value = element.GetAttribute(name);
if (value.Length > 0)
{
return "true".Equals(value);
}
}
return defaultValue;
}
private static int GetIntProperty(XmlElement element, string name, int defaultValue)
{
if (element != null)
{
string value = element.GetAttribute(name);
if (value.Length > 0)
{
int tmpRet;
if (int.TryParse(value, out tmpRet))
return tmpRet;
}
}
return defaultValue;
}
}
}