1 module chatroom.client.client;
2 
3 import hunt.imf;
4 
5 import chatroom.command;
6 import chatroom.chatroom;
7 
8 import std.stdio;
9 import std.string;
10 
11 import hunt.concurrency.thread;
12 
13 class ClientInfo {
14     __gshared string name;
15     __gshared Context context;
16 }
17 
18 class ClientService {
19     mixin MakeRouter;
20 
21     @route(Command.MESSAGE)
22     void recv(Msg message) {
23         writeln(message.name, " : ", message.message);
24     }
25 
26     @route(Command.R_LOGIN)
27     void mylogin(LoginReply login) {
28         ClientInfo.name = login.name;
29         if (login.status == LoginReply.LoginState.OK)
30             writeln("you have logined!");
31         else
32             writeln("username repeated! change username relogin please!");
33     }
34 
35     @route(Command.LOGIN)
36     void otherlogin(Login login) {
37         writeln(login.name, " login");
38     }
39 
40     @route(Command.LOGOUT)
41     void otherlogout(Login login) {
42         writeln(login.name, " logout");
43     }
44 
45 }
46 
47 //////////////////////////////////------/////////////////////////////
48 
49 void showHelp() {
50     writeln("clientchat:");
51     writeln("1 login username");
52     writeln("2 send message");
53     writeln("3 help");
54     writeln("4 quit");
55 }
56 
57 void showPromt() {
58     write(">>>>");
59 }
60 
61 void showError() {
62     writeln("input error");
63 }
64 
65 string login(string name) {
66     auto login = new Login();
67     login.name = name;
68     sendMessage(ClientInfo.context, Command.Q_LOGIN, login);
69     return name;
70 }
71 
72 void send(string m) {
73     auto msg = new Msg();
74     msg.name = ClientInfo.name;
75     msg.message = m;
76     sendMessage(ClientInfo.context, Command.MESSAGE, msg);
77 }
78 
79 int main() {
80     string c;
81     showHelp();
82     showPromt();
83 
84     auto app = new Application();
85     auto client = app.createClient("127.0.0.1", 3003);
86     client.setOpenHandler((Context context) {
87         writeln("connected to server!");
88         ClientInfo.context = context;
89     });
90     app.run(50);
91 
92     while ((c = strip(readln())) != "quit") {
93         string[] params = c.split(" ");
94         if (params.length == 0) {
95             showPromt();
96             continue;
97         }
98 
99         switch (params[0]) {
100         case "help":
101             showHelp();
102             break;
103         case "login":
104             if (params.length < 2) {
105                 showError();
106                 continue;
107             }
108             login(params[1]);
109             break;
110         case "send":
111             if (params.length < 2 || ClientInfo.name == string.init) {
112                 showError();
113                 continue;
114             }
115             send(params[1]);
116             break;
117         default:
118             showHelp();
119             break;
120 
121         }
122         showPromt();
123     }
124 
125     app.stop();
126     // thread_joinAll();
127 
128     return 0;
129 }