Mercurial > projects > dreactor
comparison test/chatclient.d @ 13:8c9b1276f623 default tip
bug fixes
author | rick@minifunk |
---|---|
date | Sat, 20 Sep 2008 18:33:11 -0400 |
parents | d6a3cfe7c3de |
children |
comparison
equal
deleted
inserted
replaced
12:d6a3cfe7c3de | 13:8c9b1276f623 |
---|---|
1 | 1 |
2 module chatclient; | 2 module chatclient; |
3 | |
4 import tango.io.Stdout; | |
5 import tango.io.Console; | |
6 import tango.net.Socket; | |
7 import tango.util.log.Log; | |
3 | 8 |
4 import dreactor.core.Vat; | 9 import dreactor.core.Vat; |
5 import dreactor.core.Task; | 10 import dreactor.core.Task; |
6 import dreactor.protocol.TcpProvider; | 11 import dreactor.protocol.TcpProvider; |
12 import dreactor.util.Emitter; | |
7 | 13 |
8 enum {EMITTER_CHAT_RECEIVE = 42}; | 14 enum { StdinReceive = 42 } |
15 | |
16 Logger log; | |
9 | 17 |
10 class ChatTask : Task | 18 class ChatTask : Task |
11 { | 19 { |
12 | 20 |
13 private | 21 private |
14 TcpProvider client; | 22 TcpProvider client; |
15 | 23 bool running; |
16 public | 24 public |
17 this(TcpProvider tcpclient) | 25 this(TcpProvider tcpclient) |
18 { | 26 { |
19 super(tcpclient); | 27 super(tcpclient); |
20 } | 28 } |
21 | 29 |
22 void run() | 30 void run() |
23 { | 31 { |
24 Message msg; | 32 Message msg; |
25 | 33 running = true; |
26 auto em = new Emitter(this, | 34 auto em = new Emitter(this, |
27 { | 35 { |
28 char buf[] = Cin.copyln(true); | 36 char buf[] = Cin.copyln(true); |
29 return new Message(buf, EMITTER_STDIN_RECEIVE, buf.size); | 37 return Message(cast(void*)buf.ptr, StdinReceive, buf.length); |
30 }); | 38 }); |
31 | 39 |
32 while (msg = receive()) | 40 while (running) |
33 { | 41 { |
34 switch(msg.type) | 42 msg = receive(); |
43 switch (msg.type) | |
35 { | 44 { |
36 case EMITTER_CHAT_RECEIVE: | 45 case StdinReceive: |
37 char[] inbuf = msg.payload; | 46 { |
47 char[] inbuf = getString(msg); | |
38 if (inbuf == "quit") | 48 if (inbuf == "quit") |
39 { | 49 { |
40 em.stopNow(); | 50 running = false; |
41 return; | |
42 } | 51 } |
43 client.send(msg.payload); | 52 client.send(inbuf); |
44 break; | 53 } |
45 | 54 case TcpProvider.Receive: |
46 case TcpProvider.RECEIVE: | 55 { |
47 Stdout(cast(char[]) msg.payload); | 56 Stdout(getString(msg)); |
48 break; | 57 } |
49 | |
50 case TcpProvider.SEND_COMPLETE: | |
51 break; | |
52 | |
53 case TcpProvider.REMOTE_CLOSED: | |
54 Stdout("--- Remote host closed connection \n"); | |
55 break; | |
56 | |
57 default: | 58 default: |
58 Stdout("Unknown message received\n"); | 59 Stdout("unknown msg received: {}", msg.type); |
59 } | 60 } |
60 } | 61 } |
61 em.stopNow(); | 62 em.stopNow(); |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 | 66 |
66 int main(int argc, char[][] argv) | 67 int main(char[][] args) |
67 { | 68 { |
68 auto vat = new Vat; | 69 log = Log.lookup("dreactor.chatserver"); |
69 auto client = new TcpProvider(new IPv4Address("localhost", 5555), vat); | 70 auto provider = new TcpProvider(new IPv4Address("localhost", 5555)); |
70 auto tsk = new ChatTask(client); | 71 auto tsk = new ChatTask(provider); |
71 vat.addTask(task); | 72 return 0; |
72 } | 73 } |