1 | #include "cf/test.hpp"
|
---|
2 | #include "cf/bin.h"
|
---|
3 | #include "cf/codec.h"
|
---|
4 | #include "cf/file.h"
|
---|
5 | #include "cf/logger.h"
|
---|
6 | #include "cf/task.h"
|
---|
7 | #include "cf/util.h"
|
---|
8 | #include "cf/network.h"
|
---|
9 |
|
---|
10 | #include <string>
|
---|
11 |
|
---|
12 | /*--------------------------------------------------------------*/
|
---|
13 |
|
---|
14 | // test string
|
---|
15 | #define STRING "test string"
|
---|
16 | #define LOGFILE "lol"
|
---|
17 | // for networking
|
---|
18 | #define HOST "localhost"
|
---|
19 | #define PORT 1234
|
---|
20 | #define TIMEOUT 3
|
---|
21 |
|
---|
22 | /*--------------------------------------------------------------*/
|
---|
23 |
|
---|
24 | static void throwEx2() throw (cf::exception);
|
---|
25 | static void throwEx1() throw (cf::exception);
|
---|
26 | static int serverWorker(void * arg);
|
---|
27 | static int clientWorker(void * arg);
|
---|
28 |
|
---|
29 | /*--------------------------------------------------------------*/
|
---|
30 |
|
---|
31 | cf::task::mutex gMutex;
|
---|
32 | cf::task::thread gServerWorker(serverWorker);
|
---|
33 |
|
---|
34 | /*--------------------------------------------------------------*/
|
---|
35 |
|
---|
36 | TEST(Exception, main)
|
---|
37 | {
|
---|
38 | try
|
---|
39 | {
|
---|
40 | throwEx1();
|
---|
41 | }
|
---|
42 | catch (cf::exception & e)
|
---|
43 | {
|
---|
44 | std::cerr << e.stackTrace();
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | TEST(Bin, main)
|
---|
49 | {
|
---|
50 | cf::bin a, b(STRING);
|
---|
51 |
|
---|
52 | a = b;
|
---|
53 |
|
---|
54 | cf::bin c = b;
|
---|
55 |
|
---|
56 | b.print("bin-value");
|
---|
57 | }
|
---|
58 |
|
---|
59 | TEST(Codec, main)
|
---|
60 | {
|
---|
61 | std::string e;
|
---|
62 | cf::bin d;
|
---|
63 | cf::codec::ICodec * codec = NULL;
|
---|
64 |
|
---|
65 | codec = cf::codec::base64::getInstance();;
|
---|
66 | e = codec->encode(STRING);
|
---|
67 | d = codec->decode(e);
|
---|
68 | std::cout << "input : " << STRING << "\n"
|
---|
69 | << "encoded : " << e << "\n";
|
---|
70 | d.print("decoded");
|
---|
71 |
|
---|
72 | codec = cf::codec::hex::getInstance();;
|
---|
73 | e = codec->encode(STRING);
|
---|
74 | d = codec->decode(e);
|
---|
75 | std::cout << "input : " << STRING << "\n"
|
---|
76 | << "encoded : " << e << "\n";
|
---|
77 | d.print("decoded");
|
---|
78 | }
|
---|
79 |
|
---|
80 | TEST(File, main)
|
---|
81 | {
|
---|
82 | cf::file f(STRING);
|
---|
83 |
|
---|
84 | f.open(cf::file::CREATE | cf::file::RW | cf::file::LOCK);
|
---|
85 | f.write(cf::bin(STRING));
|
---|
86 | f.close();
|
---|
87 | f.remove();
|
---|
88 | }
|
---|
89 |
|
---|
90 | TEST(Logger, main)
|
---|
91 | {
|
---|
92 | enum LOG_LEVEL
|
---|
93 | {
|
---|
94 | ERR = 1,
|
---|
95 | WARN,
|
---|
96 | INFO,
|
---|
97 | DEBUG
|
---|
98 | };
|
---|
99 | cf::logger * log = cf::logger::getInstance();
|
---|
100 |
|
---|
101 | log->init(".");
|
---|
102 | log->setLevel(10);
|
---|
103 |
|
---|
104 | log->add(LOGFILE, ERR , "ERROR" , cf::logger::DAILY);
|
---|
105 | log->add(LOGFILE, WARN , "WARNNING", cf::logger::DAILY);
|
---|
106 | log->add(LOGFILE, INFO , "INFO" , cf::logger::DAILY);
|
---|
107 | log->add(LOGFILE, DEBUG, "DEBUG" , cf::logger::DAILY);
|
---|
108 |
|
---|
109 | log->write(ERR, "this is log message");
|
---|
110 | }
|
---|
111 |
|
---|
112 | TEST(Util, main)
|
---|
113 | {
|
---|
114 | cf::util::datetime dt = cf::util::getInstance()->getDateTime();
|
---|
115 |
|
---|
116 | std::cout << "date : "
|
---|
117 | << dt.mYear << ":" << dt.mMonth << ":" << dt.mDay
|
---|
118 | << "(" << dt.mWeek << ":" << dt.mWeekName << ")"
|
---|
119 | << std::endl;
|
---|
120 | std::cout << "time : "
|
---|
121 | << dt.mHour << ":" << dt.mMin << ":" << dt.mSec << "." << dt.mUsec
|
---|
122 | << std::endl;
|
---|
123 | }
|
---|
124 |
|
---|
125 | TEST(ThreadNetworking, Server)
|
---|
126 | {
|
---|
127 | std::cout << "opening " << PORT << " ..." << std::endl;
|
---|
128 |
|
---|
129 | gServerWorker.start(NULL);
|
---|
130 |
|
---|
131 | // wait for server-binding
|
---|
132 | cf::task::thread::sleep(10);
|
---|
133 |
|
---|
134 | std::cout << "server worker was started" << std::endl;
|
---|
135 | }
|
---|
136 |
|
---|
137 | TEST(ThreadNetworking, Client)
|
---|
138 | {
|
---|
139 | const int max = 5;
|
---|
140 | int i = 0;
|
---|
141 |
|
---|
142 | cf::task::thread worker[max] = {
|
---|
143 | cf::task::thread(clientWorker),
|
---|
144 | cf::task::thread(clientWorker),
|
---|
145 | cf::task::thread(clientWorker),
|
---|
146 | cf::task::thread(clientWorker),
|
---|
147 | cf::task::thread(clientWorker),
|
---|
148 | };
|
---|
149 |
|
---|
150 | for (i = 0 ; i < max ; i++)
|
---|
151 | worker[i].start(NULL);
|
---|
152 |
|
---|
153 | for (i = 0 ; i < max ; i++)
|
---|
154 | worker[i].join();
|
---|
155 | }
|
---|
156 |
|
---|
157 | TEST(Networking, NIC)
|
---|
158 | {
|
---|
159 | std::cout << "H/W Address : "
|
---|
160 | << cf::network::nic::getMACAddress()
|
---|
161 | << std::endl;
|
---|
162 | }
|
---|
163 |
|
---|
164 | int main(int argc, char ** argv)
|
---|
165 | {
|
---|
166 | TEST_INIT(argc, argv);
|
---|
167 |
|
---|
168 | TEST_RUN(Exception , main);
|
---|
169 | TEST_RUN(Bin , main);
|
---|
170 | TEST_RUN(Codec , main);
|
---|
171 | TEST_RUN(File , main);
|
---|
172 | TEST_RUN(Logger , main);
|
---|
173 | TEST_RUN(Util , main);
|
---|
174 | TEST_RUN(ThreadNetworking, Server);
|
---|
175 | TEST_RUN(ThreadNetworking, Client);
|
---|
176 | TEST_RUN(Networking , NIC);
|
---|
177 |
|
---|
178 | TEST_RESULT();
|
---|
179 |
|
---|
180 | return 0;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /*--------------------------------------------------------------*/
|
---|
184 |
|
---|
185 | void throwEx2() throw (cf::exception)
|
---|
186 | {
|
---|
187 | THROW_EXCEPTION("exception test");
|
---|
188 | }
|
---|
189 |
|
---|
190 | void throwEx1() throw (cf::exception)
|
---|
191 | {
|
---|
192 | try
|
---|
193 | {
|
---|
194 | throwEx2();
|
---|
195 | }
|
---|
196 | catch (cf::exception & e)
|
---|
197 | {
|
---|
198 | FORWARD_EXCEPTION(e);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | int serverWorker(void * arg)
|
---|
203 | {
|
---|
204 | try
|
---|
205 | {
|
---|
206 | cf::network::tcp server;
|
---|
207 | cf::network::tcp client;
|
---|
208 | cf::bin msg;
|
---|
209 |
|
---|
210 | server.listen(PORT);
|
---|
211 |
|
---|
212 | while (true)
|
---|
213 | {
|
---|
214 | client.attach(server.accept().detach());
|
---|
215 | msg = client.receive();
|
---|
216 | client.send(msg);
|
---|
217 | client.close();
|
---|
218 | }
|
---|
219 |
|
---|
220 | server.close();
|
---|
221 | }
|
---|
222 | catch (cf::exception & e)
|
---|
223 | {
|
---|
224 | std::cerr << e.stackTrace();
|
---|
225 | }
|
---|
226 |
|
---|
227 | return 0;
|
---|
228 | }
|
---|
229 |
|
---|
230 | int clientWorker(void * arg)
|
---|
231 | {
|
---|
232 | std::cout << "connecting to "
|
---|
233 | << HOST << ":" << PORT << " ..."
|
---|
234 | << std::endl;
|
---|
235 |
|
---|
236 | try
|
---|
237 | {
|
---|
238 | cf::network::tcp client;
|
---|
239 | cf::ulong_t tid = cf::task::thread::id();
|
---|
240 | cf::bin msg(STR(STRING << " " << tid).c_str());
|
---|
241 | cf::bin response;
|
---|
242 |
|
---|
243 | client.connect(HOST, PORT, TIMEOUT);
|
---|
244 | client.send(msg);
|
---|
245 | response = client.receive();
|
---|
246 | client.close();
|
---|
247 |
|
---|
248 | SYNCHRONIZED(gMutex)
|
---|
249 | {
|
---|
250 | response.print("print response in synchronized-scope : ");
|
---|
251 | }
|
---|
252 | }
|
---|
253 | catch (cf::exception & e)
|
---|
254 | {
|
---|
255 | SYNCHRONIZED(gMutex)
|
---|
256 | {
|
---|
257 | std::cerr << e.stackTrace() << cf::exception::systemMessage();
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | return 0;
|
---|
262 | }
|
---|