Skip to content

Commit 72e9345

Browse files
author
gremlin
committed
initial
0 parents  commit 72e9345

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3776
-0
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
dirs = helper web event hub cluster
4+
5+
all:
6+
for x in $(dirs); do make -C $$x || exit 1; done
7+
8+
clean:
9+
for x in $(dirs); do (cd $$x; make clean); done

README.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
A fully-deployed webpush system include following processes:
3+
4+
N wp_webd
5+
M wp_hubd
6+
1 wp_eventd
7+
1 wp_clusterd
8+
9+
10+
While a minimum webpush system include just:
11+
12+
1 wp_webd
13+
1 wp_eventd
14+
15+

cluster/ClusterServant.cpp

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#include "ClusterServant.h"
2+
#include "xic/sthread.h"
3+
4+
#define DELIMIT_CHARS "+:/.#"
5+
6+
ClusterServant::MethodTab::PairType ClusterServant::_methodpairs[] = {
7+
#define CMD(X) { #X, XIC_METHOD_CAST(ClusterServant, X) },
8+
CLUSTERSERVANT_CMDS
9+
#undef CMD
10+
};
11+
12+
ClusterServant::MethodTab ClusterServant::_methodtab(_methodpairs, XS_ARRCOUNT(_methodpairs));
13+
14+
15+
ClusterServant::ClusterServant()
16+
: ServantI(&_methodtab)
17+
{
18+
_revision = ((int64_t)st_time()) * 1000;
19+
sthread_create(this, &ClusterServant::reap_fiber, 0);
20+
}
21+
22+
ClusterServant::~ClusterServant()
23+
{
24+
}
25+
26+
void ClusterServant::reap_fiber()
27+
{
28+
while (true)
29+
{
30+
st_sleep(5);
31+
time_t now = st_time();
32+
ProxyMap::iterator iter = _proxyMap.begin();
33+
while (iter != _proxyMap.end())
34+
{
35+
if (iter->second < now - 60)
36+
{
37+
_proxyMap.erase(iter++);
38+
++_revision;
39+
}
40+
else
41+
++iter;
42+
}
43+
}
44+
}
45+
46+
XIC_METHOD(ClusterServant, remove)
47+
{
48+
xic::VDict args = quest->args();
49+
xstr_t proxy = args.wantXstr("proxy");
50+
std::string the_proxy = make_string(proxy);
51+
52+
xic::AnswerWriter aw;
53+
ProxyMap::iterator iter = _proxyMap.find(the_proxy);
54+
if (iter != _proxyMap.end())
55+
{
56+
_proxyMap.erase(iter);
57+
++_revision;
58+
aw.param("ok", true);
59+
}
60+
else
61+
{
62+
aw.param("ok", false);
63+
}
64+
65+
return aw.take();
66+
}
67+
68+
static void get_service_prefix(xstr_t* tmp, xstr_t* prefix)
69+
{
70+
xstr_delimit_in_cstr(tmp, DELIMIT_CHARS, prefix);
71+
if (prefix->len == 0)
72+
{
73+
xstr_delimit_char(tmp, '@', prefix);
74+
prefix->len++;
75+
prefix->data--;
76+
}
77+
else
78+
{
79+
xstr_delimit_char(tmp, '@', NULL);
80+
prefix->len++;
81+
}
82+
}
83+
84+
XIC_METHOD(ClusterServant, renew)
85+
{
86+
xic::VDict args = quest->args();
87+
xstr_t proxy = args.wantXstr("proxy");
88+
89+
time_t now = st_time();
90+
std::string the_proxy = make_string(proxy);
91+
92+
ProxyMap::iterator iter = _proxyMap.find(the_proxy);
93+
if (iter != _proxyMap.end())
94+
{
95+
iter->second = now;
96+
}
97+
else
98+
{
99+
xstr_t tmp = proxy, tmpservname;
100+
xstr_t endpoint, servname;
101+
xstr_t netlocs[8];
102+
size_t num = 0;
103+
104+
get_service_prefix(&tmp, &tmpservname);
105+
106+
while (num < 8 && xstr_delimit_char(&tmp, '@', &endpoint))
107+
{
108+
xstr_token_cstr(&endpoint, " \t\r\n\v\f", &netlocs[num]);
109+
if (netlocs[num].len)
110+
++num;
111+
}
112+
113+
if (num > 0)
114+
{
115+
for (iter = _proxyMap.begin(); iter != _proxyMap.end(); ++iter)
116+
{
117+
//服务名一样
118+
tmp = make_xstr(iter->first);
119+
get_service_prefix(&tmp, &servname);
120+
if (!xstr_equal(&servname, &tmpservname))
121+
{
122+
continue;
123+
}
124+
125+
//服务的地址一样
126+
while (xstr_delimit_char(&tmp, '@', &endpoint))
127+
{
128+
xstr_t loc;
129+
xstr_token_cstr(&endpoint, " \t\r\n\v\f", &loc);
130+
for (size_t i = 0; i < num; ++i)
131+
{
132+
if (xstr_equal(&loc, &netlocs[i]))
133+
{
134+
_proxyMap.erase(iter);
135+
goto done;
136+
}
137+
}
138+
}
139+
}
140+
done:
141+
/* Do nothing */
142+
;
143+
}
144+
145+
_proxyMap.insert(std::make_pair(the_proxy, now));
146+
++_revision;
147+
}
148+
149+
xic::AnswerWriter aw;
150+
aw.param("revision", _revision);
151+
return aw.take();
152+
}
153+
154+
XIC_METHOD(ClusterServant, revision)
155+
{
156+
return xic::AnswerWriter()("revision", _revision);
157+
}
158+
159+
XIC_METHOD(ClusterServant, getProxies)
160+
{
161+
xic::VDict args = quest->args();
162+
xstr_t prefix = args.getXstr("prefix");
163+
164+
xic::AnswerWriter aw;
165+
aw.param("revision", _revision);
166+
xic::VListWriter lw = aw.paramVList("proxies");
167+
for (ProxyMap::iterator iter = _proxyMap.begin(); iter != _proxyMap.end(); ++iter)
168+
{
169+
xstr_t proxy = make_xstr(iter->first);
170+
if (prefix.len == 0 || xstr_start_with(&proxy, &prefix))
171+
{
172+
lw.v(proxy);
173+
}
174+
}
175+
return aw.take();
176+
}
177+

cluster/ClusterServant.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef ClusterServant_h_
2+
#define ClusterServant_h_
3+
4+
#include "xic/ServantI.h"
5+
#include <map>
6+
7+
#define CLUSTERSERVANT_CMDS \
8+
CMD(renew) \
9+
CMD(revision) \
10+
CMD(getProxies) \
11+
CMD(remove) \
12+
/* END OF LIST */
13+
14+
class ClusterServant: public xic::ServantI
15+
{
16+
static MethodTab::PairType _methodpairs[];
17+
static MethodTab _methodtab;
18+
19+
typedef std::map<std::string, time_t> ProxyMap;
20+
ProxyMap _proxyMap;
21+
int64_t _revision;
22+
23+
public:
24+
ClusterServant();
25+
virtual ~ClusterServant();
26+
27+
void reap_fiber();
28+
29+
#define CMD(X) XIC_METHOD_DECLARE(X);
30+
CLUSTERSERVANT_CMDS
31+
#undef CMD
32+
};
33+
34+
typedef XPtr<ClusterServant> ClusterServantPtr;
35+
36+
#endif

cluster/Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
##
2+
##
3+
4+
5+
EXES = wp_clusterd
6+
7+
OBJS = ClusterServant.o
8+
9+
CFLAGS = -g -Wall -rdynamic
10+
11+
CXXFLAGS = -g -Wall -rdynamic
12+
13+
CPPFLAGS = -I. -I.. -I../../knotty -I../../knotty/include
14+
15+
LIBS = -pthread -Wl,-static -L../../knotty/lib -lxic -ldlog -lxs -lst -Wl,-call_shared -lrt
16+
17+
18+
all: $(OBJS) $(EXES)
19+
20+
$(EXES): $(OBJS)
21+
22+
23+
.c.o:
24+
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
25+
26+
.cpp.o:
27+
$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o $@ $<
28+
29+
.c:
30+
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $< $(LIBS)
31+
32+
.cpp:
33+
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ $^ $(LIBS)
34+
35+
clean:
36+
$(RM) $(OBJS) $(EXES)
37+

cluster/conf.wp_cluster

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
xic.Endpoints = @tcp++11911
3+
4+
xic.user = www
5+
xic.group = www
6+

cluster/wp_cluster.xic

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
wp_cluster
3+
===========
4+
5+
6+
=> renew { proxy^%S; }
7+
<= { revision^%i; }
8+
9+
10+
=> revision {}
11+
<= { revision^%i; }
12+
13+
14+
=> getProxies { ?prefix^%s; }
15+
<= { revision^%i; proxies^[%s]; }
16+
17+
18+
=> remove { proxy^%s; }
19+
<= { ok^%t; }
20+

cluster/wp_clusterd.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "ClusterServant.h"
2+
#include "helper/wp_version.h"
3+
4+
static const char *build_info = "$build: webpush-" WP_VERSION " " __DATE__ " " __TIME__ " $";
5+
6+
int run(int argc, char **argv, const xic::EnginePtr& engine)
7+
{
8+
xic::AdapterPtr adapter = engine->createAdapter();
9+
ClusterServantPtr cm(new ClusterServant());
10+
adapter->addServant("wp_cluster", cm);
11+
adapter->activate();
12+
engine->throb(build_info);
13+
engine->waitForShutdown();
14+
return 0;
15+
}
16+
17+
int main(int argc, char **argv)
18+
{
19+
return xic::start_xic_st(run, argc, argv);
20+
}
21+

0 commit comments

Comments
 (0)