-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtt.d
62 lines (52 loc) · 1.47 KB
/
tt.d
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
module tt;
import tango.core.Thread;
import tango.net.InternetAddress;
import tango.io.Buffer;
import tango.text.stream.LineIterator;
import tangled.conduit : ASocketConduit;
import tangled.interfaces;
import tangled.protocol;
static import tangled.reactor;
import tangled.evhttp;
import tangled.log;
import libevent.http;
auto name = "tt";
mixin SimpleLogger!(name);
int main() {
auto f = new SimpleFactory!(Echo)();
auto addr = new InternetAddress("127.0.0.1", 6060);
auto listener = tangled.reactor.tcpListen(addr, f);
auto http = tangled.reactor.httpListen(new InternetAddress("127.0.0.1", 7070));
auto fh = new SimpleHTTPFactory!(HEcho)();
http.registerGenericHandler(fh);
void fun(){.log.trace("CallLater Works.");}
tangled.reactor.callLater(1.0, &fun);
tangled.reactor.run();
return 0;
}
class Echo : BaseProtocol {
this(IProtocolFactory f){
super(f);
}
void makeConnection(ASocketConduit t)
in {
assert(t, "bad conduit in makeConnection");
} body {
super.makeConnection(t);
log.trace(">>> makeConnection");
t.write("Welcome to the Echo Server!\r\n");
auto buf = new Buffer(transport);
foreach (line; new LineIterator!(char)(buf.input)) {
buf.append(line ~ "\r\n").flush;
}
}
}
class HEcho : BaseHTTPProtocol {
this(IHTTPProtocolFactory f){
super(f);
}
void handleRequest(IHTTPRequest req) {
super.handleRequest(req);
req.sendPage(HTTP_OK, "OK", format("Gotcha! : {}\r\n", req.remoteHost()));
}
}