1 module client;
2 
3 import hunt.net;
4 import hunt.net.codec.textline;
5 import hunt.logging;
6 import hunt.String;
7 import hunt.event.EventLoop;
8 
9 
10 import std.format;
11 import std.stdio;
12 
13 import core.memory;
14 import core.thread;
15 import core.time;
16 
17 
18 // enum Host = "127.0.0.1";
19 enum Host = "10.1.23.222";
20 enum Port = 8090;
21 
22 string requestString = "GET / HTTP/1.1\r\nAccept: */*\r\nHost: 10.1.23.222:80\r\nConnection: keep-alive\r\n\r\n";
23 
24 import std.parallelism;
25 
26 void main() {
27     runClientTest();
28     // runEventLoopTest();
29 
30     // runThreadTest();
31     // runThreadPoolTest();
32 }
33 
34 
35 void runThreadPoolTest() {
36     import std.parallelism;
37     Duration dur = 5.seconds;
38     tracef("Waiting for %s seconds", dur);
39     Thread.sleep(dur);
40 
41     MonoTime startTime = MonoTime.currTime;
42 
43     // Thread.sleep(5.seconds);
44     
45     tracef("Thread: %d", Thread.getAll().length);
46 
47     foreach(int index; 0..20) {
48         auto testTask = task(() { 
49             try {
50                 tracef("runing Thread: %d", Thread.getAll().length);
51                 Thread.sleep(5.seconds);
52                 warningf("ending Thread: %d", Thread.getAll().length);
53             } catch (Throwable t) {
54                 warning(t.msg);
55                 version(HUNT_DEBUG) warning(t.toString());
56             }
57         });
58 
59         // testTask.executeInNewThread();
60         taskPool.put(testTask);
61 
62         Thread.sleep(2.seconds);
63         tracef("starting %d", index);
64     }
65     
66     getchar();
67     
68     tracef("Thread: %d", Thread.getAll().length);
69 }
70 
71 void runThreadTest() {
72     Duration dur = 5.seconds;
73     tracef("Waiting for %s seconds", dur);
74     Thread.sleep(dur);
75 
76     MonoTime startTime = MonoTime.currTime;
77 
78     // Thread.sleep(5.seconds);
79     
80     tracef("Thread: %d", Thread.getAll().length);
81 
82     foreach(int index; 0..200) {
83         Thread _workThread = new Thread(() { 
84                 tracef("runing Thread: %d", Thread.getAll().length);
85                 // Thread.sleep(5.seconds);
86                 warningf("ending Thread: %d", Thread.getAll().length);
87 
88         });
89 
90         // _workThread.isDaemon = true;
91         _workThread.start();
92 
93         Thread.sleep(2.seconds);
94         tracef("starting %d", index);
95     }
96     
97     getchar();
98 
99 
100     // GC.collect();
101     // GC.minimize();
102 
103     // getchar();
104     
105     tracef("Thread: %d", Thread.getAll().length);
106 }
107 
108 // void runThreadTest_Bug() {
109 //     Duration dur = 5.seconds;
110 //     tracef("Waiting for %s seconds", dur);
111 //     Thread.sleep(dur);
112 
113 //     MonoTime startTime = MonoTime.currTime;
114 
115 //     // Thread.sleep(5.seconds);
116     
117 //     tracef("Thread: %d", Thread.getAll().length);
118 
119 //     foreach(int index; 0..2000) {
120 //         Thread _workThread = new Thread(() { 
121 //             try {
122 //                 tracef("runing Thread: %d", Thread.getAll().length);
123 //                 // Thread.sleep(5.seconds);
124 //                 warningf("ending Thread: %d", Thread.getAll().length);
125 //             } catch (Throwable t) {
126 //                 warning(t.msg);
127 //                 version(HUNT_DEBUG) warning(t.toString());
128 //             }
129 //         });
130 
131 //         _workThread.start();
132 
133 //         // Thread.sleep(2.seconds);
134 //         tracef("starting %d", index);
135 //     }
136     
137 //     getchar();
138     
139 //     tracef("Thread: %d", Thread.getAll().length);
140 // }
141 
142 void runEventLoopTest() {
143     Duration dur = 5.seconds;
144     tracef("Waiting for %s seconds", dur);
145     Thread.sleep(dur);
146 
147     MonoTime startTime = MonoTime.currTime;
148 
149     // Thread.sleep(5.seconds);
150 
151     foreach(int index; 0..50) {
152         
153         EventLoop loop = NetUtil.buildEventLoop();
154         // Thread.sleep(5.seconds);
155 
156         loop.stop();
157         infof("stoppped => %d", index);
158         Thread.sleep(2.seconds);
159     }
160 
161     infof("Press any key to exit");
162     getchar();
163 }
164 
165 void runClientTest() {
166 
167     Duration dur = 5.seconds;
168     tracef("Waiting for %s seconds", dur);
169     Thread.sleep(dur);
170 
171     MonoTime startTime = MonoTime.currTime;
172 
173     // Thread.sleep(5.seconds);
174 
175     foreach(int index; 0..5) {
176         testClient(index + 1);
177         // Thread.sleep(10.seconds);
178         Thread.sleep(500.msecs);
179     }
180 
181     MonoTime endTime = MonoTime.currTime;
182     warningf("elapsed: %s", endTime - startTime);
183 
184     // infof("Press any key to run GC Collection");
185     // getchar();
186 
187     // // loop.stop();
188     // GC.collect();
189     // GC.minimize();
190 
191     infof("Press any key to exit");
192     getchar();
193 }
194 
195 
196 
197 void testClient(int index) {
198 
199     tracef("testing %d ...", index);
200     scope(exit) {
201         infof("%d done.", index);
202     }
203 
204 import core.sync.condition;
205 import core.sync.mutex;
206 
207 
208     Mutex locker = new Mutex();
209     Condition cond = new Condition(locker);
210     bool isWaiting = false;
211 
212     NetClient client;
213     EventLoop loop = NetUtil.buildEventLoop();
214     client = new NetClientImpl(loop, new NetClientOptions()); 
215 
216     // dfmt off
217     client.setHandler(new class NetConnectionHandler {
218 
219         override void connectionOpened(Connection connection) {
220             // version(HUNT_DEBUG) infof("Connection created: %s", connection.getRemoteAddress());
221 
222             // connection.write("Hi, I'm client");
223             connection.write(requestString);
224         }
225 
226         override void connectionClosed(Connection connection) {
227             // version(HUNT_DEBUG)  infof("Connection closed: %s", connection.getRemoteAddress());
228 
229             // client.close();
230 
231             locker.lock();
232             if(isWaiting) cond.notify();
233             locker.unlock();
234         }
235 
236         override DataHandleStatus messageReceived(Connection connection, Object message) {
237             string str = format("data received: %s", message.toString());
238             tracef(str);
239 
240             import hunt.io.ByteBuffer;
241             ByteBuffer buffer = cast(ByteBuffer)message;
242             string content = cast(string)buffer.peekRemaining();
243             // warning(content);
244 
245             // if(content == "quit") {
246             //     client.close();
247             // }
248 
249             client.close();
250 
251             return DataHandleStatus.Done;
252         }
253 
254         override void exceptionCaught(Connection connection, Throwable t) {
255             warning(t);
256         }
257 
258         override void failedOpeningConnection(int connectionId, Throwable t) {
259             warning(t);
260             client.close(); 
261         }
262 
263         override void failedAcceptingConnection(int connectionId, Throwable t) {
264             warning(t);
265         }
266     }).connect(Host, Port);
267 
268     // dfmt on
269 
270     import core.time;
271 
272     locker.lock();
273     isWaiting = true;
274     cond.wait(100.msecs);
275     locker.unlock();
276 
277     // getchar();
278 }